model = new CustomerInfo(); } /** * @return \think\response\Json|void */ public function create(){ $param = $this->request->only([ 'companyName'=>"", 'parent' => 0, 'is_del' => 0, 'customer_member', 'branch', 'middle', 'area', ], 'post', 'trim'); $val = Validate::rule([ 'companyName|客户名称' => 'require|max:255|unique:app\user\model\CustomerInfo,companyName^parent^is_del', 'customer_member|联系方式' => 'require|array|max:100', 'branch|省级' => 'require', 'middle|市级' => 'require', 'area|区域' => 'require', ]); if ($val->check($param) == false) return error($val->getError()); $code= makeNo("KH"); $data = [ 'companyNo' => $code, 'companyName' => $param['companyName'], 'parent' => $param['parent'], 'itemid' => $param['parent'], 'area' =>$param['area'], 'status' => 0, 'branch' =>$param['branch'], 'middle' =>$param['middle'], 'is_del' => 0, 'createrid' => $this->uid, 'creater' => $this->uname, ]; $member=[]; if(!empty($param['customer_member'])){ foreach ($param['customer_member'] as $key => $value) { $member[] = [ 'commobile' => $value['commobile'] ?? '', 'contactor' => $value['contactor'] ?? '', 'position' => $value['position'] ?? '', 'wxaccount' => $value['wxaccount'] ?? '', 'qqaccount' => $value['qqaccount'] ?? '', 'email' => $value['email'] ?? '', 'comdepart' => $value['comdepart'] ?? '', 'status' => $value['status'] ?? '', 'createrid' => $this->uid, 'creater' => $this->uname, 'companyNo' => $code, ]; } } $this->model->startTrans(); try{ $create=$this->model->create($data); if($create->isEmpty()) return error("创建失败"); if(!empty($member)){ $memberAdd= $this->model->member()->saveAll($member); if($memberAdd==false) return error("创建失败"); } $create->updater = $this->uname; $create->updaterid = $this->uid; $heq=Headquarters::createInfo($create,2); if($heq==false) return error("创建失败"); $this->model->commit(); }catch (\Exception $e){ $this->model->rollback(); return error($e->getMessage()); } return success("创建成功"); } /** * @param id 主键id * companyName 客户名称 * parent 父级id * is_del 删除状态 * customer_member 联系方式 * branch 省级 * middle 市级 * area 区域 * @return \think\Response|\think\response\Json|void * @throws \think\exception\PDOException */ public function save(){ $param = $this->request->only([ 'id'=>"", 'companyName'=>"", 'parent' => 0, 'is_del' => 0, 'customer_member', 'branch', 'middle', 'area', ], 'post', 'trim'); $val = Validate::rule([ 'id|主键ID' => 'require', 'companyName|客户名称' => 'require|max:255|unique:app\user\model\CustomerInfo,companyName^parent^is_del', 'customer_member|联系方式' => 'require|array|max:100', 'branch|省级' => 'require', 'middle|市级' => 'require', 'area|区域' => 'require', ]); if ($val->check($param) == false) return error($val->getError()); $info=$this->model->findOrEmpty($param['id']); if($info->isEmpty()) return error("客户不存在"); if($info->status==1) return error("客户已启用,不能修改"); $member=[]; if(!empty($param['customer_member'])){ foreach ($param['customer_member'] as $key => $value) { $member[] = [ "id"=>$value['id'] ?? null, 'commobile' => $value['commobile'] ?? '', 'contactor' => $value['contactor'] ?? '', 'position' => $value['position'] ?? '', 'wxaccount' => $value['wxaccount'] ?? '', 'qqaccount' => $value['qqaccount'] ?? '', 'email' => $value['email'] ?? '', 'comdepart' => $value['comdepart'] ?? '', 'status' => $value['status'] ?? '', 'createrid' => $this->uid, 'creater' => $this->uname, 'companyNo' => $info->companyNo, ]; } } $info->companyName=$param['companyName']; $info->parent=$param['parent']; $info->branch=$param['branch']; $info->middle=$param['middle']; $info->area=$param['area']; $this->model->startTrans(); try{ $info->save(); if(!empty($member)){ $memberAdd= $this->model->member()->saveAll($member); if($memberAdd==false) return error("修改失败"); } $info->updater = $this->uname; $info->updaterid = $this->uid; $heq=Headquarters::createInfo($info,2); if($heq==false) return error("修改失败"); $this->model->commit(); }catch (\Exception $e){ $this->model->rollback(); return error($e->getMessage()); } return success("修改成功"); } /** * @param id 主键id * @return \think\Response|\think\response\Json|void * @throws \think\exception\PDOException */ public function delete(){ $id=$this->request->param('id'); if(empty($id)) return error("参数错误"); $info=$this->model->findOrEmpty($id); if($info->isEmpty()) return error("客户不存在"); if($info->status==1) return error("客户已启用,不能删除"); $this->model->startTrans(); try{ $info->is_del=1; $del=$info->save(); if($del==false) return error("删除失败"); $delHq=Headquarters::where('code',$info->companyNo)->findOrEmpty(); if($delHq->isEmpty()) return error("数据未找到"); $delHq->is_del=1; $delHq->updater = $this->uname; $delHq->updaterid = $this->uid; $delHq->save(); $this->model->commit(); }catch (\Exception $e){ $this->model->rollback(); return error($e->getMessage()); } return success("删除成功"); } /** * @return \think\Response|\think\response\Json|void */ public function info(){ $id=$this->request->param('id'); if(empty($id)) return error("参数错误"); $info=$this->model->with(['member','parentInfo'])->findOrEmpty($id); if($info->isEmpty()) return error("客户不存在"); return success("获取成功",$info); } /** * @param page 页码 * @param size 页 * companyName 客户名称 * status 状态 * createrId 创建人id * start 开始时间 * end 结束时间 * companyNo 公司编号 * itemid 项目id * @return \think\Response|\think\response\Json|void */ public function list(){ $param=$this->request->param(["page"=>1,"size"=>10,"companyName"=>"","status"=>"","createrId"=>"","start"=>"", "companyNo"=>"","itemid"=>0,"end"=>""],"post","trim"); $where= [["is_del","=",0]]; if($param['companyName']!="") $where[]=["companyName","like","%".$param['companyName']."%"]; if($param['status']!="") $where[]=["status","=",$param['status']]; if($param['createrId']!="") $where[]=["createrid","=",$param['createrId']]; if($param['start']!="")$where[]=["addtime",">=",startTime($param['start'])]; if($param['end']!="")$where[]=["addtime","<=",endTime($param['end'])]; if($param['companyNo']!="") $where[]=["companyNo","=",$param['companyNo']]; if($param['itemid']!=0)$where[]=["itemid","=",$param['itemid']]; $list=$this->model->with(["member","parentInfo"]) ->where($where) ->order("id","desc") ->paginate(["list_rows"=>$param['size'],"page"=>$param['page']]); return success("获取成功",["list"=>$list->items(),"count"=>$list->total()]); } /** * @param page 页码 * companyNo 客户编号 * status 状态 * createrId 创建人id * start 开始时间 * end 结束时间 * @return \think\Response|\think\response\Json * @throws \think\exception\DbException */ public function customerMember(){ $param=$this->request->param(["page"=>1,"size"=>10,"companyNo"=>"","status"=>"","createrId"=>"","start"=>"", "end"=>""],"post","trim"); $where= [["is_del","=",0]]; if($param['companyNo']!="") $where[]=["companyNo","=",$param['companyNo']]; if($param['status']!="") $where[]=["status","=",$param['status']]; if($param['createrId']!="") $where[]=["createrid","=",$param['createrId']]; if($param['start']!="")$where[]=["addtime",">=",startTime($param['start'])]; if($param['end']!="")$where[]=["addtime","<=",endTime($param['end'])]; $customerMember= new CustomerMember(); $list=$customerMember->with(["customer"]) ->where($where) ->order("id","desc") ->paginate(["list_rows"=>$param['size'],"page"=>$param['page']]); return success("获取成功",["list"=>$list->items(),"count"=>$list->total()]); } /** * @return \think\Response|\think\response\Json|void */ public function status(){ $param=$this->request->param(['id','status'],"post","trim"); $valid=Validate::rule([ "id|主键id"=>"require|number|gt:0", "status|状态"=>"require|number|in:0,1" ]); if(!$valid->check($param)) return error($valid->getError()); $info=$this->model->findOrEmpty($param['id']); if($info->isEmpty()) return error("客户不存在"); $this->model->startTrans(); try{ $info->status=$param['status']; $save=$info->save(); if($save==false) return error("修改失败"); $saveHq=Headquarters::where('code',$info->companyNo)->findOrEmpty(); if($saveHq->isEmpty()) return error("数据未找到"); $saveHq->status=$param['status']; $saveHq->updater =$this->uname; $saveHq->updaterid =$this->uid; $saveHq->save(); $this->model->commit(); return success("修改成功"); }catch (\Exception $e){ $this->model->rollback(); return error($e->getMessage()); } } }