CompanyItem.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  1. <?php
  2. namespace app\controller;
  3. use app\BaseController;
  4. use app\model\Account;
  5. use app\model\AccountCompany;
  6. use think\Exception;
  7. use think\facade\Db;
  8. use think\facade\Validate;
  9. //【组织架构】
  10. class CompanyItem extends BaseController
  11. {
  12. //列表
  13. public function getList()
  14. {
  15. $post = $this->request->only(['pid' => '', 'name' => '', 'nickname' => '', 'companyNo' => '', 'companyName' => ''], 'post', 'trim');
  16. $condition = $depart = [['a.is_del', '=', 0]];
  17. if ($post['pid'] !== '') $condition[] = ["a.pid", "=", $post['pid']];
  18. if ($post['name'] != "") $condition[] = ["a.name", "like", "%{$post['name']}%"];
  19. if ($post['pid'] === '' && $post['name'] == '' && $post['nickname'] == '') $condition[] = ["a.pid", "=", 0];
  20. if ($post['nickname'] != '') {
  21. $is = Db::name("account")
  22. ->alias('a')
  23. ->leftJoin('account_item b', 'b.account_id=a.id')
  24. ->leftJoin('user c', 'c.account_id=a.id')
  25. ->where('a.is_del', 0)
  26. ->whereLike('c.nickname', '%' . $post['nickname'] . '%')
  27. ->column('b.itemid');
  28. if (empty($is)) return json_show(1004, "未找到数据");
  29. $condition[] = ["a.id", "in", $is];
  30. }
  31. if ($post['companyNo'] != '') $condition[] = ['a.companyNo', 'like', '%' . $post['companyNo'] . '%'];
  32. $pidlist = Db::name("company_item")
  33. ->alias('a')
  34. ->where($condition)
  35. ->column("pid");
  36. if (!empty($pidlist)) $depart[] = [["b.itemid", "in", $pidlist]];
  37. else {
  38. if ($post['pid'] === "") $depart[] = ['b.itemid', '=', 0];
  39. else $depart[] = ['b.itemid', '=', $post['pid']];
  40. }
  41. $item = Db::name("account")
  42. ->alias('a')
  43. ->field('a.id account_id,c.nickname,b.itemid,a.is_del,a.status,b.position')
  44. ->leftJoin('account_item b', 'b.account_id=a.id')
  45. ->leftJoin('user c', 'c.account_id=a.id')
  46. ->where($depart)
  47. ->select()
  48. ->toArray();
  49. if ($post['companyName'] != '') $condition[] = ['b.name', 'like', '%' . $post['companyName'] . '%'];
  50. $list = Db::name("company_item")
  51. ->alias('a')
  52. ->field('a.*,b.name companyName')
  53. ->leftJoin('headquarters b', 'b.code=a.companyNo')
  54. ->where($condition)
  55. ->select()
  56. ->toArray();
  57. return json_show(0, '获取成功', ["depart" => $list, "item" => $item]);
  58. }
  59. //添加
  60. public function add()
  61. {
  62. $param = $this->request->only([
  63. 'companyNo',
  64. 'name',
  65. 'pid',
  66. 'level' => 1,
  67. 'weight' => 1,
  68. ], 'post', 'trim');
  69. $val = Validate::rule([
  70. 'companyNo|所属企业' => 'require|max:255',
  71. 'name|部门名称' => 'require|max:255',
  72. 'pid|父级id' => 'require|number|egt:0',
  73. 'level|部门层级' => 'number|gt:0',
  74. 'weight|排序权重' => 'number|gt:0',
  75. ]);
  76. if ($val->check($param) == false) return json_show(1004, $val->getError());
  77. Db::startTrans();
  78. try {
  79. $tmp = Db::name('company_item')
  80. ->field('id')
  81. ->where(["is_del" => 0, "name" => $param['name'], 'companyNo' => $param['companyNo']])
  82. ->findOrEmpty();
  83. if (!empty($tmp)) throw new Exception('部门名称已存在');
  84. $spid = ['depart_link' => ''];
  85. if ($param['pid'] != 0) {
  86. $spid = Db::name('company_item')
  87. ->field('depart_link')
  88. ->where(['id' => $param['pid'], 'is_del' => 0])
  89. ->find();
  90. if (empty($spid)) throw new Exception('父级数据不能为空');
  91. }
  92. $id = Db::name('company_item')
  93. ->insertGetId([
  94. "name" => $param['name'],
  95. "pid" => $param['pid'],
  96. "level" => $param['level'],
  97. "weight" => $param['weight'],
  98. "is_del" => 0,
  99. 'companyNo' => $param['companyNo'],
  100. "addtime" => date("Y-m-d H:i:s"),
  101. "updatetime" => date("Y-m-d H:i:s"),
  102. ]);
  103. $depart_link = $spid['depart_link'] . "{$id}-";
  104. $level = explode('-', $depart_link);
  105. $level = array_filter($level);
  106. $level = count($level);
  107. Db::name('company_item')
  108. ->where('id', $id)
  109. ->update([
  110. 'depart_link' => $depart_link,
  111. 'level' => $level
  112. ]);
  113. Db::commit();
  114. return json_show(0, '添加成功');
  115. } catch (Exception $e) {
  116. Db::rollback();
  117. return json_show(1002, '添加失败,' . $e->getMessage());
  118. }
  119. }
  120. //编辑
  121. public function update()
  122. {
  123. $param = $this->request->only(['id', 'companyNo', 'name', 'pid', 'level' => 1, 'weight' => 1,], 'post', 'trim');
  124. $val = Validate::rule([
  125. 'id' => 'require|number|gt:0',
  126. 'companyNo|所属企业' => 'require|max:255',
  127. 'name|部门名称' => 'require|max:255',
  128. 'pid|父级id' => 'require|number|egt:0',
  129. 'level|部门层级' => 'number|gt:0',
  130. 'weight|排序权重' => 'number|gt:0',
  131. ]);
  132. if ($val->check($param) == false) return json_show(1004, $val->getError());
  133. Db::startTrans();
  134. try {
  135. $tmp = Db::name("company_item")
  136. ->field('id')
  137. ->where(['id' => $param['id'], 'is_del' => 0])
  138. ->findOrEmpty();
  139. if (empty($tmp)) throw new Exception('部门信息不存在');
  140. $tmp = Db::name('company_item')
  141. ->field('id')
  142. ->where(["is_del" => 0, "name" => $param['name'], 'companyNo' => $param['companyNo']])
  143. ->where('id', '<>', $param['id'])
  144. ->findOrEmpty();
  145. if (!empty($tmp)) throw new Exception('部门名称已存在');
  146. $spid = ['depart_link' => ''];
  147. if ($param['pid'] != 0) {
  148. $spid = Db::name('company_item')
  149. ->field('depart_link')
  150. ->where(['id' => $param['pid'], 'is_del' => 0])
  151. ->find();
  152. if (empty($spid)) throw new Exception('父级数据不能为空');
  153. }
  154. Db::name('company_item')
  155. ->where(['id' => $param['id'], 'is_del' => 0])
  156. ->update([
  157. "name" => $param['name'],
  158. "pid" => $param['pid'],
  159. "level" => $param['level'],
  160. "weight" => $param['weight'],
  161. 'companyNo' => $param['companyNo'],
  162. "updatetime" => date("Y-m-d H:i:s"),
  163. ]);
  164. $depart_link = $spid['depart_link'] . "{$param['id']}-";
  165. $level = explode('-', $depart_link);
  166. $level = array_filter($level);
  167. $level = count($level);
  168. Db::name('company_item')
  169. ->where('id', $param['id'])
  170. ->update([
  171. 'depart_link' => $depart_link,
  172. 'level' => $level
  173. ]);
  174. Db::commit();
  175. return json_show(0, '信息更新成功');
  176. } catch (Exception $e) {
  177. Db::rollback();
  178. return json_show(1002, '信息更新失败,' . $e->getMessage());
  179. }
  180. }
  181. //查询,层级列表
  182. public function query()
  183. {
  184. $param = $this->request->filter('trim')->only(['companyNo' => ''], 'post');
  185. $where = [['pid', '=', 0], ['is_del', '=', 0]];
  186. if ($param['companyNo'] != '') $where[] = ['companyNo', '=', $param['companyNo']];
  187. //所有已经存在的公司
  188. $companyNo = Db::name('company_item')
  189. ->field('companyNo')
  190. ->where($where)
  191. ->group('companyNo')
  192. ->buildSql();
  193. $company = Db::name('headquarters')
  194. ->field('id,code,name,type')
  195. ->where('is_del', 0)
  196. ->where('code IN ' . $companyNo)
  197. ->select()
  198. ->toArray();
  199. foreach ($company as &$value) {
  200. $value['child'] = Db::name('company_item')
  201. ->where(['pid' => 0, 'is_del' => 0, 'companyNo' => $value['code']])
  202. ->order("weight desc")
  203. ->select()
  204. ->toArray();
  205. foreach ($value['child'] as &$item) {
  206. $item = crea($item);
  207. }
  208. }
  209. return json_show(0, '获取成功', $company);
  210. }
  211. //删除
  212. public function delete()
  213. {
  214. $id = $this->request->filter('trim')->post('id/d', 0);
  215. $rs = Db::name("company_item")
  216. ->where(['id' => $id, 'is_del' => 0])
  217. ->update(['is_del' => 1, 'updatetime' => date('Y-m-d H:i:s')]);
  218. return $rs ? json_show(0, '删除成功') : json_show(1003, '删除失败');
  219. }
  220. //启禁用
  221. public function status()
  222. {
  223. $id = $this->request->filter('trim')->post('id/d', 0);
  224. $rs = Db::name("company_item")
  225. ->field('id,status')
  226. ->where(['id' => $id, 'is_del' => 0])
  227. ->findOrEmpty();
  228. if (empty($rs)) return json_show(1005, '未找到部门');
  229. $status = $rs['status'] == 1 ? 0 : 1;
  230. $rs = Db::name("company_item")
  231. ->where(['id' => $id, 'is_del' => 0])
  232. ->update(['status' => $status, 'updatetime' => date('Y-m-d H:i:s')]);
  233. return $rs ? json_show(0, '更新成功') : json_show(1003, '更新失败');
  234. }
  235. //改变职位
  236. public function userp()
  237. {
  238. $post = $this->request->only(['account_id', 'itemid', 'position'], 'post', 'trim');
  239. $val = Validate::rule([
  240. 'account_id|账户id' => 'require|number|gt:0',
  241. 'itemid|部门id' => 'require|number|gt:0',
  242. 'position|职位' => 'require|number|in:1,2',
  243. ]);
  244. if ($val->check($post) == false) return json_show(1005, $val->getError());
  245. $rs = Db::name('account_item')
  246. ->where(['account_id' => $post['account_id'], 'itemid' => $post['itemid']])
  247. ->update(['position' => $post['position'], 'updatetime' => date('Y-m-d H:i:s')]);
  248. return $rs ? json_show(0, '修改成功') : json_show(1005, '修改失败');
  249. }
  250. //获取部门层级列表
  251. public function getPart()
  252. {
  253. $itemids = $this->request->filter('trim')->post('itemid');
  254. if (is_array($itemids)) {
  255. $tmp = [];
  256. foreach ($itemids as $itemid) {
  257. if (!isset($tmp[$itemid])) $tmp[$itemid] = get_part($itemid);
  258. }
  259. return json_show(0, '获取成功', $tmp);
  260. } else return json_show(0, '获取成功', get_part($itemids));
  261. }
  262. //获取详情
  263. public function info()
  264. {
  265. $id = $this->request->post('id/d', 0, 'trim');
  266. $rs = Db::name('company_item')
  267. ->field(true)
  268. ->where(['id' => $id, 'is_del' => 0])
  269. ->findOrEmpty();
  270. return json_show(0, '获取部门详情成功', $rs);
  271. }
  272. //获取某个用户所属部门名称
  273. //$uid int 用户id
  274. //$cache bool 是否启用缓存,(默认启用,不启用的话直接从数据库查)
  275. //$get_tops bool 是否获取多级部门,例如 万宇恒通/采购部/仓储物流,默认不获取
  276. public function getCompanyNameByUid()
  277. {
  278. $param = $this->request->only(['uid' => '', 'get_tops' => 1], 'post', 'trim');
  279. $itemids = Db::name('account_item')
  280. ->whereIn('account_id', $param['uid'])
  281. ->column('itemid', 'account_id');
  282. $res = $item_tmp = [];
  283. //获取所有部门id对应的部门名称
  284. foreach ($itemids as $itemid) {
  285. if (!isset($item_tmp[$itemid])) {
  286. if ($param['get_tops'] == 1) $tmp = get_part($itemid);
  287. else $tmp = Db::name('company_item')
  288. ->field('id,name,pid')
  289. ->where(['is_del' => 0, 'id' => $itemid])
  290. ->select()
  291. ->toArray();//为了跟上面保持数据一致
  292. $item_tmp[$itemid] = implode('/', array_column($tmp, 'name'));
  293. }
  294. }
  295. if (is_numeric($param['uid'])) $param['uid'] = [$param['uid']];
  296. foreach ($param['uid'] as $uid) {
  297. if (!isset($res[$uid])) $res[$uid] = $item_tmp[$itemids[$uid]] ?? '';
  298. }
  299. return json_show(0, '获取成功', $res);
  300. }
  301. //根据某个关键字匹配所有子级部门及用户
  302. public function getCompanyItemUserByName()
  303. {
  304. $company_name = $this->request->post('company_name', '', 'trim');
  305. //先查询所有的部门id(包括子部门)
  306. $company_ids = Db::name("company_item")
  307. ->where(['is_del' => 0])
  308. ->whereLike('name', '%' . $company_name . '%')
  309. ->column('id');
  310. $pid = $company_ids;
  311. for ($i = 1; $i > 0; $i++) {
  312. $tmp = Db::name("company_item")
  313. ->where(['is_del' => 0])
  314. ->whereIn('pid', $pid)
  315. ->column('id');
  316. if (empty($tmp)) break;
  317. else {
  318. $company_ids = array_merge($company_ids, $tmp);
  319. $pid = $tmp;
  320. }
  321. }
  322. $company_ids = array_unique($company_ids);
  323. sort($company_ids);
  324. $uid = Db::name('account_item')
  325. ->alias('a')
  326. ->leftJoin('account b', 'b.id=a.account_id')
  327. ->where(['b.is_del' => 0, 'b.status' => 1])
  328. ->whereIn('a.itemid', $company_ids)
  329. ->column('a.account_id');
  330. return json_show(0, '获取成功', $uid);
  331. }
  332. }