post['page']) && $this->post['page']!==""? intval($this->post['page']):"1"; $size = isset($this->post['size']) && $this->post['size'] != "" ? intval($this->post['size']) :"10"; $pid = isset($this->post['pid']) &&$this->post['pid']!=="" ?intval($this->post['pid']): "0"; $where =[]; $where []= ['ci.is_del',"=",0]; $condition = [['co.is_del',"=",0]]; if($pid!==""){ $condition[]=["co.pid","=",$pid]; } $companyNo = isset($this->post['companyNo']) && $this->post['companyNo'] !=="" ? trim($this->post['companyNo']):""; if($companyNo !==""){ $where[] = ['ci.companyNo',"like","%$companyNo%"]; } $creater = isset($this->post['creater']) && $this->post['creater'] !=="" ? trim($this->post['creater']):""; if($creater !==""){ $where[] = ['ci.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 != "") { $where[] = ["ci.addtime", '>=', $start]; } $end = isset($this->post['end']) && $this->post['end'] !== "" ? $this->post['end'] : ""; if ($end != "") { $where[] = ["ci.addtime", '<=', $end]; } $status = isset($this->post['status']) && $this->post['status'] !=="" ? intval($this->post['status']):""; if($status!==""){ $where[] = ['ci.status',"=",$status]; } // $count = Db::name('customer_org1')->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 ($company_name !== "") { $company_ids = get_company_item_user_by_name($company_name); $where[] = ["u.uid", 'in', $company_ids]; $condition[] = ['u.uid',"in",$company_ids]; } $pidlist = Db::name("customer_org1") ->alias('co') ->leftJoin("depart_user u", "u.uid=co.createrid AND u.is_del=0") ->where($condition) ->order("co.addtime desc")->column("co.pid"); if(!empty($pidlist)){ $where[]=[["ci.itemid","in",$pidlist]]; }else{ if($pid===""){ $where[]=['ci.itemid','=',0]; }else{ $where[]=['ci.itemid','=',$pid]; } } $list = Db::name('customer_org1') ->alias('co') ->where($condition) ->order("co.addtime",'desc') ->leftJoin("depart_user u", "u.uid=co.createrid AND u.is_del=0") ->column("co.id,co.pid,co.name,co.level,co.depart_link,co.creater,co.addtime,co.status,u.itemid"); $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'] = implode('/', array_column(GetPart($item['itemid']), 'name')); $var[]=$iten; } $itm = Db::name('customer_info') ->alias('ci') ->field('ci.*,u.itemid') ->leftJoin("depart_user u", "u.nickname=ci.creater AND u.is_del=0") ->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['status']=$vat['status']; $inm['creater']=$vat['creater']; $inm['addtime']=$vat['addtime']; $inm['kh']=1;//客户 $inm['company_name'] = implode('/', array_column(GetPart($vat['itemid']), 'name')); $var[]=$inm; } return app_show(0,"获取成功",$var); } /*新建*/ public function create() { $name = isset($this->post['name']) && $this->post['name'] !== "" ? trim($this->post['name']) : ""; if ($name == "") { return error_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 error_show(1002, "父级id不能为空"); // } if($pid!=0){ $spid = Db::name('customer_org1')->where(['id'=>$pid, 'is_del'=>0])->find(); //var_dump( Db::name('customer_org1')->getLastSql()); if(empty($spid)){ return error_show(1004,"父级不能为空"); } } $rename = Db::name('customer_org1')->where(['is_del' => 0, 'name' => $name,'pid'=>$pid])->find(); if (!empty($rename)) { return error_show(1002, "组织名称已存在"); } $token = isset($this->post['token'])&& $this->post['token']!='' ? trim($this->post['token']):""; if($token==''){ return error_show(105,"参数token不能为空"); } $user =GetUserInfo($token); if(empty($user)||$user['code']!=0){ return error_show(1002,"创建人数据不存在"); } $createrid= isset($user["data"]['id']) ? $user["data"]['id'] : ""; $creater= isset($user["data"]['nickname']) ? $user["data"]['nickname'] : ""; $level = isset($this->post['level']) && $this->post['level'] !== "" ? trim($this->post['level']) : "1"; Db::startTrans(); try { $tada = [ "name" => $name, "weight" => $weight, "pid" => $pid, "level" => $level, "updatetime" => date("Y-m-d H:i:s"), "addtime" => date("Y-m-d H:i:s"), "depart_link" => "", "is_del" => 0, "pname"=>isset($spid['name']) ? $spid['name']:"", "creater"=>$creater, "createrid"=>$createrid ]; $t = Db::name('customer_org1')->insert($tada, true); //var_dump(Db::name('customer_org1')->getLastSql()); if ($t > 0) { if(isset($spid)){ // var_dump($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('customer_org1')->where(['id' => $t])->save($k); // var_dump( Db::name('customer_org1')->getLastSql()); if ($u) { Db::commit(); return error_show(0, "新建成功"); } } Db::rollback(); } catch (\Exception $e) { Db::rollback(); return error_show(1003, $e->getMessage()); } } /*更新*/ public function updat(){ $id = isset($this->post['id'])?intval($this->post['id']):""; if($id===""){ return error_show(1004,"参数id不能为空"); } $sid = Db::name('customer_org1')->where("id","=","$id")->find(); // var_dump(Db::name('customer_org1')->getLastSql()); if($sid==false){ return error_show(1004,"公司不存在"); } if($sid['status']==1){ return error_show(1002,"状态是启用状态,无法编辑"); } $pid = isset($this->post['pid']) && $this->post['pid'] !=="" ? intval($this->post['pid']):""; if($pid===""){ return error_show(1004,"父级id不能为空"); } if($pid!=0){ $fpid = Db::name('customer_org1')->where(['id'=>"$pid",'is_del'=>0])->find(); if(empty($fpid)){ return error_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 error_show(1004,"公司名称不能为空"); } $repeat_name = Db::name('customer_org1')->where(["is_del"=>0,"name"=>$name,'pid'=>$pid])->where("id","<>","$id")->find(); // echo Db::name('customer_org1')->getLastSql(); if(!empty($repeat_name)){ return error_show(1004,"部门名称已存在"); } if(isset($fpid)){ $depart_link=$fpid['depart_link']."{$id}-"; }else{ $depart_link= "{$id}-"; } $level =explode('-',$depart_link); $level = array_filter($level); $level= count($level); $vir=[ "id"=>$id, "name"=>$name,"pid"=>$pid, "weight"=>$weight,"depart_link"=>$depart_link, "level"=>$level,"is_del"=>0,"addtime"=>date("Y-m-d H:i:s"), "updatetime"=>date("Y-m-d H:i:s"), ]; $org = Db::name('customer_org1')->save($vir); if($org){ return error_show(0,"更新成功"); }else{ return error_show(1004,"更新失败"); } } /*查询*/ public function selec(){ $id =isset($this->post['id']) && $this->post['id'] !==""? intval($this->post['id']) :""; if($id==""){ return error_show(1004,"公司客户不存在"); } $tod=Db::name('customer_org1')->where(['id'=>$id,'is_del'=>0])->find(); //var_dump(Db::name('customer_org1')->getLastSql()); return app_show(0,"获取成功",$tod); } /*删除*/ public function dell(){ $id=isset($this->post['id']) ?intval($this->post['id']):""; $custy = Db::name('customer_org1')->where(["is_del"=>0,'id'=>$id])->find(); if($custy==false){ return error_show(1004,"公司不存在"); } //$var= Db::name('customer_info')->where(['itemid'=>$id,'is_del'=>0])->find(); $db= Db::name('customer_org1')->where(['pid'=>$custy['id'],'is_del'=>0])->count(); if($db>0){ return error_show(1004,"下一级还有组织,不允许删除"); } $var= Db::name('customer_info')->where(['itemid'=>$id,'is_del'=>0])->count(); if($var>0){ return error_show(1004,"下一级还有分公司,不允许删除"); } $custy['is_del']=1; $custy['updatetime']=date("Y-m-d H:i:s"); $compy = Db::name('customer_org1')->save($custy); return $compy ? error_show(0,"删除成功"):error_show(1004,"删除失败"); } public function status(){ $id = isset($this->post['id']) && $this->post['id'] !==""? intval($this->post['id']):""; if($id==""){ return error_show(1002,"参数id不能为空"); } $info = Db::name("customer_org1")->where([["id","=",$id]])->find(); if(!$info){ return error_show(1002,"未找到对应数据"); } $status = isset($this->post['status']) && $this->post['status']!==""? intval($this->post['status']):""; if($status===""){ return error_show(1002,"参数status不能为空"); } if(!in_array($status,[0,1])){ return error_show(1002,"参数status无效"); } $info['status']=$status; $info['updatetime']=date("Y-m-d H:i:s"); $msg = $status==1?"启用":"禁用"; $update = Db::name("customer_org1")->save($info); return $update? error_show(0,"{$msg}成功"):error_show(1004,"{$msg}失败"); } }