CompanyItem.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  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. ->column('companyNo');
  193. $company_1 = Db::name('headquarters')
  194. ->where(['is_del' => 0, 'code' => $companyNo])
  195. ->column('id,code,name,type', 'code');
  196. $company_2 = Db::name('headquarters')
  197. ->where(['is_del' => 0, 'relation_code' => $companyNo])
  198. ->column('id,relation_code code,name,type', 'relation_code');
  199. $company = array_merge($company_1, $company_2);
  200. foreach ($company as &$value) {
  201. $value['child'] = Db::name('company_item')
  202. ->where(['pid' => 0, 'is_del' => 0, 'companyNo' => $value['code']])
  203. ->order("weight desc")
  204. ->select()
  205. ->toArray();
  206. foreach ($value['child'] as &$item) {
  207. $item = crea($item);
  208. }
  209. }
  210. return json_show(0, '获取成功', array_values($company));
  211. }
  212. //删除
  213. public function delete()
  214. {
  215. $id = $this->request->filter('trim')->post('id/d', 0);
  216. $rs = Db::name("company_item")
  217. ->where(['id' => $id, 'is_del' => 0])
  218. ->update(['is_del' => 1, 'updatetime' => date('Y-m-d H:i:s')]);
  219. return $rs ? json_show(0, '删除成功') : json_show(1003, '删除失败');
  220. }
  221. //启禁用
  222. public function status()
  223. {
  224. $id = $this->request->filter('trim')->post('id/d', 0);
  225. $rs = Db::name("company_item")
  226. ->field('id,status')
  227. ->where(['id' => $id, 'is_del' => 0])
  228. ->findOrEmpty();
  229. if (empty($rs)) return json_show(1005, '未找到部门');
  230. $status = $rs['status'] == 1 ? 0 : 1;
  231. $rs = Db::name("company_item")
  232. ->where(['id' => $id, 'is_del' => 0])
  233. ->update(['status' => $status, 'updatetime' => date('Y-m-d H:i:s')]);
  234. return $rs ? json_show(0, '更新成功') : json_show(1003, '更新失败');
  235. }
  236. //改变职位
  237. public function userp()
  238. {
  239. $post = $this->request->only(['account_id', 'itemid', 'position'], 'post', 'trim');
  240. $val = Validate::rule([
  241. 'account_id|账户id' => 'require|number|gt:0',
  242. 'itemid|部门id' => 'require|number|gt:0',
  243. 'position|职位' => 'require|number|in:1,2',
  244. ]);
  245. if ($val->check($post) == false) return json_show(1005, $val->getError());
  246. $rs = Db::name('account_item')
  247. ->where(['account_id' => $post['account_id'], 'itemid' => $post['itemid']])
  248. ->update(['position' => $post['position'], 'updatetime' => date('Y-m-d H:i:s')]);
  249. return $rs ? json_show(0, '修改成功') : json_show(1005, '修改失败');
  250. }
  251. //获取部门层级列表
  252. public function getPart()
  253. {
  254. $itemids = $this->request->filter('trim')->post('itemid');
  255. if (is_array($itemids)) {
  256. $tmp = [];
  257. foreach ($itemids as $itemid) {
  258. if (!isset($tmp[$itemid])) $tmp[$itemid] = get_part($itemid);
  259. }
  260. return json_show(0, '获取成功', $tmp);
  261. } else return json_show(0, '获取成功', get_part($itemids));
  262. }
  263. //获取详情
  264. public function info()
  265. {
  266. $id = $this->request->post('id/d', 0, 'trim');
  267. $rs = Db::name('company_item')
  268. ->field(true)
  269. ->where(['id' => $id, 'is_del' => 0])
  270. ->findOrEmpty();
  271. return json_show(0, '获取部门详情成功', $rs);
  272. }
  273. //获取某个用户所属部门名称
  274. //$uid int 用户id
  275. //$cache bool 是否启用缓存,(默认启用,不启用的话直接从数据库查)
  276. //$get_tops bool 是否获取多级部门,例如 万宇恒通/采购部/仓储物流,默认不获取
  277. public function getCompanyNameByUid()
  278. {
  279. $param = $this->request->only(['uid' => '', 'get_tops' => 1], 'post', 'trim');
  280. $itemids = Db::name('account_item')
  281. ->whereIn('account_id', $param['uid'])
  282. ->column('itemid', 'account_id');
  283. $res = $item_tmp = [];
  284. //获取所有部门id对应的部门名称
  285. foreach ($itemids as $itemid) {
  286. if (!isset($item_tmp[$itemid])) {
  287. if ($param['get_tops']==1) $tmp = get_part($itemid);
  288. else $tmp = Db::name('company_item')
  289. ->field('id,name,pid')
  290. ->where(['is_del' => 0, 'id' => $itemid])
  291. ->select()
  292. ->toArray();//为了跟上面保持数据一致
  293. $item_tmp[$itemid] = implode('/', array_column($tmp, 'name'));
  294. }
  295. }
  296. if (is_numeric($param['uid'])) $param['uid'] = [$param['uid']];
  297. foreach ($itemids as $uid=>$itemid) {
  298. if(!isset($res[$uid])) $res[$uid] = $item_tmp[$itemid] ?? '';
  299. }
  300. return json_show(0, '获取成功', $res);
  301. }
  302. //根据某个关键字匹配所有子级部门及用户
  303. public function getCompanyItemUserByName()
  304. {
  305. $param = $this->request->only(['company_name'=>"","depart_id"=>""], 'post', 'trim');
  306. $where=[["is_del","=",0]];
  307. //先查询所有的部门id(包括子部门)
  308. if($param['company_name']!='')$where[]=["name","like",'%' .$param['company_name']. '%'];
  309. if($param['depart_id']!='')$where[]=["id","=",$param['id']];
  310. $company_ids = Db::name("company_item")
  311. ->where($where)
  312. ->column('id');
  313. $pid = $company_ids;
  314. for ($i = 1; $i > 0; $i++) {
  315. $tmp = Db::name("company_item")
  316. ->where(['is_del' => 0])
  317. ->whereIn('pid', $pid)
  318. ->column('id');
  319. if (empty($tmp)) break;
  320. else {
  321. $company_ids = array_merge($company_ids, $tmp);
  322. $pid = $tmp;
  323. }
  324. }
  325. $company_ids = array_unique($company_ids);
  326. sort($company_ids);
  327. $uid = Db::name('account_item')
  328. ->alias('a')
  329. ->leftJoin('account b', 'b.id=a.account_id')
  330. ->where(['b.is_del' => 0, 'b.status' => 1])
  331. ->whereIn('a.itemid', $company_ids)
  332. ->column('a.account_id');
  333. return json_show(0, '获取成功', $uid);
  334. }
  335. }