Customer.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. <?php
  2. namespace app\user\controller;
  3. use app\user\model\CustomerInfo;
  4. use app\user\model\CustomerMember;
  5. use app\user\model\Headquarters;
  6. use think\App;
  7. use think\facade\Validate;
  8. class Customer extends Base{
  9. public function __construct(App $app) {
  10. parent::__construct($app);
  11. $this->model = new CustomerInfo();
  12. }
  13. /**
  14. * @return \think\response\Json|void
  15. */
  16. public function create(){
  17. $param = $this->request->only([
  18. 'companyName'=>"",
  19. 'parent' => 0,
  20. 'is_del' => 0,
  21. 'customer_member',
  22. 'branch',
  23. 'middle',
  24. 'area',
  25. ], 'post', 'trim');
  26. $val = Validate::rule([
  27. 'companyName|客户名称' => 'require|max:255|unique:app\user\model\CustomerInfo,companyName^parent^is_del',
  28. 'customer_member|联系方式' => 'require|array|max:100',
  29. 'branch|省级' => 'require',
  30. 'middle|市级' => 'require',
  31. 'area|区域' => 'require',
  32. ]);
  33. if ($val->check($param) == false) return error($val->getError());
  34. $code= makeNo("KH");
  35. $data = [
  36. 'companyNo' => $code,
  37. 'companyName' => $param['companyName'],
  38. 'parent' => $param['parent'],
  39. 'itemid' => $param['parent'],
  40. 'area' =>$param['area'],
  41. 'status' => 0,
  42. 'branch' =>$param['branch'],
  43. 'middle' =>$param['middle'],
  44. 'is_del' => 0,
  45. 'createrid' => $this->uid,
  46. 'creater' => $this->uname,
  47. ];
  48. $member=[];
  49. if(!empty($param['customer_member'])){
  50. foreach ($param['customer_member'] as $key => $value) {
  51. $member[] = [
  52. 'commobile' => $value['commobile'] ?? '',
  53. 'contactor' => $value['contactor'] ?? '',
  54. 'position' => $value['position'] ?? '',
  55. 'wxaccount' => $value['wxaccount'] ?? '',
  56. 'qqaccount' => $value['qqaccount'] ?? '',
  57. 'email' => $value['email'] ?? '',
  58. 'comdepart' => $value['comdepart'] ?? '',
  59. 'status' => $value['status'] ?? '',
  60. 'createrid' => $this->uid,
  61. 'creater' => $this->uname,
  62. 'companyNo' => $code,
  63. ];
  64. }
  65. }
  66. $this->model->startTrans();
  67. try{
  68. $create=$this->model->create($data);
  69. if($create->isEmpty()) return error("创建失败");
  70. if(!empty($member)){
  71. $memberAdd= $this->model->member()->saveAll($member);
  72. if($memberAdd==false) return error("创建失败");
  73. }
  74. $heq=Headquarters::createInfo($create,2);
  75. if($heq==false) return error("创建失败");
  76. $this->model->commit();
  77. }catch (\Exception $e){
  78. $this->model->rollback();
  79. return error($e->getMessage());
  80. }
  81. return success("创建成功");
  82. }
  83. /**
  84. * @param id 主键id
  85. * companyName 客户名称
  86. * parent 父级id
  87. * is_del 删除状态
  88. * customer_member 联系方式
  89. * branch 省级
  90. * middle 市级
  91. * area 区域
  92. * @return \think\Response|\think\response\Json|void
  93. * @throws \think\exception\PDOException
  94. */
  95. public function save(){
  96. $param = $this->request->only([
  97. 'id'=>"",
  98. 'companyName'=>"",
  99. 'parent' => 0,
  100. 'is_del' => 0,
  101. 'customer_member',
  102. 'branch',
  103. 'middle',
  104. 'area',
  105. ], 'post', 'trim');
  106. $val = Validate::rule([
  107. 'id|主键ID' => 'require',
  108. 'companyName|客户名称' => 'require|max:255|unique:app\user\model\CustomerInfo,companyName^parent^is_del',
  109. 'customer_member|联系方式' => 'require|array|max:100',
  110. 'branch|省级' => 'require',
  111. 'middle|市级' => 'require',
  112. 'area|区域' => 'require',
  113. ]);
  114. if ($val->check($param) == false) return error($val->getError());
  115. $info=$this->model->findOrEmpty($param['id']);
  116. if($info->isEmpty()) return error("客户不存在");
  117. if($info->status==1) return error("客户已启用,不能修改");
  118. $member=[];
  119. if(!empty($param['customer_member'])){
  120. foreach ($param['customer_member'] as $key => $value) {
  121. $member[] = [
  122. "id"=>$value['id'] ?? null,
  123. 'commobile' => $value['commobile'] ?? '',
  124. 'contactor' => $value['contactor'] ?? '',
  125. 'position' => $value['position'] ?? '',
  126. 'wxaccount' => $value['wxaccount'] ?? '',
  127. 'qqaccount' => $value['qqaccount'] ?? '',
  128. 'email' => $value['email'] ?? '',
  129. 'comdepart' => $value['comdepart'] ?? '',
  130. 'status' => $value['status'] ?? '',
  131. 'createrid' => $this->uid,
  132. 'creater' => $this->uname,
  133. 'companyNo' => $info->companyNo,
  134. ];
  135. }
  136. }
  137. $info->companyName=$param['companyName'];
  138. $info->parent=$param['parent'];
  139. $info->branch=$param['branch'];
  140. $info->middle=$param['middle'];
  141. $info->area=$param['area'];
  142. $this->model->startTrans();
  143. try{
  144. $info->save();
  145. if(!empty($member)){
  146. $memberAdd= $this->model->member()->saveAll($member);
  147. if($memberAdd==false) return error("修改失败");
  148. }
  149. $heq=Headquarters::createInfo($info,2);
  150. if($heq==false) return error("修改失败");
  151. $this->model->commit();
  152. }catch (\Exception $e){
  153. $this->model->rollback();
  154. return error($e->getMessage());
  155. }
  156. return success("修改成功");
  157. }
  158. /**
  159. * @param id 主键id
  160. * @return \think\Response|\think\response\Json|void
  161. * @throws \think\exception\PDOException
  162. */
  163. public function delete(){
  164. $id=$this->request->param('id');
  165. if(empty($id)) return error("参数错误");
  166. $info=$this->model->findOrEmpty($id);
  167. if($info->isEmpty()) return error("客户不存在");
  168. if($info->status==1) return error("客户已启用,不能删除");
  169. $this->model->startTrans();
  170. try{
  171. $info->is_del=1;
  172. $del=$info->save();
  173. if($del==false) return error("删除失败");
  174. $delHq=Headquarters::where('code',$info->companyNo)->update(["is_del"=>0,"updater"=>$this->uname,"updaterid"=>$this->uid]);
  175. if($delHq==false) return error("删除失败");
  176. $this->model->commit();
  177. }catch (\Exception $e){
  178. $this->model->rollback();
  179. return error($e->getMessage());
  180. }
  181. return success("删除成功");
  182. }
  183. /**
  184. * @return \think\Response|\think\response\Json|void
  185. */
  186. public function info(){
  187. $id=$this->request->param('id');
  188. if(empty($id)) return error("参数错误");
  189. $info=$this->model->with(['member','parentInfo'])->findOrEmpty($id);
  190. if($info->isEmpty()) return error("客户不存在");
  191. return success("获取成功",$info);
  192. }
  193. /**
  194. * @param page 页码
  195. * @param size 页
  196. * companyName 客户名称
  197. * status 状态
  198. * createrId 创建人id
  199. * start 开始时间
  200. * end 结束时间
  201. * companyNo 公司编号
  202. * itemid 项目id
  203. * @return \think\Response|\think\response\Json|void
  204. */
  205. public function list(){
  206. $param=$this->request->param(["page"=>1,"size"=>10,"companyName"=>"","status"=>"","createrId"=>"","start"=>"",
  207. "companyNo"=>"","itemid"=>0,"end"=>""],"post","trim");
  208. $where= [["is_del","=",0]];
  209. if($param['companyName']!="") $where[]=["companyName","like","%".$param['companyName']."%"];
  210. if($param['status']!="") $where[]=["status","=",$param['status']];
  211. if($param['createrId']!="") $where[]=["createrid","=",$param['createrId']];
  212. if($param['start']!="")$where[]=["addtime",">=",startTime($param['start'])];
  213. if($param['end']!="")$where[]=["addtime","<=",endTime($param['end'])];
  214. if($param['companyNo']!="") $where[]=["companyNo","=",$param['companyNo']];
  215. if($param['itemid']!=0)$where[]=["itemid","=",$param['itemid']];
  216. $list=$this->model->with(["member","parentInfo"])
  217. ->where($where)
  218. ->order("id","desc")
  219. ->paginate(["list_rows"=>$param['size'],"page"=>$param['page']]);
  220. return success("获取成功",["list"=>$list->items(),"count"=>$list->total()]);
  221. }
  222. /**
  223. * @param page 页码
  224. * companyNo 客户编号
  225. * status 状态
  226. * createrId 创建人id
  227. * start 开始时间
  228. * end 结束时间
  229. * @return \think\Response|\think\response\Json
  230. * @throws \think\exception\DbException
  231. */
  232. public function customerMember(){
  233. $param=$this->request->param(["page"=>1,"size"=>10,"companyNo"=>"","status"=>"","createrId"=>"","start"=>"",
  234. "end"=>""],"post","trim");
  235. $where= [["is_del","=",0]];
  236. if($param['companyNo']!="") $where[]=["companyNo","=",$param['companyNo']];
  237. if($param['status']!="") $where[]=["status","=",$param['status']];
  238. if($param['createrId']!="") $where[]=["createrid","=",$param['createrId']];
  239. if($param['start']!="")$where[]=["addtime",">=",startTime($param['start'])];
  240. if($param['end']!="")$where[]=["addtime","<=",endTime($param['end'])];
  241. $customerMember= new CustomerMember();
  242. $list=$customerMember->with(["customer"])
  243. ->where($where)
  244. ->order("id","desc")
  245. ->paginate(["list_rows"=>$param['size'],"page"=>$param['page']]);
  246. return success("获取成功",["list"=>$list->items(),"count"=>$list->total()]);
  247. }
  248. }