123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431 |
- <?php
- namespace app\controller;
- use app\BaseController;
- use app\model\Account;
- use app\model\AccountCompany;
- use think\Exception;
- use think\facade\Db;
- use think\facade\Validate;
- //【组织架构】
- class CompanyItem extends BaseController
- {
- //列表
- public function getList()
- {
- $post = $this->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);
- }
- }
|