CustomerOrg.php 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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. if ($param['pid'] != 0) {
  22. $spid = Db::name($this->table_name)
  23. ->where(['id' => $param['pid'], 'is_del' => 0])
  24. ->findOrEmpty();
  25. if (empty($spid)) return json_show(1004, "父级不能为空");
  26. }
  27. $rename = Db::name($this->table_name)
  28. ->field('id')
  29. ->where(['is_del' => 0, 'name' => $param['name'], 'pid' => $param['pid']])
  30. ->findOrEmpty();
  31. if (!empty($rename)) return json_show(1002, "组织名称已存在");
  32. $createrid = $param['uid'];//isset($user["data"]['id']) ? $user["data"]['id'] : "";
  33. $creater = $param['uname'];//isset($user["data"]['nickname']) ? $user["data"]['nickname'] : "";
  34. Db::startTrans();
  35. try {
  36. $date = date('Y-m-d H:i:s');
  37. $tada = [
  38. "name" => $param['name'],
  39. "weight" => $param['weight'],
  40. "pid" => $param['pid'],
  41. "level" => $param['level'],
  42. "updatetime" => $date,
  43. "addtime" => $date,
  44. "depart_link" => "",
  45. "is_del" => 0,
  46. "pname" => isset($spid['name']) ? $spid['name'] : "",
  47. "creater" => $creater,
  48. "createrid" => $createrid
  49. ];
  50. $t = Db::name($this->table_name)->insert($tada, true);
  51. if ($t > 0) {
  52. if (isset($spid)) $depart_link = $spid['depart_link'] . "{$t}-";
  53. else $depart_link = "{$t}-";
  54. $level = explode('-', $depart_link);
  55. $level = array_filter($level);
  56. $level = count($level);
  57. $k = ['depart_link' => $depart_link, 'level' => $level];
  58. $u = Db::name($this->table_name)->where(['id' => $t])->save($k);
  59. if ($u) {
  60. Db::commit();
  61. return json_show(0, "新建成功");
  62. }
  63. }
  64. throw new \Exception('新建失败');
  65. } catch (\Exception $e) {
  66. Db::rollback();
  67. return json_show(1003, $e->getMessage());
  68. }
  69. }
  70. public function list()
  71. {
  72. $param = $this->request->only(['pid' => 0, 'companyNo' => '', 'creater' => '', 'name' => '', 'start' => '', 'end' => '', 'status' => '', 'company_name' => ''], 'post', 'trim');
  73. $where = [['ci.is_del', "=", 0]];
  74. $condition = [['co.is_del', "=", 0]];
  75. if (intval($param['pid']) !== "") $condition[] = ["co.pid", "=", intval($param['pid'])];
  76. if ($param['companyNo'] !== "") $where[] = ['ci.companyNo', "like", '%' . $param['companyNo'] . '%'];
  77. if ($param['creater'] !== "") $where[] = ['ci.creater', "like", '%' . $param['creater'] . '%'];
  78. if ($param['name'] != "") $condition[] = ["co.name", "like", "%{$param['name']}%"];
  79. if ($param['start'] != "") $where[] = ["ci.addtime", '>=', $param['start']];
  80. if ($param['end'] != "") $where[] = ["ci.addtime", '<=', $param['end'] . ' 23:59:59'];
  81. if ($param['status'] !== "") $where[] = ['ci.status', "=", $param['status']];
  82. if ($param['company_name'] !== "") {
  83. $where[] = ['c.name', "like", '%' . $param['company_name'] . '%'];
  84. $condition[] = ['c.name', "like", '%' . $param['company_name'] . '%'];
  85. }
  86. $pidlist = Db::name("customer_org1")
  87. ->alias('co')
  88. ->where($condition)
  89. ->group('co.pid')
  90. ->column("co.pid");
  91. if (!empty($pidlist)) $where[] = [["ci.itemid", "in", $pidlist]];
  92. else {
  93. if ($param['pid'] === "") $where[] = ['ci.itemid', '=', 0];
  94. else $where[] = ['ci.itemid', '=', $param['pid']];
  95. }
  96. $list = Db::name($this->table_name)
  97. ->alias('co')
  98. ->field("co.id,co.name,co.creater,co.status,co.addtime,0 kh,c.name company_name")
  99. ->where($condition)
  100. ->leftJoin('account_item b', 'b.account_id=co.createrid')
  101. ->leftJoin('company_item c', 'c.id=b.itemid')
  102. ->order("co.addtime", 'desc')
  103. ->select()
  104. ->toArray();
  105. $itm = Db::name('customer_info')
  106. ->alias('ci')
  107. ->field('ci.id,ci.companyName name,ci.companyNo code,ci.status,ci.creater,ci.addtime,1 kh,c.name company_name')
  108. ->leftJoin('account_item b', 'b.account_id=ci.createrid')
  109. ->leftJoin('company_item c', 'c.id=b.itemid')
  110. ->where($where)
  111. ->order("ci.addtime desc")
  112. ->select()
  113. ->toArray();
  114. return json_show(0, "获取成功", array_merge($list, $itm));
  115. }
  116. public function update()
  117. {
  118. $param = $this->request->only(['id', 'name', 'weight' => 0, 'pid' => 0, 'uid', 'uname', 'level' => 1], 'post', 'trim');
  119. $val = Validate::rule([
  120. 'id' => 'require|number|gt:0',
  121. 'pid' => 'require|number|egt:0',
  122. 'weight' => 'require|number|egt:0',
  123. 'name' => 'require',
  124. 'uid' => 'require|number|gt:0',
  125. 'uname' => 'require',
  126. 'level' => 'require|number|gt:0',
  127. ]);
  128. if ($val->check($param) == false) return json_show(1004, $val->getError());
  129. $sid = Db::name($this->table_name)
  130. ->field('id,status')
  131. ->where("id", $param['id'])
  132. ->findOrEmpty();
  133. if (empty($sid)) return json_show(1004, "公司不存在");
  134. if ($sid['status'] == 1) return json_show(1002, "状态是启用状态,无法编辑");
  135. if ($param['pid'] != 0) {
  136. $fpid = Db::name($this->table_name)
  137. ->field('id,depart_link')
  138. ->where(['id' => $param['pid'], 'is_del' => 0])
  139. ->findOrEmpty();
  140. if (empty($fpid)) return json_show(1004, "父级不能为空");
  141. }
  142. $repeat_name = Db::name($this->table_name)
  143. ->field('id')
  144. ->where(["is_del" => 0, "name" => $param['name'], 'pid' => $param['pid']])
  145. ->where("id", "<>", $param['id'])
  146. ->findOrEmpty();
  147. if (!empty($repeat_name)) return json_show(1004, "部门名称已存在");
  148. if (isset($fpid)) $depart_link = $fpid['depart_link'] . $param['id'] . "-";
  149. else $depart_link = $param['id'] . "-";
  150. $level = explode('-', $depart_link);
  151. $level = array_filter($level);
  152. $level = count($level);
  153. $vir = [
  154. "id" => $param['id'],
  155. "name" => $param['name'],
  156. "pid" => $param['pid'],
  157. "weight" => $param['weight'],
  158. "depart_link" => $depart_link,
  159. "level" => $level,
  160. "updatetime" => date("Y-m-d H:i:s"),
  161. ];
  162. $org = Db::name($this->table_name)->save($vir);
  163. return $org ? json_show(0, "更新成功") : json_show(1004, "更新失败");
  164. }
  165. public function info()
  166. {
  167. $id = $this->request->post('id/d', 0, 'trim');
  168. $res = Db::name($this->table_name)
  169. ->where(['id' => $id, 'is_del' => 0])
  170. ->findOrEmpty();
  171. return json_show(0, "获取成功", $res);
  172. }
  173. public function delete()
  174. {
  175. $id = $this->request->post('id/d', 0, 'trim');
  176. $custy = Db::name('customer_org1')
  177. ->field('id')
  178. ->where(["is_del" => 0, 'id' => $id])
  179. ->findOrEmpty();
  180. if (empty($custy)) return json_show(1004, "公司不存在");
  181. //$var= Db::name('customer_info')->where(['itemid'=>$id,'is_del'=>0])->find();
  182. $db = Db::name('customer_org1')
  183. ->field('id')
  184. ->where(['pid' => $custy['id'], 'is_del' => 0])
  185. ->findOrEmpty();
  186. if (!empty($db)) return json_show(1004, "下一级还有组织,不允许删除");
  187. $var = Db::name('customer_info')
  188. ->field('id')
  189. ->where(['itemid' => $id, 'is_del' => 0])
  190. ->findOrEmpty();
  191. if (!empty($var)) return json_show(1004, "下一级还有分公司,不允许删除");
  192. $custy['is_del'] = 1;
  193. $custy['updatetime'] = date("Y-m-d H:i:s");
  194. $compy = Db::name('customer_org1')->save($custy);
  195. return $compy ? json_show(0, "删除成功") : json_show(1004, "删除失败");
  196. }
  197. public function status()
  198. {
  199. $param = $this->request->only(['id', 'status'], 'post', 'trim');
  200. $val = Validate::rule([
  201. 'id' => 'require|number|gt:0',
  202. 'status' => 'require|number|in:0,1',
  203. ]);
  204. if ($val->check($param) == false) return json_show(1004, $val->getError());
  205. $update = Db::name("customer_org1")
  206. ->where(['is_del' => 0, 'id' => $param['id']])
  207. ->where('status', '<>', $param['status'])
  208. ->update([
  209. 'status' => $param['status'],
  210. 'updatetime' => date("Y-m-d H:i:s"),
  211. ]);
  212. return $update ? json_show(0, "操作成功") : json_show(1004, "操作失败");
  213. }
  214. //获取三级架构
  215. public function getTopCustomerOrg()
  216. {
  217. $itemid = $this->request->filter('trim')->post('itemid');
  218. $res = [];
  219. foreach ($itemid as $val){
  220. $res[$val] = get_top_customer_org($val);
  221. }
  222. return json_show(0, '获取架构成功', $res);
  223. }
  224. }