Customar.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. <?php
  2. namespace app\admin\controller;
  3. use app\BaseController;
  4. use think\App;
  5. use think\facade\Db;
  6. class Customar extends BaseController
  7. {
  8. public $post="";
  9. public function __construct(App $app)
  10. {
  11. parent::__construct($app);
  12. $this->post=$this->request->post();
  13. }
  14. public function create(){
  15. $token = isset($this->post['token'])&& $this->post['token']!='' ? trim($this->post['token']):"";
  16. if($token==''){
  17. return error_show(1005,"参数token不能为空");
  18. }
  19. $companyNo=makeNo("KH");
  20. $companyName=isset($this->post['companyName']) && $this->post['companyName'] !== "" ? $this->post['companyName'] : "";
  21. if ($companyName == "") {
  22. return error_show(1002, "参数companyName不能为空");
  23. }
  24. $parent=isset($this->post['parent']) && $this->post['parent'] !== "" ? $this->post['parent'] : "";
  25. if ($parent == "") {
  26. return error_show(1002, "参数parent不能为空");
  27. }
  28. $customer_member=isset($this->post['customer_member']) && $this->post['customer_member'] !== "" ? $this->post['customer_member'] : "";
  29. if ($customer_member == "") {
  30. return error_show(1002, "参数customer_member不能为空");
  31. }
  32. // $status = isset($this->post['status']) && $this->post['status'] !=="" ? intval($this->post['status']):"";
  33. // if($status==""){
  34. // return error_show(1002,"参数status不能为空");
  35. // }
  36. $user =GetUserInfo($token);
  37. if(empty($user)||$user['code']!=0){
  38. return error_show(1002,"创建人数据不存在");
  39. }
  40. $createrid= isset($user["data"]['id']) ? $user["data"]['id'] : "";
  41. $creater= isset($user["data"]['nickname']) ? $user["data"]['nickname'] : "";
  42. Db::startTrans();
  43. try {
  44. $data=[
  45. "companyNo"=>$companyNo,
  46. "companyName"=>$companyName,
  47. "parent"=>"",
  48. "itemid"=>"",
  49. "area"=>"",
  50. "comdepart"=>"",
  51. "commobile"=>"",
  52. "comtel"=>"",
  53. "contactor"=>"",
  54. "depart"=>"",
  55. "mobile"=>"",
  56. "invoice_title"=>"",
  57. "invoice_people"=>"",
  58. "invoice_addr"=>"",
  59. "invoice_mobile"=>"",
  60. "invoice_code"=>"",
  61. "invoice_bank"=>"",
  62. "invoice_bankNo"=>"",
  63. "companyCode"=>"",
  64. "status"=>0,
  65. "sales_name"=>"",
  66. "sales_depart"=>"",
  67. "branch"=>"",
  68. "middle"=>"",
  69. "country"=>"",
  70. "is_del"=>0,
  71. "creater"=>"",
  72. "modifier"=>"",
  73. "addtime"=>date("Y-m-d H:i:s"),
  74. "updatetime"=>date("Y-m-d H:i:s"),
  75. ];
  76. $datainfo=Db::name('customer_info')->insert($data);
  77. if($datainfo){
  78. $var=[];
  79. //var_dump($customer_member);
  80. foreach ($customer_member as $value){
  81. $to= GetInfoById($token,['id'=>$value['id']]);
  82. if(empty($to)||$to['code']!=0) {
  83. return app_show(1002, "数据拥有者信息不存在");
  84. }
  85. $item=[];
  86. $info =$to['data'];
  87. $item['owner']=$info['nickname'];
  88. $item['ownerid']=$info['id'];
  89. $item['commobile']=$info['mobile'];
  90. $item['comtel']="";
  91. $item['contactor']=isset($value['contactor']) ? $value['contactor']:"" ;
  92. $item['position']=isset($value['position']) ? $value['position']:"" ;
  93. $item['wxaccount']=isset($value['wxaccount']) ? $value['wxaccount']:"" ;
  94. $item['qqaccount']=isset($value['qqaccount']) ? $value['qqaccount']:"" ;
  95. $item['email']=isset($value['email']) ? $value['email']:"" ;
  96. $item['comdepart']=isset($value['comdepart']) ? $value['comdepart']:"" ;
  97. $item['is_private']=isset($value['is_private']) ? $value['is_private']:"" ;
  98. $item['status']=$value['status'];
  99. $item['createrid']=$createrid;
  100. $item['creater']=$creater;
  101. $item['companyNo']=$companyNo;
  102. $item['is_del']=0;
  103. $item['addtime']=date("Y-m-d H:i:s");
  104. $item['updatetime']=date("Y-m-d H:i:s");
  105. $var[]=$item;
  106. }
  107. $vp = Db::name('customer_member')->insertAll($var);
  108. if ($vp == "") {
  109. Db::rollback();
  110. return error_show(1002, "新建失败");
  111. }
  112. Db::commit();
  113. return error_show(0,"新建成功");
  114. }else{
  115. Db::rollback();
  116. return error_show(1002,"新建失败");
  117. }
  118. }catch (\Exception $e) {
  119. Db::rollback();
  120. return error_show(1005, $e->getMessage());
  121. }
  122. }
  123. public function list(){
  124. $page = isset($this->post['page']) && $this->post['page'] !=="" ? intval($this->post['page']):"1";
  125. $size = isset($this->post['size']) && $this->post['size'] !=="" ? intval($this->post['size']):"10";
  126. $where=[["a.is_del","=",0]];
  127. $companyName= isset($this->post['companyName']) && $this->post['companyName'] !=="" ? trim($this->post['companyName']):"";
  128. if($companyName!=""){
  129. $where[]=['b.companyName',"like","%$companyName%"];
  130. }
  131. $status = isset($this->post['status']) && $this->post['status'] !=="" ? trim($this->post['status']):"";
  132. if($status!=""){
  133. $where[]=['a.status',"=",$status];
  134. }
  135. $creater = isset($this->post['creater']) && $this->post['creater'] !=="" ? intval($this->post['creater']):"";
  136. if($creater!=""){
  137. $where[]=['a.creater',"like","%$creater%"];
  138. }
  139. $start = isset($this->post['start']) && $this->post['start']!=="" ? $this->post['start']:"";
  140. if($start!==""){
  141. $where[]=['a.addtime',">=",date('Y-m-d H:i:s',strtotime($start))];
  142. }
  143. $end = isset($this->post['end']) && $this->post['end']!=="" ? $this->post['end']:"";
  144. if($end!==""){
  145. $where[]=['a.addtime',"<",date('Y-m-d H:i:s',strtotime($end)+24*3600)];
  146. }
  147. $count = Db::name('customer_member')->alias('a')->join('customer_info b',"b.companyNo=a.companyNo","left")
  148. ->where($where)->count();
  149. $total = ceil($count / $size);
  150. $page = $page >= $total ? $total : $page;
  151. $list = Db::name('customer_member')->alias('a')->join('customer_info b',"b.companyNo=a.companyNo","left")
  152. ->where($where)->page($page,$size)->order("addtime desc")->field("a.*,b.companyName,b.parent")->select();
  153. return app_show(0,"获取成功",['list'=>$list,'count'=>$count]);
  154. }
  155. public function edit(){
  156. $token = isset($this->post['token'])&& $this->post['token']!='' ? trim($this->post['token']):"";
  157. if($token==''){
  158. return error_show(1005,"参数token不能为空");
  159. }
  160. $id = isset($this->post['id']) && $this->post['id'] !=="" ? intval($this->post['id']):"";
  161. if($id==""){
  162. return error_show(1002,"参数id不能为空");
  163. }
  164. $idinfo = Db::name('customer_info')->where(['id'=>$id,'is_del'=>0])->find();
  165. if(empty($idinfo)){
  166. return error_show(1004,"未找到数据");
  167. }
  168. $companyNo=isset($this->post['companyNo']) && $this->post['companyNo'] !== "" ? $this->post['companyNo'] : "";
  169. if ($companyNo == "") {
  170. return error_show(1002, "参数companyNo不能为空");
  171. }
  172. // $company = Db::name('customer_info')->where(['companyNo'=>$companyNo,'is_del'=>0])->find();
  173. // if(empty($company)){
  174. // return error_show(1004,"未找到数据");
  175. // }
  176. $companyName=isset($this->post['companyName']) && $this->post['companyName'] !== "" ? $this->post['companyName'] : "";
  177. if ($companyName == "") {
  178. return error_show(1002, "参数companyName不能为空");
  179. }
  180. $parent=isset($this->post['parent']) && $this->post['parent'] !== "" ? $this->post['parent'] : "";
  181. if ($parent == "") {
  182. return error_show(1002, "参数parent不能为空");
  183. }
  184. $customer_member=isset($this->post['customer_member']) && $this->post['customer_member'] !== "" ? $this->post['customer_member'] : "";
  185. if ($customer_member == "") {
  186. return error_show(1002, "参数customer_member不能为空");
  187. }
  188. $user =GetUserInfo($token);
  189. if(empty($user)||$user['code']!=0){
  190. return error_show(1002,"创建人数据不存在");
  191. }
  192. $createrid= isset($user["data"]['id']) ? $user["data"]['id'] : "";
  193. $creater= isset($user["data"]['nickname']) ? $user["data"]['nickname'] : "";
  194. Db::startTrans();
  195. try {
  196. $data=[
  197. "id"=>$id,
  198. "companyName"=>$companyName,
  199. "companyNo"=>$companyNo,
  200. "parent"=>$parent,
  201. "updatetime"=>date("Y-m-d H:i:s"),
  202. ];
  203. $datainfo=Db::name('customer_info')->save($data);
  204. // var_dump(Db::name('customer_info')->getLastSql());
  205. if($datainfo){
  206. //$var=[];
  207. //var_dump($customer_member);
  208. foreach ($customer_member as $value){
  209. $to= GetInfoById($token,['id'=>$value['ownerid']]);
  210. if(empty($to)||$to['code']!=0) {
  211. return app_show(1002, "数据拥有者信息不存在");
  212. }
  213. $item=[];
  214. $info =$to['data'];
  215. $item['owner']=$info['nickname'];
  216. $item['ownerid']=$info['id'];
  217. $item['commobile']=$info['mobile'];
  218. $item['comtel']="";
  219. $item['id']=isset($value['id']) ? $value['id']:"" ;
  220. $item['contactor']=isset($value['contactor']) ? $value['contactor']:"" ;
  221. $item['position']=isset($value['position']) ? $value['position']:"" ;
  222. $item['wxaccount']=isset($value['wxaccount']) ? $value['wxaccount']:"" ;
  223. $item['qqaccount']=isset($value['qqaccount']) ? $value['qqaccount']:"" ;
  224. $item['email']=isset($value['email']) ? $value['email']:"" ;
  225. $item['comdepart']=isset($value['comdepart']) ? $value['comdepart']:"" ;
  226. $item['is_private']=isset($value['is_private']) ? $value['is_private']:"" ;
  227. $item['status']=$value['status'];
  228. $item['createrid']=$createrid;
  229. $item['creater']=$creater;
  230. $item['companyNo']=$companyNo;
  231. $item['is_del']=0;
  232. // $item['addtime']=date("Y-m-d H:i:s");
  233. $item['updatetime']=date("Y-m-d H:i:s");
  234. // $var[]=$item;
  235. }
  236. $vp = Db::name('customer_member')->save($item);
  237. // var_dump(Db::name('customer_member')->getLastSql());
  238. if ($vp == false) {
  239. Db::rollback();
  240. return error_show(1003, "更新失败");
  241. }
  242. Db::commit();
  243. return error_show(0,"更新成功");
  244. }else{
  245. Db::rollback();
  246. return error_show(1002,"更新失败");
  247. }
  248. }catch (\Exception $e) {
  249. Db::rollback();
  250. return error_show(1005, $e->getMessage());
  251. }
  252. }
  253. public function info(){
  254. $id = isset($this->post['id']) && $this->post['id'] !=="" ? intval($this->post['id']):"";
  255. if($id==""){
  256. return error_show(1002,"参数id不能为空");
  257. }
  258. $idinfo = Db::name('customer_member')->alias('a')
  259. ->join("customer_info b","b.companyNo=a.companyNo","left")
  260. ->join("customer_attr c","c.id=b.parent","left")
  261. ->field("a.*,b.companyName,b.parent,c.name as attr_name")
  262. ->where(['a.id'=>$id,'a.is_del'=>0])->find();
  263. if($idinfo==""){
  264. return error_show(1002,"未找到数据");
  265. }
  266. return app_show(0,"获取成功",$idinfo);
  267. }
  268. }