Customar.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  1. <?php
  2. namespace app\admin\controller;
  3. use app\admin\model\ChangeLog;
  4. use app\BaseController;
  5. use think\App;
  6. use think\facade\Db;
  7. //客户
  8. class Customar extends Base
  9. {
  10. public $post = "";
  11. public function __construct(App $app)
  12. {
  13. parent::__construct($app);
  14. $this->post = $this->request->post();
  15. }
  16. public function create()
  17. {
  18. $param = $this->request->filter('trim')->post();
  19. $param['uid']= $this->uid;
  20. $param['uname']= $this->uname;
  21. $userCommon = new \app\admin\common\User();
  22. $rs = $userCommon->handle('cCreate',$param);
  23. return json_show($rs['code'],$rs['message'],$rs['data']);
  24. // $token = isset($this->post['token']) && $this->post['token'] != '' ? trim($this->post['token']) : "";
  25. // if ($token == '') {
  26. // return error_show(1005, "参数token不能为空");
  27. // }
  28. // $companyNo = makeNo("KH");
  29. // $companyName = isset($this->post['companyName']) && $this->post['companyName'] !== "" ? $this->post['companyName'] : "";
  30. // if ($companyName == "") {
  31. // return error_show(1002, "参数companyName不能为空");
  32. // }
  33. // $parent = isset($this->post['parent']) && $this->post['parent'] !== "" ? intval($this->post['parent'] ) : "";
  34. // $rename = Db::name('customer_org1')->where(['is_del' =>0,'id'=>$parent])->find();
  35. // $item = Db::name('customer_info')->where(['itemid'=>$rename['id'],'companyName'=>$companyName,'is_del'=>0])->find();
  36. // if ($item!=false) {
  37. // return error_show(1002, "公司名称已存在");
  38. // }
  39. // if ($parent === "") {
  40. // return error_show(1002, "参数parent不能为空");
  41. // }
  42. //$itemid = isset($this->post['itemid']) && $this->post['itemid'] !== "" ? intval($this->post['itemid'] ) : "";
  43. // $customer_member = isset($this->post['customer_member']) && $this->post['customer_member'] !== "" ? $this->post['customer_member'] : "";
  44. // if ($customer_member == "") {
  45. // return error_show(1002, "参数customer_member不能为空");
  46. // }
  47. // $status = isset($this->post['status']) && $this->post['status'] !=="" ? intval($this->post['status']):"";
  48. // if($status==""){
  49. // return error_show(1002,"参数status不能为空");
  50. // }
  51. // $user = GetUserInfo($token);
  52. // if (empty($user) || $user['code'] != 0) {
  53. // return error_show(102, "创建人数据不存在");
  54. // }
  55. // $createrid = isset($user["data"]['id']) ? $user["data"]['id'] : "";
  56. // $creater = isset($user["data"]['nickname']) ? $user["data"]['nickname'] : "";
  57. // Db::startTrans();
  58. // try {
  59. // $data = [
  60. // "companyNo" => $companyNo,
  61. // "companyName" => $companyName,
  62. // "parent" => $parent,
  63. // "itemid" => $parent,
  64. // "area" => "",
  65. // "comdepart" => "",
  66. // "commobile" => "",
  67. // "comtel" => "",
  68. // "contactor" => "",
  69. // "depart" => "",
  70. // "mobile" => "",
  71. // "invoice_title" => "",
  72. // "invoice_people" => "",
  73. // "invoice_addr" => "",
  74. // "invoice_mobile" => "",
  75. // "invoice_code" => "",
  76. // "invoice_bank" => "",
  77. // "invoice_bankNo" => "",
  78. // "companyCode" => "",
  79. // "status" => 0,
  80. // "sales_name" => "",
  81. // "sales_depart" => "",
  82. // "branch" => "",
  83. // "middle" => "",
  84. // "country" => "",
  85. // "is_del" => 0,
  86. // "creater" => $creater,
  87. // "modifier" => "",
  88. // "addtime" => date("Y-m-d H:i:s"),
  89. // "updatetime" => date("Y-m-d H:i:s"),
  90. // ];
  91. // $datainfo = Db::name('customer_info')->insert($data);
  92. // if ($datainfo) {
  93. //
  94. //// $var = [];
  95. //// //var_dump($customer_member);
  96. // foreach ($customer_member as $value) {
  97. //// if($value['ownerid']!==""){
  98. //// $to = GetInfoById($token, ['id' => $value['ownerid']]);
  99. //// if (empty($to) || $to['code'] != 0) {
  100. //// return app_show(1002, "数据拥有者信息不存在");
  101. //// }
  102. //// }
  103. // $item = [];
  104. //// $info = $to['data'];
  105. //// $item['owner'] = isset($info['nickname']) ? $info['nickname'] : "";
  106. // // $item['ownerid'] = $info['id'];
  107. // $item['commobile'] = isset($value['commobile']) ? $value['commobile'] : "";;
  108. // $item['comtel'] = "";
  109. // $item['contactor'] = isset($value['contactor']) ? $value['contactor'] : "";
  110. // $item['position'] = isset($value['position']) ? $value['position'] : "";
  111. // $item['wxaccount'] = isset($value['wxaccount']) ? $value['wxaccount'] : "";
  112. // $item['qqaccount'] = isset($value['qqaccount']) ? $value['qqaccount'] : "";
  113. // $item['email'] = isset($value['email']) ? $value['email'] : "";
  114. // $item['comdepart'] = isset($value['comdepart']) ? $value['comdepart'] : "";
  115. // //$item['is_private'] = isset($value['is_private']) ? $value['is_private'] : "";
  116. // $item['status'] = $value['status'];
  117. // $item['createrid'] = $createrid;
  118. // $item['creater'] = $creater;
  119. // $item['companyNo'] = $companyNo;
  120. // $item['is_del'] = 0;
  121. // $item['addtime'] = date("Y-m-d H:i:s");
  122. // $item['updatetime'] = date("Y-m-d H:i:s");
  123. // $var[] = $item;
  124. // }
  125. // $vp = Db::name('customer_member')->insertAll($var);
  126. // if ($vp == "") {
  127. // Db::rollback();
  128. // return error_show(1002, "新建失败");
  129. // }
  130. //
  131. // Db::commit();
  132. // return error_show(0, "新建成功");
  133. // } else {
  134. // Db::rollback();
  135. // return error_show(1002, "新建失败");
  136. // }
  137. // } catch (\Exception $e) {
  138. // Db::rollback();
  139. // return error_show(1005, $e->getMessage());
  140. // }
  141. }
  142. public function list()
  143. {
  144. $page = isset($this->post['page']) && $this->post['page'] !== "" ? intval($this->post['page']) : "1";
  145. $size = isset($this->post['size']) && $this->post['size'] !== "" ? intval($this->post['size']) : "10";
  146. $where = [["a.is_del", "=", 0]];
  147. $companyName = isset($this->post['companyName']) && $this->post['companyName'] !== "" ? trim($this->post['companyName']) : "";
  148. if ($companyName != "") {
  149. $where[] = ['b.companyName', "like", "%$companyName%"];
  150. }
  151. $status = isset($this->post['status']) && $this->post['status'] !=="" ? intval($this->post['status'])
  152. :"";
  153. if($status!==""){
  154. $where[]=['status',"=",$status];
  155. }
  156. $creater = isset($this->post['creater']) && $this->post['creater'] !== "" ? intval($this->post['creater']) : "";
  157. if ($creater != "") {
  158. $where[] = ['a.creater', "like", "%$creater%"];
  159. }
  160. $start = isset($this->post['start']) && $this->post['start'] !== "" ? $this->post['start'] : "";
  161. if ($start !== "") {
  162. $where[] = ['a.addtime', ">=", date('Y-m-d H:i:s', strtotime($start))];
  163. }
  164. $end = isset($this->post['end']) && $this->post['end'] !== "" ? $this->post['end'] : "";
  165. if ($end !== "") {
  166. $where[] = ['a.addtime', "<", date('Y-m-d H:i:s', strtotime($end) + 24 * 3600)];
  167. }
  168. $count = Db::name('customer_member')->alias('a')->join('customer_info b', "b.companyNo=a.companyNo", "left")
  169. ->where($where)->count();
  170. $total = ceil($count / $size);
  171. $page = $page >= $total ? $total : $page;
  172. $list = Db::name('customer_member')->alias('a')->join('customer_info b', "b.companyNo=a.companyNo", "left")
  173. ->where($where)->page($page, $size)->order("addtime desc")->field("a.*,b.companyName,b.parent")->select();
  174. return app_show(0, "获取成功", ['list' => $list, 'count' => $count]);
  175. }
  176. public function edit()
  177. {
  178. $token = isset($this->post['token']) && $this->post['token'] != '' ? trim($this->post['token']) : "";
  179. if ($token == '') {
  180. return error_show(1005, "参数token不能为空");
  181. }
  182. $id = isset($this->post['id']) && $this->post['id'] !== "" ? intval($this->post['id']) : "";
  183. if ($id == "") {
  184. return error_show(1002, "参数id不能为空");
  185. }
  186. $idinfo = Db::name('customer_info')->where(['id' => $id, 'is_del' => 0])->find();
  187. if (empty($idinfo)) {
  188. return error_show(1004, "未找到数据");
  189. }
  190. if($idinfo['status']==1){
  191. return error_show(1002,"状态是启用状态,无法编辑");
  192. }
  193. // $companyNo = isset($this->post['companyNo']) && $this->post['companyNo'] !== "" ? $this->post['companyNo'] : "";
  194. // if ($companyNo == "") {
  195. // return error_show(1002, "参数companyNo不能为空");
  196. // }
  197. // $company = Db::name('customer_info')->where(['companyNo'=>$companyNo,'is_del'=>0])->find();
  198. // if(empty($company)){
  199. // return error_show(1004,"未找到数据");
  200. // }
  201. $companyName = isset($this->post['companyName']) && $this->post['companyName'] !== "" ? $this->post['companyName'] : "";
  202. if ($companyName == "") {
  203. return error_show(1002, "参数companyName不能为空");
  204. }
  205. $parent = isset($this->post['parent']) && $this->post['parent'] !== "" ? $this->post['parent'] : "";
  206. if ($parent == "") {
  207. return error_show(1002, "参数parent不能为空");
  208. }
  209. $rename = Db::name('customer_org1')->where(['is_del' =>0,'id'=>$parent])->find();
  210. $item = Db::name('customer_info')->where(['itemid'=>$rename['id'],'companyName'=>$companyName,'is_del'=>0])->where([['id','<>',$id]])->find();
  211. if ($item!=false) {
  212. return error_show(1002, "公司名称已存在");
  213. }
  214. $customer_member = isset($this->post['customer_member']) && $this->post['customer_member'] !== "" ? $this->post['customer_member'] : "";
  215. if ($customer_member == "") {
  216. return error_show(1002, "参数customer_member不能为空");
  217. }
  218. $user = GetUserInfo($token);
  219. if (empty($user) || $user['code'] != 0) {
  220. return error_show(102, "创建人数据不存在");
  221. }
  222. $createrid = isset($user["data"]['id']) ? $user["data"]['id'] : "";
  223. $creater = isset($user["data"]['nickname']) ? $user["data"]['nickname'] : "";
  224. Db::startTrans();
  225. try {
  226. $data = [
  227. "id" => $id,
  228. "companyName" => $companyName,
  229. //"companyNo" => $companyNo,
  230. "parent" => $parent,
  231. "updatetime" => date("Y-m-d H:i:s"),
  232. ];
  233. $datainfo = Db::name('customer_info')->save($data);
  234. // var_dump(Db::name('customer_info')->getLastSql());
  235. $stn = array_diff($data,$idinfo);
  236. $json = json_encode($stn,JSON_UNESCAPED_UNICODE);
  237. $jsp = json_encode($idinfo,JSON_UNESCAPED_UNICODE);
  238. if ($datainfo) {
  239. ChangeLog::logAdd("2",$idinfo['companyNo'],$jsp,$json,$this->post['token'],$this->post);
  240. // $var=[];
  241. // var_dump($customer_member);
  242. foreach ($customer_member as $value) {
  243. // $to = GetInfoById($token, ['id' => $value['ownerid']]);
  244. //
  245. // if (empty($to) || $to['code'] != 0) {
  246. // return app_show(1002, "数据拥有者信息不存在");
  247. // }
  248. $item = [];
  249. // $info = $to['data'];
  250. // $item['owner'] = $info['nickname'];
  251. // $item['ownerid'] = $info['id'];
  252. $item['commobile'] =isset($value['commobile']) ? $value['commobile'] : "";
  253. $item['comtel'] = "";
  254. // $item['id'] =isset($value['id']) ? $value['id'] : "";
  255. isset($value['id'])&&$value['id']!=="" ? $item['id'] = $value['id']:'';
  256. $item['contactor'] = isset($value['contactor']) ? $value['contactor'] : "";
  257. $item['position'] = isset($value['position']) ? $value['position'] : "";
  258. $item['wxaccount'] = isset($value['wxaccount']) ? $value['wxaccount'] : "";
  259. $item['qqaccount'] = isset($value['qqaccount']) ? $value['qqaccount'] : "";
  260. $item['email'] = isset($value['email']) ? $value['email'] : "";
  261. $item['comdepart'] = isset($value['comdepart']) ? $value['comdepart'] : "";
  262. // $item['is_private'] = isset($value['is_private']) ? $value['is_private'] : "";
  263. $item['status'] = $value['status'];
  264. $item['createrid'] = $createrid;
  265. $item['creater'] = $creater;
  266. $item['companyNo'] = isset( $idinfo['companyNo']) ? $idinfo['companyNo'] : "";
  267. $item['is_del'] = 0;
  268. $item['addtime']=date("Y-m-d H:i:s");
  269. $item['updatetime'] = date("Y-m-d H:i:s");
  270. // $var[]=$item;
  271. $vp = Db::name('customer_member')->save($item);
  272. // var_dump(Db::name('customer_member')->getLastSql());
  273. // $var = array_diff($customer_member,$item);
  274. // $jsn = json_encode($var);
  275. if ($vp == false) {
  276. // ChangeLog::logAdd("2",$item['companyNo'],$item,$jsn,$this->post['token'],$this->post);
  277. Db::rollback();
  278. return error_show(1003, "更新失败");
  279. }
  280. }
  281. Db::commit();
  282. return error_show(0, "更新成功");
  283. } else {
  284. Db::rollback();
  285. return error_show(1002, "更新失败");
  286. }
  287. } catch (\Exception $e) {
  288. Db::rollback();
  289. return error_show(1005, $e->getMessage());
  290. }
  291. }
  292. public function info()
  293. {
  294. $companyNo = isset($this->post['companyNo']) && $this->post['companyNo'] !== "" ? trim($this->post['companyNo']) : "";
  295. if ($companyNo == "") return error_show(1002, "参数companyNo不能为空");
  296. $userCommon = new \app\admin\common\User();
  297. $rs = $userCommon->handle('cInfo',['companyNo'=>$companyNo]);
  298. return json_show($rs['code'],$rs['message'],$rs['data']);
  299. //>field("companyNo,companyName,itemid as parent,id")-
  300. // $idinfo = Db::name('customer_info')->where(['companyNo' => $companyNo, 'is_del' => 0])->find();
  301. // if ($idinfo == "") {
  302. // return error_show(1002, "未找到数据");
  303. // }
  304. // $info = Db::name("customer_member")->where(['companyNo' => $idinfo['companyNo'], "is_del" => 0])->select();
  305. // if ($info == "") {
  306. // return error_show(1002, "未找到customer_member数据");
  307. // }
  308. // $in = Db::name("customer_attr")->where(['id' => $idinfo['parent'], "is_del" => 0])->find();
  309. // if ($in == "") {
  310. // return error_show(1002, "未找到customer_attr数据");
  311. // }
  312. // $idinfo['attr_name'] = $in['name'];
  313. // $idinfo['member'] = $info;
  314. // if ($idinfo == "") {
  315. // return error_show(1002, "未找到数据");
  316. // }
  317. // return app_show(0, "获取成功", $idinfo);
  318. }
  319. public function del(){
  320. $param = $this->request->only(['id' => 0], 'post', 'trim');
  321. $userCommon = new \app\admin\common\User();
  322. $rs = $userCommon->handle('delete', [
  323. 'ids' => [$param['id']],
  324. 'type' => 3,
  325. 'updater' => $this->uname,
  326. 'updaterid' => $this->uid,
  327. ]);
  328. return json_show($rs['code'], $rs['message'], $rs['data']);
  329. // $id = $this->post['id'] && $this->post['id'] !=="" ? intval($this->post['id']) :"";
  330. // if($id===""){
  331. // return error_show(1004,"参数id不能为空");
  332. // }
  333. // $str= Db::name('customer_info')->where(['id'=>$id,'is_del'=>0])->find();
  334. // if(empty($str)){
  335. // return error_show(1002,"未找到数据");
  336. // }
  337. // if($str['status']==1){
  338. // return error_show(1005,"启用状态,不允许删除");
  339. // }
  340. // $end = Db::name('customer_info')->update(['id'=>$id,'is_del'=>1]);
  341. // if($end){
  342. // return error_show(0,"删除成功");
  343. // }else{
  344. // return error_show(1002,"删除失败");
  345. // }
  346. }
  347. public function status(){
  348. $id = isset($this->post['id']) && $this->post['id'] !==""? intval($this->post['id']):"";
  349. if($id==""){
  350. return error_show(1002,"参数id不能为空");
  351. }
  352. $info = Db::name("customer_info")->where([["id","=",$id],["is_del","=",0]])->find();
  353. if(!$info){
  354. return error_show(1002,"未找到对应数据");
  355. }
  356. $status = isset($this->post['status']) && $this->post['status']!==""? intval($this->post['status']):"";
  357. if($status===""){
  358. return error_show(1002,"参数status不能为空");
  359. }
  360. if(!in_array($status,[0,1])){
  361. return error_show(1002,"参数status无效");
  362. }
  363. $info['status']=$status;
  364. $msg = $status==1?"启用":"禁用";
  365. $update = Db::name("customer_info")->save($info);
  366. return $update? error_show(0,"{$msg}成功"):error_show(1004,"{$msg}失败");
  367. }
  368. }