Customar.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  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($idinfo,$data);
  227. $json = json_encode($stn);
  228. if ($datainfo) {
  229. ChangeLog::logAdd("2",$idinfo['companyNo'],$idinfo,$json,$this->post['token'],$this->post);
  230. // $var=[];
  231. // var_dump($customer_member);
  232. foreach ($customer_member as $value) {
  233. // $to = GetInfoById($token, ['id' => $value['ownerid']]);
  234. //
  235. // if (empty($to) || $to['code'] != 0) {
  236. // return app_show(1002, "数据拥有者信息不存在");
  237. // }
  238. $item = [];
  239. // $info = $to['data'];
  240. // $item['owner'] = $info['nickname'];
  241. // $item['ownerid'] = $info['id'];
  242. $item['commobile'] =isset($value['commobile']) ? $value['commobile'] : "";
  243. $item['comtel'] = "";
  244. // $item['id'] =isset($value['id']) ? $value['id'] : "";
  245. isset($value['id'])&&$value['id']!=="" ? $item['id'] = $value['id']:'';
  246. $item['contactor'] = isset($value['contactor']) ? $value['contactor'] : "";
  247. $item['position'] = isset($value['position']) ? $value['position'] : "";
  248. $item['wxaccount'] = isset($value['wxaccount']) ? $value['wxaccount'] : "";
  249. $item['qqaccount'] = isset($value['qqaccount']) ? $value['qqaccount'] : "";
  250. $item['email'] = isset($value['email']) ? $value['email'] : "";
  251. $item['comdepart'] = isset($value['comdepart']) ? $value['comdepart'] : "";
  252. // $item['is_private'] = isset($value['is_private']) ? $value['is_private'] : "";
  253. $item['status'] = $value['status'];
  254. $item['createrid'] = $createrid;
  255. $item['creater'] = $creater;
  256. $item['companyNo'] = isset( $idinfo['companyNo']) ? $idinfo['companyNo'] : "";
  257. $item['is_del'] = 0;
  258. $item['addtime']=date("Y-m-d H:i:s");
  259. $item['updatetime'] = date("Y-m-d H:i:s");
  260. // $var[]=$item;
  261. $vp = Db::name('customer_member')->save($item);
  262. // var_dump(Db::name('customer_member')->getLastSql());
  263. // $var = array_diff($customer_member,$item);
  264. // $jsn = json_encode($var);
  265. if ($vp == false) {
  266. // ChangeLog::logAdd("2",$item['companyNo'],$item,$jsn,$this->post['token'],$this->post);
  267. Db::rollback();
  268. return error_show(1003, "更新失败");
  269. }
  270. }
  271. Db::commit();
  272. return error_show(0, "更新成功");
  273. } else {
  274. Db::rollback();
  275. return error_show(1002, "更新失败");
  276. }
  277. } catch (\Exception $e) {
  278. Db::rollback();
  279. return error_show(1005, $e->getMessage());
  280. }
  281. }
  282. public function info()
  283. {
  284. $id = isset($this->post['id']) && $this->post['id'] !== "" ? intval($this->post['id']) : "";
  285. if ($id == "") {
  286. return error_show(1002, "参数id不能为空");
  287. }
  288. //>field("companyNo,companyName,itemid as parent,id")-
  289. $idinfo = Db::name('customer_info')->where(['id' => $id, 'is_del' => 0])->find();
  290. if ($idinfo == "") {
  291. return error_show(1002, "未找到数据");
  292. }
  293. $info = Db::name("customer_member")->where(['companyNo' => $idinfo['companyNo'], "is_del" => 0])->select();
  294. if ($info == "") {
  295. return error_show(1002, "未找到customer_member数据");
  296. }
  297. // $in = Db::name("customer_attr")->where(['id' => $idinfo['parent'], "is_del" => 0])->find();
  298. // if ($in == "") {
  299. // return error_show(1002, "未找到customer_attr数据");
  300. // }
  301. // $idinfo['attr_name'] = $in['name'];
  302. $idinfo['member'] = $info;
  303. if ($idinfo == "") {
  304. return error_show(1002, "未找到数据");
  305. }
  306. return app_show(0, "获取成功", $idinfo);
  307. }
  308. public function del(){
  309. $id = $this->post['id'] && $this->post['id'] !=="" ? intval($this->post['id']) :"";
  310. if($id===""){
  311. return error_show(1004,"参数id不能为空");
  312. }
  313. $str= Db::name('customer_info')->where(['id'=>$id,'is_del'=>0])->find();
  314. if(empty($str)){
  315. return error_show(1002,"未找到数据");
  316. }
  317. if($str['status']==1){
  318. return error_show(1005,"启用状态,不允许删除");
  319. }
  320. $end = Db::name('customer_info')->update(['id'=>$id,'is_del'=>1]);
  321. if($end){
  322. return error_show(0,"删除成功");
  323. }else{
  324. return error_show(1002,"删除失败");
  325. }
  326. }
  327. public function status(){
  328. $id = isset($this->post['id']) && $this->post['id'] !==""? intval($this->post['id']):"";
  329. if($id==""){
  330. return error_show(1002,"参数id不能为空");
  331. }
  332. $info = Db::name("customer_info")->where([["id","=",$id],["is_del","=",0]])->find();
  333. if(!$info){
  334. return error_show(1002,"未找到对应数据");
  335. }
  336. $status = isset($this->post['status']) && $this->post['status']!==""? intval($this->post['status']):"";
  337. if($status===""){
  338. return error_show(1002,"参数status不能为空");
  339. }
  340. if(!in_array($status,[0,1])){
  341. return error_show(1002,"参数status无效");
  342. }
  343. $info['status']=$status;
  344. $msg = $status==1?"启用":"禁用";
  345. $update = Db::name("customer_info")->save($info);
  346. return $update? error_show(0,"{$msg}成功"):error_show(1004,"{$msg}失败");
  347. }
  348. }