Customar.php 14 KB

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