Customar.php 15 KB

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