Customar.php 17 KB

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