Customar.php 17 KB

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