Customar.php 14 KB

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