123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414 |
- <?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());
- // $name = isset($this->post['name']) && $this->post['name'] !== "" ? trim($this->post['name']) : "";
- // if ($name == "") {
- // return json_show(1003, "公司名称不能为空");
- // }
- // $weight = isset($this->post['weight']) && $this->post['weight'] !== "" ? trim($this->post['weight']) : "0";
- // $pid = isset($this->post['pid']) && $this->post['pid'] !== "" ? trim($this->post['pid']) : "0";
- // if ($pid === "") {
- // return json_show(1002, "父级id不能为空");
- // }
- if ($param['pid'] != 0) {
- $spid = Db::name($this->table_name)
- ->where(['id' => $param['pid'], 'is_del' => 0])
- ->findOrEmpty();
- //var_dump( Db::name($this->table_name)->getLastSql());
- 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, "组织名称已存在");
- // $token = isset($this->post['token'])&& $this->post['token']!='' ? trim($this->post['token']):"";
- // if($token==''){
- // return json_show(105,"参数token不能为空");
- // }
- // $user =GetUserInfo($token);
- // if(empty($user)||$user['code']!=0){
- // return json_show(1002,"创建人数据不存在");
- // }
- $createrid = $param['uid'];//isset($user["data"]['id']) ? $user["data"]['id'] : "";
- $creater = $param['uname'];//isset($user["data"]['nickname']) ? $user["data"]['nickname'] : "";
- // $level = isset($this->post['level']) && $this->post['level'] !== "" ? trim($this->post['level']) : "1";
- 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);
- //var_dump(Db::name($this->table_name)->getLastSql());
- if ($t > 0) {
- if (isset($spid)) $depart_link = $spid['depart_link'] . "{$t}-";
- else $depart_link = "{$t}-";
- $level = explode('-', $depart_link);
- // var_dump($level);
- $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);
- // var_dump( Db::name($this->table_name)->getLastSql());
- 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');
- // $pid = isset($this->post['pid']) &&$this->post['pid']!=="" ?intval($this->post['pid']): "0";
- $where = [['ci.is_del', "=", 0]];
- $condition = [['co.is_del', "=", 0]];
- if (intval($param['pid']) !== "") $condition[] = ["co.pid", "=", intval($param['pid'])];
- // $companyNo = isset($this->post['companyNo']) && $this->post['companyNo'] !=="" ? trim($this->post['companyNo']):"";
- if ($param['companyNo'] !== "") $where[] = ['ci.companyNo', "like", '%' . $param['companyNo'] . '%'];
- // $creater = isset($this->post['creater']) && $this->post['creater'] !=="" ? trim($this->post['creater']):"";
- if ($param['creater'] !== "") $where[] = ['ci.creater', "like", '%' . $param['creater'] . '%'];
- // $name= isset($this->post['name'])&&$this->post['name']!=="" ? trim($this->post['name']):"";
- if ($param['name'] != "") $condition[] = ["co.name", "like", "%{$param['name']}%"];
- // $start = isset($this->post['start']) && $this->post['start'] !== "" ? $this->post['start'] : "";
- if ($param['start'] != "") $where[] = ["ci.addtime", '>=', $param['start']];
- // $end = isset($this->post['end']) && $this->post['end'] !== "" ? $this->post['end'] : "";
- if ($param['end'] != "") $where[] = ["ci.addtime", '<=', $param['end'] . ' 23:59:59'];
- // $status = isset($this->post['status']) && $this->post['status'] !=="" ? intval($this->post['status']):"";
- if ($param['status'] !== "") $where[] = ['ci.status', "=", $param['status']];
- // $count = Db::name($this->table_name)->where($condition)->count();
- // $page>=ceil($count/$size) ? $page=ceil($count/$size) :"";
- // $creater = isset($this->post['creater']) && $this->post['creater'] !=="" ? trim($this->post['creater']):"";
- // if($creater !==""){
- // $condition[] = ['co.creater',"like","%$creater%"];
- // }
- // $name= isset($this->post['name'])&&$this->post['name']!=="" ? trim($this->post['name']):"";
- // if($name!=""){
- // $condition[]=["co.name","like","%{$this->post['name']}%"];
- // }
- // $start = isset($this->post['start']) && $this->post['start'] !== "" ? $this->post['start'] : "";
- // if ($start != "") {
- // $condition[] = ["co.addtime", '>=', $start];
- // }
- // $end = isset($this->post['end']) && $this->post['end'] !== "" ? $this->post['end'] : "";
- // if ($end != "") {
- // $condition[] = ["co.addtime", '<=', $end];
- // }
- // $status = isset($this->post['status']) && $this->post['status'] !=="" ? intval($this->post['status']):"";
- // if($status!==""){
- // $condition[] = ['co.status',"=",$status];
- // }
- // $company_name = isset($this->post['company_name']) && $this->post['company_name'] !== "" ? trim($this->post['company_name']) : "";
- if ($param['company_name'] !== "") {
- // $company_ids = get_company_item_user_by_name($param['company_name']);
- $where[] = ['c.name', "like", '%' . $param['company_name'] . '%'];
- $condition[] = ['c.name', "like", '%' . $param['company_name'] . '%'];
- }
- $pidlist = Db::name("customer_org1")
- ->alias('co')
- // ->leftJoin("depart_user u", "u.uid=co.createrid AND u.is_del=0")
- ->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();
- // $all_createrid = array_column($list,'createrid');
- // $all_item = get_company_name_by_uid($all_createrid);
- // $var=[];
- // foreach ($list as $item) {
- // $iten=[];
- // $iten['name']=$item['name'];
- //// $iten['code']="";
- // $iten['id']=$item['id'];
- // $iten['creater']=$item['creater'];
- // $iten['status']=$item['status'];
- // $iten['addtime']=$item['addtime'];
- // $iten['kh']=0;//组织架构
- // $iten['company_name'] = $all_item[$item['createrid']]??'';
- //
- // $var[]=$iten;
- // }
- $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("depart_user u", "u.nickname=ci.creater AND u.is_del=0")
- ->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();
- // foreach ($itm as $vat){
- // $inm=[];
- // $inm['name']=$vat['companyName'];
- // $inm['id']=$vat['id'];
- // $inm['code']=$vat['companyNo'];
- //// $inm['companyName']=$vat['companyName'];
- //// $inm['invoice_bank']=$vat['invoice_bank'];
- //// $inm['invoice_bankNo']=$vat['invoice_bankNo'];
- //// $inm['invoice_code']=$vat['invoice_code'];
- //// $inm['invoice_people']=$vat['invoice_people'];
- //// $inm['invoice_mobile']=$vat['invoice_mobile'];
- //// $inm['invoice_addr']=$vat['invoice_addr'];
- //// $inm['parent']=$vat['parent'];
- //// $inm['branch']=$vat['branch'];
- //// $inm['middle']=$vat['middle'];
- //// $inm['country']=$vat['country'];
- // $inm['status']=$vat['status'];
- // $inm['creater']=$vat['creater'];
- // $inm['addtime']=$vat['addtime'];
- // $inm['kh']=1;//客户
- // $inm['company_name'] = '待处理';
- // $var[]=$inm;
- // }
- 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());
- // $id = isset($this->post['id'])?intval($this->post['id']):"";
- // if($id===""){
- // return json_show(1004,"参数id不能为空");
- // }
- $sid = Db::name($this->table_name)
- ->field('id,status')
- ->where("id", $param['id'])
- ->findOrEmpty();
- // var_dump(Db::name($this->table_name)->getLastSql());
- if (empty($sid)) return json_show(1004, "公司不存在");
- if ($sid['status'] == 1) return json_show(1002, "状态是启用状态,无法编辑");
- // $pid = isset($this->post['pid']) && $this->post['pid'] !=="" ? intval($this->post['pid']):"";
- // if($pid===""){
- // return json_show(1004,"父级id不能为空");
- // }
- 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, "父级不能为空");
- }
- // $weight = isset($this->post['weight']) && $this->post['weight'] !==""? intval($this->post['weight']):"0";
- // $level = isset($this->post['level']) && $this->post['level'] !==""? intval($this->post['level']):"";
- // $name = isset($this->post['name'])? trim($this->post['name']):"";
- // if($name==""){
- // 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();
- // echo Db::name($this->table_name)->getLastSql();
- 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, "更新失败");
- // if($org)
- // return json_show(0,"更新成功");
- // else{
- // return 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());
- // $id = isset($this->post['id']) && $this->post['id'] !==""? intval($this->post['id']):"";
- // if($id==""){
- // return json_show(1002,"参数id不能为空");
- // }
- // $info = Db::name("customer_org1")->where([["id","=",$id]])->find();
- // if(!$info){
- // return json_show(1002,"未找到对应数据");
- // }
- // $status = isset($this->post['status']) && $this->post['status']!==""? intval($this->post['status']):"";
- // if($status===""){
- // return json_show(1002,"参数status不能为空");
- // }
- // if(!in_array($status,[0,1])){
- // return json_show(1002,"参数status无效");
- // }
- // $info['status']=$status;
- // $info['updatetime']=date("Y-m-d H:i:s");
- // $msg = $status==1?"启用":"禁用";
- $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);
- }
- }
|