Customar.php 15 KB

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