Unit.php 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. <?php
  2. namespace app\admin\controller;
  3. use app\admin\model\ChangeLog;
  4. use app\BaseController;
  5. use think\App;
  6. use think\facade\Db;
  7. //单位
  8. class Unit extends Base
  9. {
  10. public $post = "";
  11. public function __construct(App $app)
  12. {
  13. parent::__construct($app);
  14. $this->post = $this->request->post();
  15. }
  16. public function create()
  17. {
  18. $param = $this->request->only(['unit', 'status' => 1], 'post', 'trim');
  19. // $unit = isset($this->post['unit']) && $this->post['unit'] !==""? trim($this->post['unit']):"";
  20. if ($param['unit'] == "") return error_show(1002, "参数unit不能为空");
  21. $tmp = Db::name("unit")
  22. ->field('id')
  23. ->where(['is_del' => 0, "unit" => $param['unit']])
  24. ->findOrEmpty();
  25. if (!empty($tmp)) return json_show(1004, '该单位已存在');
  26. // $token = isset($this->post['token'])&&$this->post['token']!='' ? trim($this->post['token']):"";
  27. // if($token==''){
  28. // return error_show(105,"参数token不能为空");
  29. // }
  30. // $user =GetUserInfo($token);
  31. // if(empty($user)||$user['code']!=0){
  32. // return error_show(1002,"创建人数据不存在");
  33. // }
  34. $createrid = $this->uid;//isset($user["data"]['id']) ? $user["data"]['id'] : "";
  35. $creater = $this->uname;//isset($user["data"]['nickname']) ? $user["data"]['nickname'] : "";
  36. // $status = isset($this->post['status']) &&$this->post['status'] !==""? intval($this->post['status']):"1";
  37. $data = [
  38. "unit" => $param['unit'],
  39. "creater" => $creater,
  40. "createrid" => $createrid,
  41. "status" => $param['status'],
  42. "is_del" => 0,
  43. "addtime" => date("Y-m-d H:i:s"),
  44. "updatetime" => date("Y-m-d H:i:s")
  45. ];
  46. $info = Db::name("unit")->insert($data);
  47. return $info ? json_show(0, "新建成功") : json_show(1002, "新建失败");
  48. // if($info){
  49. // return error_show(0,"新建成功");
  50. // }else{
  51. // return error_show(1002,"新建失败");
  52. // }
  53. }
  54. public function list()
  55. {
  56. $page = isset($this->post['page']) && $this->post['page'] !== "" ? intval($this->post['page']) : "1";
  57. $size = isset($this->post['size']) && $this->post['size'] !== "" ? intval($this->post['size']) : "10";
  58. $where = [["u.is_del", "=", 0]];
  59. $unit = isset($this->post['unit']) && $this->post['unit'] !== "" ? trim($this->post['unit']) : "";
  60. if ($unit !== "") {
  61. $where[] = ['u.unit', "like", "%$unit%"];
  62. }
  63. $status = isset($this->post['status']) && $this->post['status'] !== "" ? intval($this->post['status']) : "";
  64. if ($status !== "") {
  65. $where[] = ['u.status', "=", $status];
  66. }
  67. $creater = isset($this->post['creater']) && $this->post['creater'] !== "" ? trim($this->post['creater']) : "";
  68. if ($creater != "") {
  69. $where[] = ['u.creater', "like", "%$creater%"];
  70. }
  71. $start = isset($this->post['start']) && $this->post['start'] !== "" ? $this->post['start'] : "";
  72. if ($start !== "") {
  73. // $where[]=['u.addtime',">=",date('Y-m-d H:i:s')];//,strtotime($start)
  74. $where[] = ['u.addtime', ">=", $start];
  75. }
  76. $end = isset($this->post['end']) && $this->post['end'] !== "" ? $this->post['end'] : "";
  77. if ($end !== "") {
  78. // $where[]=['u.addtime',"<",date('Y-m-d H:i:s')];//,strtotime($end)+24*3600
  79. $where[] = ['u.addtime', "<=", $end];
  80. }
  81. $company_name = isset($this->post['company_name']) && $this->post['company_name'] !== "" ? trim($this->post['company_name']) : "";
  82. if ($company_name !== "") $where[] = ["u.createrid", 'in', get_company_item_user_by_name($company_name)];
  83. $count = Db::name('unit')
  84. ->alias('u')
  85. ->leftJoin("depart_user user", "user.uid=u.createrid AND user.is_del=0")
  86. ->where($where)
  87. ->count();
  88. $total = ceil($count / $size);
  89. $page = $page >= $total ? $total : $page;
  90. $list = Db::name('unit')
  91. ->alias('u')
  92. ->where($where)
  93. ->append(['company_name'])
  94. ->page($page, $size)
  95. ->order("addtime desc")
  96. ->select()
  97. ->toArray();
  98. $all_createrid = array_column($list, 'createrid');
  99. $item = get_company_name_by_uid($all_createrid);
  100. foreach ($list as &$val) {
  101. $val['company_name'] = $item[$val['createrid']] ?? '';
  102. }
  103. return app_show(0, "获取成功", ['list' => $list, 'count' => $count]);
  104. }
  105. public function edit()
  106. {
  107. $id = isset($this->post['id']) && $this->post['id'] !== "" ? intval($this->post['id']) : "";
  108. if ($id == "") {
  109. return error_show(1002, "参数id不能为空");
  110. }
  111. $info = Db::name('unit')->where(['id' => $id, 'is_del' => 0])->find();
  112. if ($info == "") {
  113. return error_show(1003, "未找到数据");
  114. }
  115. if ($info['status'] == 1) {
  116. return error_show(1002, "状态是启用状态,无法编辑");
  117. }
  118. $unit = isset($this->post['unit']) && $this->post['unit'] !== "" ? trim($this->post['unit']) : "";
  119. if ($unit == "") {
  120. return error_show(1002, "参数unit不能为空");
  121. }
  122. // $token = isset($this->post['token'])&&$this->post['token']!='' ? trim($this->post['token']):"";
  123. // if($token==''){
  124. // return error_show(105,"参数token不能为空");
  125. // }
  126. // $user =GetUserInfo($token);
  127. // if(empty($user)||$user['code']!=0){
  128. // return error_show(1002,"创建人数据不存在");
  129. // }
  130. // $createrid= isset($user["data"]['id']) ? $user["data"]['id'] : "";
  131. // $creater= isset($user["data"]['nickname']) ? $user["data"]['nickname'] : "";
  132. $status = isset($this->post['status']) && $this->post['status'] !== "" ? intval($this->post['status']) : "0";
  133. $tmp = Db::name("unit")
  134. ->field('id')
  135. ->where(['is_del' => 0, "unit" => $unit])
  136. ->where('id','<>',$id)
  137. ->findOrEmpty();
  138. if (!empty($tmp)) return json_show(1004, '该单位已存在');
  139. $data = [
  140. "id" => $id,
  141. "unit" => $unit,
  142. // "creater"=>$creater,
  143. // "createrid"=>$createrid,
  144. // "status"=>$status,
  145. "is_del" => 0,
  146. "updatetime" => date("Y-m-d H:i:s")
  147. ];
  148. $datainfo = Db::name('unit')->where(['is_del'=>0,'id'=>$id])->save($data);
  149. $item = array_diff($data, $info);
  150. $json = json_encode($item, JSON_UNESCAPED_UNICODE);
  151. $jsp = json_encode($info, JSON_UNESCAPED_UNICODE);
  152. if ($datainfo) {
  153. ChangeLog::logAdd(4, $info['id'], $jsp, $json, ['id' => $this->uid, 'nickname' => $this->uname]);
  154. return error_show(0, "更新成功");
  155. } else {
  156. return error_show(1002, "更新失败");
  157. }
  158. }
  159. public function del()
  160. {
  161. $id = $this->post['id'] && $this->post['id'] !== "" ? intval($this->post['id']) : "";
  162. if ($id === "") {
  163. return error_show(1004, "参数id不能为空");
  164. }
  165. $str = Db::name('unit')->where(['id' => $id, 'is_del' => 0])->find();
  166. if (empty($str)) {
  167. return error_show(1002, "未找到数据");
  168. }
  169. $end = Db::name('unit')->update(['id' => $id, 'is_del' => 1]);
  170. if ($end) {
  171. return error_show(0, "删除成功");
  172. } else {
  173. return error_show(1002, "删除失败");
  174. }
  175. }
  176. public function status()
  177. {
  178. $id = isset($this->post['id']) && $this->post['id'] !== "" ? intval($this->post['id']) : "";
  179. if ($id == "") {
  180. return error_show(1002, "参数id不能为空");
  181. }
  182. $info = Db::name("unit")->where([["id", "=", $id], ["is_del", "=", 0]])->find();
  183. if (!$info) {
  184. return error_show(1002, "未找到对应数据");
  185. }
  186. $status = isset($this->post['status']) && $this->post['status'] !== "" ? intval($this->post['status']) : "";
  187. if ($status === "") {
  188. return error_show(1002, "参数status不能为空");
  189. }
  190. if (!in_array($status, [0, 1])) {
  191. return error_show(1002, "参数status无效");
  192. }
  193. $info['status'] = $status;
  194. $info['updatetime'] = date("Y-m-d H:i:s");
  195. $msg = $status == 1 ? "启用" : "禁用";
  196. $update = Db::name("unit")->save($info);
  197. return $update ? error_show(0, "{$msg}成功") : error_show(1004, "{$msg}失败");
  198. }
  199. }