Newfill.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. <?php
  2. namespace app\admin\controller;
  3. use app\BaseController;
  4. use think\App;
  5. use think\facade\Db;
  6. use app\admin\model\ActionLog;
  7. use think\facade\Validate;
  8. //系统的组织架构
  9. class Newfill extends Base
  10. {
  11. public function __construct(App $app)
  12. {
  13. parent::__construct($app);
  14. }
  15. public function list()
  16. {
  17. $post = $this->request->only(['pid' => '', 'name' => '', 'nickname' => '', 'companyNo' => ''], 'post', 'trim');
  18. $depart = $condition = [['is_del', '=', 0]];
  19. if ($post['pid'] !== '') $condition[] = ["pid", "=", $post['pid']];
  20. if ($post['name'] != "") $condition[] = ["name", "like", "%{$post['name']}%"];
  21. if ($post['nickname'] != '') {
  22. $is = Db::name("depart_user")
  23. ->where([['nickname', 'like', "%{$post['nickname']}%"]])
  24. ->column('itemid');
  25. if (empty($is)) return json_show(1004, "未找到数据");
  26. $condition[] = ["id", "in", $is];
  27. }
  28. if ($post['pid'] === '' && $post['name'] == '' && $post['nickname'] == '') $condition[] = ["pid", "=", 0];
  29. $pidlist = Db::name("company_item")
  30. ->where($condition)
  31. ->column("pid");
  32. if (!empty($pidlist)) $depart[] = [["itemid", "in", $pidlist]];
  33. else {
  34. if ($post['pid'] === "") $depart[] = ['itemid', '=', 0];
  35. else $depart[] = ['itemid', '=', $post['pid']];
  36. }
  37. $item = Db::name("depart_user")->where($depart)->select()->toArray();
  38. $list = Db::name("company_item")->where($condition)->select()->toArray();
  39. return app_show(0, "获取成功", ["depart" => $list, "item" => $item]);
  40. }
  41. /*状态*/
  42. public function stat()
  43. {
  44. $id = $this->request->post('id/d', 0, 'trim');
  45. if ($id == 0) return json_show(1004, "参数id 不能为空");
  46. $s = Db::name('company_item')
  47. ->field('id,status')
  48. ->where('id', $id)
  49. ->findOrEmpty();
  50. if (empty($s)) return json_show(1005, "未找到部门");
  51. $var = $s['status'];
  52. $di = $s['status'] == 0 ? 1 : 0;
  53. $s['status'] = $di;
  54. $s['updatetime'] = date("Y-m-d H:i:s");
  55. // var_dump($s);
  56. $rs = Db::name('company_item')->where('id', $id)->save($s);
  57. if ($rs) {
  58. $orde = ["order_code" => $s['name'], "status" => $var, "action_remark" => '', "action_type" => "edit"];
  59. ActionLog::logAdd(['id' => $this->uid, 'nickname' => $this->uname], $orde, "company_item", $s['status'], $orde);
  60. return json_show(0, "更新成功");
  61. } else return json_show(1005, '更新失败');
  62. }
  63. /*职位*/
  64. public function userp()
  65. {
  66. // $token = isset($this->post['token']) ? trim($this->post['token']) : "";
  67. $id = isset($this->post['id']) ? intval($this->post['id']) : "";
  68. if ($id == "") {
  69. return error_show(1004, "参数id不能为空");
  70. }
  71. $t = Db::name('depart_user')->where(["id" => $id, "is_del" => 0])->find();
  72. if (empty($t)) {
  73. return error_show(1005, "未找到用户");
  74. }
  75. $position = isset($this->post['position']) ? intval($this->post['position']) : "";
  76. if ($position == "") {
  77. return error_show(1004, "职位不能为空");
  78. }
  79. Db::startTrans();
  80. if ($position == 2) {
  81. $f = Db::name('depart_user')->where(['itemid' => $t['itemid'], "is_del" => 0, "position" => 2])->find();
  82. if (!empty($f) && $f['id'] != $id) {
  83. $f['position'] = 1;
  84. $f['updatetime'] = date("Y-m-d H:i:s");
  85. $m = Db::name('depart_user')->save($f);
  86. $orde = ["order_code" => $t['nickname'], "status" => 0, "action_remark" => '', "action_type" => "edit"];
  87. ActionLog::logAdd($this->post['token'], $orde, "depart_user", 0, $orde);
  88. if ($m == false) {
  89. Db::rollback();
  90. return error_show(1004, "负责人修改失败");
  91. }
  92. }
  93. }
  94. $t['position'] = $position;
  95. $t['updatetime'] = date("Y-m-d H:i:s");
  96. //var_dump($t);
  97. $ti = Db::name('depart_user')->save($t);
  98. if ($ti == true) {
  99. Db::commit();
  100. return app_show(0, "修改成功");
  101. } else {
  102. Db::rollback();
  103. return error_show(1004, "修改失败");
  104. }
  105. }
  106. public function add()
  107. {
  108. // $token = isset($this->post['token']) ? trim($this->post['token']) : "";
  109. // if($token==""){
  110. // return error_show(101,'token不能为空');
  111. // }
  112. // $effetc = VerifyTokens($token);
  113. // if(!empty($effetc) && $effetc['code']!=0){
  114. // return error_show($effetc['code'],$effetc['message']);
  115. // }
  116. $companyNo = isset($this->post['companyNo']) && $this->post['companyNo'] !== "" ? trim($this->post['companyNo']) : "";
  117. if ($companyNo == '') return json_show(1004, '所属企业不能为空');
  118. $item_name = isset($this->post['name']) && $this->post['name'] !== "" ? trim($this->post['name']) : "";
  119. if ($item_name == "") {
  120. return error_show(0, "部门名称不能为空");
  121. }
  122. $repeat_name = Db::name("company_item")
  123. ->where(["is_del" => 0, "name" => $item_name, 'companyNo' => $companyNo])
  124. ->findOrEmpty();
  125. if (!empty($repeat_name)) {
  126. return error_show(1004, "部门名称已存在");
  127. }
  128. $pid = isset($this->post['pid']) && $this->post['pid'] !== "" ? trim($this->post['pid']) : "";
  129. if ($pid === "") {
  130. return error_show(1002, "父级id不能为空");
  131. }
  132. //$spid = Db::name('company_item')->where(['id'=>$pid,'is_del'=>0])->find();
  133. $spid = ['depart_link' => ""];
  134. if ($pid != 0) {
  135. $spid = Db::name('company_item')->where(['id' => $pid, 'is_del' => 0])->find();
  136. if (empty($spid)) {
  137. return error_show(1004, "父级数据不能为空");
  138. }
  139. }
  140. $level = isset($this->post['level']) && $this->post['level'] !== "" ? trim($this->post['level']) : "1";
  141. $weight = isset($this->post['weight']) && $this->post['weight'] !== "" ? trim($this->post['weight']) : "1";
  142. Db::startTrans();
  143. try {
  144. $data = [
  145. "name" => $item_name,
  146. "pid" => $pid,
  147. "level" => $level,
  148. "weight" => $weight,
  149. "is_del" => 0,
  150. 'companyNo' => $companyNo,
  151. "addtime" => date("Y-m-d H:i:s"),
  152. "updatetime" => date("Y-m-d H:i:s"),
  153. ];
  154. $t = Db::name("company_item")->insert($data, true);
  155. if ($t > 0) {
  156. $orde = ["order_code" => $repeat_name['name'], "status" => 0, "action_remark" => '', "action_type" => "creat"];
  157. ActionLog::logAdd(['id' => $this->uid, 'nickname' => $this->uname], $orde, "company_item", 0, $orde);
  158. $depart_link = $spid['depart_link'] . "{$t}-";
  159. $level = explode('-', $depart_link);
  160. $level = array_filter($level);
  161. $level = count($level);
  162. $i = ['depart_link' => $depart_link, 'level' => $level];
  163. $o = Db::name('company_item')->where(['id' => $t])->update($i);
  164. if ($o) {
  165. $stn = ["order_code" => $repeat_name['name'], "status" => 0, "action_remark" => '', "action_type" => "edit"];
  166. ActionLog::logAdd(['id' => $this->uid, 'nickname' => $this->uname], $stn, "company_item", 0, $stn);
  167. Db::commit();
  168. return error_show(0, "添加成功");
  169. }
  170. }
  171. Db::rollback();
  172. } catch (\Exception $e) {
  173. Db::rollback();
  174. return error_show(1003, $e->getMessage());
  175. }
  176. }
  177. public function refresh()
  178. {
  179. // $token = isset($this->post['token']) ? trim($this->post['token']) : "";
  180. // if ($token == "") {
  181. // return error_show(101, 'token不能为空');
  182. // }
  183. // $effetc = VerifyTokens($token);
  184. // if (!empty($effetc) && $effetc['code'] != 0) {
  185. // return error_show($effetc['code'], $effetc['message']);
  186. // }
  187. $id = isset($this->post['id']) ? intval($this->post['id']) : "";
  188. $items = Db::name("company_item")->where(['id' => $id, 'is_del' => 0])->find();
  189. if ($items == false) {
  190. return error_show(1003, "部门信息不存在");
  191. }
  192. $pid = isset($this->post['pid']) && $this->post['pid'] !== "" ? trim($this->post['pid']) : "";
  193. if ($pid == "") {
  194. return error_show(1002, "父级id不能为空");
  195. }
  196. $companyNo = isset($this->post['companyNo']) && $this->post['companyNo'] !== "" ? trim($this->post['companyNo']) : "";
  197. if ($companyNo == "") return error_show(1002, "所属企业不能为空");
  198. $spid = ['depart_link' => ""];
  199. if ($pid != 0) {
  200. $spid = Db::name('company_item')->where(['id' => $pid, 'is_del' => 0])->find();
  201. if (empty($spid)) {
  202. return error_show(1004, "父级数据不能为空");
  203. }
  204. }
  205. $weight = isset($this->post['weight']) && $this->post['weight'] !== "" ? trim($this->post['weight']) : "1";
  206. $itemname = isset($this->post['name']) ? trim($this->post['name']) : "";
  207. if ($itemname == "") {
  208. return error_show(1002, "部门名称不能为空");
  209. }
  210. $repeat_name = Db::name("company_item")->where(["is_del" => 0, "name" => $itemname, 'companyNo' => $companyNo])->where('id', '<>', $id)->find();
  211. //echo Db::name("company_item")->getLastSql();
  212. if (!empty($repeat_name)) {
  213. return error_show(1004, "部门名称已存在");
  214. }
  215. $depart_link = $spid['depart_link'] . "{$id}-";
  216. $itemlevel = explode('-', $depart_link);
  217. $itemlevel = array_filter($itemlevel);
  218. $itemlevel = count($itemlevel);
  219. $item = [
  220. "id" => $id,
  221. "name" => $itemname,
  222. "updatetime" => date("Y-m-d H:i:s"),
  223. "weight" => $weight, "pid" => $pid,
  224. 'depart_link' => $depart_link,
  225. 'level' => $itemlevel,
  226. 'companyNo' => $companyNo
  227. ];
  228. $re = Db::name("company_item")->save($item);
  229. $stn = ["order_code" => $repeat_name['name'], "status" => 0, "action_remark" => '', "action_type" => "edit"];
  230. ActionLog::logAdd(['id' => $this->uid, 'nickname' => $this->uname], $stn, "company_item", 0, $stn);
  231. return $re ? app_show(0, "信息更新成功") : error_show(1003, "信息更新失败");
  232. }
  233. /*查询*/
  234. public function query()
  235. {
  236. $companyNo = isset($this->post['companyNo']) ? trim($this->post['companyNo']) : "";
  237. if ($companyNo == "") return error_show(1004, '所属企业不能为空');
  238. // $effetc = VerifyTokens($token);
  239. // if (!empty($effetc) && $effetc['code'] != 0) {
  240. // return error_show($effetc['code'], $effetc['message']);
  241. // }
  242. $dati = Db::name("company_item")
  243. ->where(['pid' => 0, 'is_del' => 0, 'companyNo' => $companyNo])
  244. ->order("weight desc")
  245. ->select()
  246. ->toArray();
  247. $k = [];
  248. foreach ($dati as $key => $value) {
  249. // $temp=[];
  250. //$temp= Db::name("company_item")->where(['pid'=>$value['id'],'is_del'=>0])->order("weight desc")->select();
  251. // $value['child'] =$temp;
  252. // $k[]=$value;
  253. $k[] = crea($value);
  254. }
  255. return app_show(0, "获取成功", $k);
  256. }
  257. public function itemdel()
  258. {
  259. // $token = isset($this->post['token']) ? trim($this->post['token']) : "";
  260. // if ($token == "") {
  261. // return error_show(101, 'token不能为空');
  262. // }
  263. // $effetc = VerifyTokens($token);
  264. // if (!empty($effetc) && $effetc['code'] != 0) {
  265. // return error_show($effetc['code'], $effetc['message']);
  266. // }
  267. $id = isset($this->post['id']) ? intval($this->post['id']) : "";
  268. $items = Db::name("company_item")
  269. ->field('id,is_del,updatetime')
  270. ->where(['is_del' => 0, 'id' => $id])
  271. ->findOrEmpty();
  272. if (empty($items)) {
  273. return error_show(1003, "部门信息不存在");
  274. }
  275. $items['is_del'] = 1;
  276. $items['updatetime'] = date("Y-m-d H:i:s");
  277. $result = Db::name("company_item")->save($items);
  278. $stn = ["order_code" => $items['name'], "status" => 0, "action_remark" => '', "action_type" => "delete"];
  279. ActionLog::logAdd(['id' => $this->uid, 'nickname' => $this->uname], $stn, "company_item", 0, $stn);
  280. return $result ? app_show(0, "删除成功") : error_show(1003, "删除失败");
  281. }
  282. }