request->only(['pid' => '', 'name' => '', 'nickname' => '', 'companyNo' => '', 'companyName' => ''], 'post', 'trim'); $condition = $depart = [['a.is_del', '=', 0]]; if ($post['pid'] !== '') $condition[] = ["a.pid", "=", $post['pid']]; if ($post['name'] != "") $condition[] = ["a.name", "like", "%{$post['name']}%"]; if ($post['pid'] === '' && $post['name'] == '' && $post['nickname'] == '') $condition[] = ["a.pid", "=", 0]; if ($post['nickname'] != '') { $is = Db::name("account") ->alias('a') ->leftJoin('account_item b', 'b.account_id=a.id') ->leftJoin('user c', 'c.account_id=a.id') ->where('a.is_del', 0) ->whereLike('c.nickname', '%' . $post['nickname'] . '%') ->column('b.itemid'); if (empty($is)) return json_show(1004, "未找到数据"); $condition[] = ["a.id", "in", $is]; } if ($post['companyNo'] != '') $condition[] = ['a.companyNo', 'like', '%' . $post['companyNo'] . '%']; $pidlist = Db::name("company_item") ->alias('a') ->where($condition) ->column("pid"); if (!empty($pidlist)) $depart[] = [["b.itemid", "in", $pidlist]]; else { if ($post['pid'] === "") $depart[] = ['b.itemid', '=', 0]; else $depart[] = ['b.itemid', '=', $post['pid']]; } $item = Db::name("account") ->alias('a') ->field('a.id account_id,c.nickname,b.itemid,a.is_del,a.status,b.position') ->leftJoin('account_item b', 'b.account_id=a.id') ->leftJoin('user c', 'c.account_id=a.id') ->where($depart) ->select() ->toArray(); if ($post['companyName'] != '') $condition[] = ['b.name', 'like', '%' . $post['companyName'] . '%']; $list = Db::name("company_item") ->alias('a') ->field('a.*,b.name companyName') ->leftJoin('headquarters b', 'b.code=a.companyNo') ->where($condition) ->select() ->toArray(); return json_show(0, '获取成功', ["depart" => $list, "item" => $item]); } //添加 public function add() { $param = $this->request->only([ 'companyNo', 'name', 'pid', 'level' => 1, 'weight' => 1, ], 'post', 'trim'); $val = Validate::rule([ 'companyNo|所属企业' => 'require|max:255', 'name|部门名称' => 'require|max:255', 'pid|父级id' => 'require|number|egt:0', 'level|部门层级' => 'number|gt:0', 'weight|排序权重' => 'number|gt:0', ]); if ($val->check($param) == false) return json_show(1004, $val->getError()); Db::startTrans(); try { $tmp = Db::name('company_item') ->field('id') ->where(["is_del" => 0, "name" => $param['name'], 'companyNo' => $param['companyNo']]) ->findOrEmpty(); if (!empty($tmp)) throw new Exception('部门名称已存在'); $spid = ['depart_link' => '']; if ($param['pid'] != 0) { $spid = Db::name('company_item') ->field('depart_link') ->where(['id' => $param['pid'], 'is_del' => 0]) ->find(); if (empty($spid)) throw new Exception('父级数据不能为空'); } $id = Db::name('company_item') ->insertGetId([ "name" => $param['name'], "pid" => $param['pid'], "level" => $param['level'], "weight" => $param['weight'], "is_del" => 0, 'companyNo' => $param['companyNo'], "addtime" => date("Y-m-d H:i:s"), "updatetime" => date("Y-m-d H:i:s"), ]); $depart_link = $spid['depart_link'] . "{$id}-"; $level = explode('-', $depart_link); $level = array_filter($level); $level = count($level); Db::name('company_item') ->where('id', $id) ->update([ 'depart_link' => $depart_link, 'level' => $level ]); Db::commit(); return json_show(0, '添加成功'); } catch (Exception $e) { Db::rollback(); return json_show(1002, '添加失败,' . $e->getMessage()); } } //编辑 public function update() { $param = $this->request->only(['id', 'companyNo', 'name', 'pid', 'level' => 1, 'weight' => 1], 'post', 'trim'); $val = Validate::rule([ 'id' => 'require|number|gt:0', 'companyNo|所属企业' => 'require|max:255', 'name|部门名称' => 'require|max:255', 'pid|父级id' => 'require|number|egt:0', 'level|部门层级' => 'number|gt:0', 'weight|排序权重' => 'number|gt:0', ]); if ($val->check($param) == false) return json_show(1004, $val->getError()); Db::startTrans(); try { $tmp = Db::name("company_item") ->field('id') ->where(['id' => $param['id'], 'is_del' => 0]) ->findOrEmpty(); if (empty($tmp)) throw new Exception('部门信息不存在'); $tmp = Db::name('company_item') ->field('id') ->where(["is_del" => 0, "name" => $param['name'], 'companyNo' => $param['companyNo']]) ->where('id', '<>', $param['id']) ->findOrEmpty(); if (!empty($tmp)) throw new Exception('部门名称已存在'); $spid = ['depart_link' => '']; if ($param['pid'] != 0) { $spid = Db::name('company_item') ->field('depart_link') ->where(['id' => $param['pid'], 'is_del' => 0]) ->find(); if (empty($spid)) throw new Exception('父级数据不能为空'); } Db::name('company_item') ->where(['id' => $param['id'], 'is_del' => 0]) ->update([ "name" => $param['name'], "pid" => $param['pid'], "level" => $param['level'], "weight" => $param['weight'], 'companyNo' => $param['companyNo'], "updatetime" => date("Y-m-d H:i:s"), ]); $depart_link = $spid['depart_link'] . "{$param['id']}-"; $level = explode('-', $depart_link); $level = array_filter($level); $level = count($level); Db::name('company_item') ->where('id', $param['id']) ->update([ 'depart_link' => $depart_link, 'level' => $level ]); Db::commit(); return json_show(0, '信息更新成功'); } catch (Exception $e) { Db::rollback(); return json_show(1002, '信息更新失败,' . $e->getMessage()); } } //查询,层级列表 public function query() { $param = $this->request->filter('trim')->only(['companyNo' => ''], 'post'); $where = [['pid', '=', 0], ['is_del', '=', 0]]; if ($param['companyNo'] != '') $where[] = ['companyNo', '=', $param['companyNo']]; //所有已经存在的公司 $companyNo = Db::name('company_item') ->field('companyNo') ->where($where) ->group('companyNo') ->column('companyNo'); $company_1 = Db::name('headquarters') ->where(['is_del' => 0, 'code' => $companyNo]) ->column('id,code,name,type', 'code'); $company_2 = Db::name('headquarters') ->where(['is_del' => 0, 'relation_code' => $companyNo]) ->column('id,relation_code code,name,type', 'relation_code'); $company = array_merge($company_1, $company_2); foreach ($company as &$value) { $value['child'] = Db::name('company_item') ->where(['pid' => 0, 'is_del' => 0, 'companyNo' => $value['code']]) ->order("weight desc") ->select() ->toArray(); foreach ($value['child'] as &$item) { $item = crea($item); } } return json_show(0, '获取成功', array_values($company)); } //删除 public function delete() { $id = $this->request->filter('trim')->post('id/d', 0); $rs = Db::name("company_item") ->where(['id' => $id, 'is_del' => 0]) ->update(['is_del' => 1, 'updatetime' => date('Y-m-d H:i:s')]); return $rs ? json_show(0, '删除成功') : json_show(1003, '删除失败'); } //启禁用 public function status() { $id = $this->request->filter('trim')->post('id/d', 0); $rs = Db::name("company_item") ->field('id,status') ->where(['id' => $id, 'is_del' => 0]) ->findOrEmpty(); if (empty($rs)) return json_show(1005, '未找到部门'); $status = $rs['status'] == 1 ? 0 : 1; $rs = Db::name("company_item") ->where(['id' => $id, 'is_del' => 0]) ->update(['status' => $status, 'updatetime' => date('Y-m-d H:i:s')]); return $rs ? json_show(0, '更新成功') : json_show(1003, '更新失败'); } //改变职位 public function userp() { $post = $this->request->only(['account_id', 'itemid', 'position'], 'post', 'trim'); $val = Validate::rule([ 'account_id|账户id' => 'require|number|gt:0', 'itemid|部门id' => 'require|number|gt:0', 'position|职位' => 'require|number|in:1,2', ]); if ($val->check($post) == false) return json_show(1005, $val->getError()); $rs = Db::name('account_item') ->where(['account_id' => $post['account_id'], 'itemid' => $post['itemid']]) ->update(['position' => $post['position'], 'updatetime' => date('Y-m-d H:i:s')]); return $rs ? json_show(0, '修改成功') : json_show(1005, '修改失败'); } //获取部门层级列表 public function getPart() { $itemids = $this->request->filter('trim')->post('itemid'); if (is_array($itemids)) { $tmp = []; foreach ($itemids as $itemid) { if (!isset($tmp[$itemid])) $tmp[$itemid] = get_part($itemid); } return json_show(0, '获取成功', $tmp); } else return json_show(0, '获取成功', get_part($itemids)); } //获取详情 public function info() { $id = $this->request->post('id/d', 0, 'trim'); $rs = Db::name('company_item') ->field(true) ->where(['id' => $id, 'is_del' => 0]) ->findOrEmpty(); return json_show(0, '获取部门详情成功', $rs); } //获取某个用户所属部门名称 //$uid int 用户id //$cache bool 是否启用缓存,(默认启用,不启用的话直接从数据库查) //$get_tops bool 是否获取多级部门,例如 万宇恒通/采购部/仓储物流,默认不获取 public function getCompanyNameByUid() { $param = $this->request->only(['uid' => '', 'get_tops' => 1], 'post', 'trim'); $itemids = Db::name('account_item') ->whereIn('account_id', $param['uid']) ->column('itemid', 'account_id'); $res = $item_tmp = []; //获取所有部门id对应的部门名称 foreach ($itemids as $itemid) { if (!isset($item_tmp[$itemid])) { if ($param['get_tops']==1) $tmp = get_part($itemid); else $tmp = Db::name('company_item') ->field('id,name,pid') ->where(['is_del' => 0, 'id' => $itemid]) ->select() ->toArray();//为了跟上面保持数据一致 $item_tmp[$itemid] = implode('/', array_column($tmp, 'name')); } } if (is_numeric($param['uid'])) $param['uid'] = [$param['uid']]; foreach ($itemids as $uid=>$itemid) { if(!isset($res[$uid])) $res[$uid] = $item_tmp[$itemid] ?? ''; } return json_show(0, '获取成功', $res); } //根据某个关键字匹配所有子级部门及用户 public function getCompanyItemUserByName() { $param = $this->request->only(['company_name'=>"","depart_id"=>""], 'post', 'trim'); $where=[["is_del","=",0]]; //先查询所有的部门id(包括子部门) if($param['company_name']!='')$where[]=["name","like",'%' .$param['company_name']. '%']; if($param['depart_id']!='')$where[]=["id","=",$param['id']]; $company_ids = Db::name("company_item") ->where($where) ->column('id'); $pid = $company_ids; for ($i = 1; $i > 0; $i++) { $tmp = Db::name("company_item") ->where(['is_del' => 0]) ->whereIn('pid', $pid) ->column('id'); if (empty($tmp)) break; else { $company_ids = array_merge($company_ids, $tmp); $pid = $tmp; } } $company_ids = array_unique($company_ids); sort($company_ids); $uid = Db::name('account_item') ->alias('a') ->leftJoin('account b', 'b.id=a.account_id') ->where(['b.is_del' => 0, 'b.status' => 1]) ->whereIn('a.itemid', $company_ids) ->column('a.account_id'); return json_show(0, '获取成功', $uid); } }