123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265 |
- <?php
- namespace app\controller;
- use app\BaseController;
- use think\facade\Db;
- use think\facade\Validate;
- class CustomerOrg extends BaseController
- {
- private $table_name = 'customer_org1';//此处的表名比较特殊
- public function create()
- {
- $param = $this->request->only(['name', 'weight' => 0, 'pid' => 0, 'uid', 'uname', 'level' => 1], 'post', 'trim');
- $val = Validate::rule([
- 'name' => 'require',
- 'weight' => 'require|number|egt:0',
- 'pid' => 'require|number|egt:0',
- 'uid' => 'require|number|gt:0',
- 'uname' => 'require',
- 'level' => 'require|number|gt:0',
- ]);
- if ($val->check($param) == false) return json_show(1004, $val->getError());
- if ($param['pid'] != 0) {
- $spid = Db::name($this->table_name)
- ->where(['id' => $param['pid'], 'is_del' => 0])
- ->findOrEmpty();
- if (empty($spid)) return json_show(1004, "父级不能为空");
- }
- $rename = Db::name($this->table_name)
- ->field('id')
- ->where(['is_del' => 0, 'name' => $param['name'], 'pid' => $param['pid']])
- ->findOrEmpty();
- if (!empty($rename)) return json_show(1002, "组织名称已存在");
- $createrid = $param['uid'];//isset($user["data"]['id']) ? $user["data"]['id'] : "";
- $creater = $param['uname'];//isset($user["data"]['nickname']) ? $user["data"]['nickname'] : "";
- Db::startTrans();
- try {
- $date = date('Y-m-d H:i:s');
- $tada = [
- "name" => $param['name'],
- "weight" => $param['weight'],
- "pid" => $param['pid'],
- "level" => $param['level'],
- "updatetime" => $date,
- "addtime" => $date,
- "depart_link" => "",
- "is_del" => 0,
- "pname" => isset($spid['name']) ? $spid['name'] : "",
- "creater" => $creater,
- "createrid" => $createrid
- ];
- $t = Db::name($this->table_name)->insert($tada, true);
- if ($t > 0) {
- if (isset($spid)) $depart_link = $spid['depart_link'] . "{$t}-";
- else $depart_link = "{$t}-";
- $level = explode('-', $depart_link);
- $level = array_filter($level);
- $level = count($level);
- $k = ['depart_link' => $depart_link, 'level' => $level];
- $u = Db::name($this->table_name)->where(['id' => $t])->save($k);
- if ($u) {
- Db::commit();
- return json_show(0, "新建成功");
- }
- }
- throw new \Exception('新建失败');
- } catch (\Exception $e) {
- Db::rollback();
- return json_show(1003, $e->getMessage());
- }
- }
- public function list()
- {
- $param = $this->request->only(['pid' => 0, 'companyNo' => '', 'creater' => '', 'name' => '', 'start' => '', 'end' => '', 'status' => '', 'company_name' => ''], 'post', 'trim');
- $where = [['ci.is_del', "=", 0]];
- $condition = [['co.is_del', "=", 0]];
- if (intval($param['pid']) !== "") $condition[] = ["co.pid", "=", intval($param['pid'])];
- if ($param['companyNo'] !== "") $where[] = ['ci.companyNo', "like", '%' . $param['companyNo'] . '%'];
- if ($param['creater'] !== "") $where[] = ['ci.creater', "like", '%' . $param['creater'] . '%'];
- if ($param['name'] != "") $condition[] = ["co.name", "like", "%{$param['name']}%"];
- if ($param['start'] != "") $where[] = ["ci.addtime", '>=', $param['start']];
- if ($param['end'] != "") $where[] = ["ci.addtime", '<=', $param['end'] . ' 23:59:59'];
- if ($param['status'] !== "") $where[] = ['ci.status', "=", $param['status']];
- if ($param['company_name'] !== "") {
- $where[] = ['c.name', "like", '%' . $param['company_name'] . '%'];
- $condition[] = ['c.name', "like", '%' . $param['company_name'] . '%'];
- }
- $pidlist = Db::name("customer_org1")
- ->alias('co')
- ->where($condition)
- ->group('co.pid')
- ->column("co.pid");
- if (!empty($pidlist)) $where[] = [["ci.itemid", "in", $pidlist]];
- else {
- if ($param['pid'] === "") $where[] = ['ci.itemid', '=', 0];
- else $where[] = ['ci.itemid', '=', $param['pid']];
- }
- $list = Db::name($this->table_name)
- ->alias('co')
- ->field("co.id,co.name,co.creater,co.status,co.addtime,0 kh,c.name company_name")
- ->where($condition)
- ->leftJoin('account_item b', 'b.account_id=co.createrid')
- ->leftJoin('company_item c', 'c.id=b.itemid')
- ->order("co.addtime", 'desc')
- ->select()
- ->toArray();
- $itm = Db::name('customer_info')
- ->alias('ci')
- ->field('ci.id,ci.companyName name,ci.companyNo code,ci.status,ci.creater,ci.addtime,1 kh,c.name company_name')
- ->leftJoin('account_item b', 'b.account_id=ci.createrid')
- ->leftJoin('company_item c', 'c.id=b.itemid')
- ->where($where)
- ->order("ci.addtime desc")
- ->select()
- ->toArray();
- return json_show(0, "获取成功", array_merge($list, $itm));
- }
- public function update()
- {
- $param = $this->request->only(['id', 'name', 'weight' => 0, 'pid' => 0, 'uid', 'uname', 'level' => 1], 'post', 'trim');
- $val = Validate::rule([
- 'id' => 'require|number|gt:0',
- 'pid' => 'require|number|egt:0',
- 'weight' => 'require|number|egt:0',
- 'name' => 'require',
- 'uid' => 'require|number|gt:0',
- 'uname' => 'require',
- 'level' => 'require|number|gt:0',
- ]);
- if ($val->check($param) == false) return json_show(1004, $val->getError());
- $sid = Db::name($this->table_name)
- ->field('id,status')
- ->where("id", $param['id'])
- ->findOrEmpty();
- if (empty($sid)) return json_show(1004, "公司不存在");
- if ($sid['status'] == 1) return json_show(1002, "状态是启用状态,无法编辑");
- if ($param['pid'] != 0) {
- $fpid = Db::name($this->table_name)
- ->field('id,depart_link')
- ->where(['id' => $param['pid'], 'is_del' => 0])
- ->findOrEmpty();
- if (empty($fpid)) return json_show(1004, "父级不能为空");
- }
- $repeat_name = Db::name($this->table_name)
- ->field('id')
- ->where(["is_del" => 0, "name" => $param['name'], 'pid' => $param['pid']])
- ->where("id", "<>", $param['id'])
- ->findOrEmpty();
- if (!empty($repeat_name)) return json_show(1004, "部门名称已存在");
- if (isset($fpid)) $depart_link = $fpid['depart_link'] . $param['id'] . "-";
- else $depart_link = $param['id'] . "-";
- $level = explode('-', $depart_link);
- $level = array_filter($level);
- $level = count($level);
- $vir = [
- "id" => $param['id'],
- "name" => $param['name'],
- "pid" => $param['pid'],
- "weight" => $param['weight'],
- "depart_link" => $depart_link,
- "level" => $level,
- "updatetime" => date("Y-m-d H:i:s"),
- ];
- $org = Db::name($this->table_name)->save($vir);
- return $org ? json_show(0, "更新成功") : json_show(1004, "更新失败");
- }
- public function info()
- {
- $id = $this->request->post('id/d', 0, 'trim');
- $res = Db::name($this->table_name)
- ->where(['id' => $id, 'is_del' => 0])
- ->findOrEmpty();
- return json_show(0, "获取成功", $res);
- }
- public function delete()
- {
- $id = $this->request->post('id/d', 0, 'trim');
- $custy = Db::name('customer_org1')
- ->field('id')
- ->where(["is_del" => 0, 'id' => $id])
- ->findOrEmpty();
- if (empty($custy)) return json_show(1004, "公司不存在");
- //$var= Db::name('customer_info')->where(['itemid'=>$id,'is_del'=>0])->find();
- $db = Db::name('customer_org1')
- ->field('id')
- ->where(['pid' => $custy['id'], 'is_del' => 0])
- ->findOrEmpty();
- if (!empty($db)) return json_show(1004, "下一级还有组织,不允许删除");
- $var = Db::name('customer_info')
- ->field('id')
- ->where(['itemid' => $id, 'is_del' => 0])
- ->findOrEmpty();
- if (!empty($var)) return json_show(1004, "下一级还有分公司,不允许删除");
- $custy['is_del'] = 1;
- $custy['updatetime'] = date("Y-m-d H:i:s");
- $compy = Db::name('customer_org1')->save($custy);
- return $compy ? json_show(0, "删除成功") : json_show(1004, "删除失败");
- }
- public function status()
- {
- $param = $this->request->only(['id', 'status'], 'post', 'trim');
- $val = Validate::rule([
- 'id' => 'require|number|gt:0',
- 'status' => 'require|number|in:0,1',
- ]);
- if ($val->check($param) == false) return json_show(1004, $val->getError());
- $update = Db::name("customer_org1")
- ->where(['is_del' => 0, 'id' => $param['id']])
- ->where('status', '<>', $param['status'])
- ->update([
- 'status' => $param['status'],
- 'updatetime' => date("Y-m-d H:i:s"),
- ]);
- return $update ? json_show(0, "操作成功") : json_show(1004, "操作失败");
- }
- //获取三级架构
- public function getTopCustomerOrg()
- {
- $itemid = $this->request->filter('trim')->post('itemid');
- $res = [];
- foreach ($itemid as $val){
- $res[$val] = get_top_customer_org($val);
- }
- return json_show(0, '获取架构成功', $res);
- }
- }
|