Customar.php 16 KB

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