CustomerOrg.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  1. <?php
  2. namespace app\controller;
  3. use app\BaseController;
  4. use think\facade\Db;
  5. use think\facade\Validate;
  6. class CustomerOrg extends BaseController
  7. {
  8. private $table_name = 'customer_org1';//此处的表名比较特殊
  9. public function create()
  10. {
  11. $param = $this->request->only(['name', 'weight' => 0, 'pid' => 0, 'uid', 'uname', 'level' => 1], 'post', 'trim');
  12. $val = Validate::rule([
  13. 'name' => 'require',
  14. 'weight' => 'require|number|egt:0',
  15. 'pid' => 'require|number|egt:0',
  16. 'uid' => 'require|number|gt:0',
  17. 'uname' => 'require',
  18. 'level' => 'require|number|gt:0',
  19. ]);
  20. if ($val->check($param) == false) return json_show(1004, $val->getError());
  21. // $name = isset($this->post['name']) && $this->post['name'] !== "" ? trim($this->post['name']) : "";
  22. // if ($name == "") {
  23. // return json_show(1003, "公司名称不能为空");
  24. // }
  25. // $weight = isset($this->post['weight']) && $this->post['weight'] !== "" ? trim($this->post['weight']) : "0";
  26. // $pid = isset($this->post['pid']) && $this->post['pid'] !== "" ? trim($this->post['pid']) : "0";
  27. // if ($pid === "") {
  28. // return json_show(1002, "父级id不能为空");
  29. // }
  30. if ($param['pid'] != 0) {
  31. $spid = Db::name($this->table_name)
  32. ->where(['id' => $param['pid'], 'is_del' => 0])
  33. ->findOrEmpty();
  34. //var_dump( Db::name($this->table_name)->getLastSql());
  35. if (empty($spid)) return json_show(1004, "父级不能为空");
  36. }
  37. $rename = Db::name($this->table_name)
  38. ->field('id')
  39. ->where(['is_del' => 0, 'name' => $param['name'], 'pid' => $param['pid']])
  40. ->findOrEmpty();
  41. if (!empty($rename)) return json_show(1002, "组织名称已存在");
  42. // $token = isset($this->post['token'])&& $this->post['token']!='' ? trim($this->post['token']):"";
  43. // if($token==''){
  44. // return json_show(105,"参数token不能为空");
  45. // }
  46. // $user =GetUserInfo($token);
  47. // if(empty($user)||$user['code']!=0){
  48. // return json_show(1002,"创建人数据不存在");
  49. // }
  50. $createrid = $param['uid'];//isset($user["data"]['id']) ? $user["data"]['id'] : "";
  51. $creater = $param['uname'];//isset($user["data"]['nickname']) ? $user["data"]['nickname'] : "";
  52. // $level = isset($this->post['level']) && $this->post['level'] !== "" ? trim($this->post['level']) : "1";
  53. Db::startTrans();
  54. try {
  55. $date = date('Y-m-d H:i:s');
  56. $tada = [
  57. "name" => $param['name'],
  58. "weight" => $param['weight'],
  59. "pid" => $param['pid'],
  60. "level" => $param['level'],
  61. "updatetime" => $date,
  62. "addtime" => $date,
  63. "depart_link" => "",
  64. "is_del" => 0,
  65. "pname" => isset($spid['name']) ? $spid['name'] : "",
  66. "creater" => $creater,
  67. "createrid" => $createrid
  68. ];
  69. $t = Db::name($this->table_name)->insert($tada, true);
  70. //var_dump(Db::name($this->table_name)->getLastSql());
  71. if ($t > 0) {
  72. if (isset($spid)) $depart_link = $spid['depart_link'] . "{$t}-";
  73. else $depart_link = "{$t}-";
  74. $level = explode('-', $depart_link);
  75. // var_dump($level);
  76. $level = array_filter($level);
  77. $level = count($level);
  78. $k = ['depart_link' => $depart_link, 'level' => $level];
  79. $u = Db::name($this->table_name)->where(['id' => $t])->save($k);
  80. // var_dump( Db::name($this->table_name)->getLastSql());
  81. if ($u) {
  82. Db::commit();
  83. return json_show(0, "新建成功");
  84. }
  85. }
  86. throw new Exception('新建失败');
  87. } catch (Exception $e) {
  88. Db::rollback();
  89. return json_show(1003, $e->getMessage());
  90. }
  91. }
  92. public function list()
  93. {
  94. $param = $this->request->only(['pid' => 0, 'companyNo' => '', 'creater' => '', 'name' => '', 'start' => '', 'end' => '', 'status' => '', 'company_name' => ''], 'post', 'trim');
  95. // $pid = isset($this->post['pid']) &&$this->post['pid']!=="" ?intval($this->post['pid']): "0";
  96. $where = [['ci.is_del', "=", 0]];
  97. $condition = [['co.is_del', "=", 0]];
  98. if (intval($param['pid']) !== "") $condition[] = ["co.pid", "=", intval($param['pid'])];
  99. // $companyNo = isset($this->post['companyNo']) && $this->post['companyNo'] !=="" ? trim($this->post['companyNo']):"";
  100. if ($param['companyNo'] !== "") $where[] = ['ci.companyNo', "like", '%' . $param['companyNo'] . '%'];
  101. // $creater = isset($this->post['creater']) && $this->post['creater'] !=="" ? trim($this->post['creater']):"";
  102. if ($param['creater'] !== "") $where[] = ['ci.creater', "like", '%' . $param['creater'] . '%'];
  103. // $name= isset($this->post['name'])&&$this->post['name']!=="" ? trim($this->post['name']):"";
  104. if ($param['name'] != "") $condition[] = ["co.name", "like", "%{$param['name']}%"];
  105. // $start = isset($this->post['start']) && $this->post['start'] !== "" ? $this->post['start'] : "";
  106. if ($param['start'] != "") $where[] = ["ci.addtime", '>=', $param['start']];
  107. // $end = isset($this->post['end']) && $this->post['end'] !== "" ? $this->post['end'] : "";
  108. if ($param['end'] != "") $where[] = ["ci.addtime", '<=', $param['end'] . ' 23:59:59'];
  109. // $status = isset($this->post['status']) && $this->post['status'] !=="" ? intval($this->post['status']):"";
  110. if ($param['status'] !== "") $where[] = ['ci.status', "=", $param['status']];
  111. // $count = Db::name($this->table_name)->where($condition)->count();
  112. // $page>=ceil($count/$size) ? $page=ceil($count/$size) :"";
  113. // $creater = isset($this->post['creater']) && $this->post['creater'] !=="" ? trim($this->post['creater']):"";
  114. // if($creater !==""){
  115. // $condition[] = ['co.creater',"like","%$creater%"];
  116. // }
  117. // $name= isset($this->post['name'])&&$this->post['name']!=="" ? trim($this->post['name']):"";
  118. // if($name!=""){
  119. // $condition[]=["co.name","like","%{$this->post['name']}%"];
  120. // }
  121. // $start = isset($this->post['start']) && $this->post['start'] !== "" ? $this->post['start'] : "";
  122. // if ($start != "") {
  123. // $condition[] = ["co.addtime", '>=', $start];
  124. // }
  125. // $end = isset($this->post['end']) && $this->post['end'] !== "" ? $this->post['end'] : "";
  126. // if ($end != "") {
  127. // $condition[] = ["co.addtime", '<=', $end];
  128. // }
  129. // $status = isset($this->post['status']) && $this->post['status'] !=="" ? intval($this->post['status']):"";
  130. // if($status!==""){
  131. // $condition[] = ['co.status',"=",$status];
  132. // }
  133. // $company_name = isset($this->post['company_name']) && $this->post['company_name'] !== "" ? trim($this->post['company_name']) : "";
  134. if ($param['company_name'] !== "") {
  135. // $company_ids = get_company_item_user_by_name($param['company_name']);
  136. $where[] = ['c.name', "like", '%' . $param['company_name'] . '%'];
  137. $condition[] = ['c.name', "like", '%' . $param['company_name'] . '%'];
  138. }
  139. $pidlist = Db::name("customer_org1")
  140. ->alias('co')
  141. // ->leftJoin("depart_user u", "u.uid=co.createrid AND u.is_del=0")
  142. ->where($condition)
  143. ->group('co.pid')
  144. ->column("co.pid");
  145. if (!empty($pidlist)) $where[] = [["ci.itemid", "in", $pidlist]];
  146. else {
  147. if ($param['pid'] === "") $where[] = ['ci.itemid', '=', 0];
  148. else $where[] = ['ci.itemid', '=', $param['pid']];
  149. }
  150. $list = Db::name($this->table_name)
  151. ->alias('co')
  152. ->field("co.id,co.name,co.creater,co.status,co.addtime,0 kh,c.name company_name")
  153. ->where($condition)
  154. ->leftJoin('account_item b', 'b.account_id=co.createrid')
  155. ->leftJoin('company_item c', 'c.id=b.itemid')
  156. ->order("co.addtime", 'desc')
  157. ->select()
  158. ->toArray();
  159. // $all_createrid = array_column($list,'createrid');
  160. // $all_item = get_company_name_by_uid($all_createrid);
  161. // $var=[];
  162. // foreach ($list as $item) {
  163. // $iten=[];
  164. // $iten['name']=$item['name'];
  165. //// $iten['code']="";
  166. // $iten['id']=$item['id'];
  167. // $iten['creater']=$item['creater'];
  168. // $iten['status']=$item['status'];
  169. // $iten['addtime']=$item['addtime'];
  170. // $iten['kh']=0;//组织架构
  171. // $iten['company_name'] = $all_item[$item['createrid']]??'';
  172. //
  173. // $var[]=$iten;
  174. // }
  175. $itm = Db::name('customer_info')
  176. ->alias('ci')
  177. ->field('ci.id,ci.companyName name,ci.companyNo code,ci.status,ci.creater,ci.addtime,1 kh,c.name company_name')
  178. // ->leftJoin("depart_user u", "u.nickname=ci.creater AND u.is_del=0")
  179. ->leftJoin('account_item b', 'b.account_id=ci.createrid')
  180. ->leftJoin('company_item c', 'c.id=b.itemid')
  181. ->where($where)
  182. ->order("ci.addtime desc")
  183. ->select()
  184. ->toArray();
  185. // foreach ($itm as $vat){
  186. // $inm=[];
  187. // $inm['name']=$vat['companyName'];
  188. // $inm['id']=$vat['id'];
  189. // $inm['code']=$vat['companyNo'];
  190. //// $inm['companyName']=$vat['companyName'];
  191. //// $inm['invoice_bank']=$vat['invoice_bank'];
  192. //// $inm['invoice_bankNo']=$vat['invoice_bankNo'];
  193. //// $inm['invoice_code']=$vat['invoice_code'];
  194. //// $inm['invoice_people']=$vat['invoice_people'];
  195. //// $inm['invoice_mobile']=$vat['invoice_mobile'];
  196. //// $inm['invoice_addr']=$vat['invoice_addr'];
  197. //// $inm['parent']=$vat['parent'];
  198. //// $inm['branch']=$vat['branch'];
  199. //// $inm['middle']=$vat['middle'];
  200. //// $inm['country']=$vat['country'];
  201. // $inm['status']=$vat['status'];
  202. // $inm['creater']=$vat['creater'];
  203. // $inm['addtime']=$vat['addtime'];
  204. // $inm['kh']=1;//客户
  205. // $inm['company_name'] = '待处理';
  206. // $var[]=$inm;
  207. // }
  208. return json_show(0, "获取成功", array_merge($list, $itm));
  209. }
  210. public function update()
  211. {
  212. $param = $this->request->only(['id', 'name', 'weight' => 0, 'pid' => 0, 'uid', 'uname', 'level' => 1], 'post', 'trim');
  213. $val = Validate::rule([
  214. 'id' => 'require|number|gt:0',
  215. 'pid' => 'require|number|egt:0',
  216. 'weight' => 'require|number|egt:0',
  217. 'name' => 'require',
  218. 'uid' => 'require|number|gt:0',
  219. 'uname' => 'require',
  220. 'level' => 'require|number|gt:0',
  221. ]);
  222. if ($val->check($param) == false) return json_show(1004, $val->getError());
  223. // $id = isset($this->post['id'])?intval($this->post['id']):"";
  224. // if($id===""){
  225. // return json_show(1004,"参数id不能为空");
  226. // }
  227. $sid = Db::name($this->table_name)
  228. ->field('id,status')
  229. ->where("id", $param['id'])
  230. ->findOrEmpty();
  231. // var_dump(Db::name($this->table_name)->getLastSql());
  232. if (empty($sid)) return json_show(1004, "公司不存在");
  233. if ($sid['status'] == 1) return json_show(1002, "状态是启用状态,无法编辑");
  234. // $pid = isset($this->post['pid']) && $this->post['pid'] !=="" ? intval($this->post['pid']):"";
  235. // if($pid===""){
  236. // return json_show(1004,"父级id不能为空");
  237. // }
  238. if ($param['pid'] != 0) {
  239. $fpid = Db::name($this->table_name)
  240. ->field('id,depart_link')
  241. ->where(['id' => $param['pid'], 'is_del' => 0])
  242. ->findOrEmpty();
  243. if (empty($fpid)) return json_show(1004, "父级不能为空");
  244. }
  245. // $weight = isset($this->post['weight']) && $this->post['weight'] !==""? intval($this->post['weight']):"0";
  246. // $level = isset($this->post['level']) && $this->post['level'] !==""? intval($this->post['level']):"";
  247. // $name = isset($this->post['name'])? trim($this->post['name']):"";
  248. // if($name==""){
  249. // return json_show(1004,"公司名称不能为空");
  250. // }
  251. $repeat_name = Db::name($this->table_name)
  252. ->field('id')
  253. ->where(["is_del" => 0, "name" => $param['name'], 'pid' => $param['pid']])
  254. ->where("id", "<>", $param['id'])
  255. ->findOrEmpty();
  256. // echo Db::name($this->table_name)->getLastSql();
  257. if (!empty($repeat_name)) return json_show(1004, "部门名称已存在");
  258. if (isset($fpid)) $depart_link = $fpid['depart_link'] . $param['id'] . "-";
  259. else $depart_link = $param['id'] . "-";
  260. $level = explode('-', $depart_link);
  261. $level = array_filter($level);
  262. $level = count($level);
  263. $vir = [
  264. "id" => $param['id'],
  265. "name" => $param['name'],
  266. "pid" => $param['pid'],
  267. "weight" => $param['weight'],
  268. "depart_link" => $depart_link,
  269. "level" => $level,
  270. "updatetime" => date("Y-m-d H:i:s"),
  271. ];
  272. $org = Db::name($this->table_name)->save($vir);
  273. return $org ? json_show(0, "更新成功") : json_show(1004, "更新失败");
  274. // if($org)
  275. // return json_show(0,"更新成功");
  276. // else{
  277. // return json_show(1004,"更新失败");
  278. // }
  279. }
  280. public function info()
  281. {
  282. $id = $this->request->post('id/d', 0, 'trim');
  283. $res = Db::name($this->table_name)
  284. ->where(['id' => $id, 'is_del' => 0])
  285. ->findOrEmpty();
  286. return json_show(0, "获取成功", $res);
  287. }
  288. public function delete()
  289. {
  290. $id = $this->request->post('id/d', 0, 'trim');
  291. $custy = Db::name('customer_org1')
  292. ->field('id')
  293. ->where(["is_del" => 0, 'id' => $id])
  294. ->findOrEmpty();
  295. if (empty($custy)) return json_show(1004, "公司不存在");
  296. //$var= Db::name('customer_info')->where(['itemid'=>$id,'is_del'=>0])->find();
  297. $db = Db::name('customer_org1')
  298. ->field('id')
  299. ->where(['pid' => $custy['id'], 'is_del' => 0])
  300. ->findOrEmpty();
  301. if (!empty($db)) return json_show(1004, "下一级还有组织,不允许删除");
  302. $var = Db::name('customer_info')
  303. ->field('id')
  304. ->where(['itemid' => $id, 'is_del' => 0])
  305. ->findOrEmpty();
  306. if (!empty($var)) return json_show(1004, "下一级还有分公司,不允许删除");
  307. $custy['is_del'] = 1;
  308. $custy['updatetime'] = date("Y-m-d H:i:s");
  309. $compy = Db::name('customer_org1')->save($custy);
  310. return $compy ? json_show(0, "删除成功") : json_show(1004, "删除失败");
  311. }
  312. public function status()
  313. {
  314. $param = $this->request->only(['id', 'status'], 'post', 'trim');
  315. $val = Validate::rule([
  316. 'id' => 'require|number|gt:0',
  317. 'status' => 'require|number|in:0,1',
  318. ]);
  319. if ($val->check($param) == false) return json_show(1004, $val->getError());
  320. // $id = isset($this->post['id']) && $this->post['id'] !==""? intval($this->post['id']):"";
  321. // if($id==""){
  322. // return json_show(1002,"参数id不能为空");
  323. // }
  324. // $info = Db::name("customer_org1")->where([["id","=",$id]])->find();
  325. // if(!$info){
  326. // return json_show(1002,"未找到对应数据");
  327. // }
  328. // $status = isset($this->post['status']) && $this->post['status']!==""? intval($this->post['status']):"";
  329. // if($status===""){
  330. // return json_show(1002,"参数status不能为空");
  331. // }
  332. // if(!in_array($status,[0,1])){
  333. // return json_show(1002,"参数status无效");
  334. // }
  335. // $info['status']=$status;
  336. // $info['updatetime']=date("Y-m-d H:i:s");
  337. // $msg = $status==1?"启用":"禁用";
  338. $update = Db::name("customer_org1")
  339. ->where(['is_del' => 0, 'id' => $param['id']])
  340. ->where('status', '<>', $param['status'])
  341. ->update([
  342. 'status' => $param['status'],
  343. 'updatetime' => date("Y-m-d H:i:s"),
  344. ]);
  345. return $update ? json_show(0, "操作成功") : json_show(1004, "操作失败");
  346. }
  347. //获取三级架构
  348. public function getTopCustomerOrg()
  349. {
  350. $itemid = $this->request->filter('trim')->post('itemid');
  351. $res = [];
  352. foreach ($itemid as $val){
  353. $res[$val] = get_top_customer_org($val);
  354. }
  355. return json_show(0, '获取架构成功', $res);
  356. }
  357. }