UserCompanyBasic.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  1. <?php
  2. namespace app\controller;
  3. use app\BaseController;
  4. use app\model\Account;
  5. use app\model\AccountCompany;
  6. use think\Exception;
  7. use think\facade\Db;
  8. use think\facade\Validate;
  9. //【公司账号管理】
  10. class UserCompanyBasic extends BaseController
  11. {
  12. //列表
  13. public function getList()
  14. {
  15. $post = $this->request->only(['nickname' => '', 'username' => '', 'status' => '', 'page' => 1, 'size' => 10, 'islevel' => 1,'level'=>'2,3','companyNo'=>''], 'post');
  16. $condition = [['a.is_del', '=', 0]];
  17. if ($post['nickname'] != '') $condition[] = ['nickname', 'like', "%{$post['nickname']}%"];
  18. if ($post['username'] != '') $condition[] = ['username', 'like', "%{$post['username']}%"];
  19. if ($post['status'] != '') $condition[] = ['a.status', '=', $post['status']];
  20. if ($post['level'] != '') $condition[] = ['a.level', 'in', $post['level']];
  21. // if ($post['islevel'] == 1) $condition[] = ['a.level', '=', 1];
  22. // else $condition[] = ['a.level', '<>', 1];
  23. if ($post['companyNo'] !== '') {
  24. //根据公司筛选账号
  25. $rs = Db::name('account_company')
  26. ->where(['is_del' => 0, 'status' => 1])
  27. ->whereLike('companyCode', '%' . $post['companyNo'] . '%')
  28. ->column('account_id');
  29. $condition[] = ['a.id', 'in', $rs];
  30. }
  31. $count = Db::name('account')
  32. ->alias('a')
  33. ->leftJoin('user b', 'a.id=b.account_id and b.status=1')
  34. ->where($condition)
  35. ->count('a.id');
  36. $list = Db::name('account')
  37. ->alias('a')
  38. ->field('a.id,a.username,a.mobile,a.source,a.status,b.nickname,b.sex,b.email,a.addtime,a.updatetime,a.level')
  39. ->leftJoin('user b', 'a.id=b.account_id and b.status=1')
  40. ->where($condition)
  41. ->page($post['page'], $post['size'])
  42. ->append(['plat', 'company_relaton'])
  43. // ->withAttr('plat', function ($val, $da) {
  44. // return Db::name('account_plat')
  45. // ->alias('a')
  46. // ->leftJoin('platform b', 'a.plat_code=b.plat_code and b.is_del=0 and b.status=1')
  47. // ->where(['a.status' => 1, 'a.is_del' => 0, 'a.account_id' => $da['id']])
  48. // ->field('a.plat_code,plat_name')
  49. // ->select()
  50. // ->toArray();
  51. // })
  52. ->withAttr('company_relaton', function ($val, $da) {
  53. return Db::name('account_company')
  54. ->where(['account_id' => $da['id'], 'is_del' => 0])
  55. ->field('companyCode,companyName,company_type,is_main,status')
  56. ->select()
  57. ->toArray();
  58. })
  59. ->order('a.addtime desc')
  60. ->select()
  61. ->toArray();
  62. return json_show(0, '获取成功', ['list' => $list, 'count' => $count]);
  63. }
  64. //注册
  65. public function add()
  66. {
  67. $post = $this->request->only(['nickname' => '', 'mobile' => '', 'email' => '', 'level' => 2, 'companyArr' => []], 'post', 'trim');
  68. $validate = Validate::rule([
  69. 'nickname|真实姓名' => 'require|min:2|max:200',
  70. 'mobile|手机号' => 'require|number|length:11|mobile',
  71. 'email|邮箱' => 'email',
  72. 'level|账号等级' => 'number|in:1,2',
  73. 'companyArr|关联业务公司' => 'require|array|max:100',
  74. ]);
  75. if ($validate->check($post) == false) return json_show(1004, $validate->getError());
  76. Db::startTrans();
  77. try {
  78. $uiq = Db::table('sys_account')
  79. ->field('id')
  80. ->where(['mobile' => $post['mobile']])
  81. ->findOrEmpty();
  82. if ($uiq) throw new Exception('手机号已注册!');
  83. $date = date('Y-m-d H:i:s');
  84. $salt = makeSalt();
  85. $password = sha1('dingding123' . $salt);
  86. $data = [
  87. 'username' => $post['mobile'],
  88. 'password' => $password,
  89. 'salt' => $salt,
  90. 'status' => 1,
  91. 'mobile' => $post['mobile'],
  92. 'level' => $post['level'],
  93. 'source' => 'paltadd',
  94. 'addtime' => $date,
  95. 'updatetime' => $date
  96. ];
  97. $reuslt = Db::table('sys_account')->insertGetId($data);
  98. if ($reuslt) {
  99. $data = [
  100. 'nickname' => $post['nickname'],
  101. 'mobile' => $post['mobile'],
  102. 'email' => $post['email'],
  103. 'portrait' => '',
  104. 'sex' => 1,
  105. 'post' => '',
  106. 'department' => '',
  107. 'account_id' => $reuslt,
  108. 'status' => 1,
  109. 'addtime' => $date,
  110. 'updatetime' => $date
  111. ];
  112. $user = Db::table('sys_user')->insert($data);
  113. if ($user != false) {
  114. if (!empty($post['companyArr'])) {
  115. $company_insert = [];
  116. $acount = new AccountCompany();
  117. foreach ($post['companyArr'] as $company) {
  118. $company_insert[] = [
  119. 'account_id' => $reuslt,
  120. 'companyCode' => $company['companyCode'],
  121. 'companyName' => $company['companyName'],
  122. 'company_type' => $company['company_type'],
  123. 'is_main' => $company['is_main'],
  124. 'status' => 1,
  125. 'is_del' => 0,
  126. 'addtime' => $date,
  127. 'updatetime' => $date,
  128. ];
  129. }
  130. $acount->saveAll($company_insert);
  131. }
  132. Db::commit();
  133. return json_show(0, '账户注册成功', ['uid' => $reuslt]);
  134. }
  135. } else throw new Exception();
  136. } catch (Exception $e) {
  137. Db::rollback();
  138. return json_show(1002, '账户注册失败,' . $e->getMessage());
  139. }
  140. }
  141. //删除
  142. public function delete()
  143. {
  144. $param = $this->request->only(['ids'], 'post', 'trim');
  145. $val = Validate::rule([
  146. 'ids' => 'require|array|max:100',
  147. ]);
  148. if (!$val->check($param)) return json_show(1004, $val->getError());
  149. Db::startTrans();
  150. try {
  151. Db::name('account')
  152. ->where(['is_del' => 0])
  153. ->whereIn('id', $param['ids'])
  154. ->update([
  155. 'is_del' => 1,
  156. 'updatetime' => date('Y-m-d H:i:s')
  157. ]);
  158. Db::name('account_company')
  159. ->where(['is_del' => 0])
  160. ->whereIn('account_id', $param['ids'])
  161. ->update([
  162. 'is_del' => 1,
  163. 'updatetime' => date('Y-m-d H:i:s')
  164. ]);
  165. Db::commit();
  166. return json_show(0, '删除成功');
  167. } catch (Exception $exception) {
  168. Db::rollback();
  169. return json_show(1005, '删除失败,' . $exception->getMessage());
  170. }
  171. }
  172. //编辑
  173. public function update()
  174. {
  175. $post = $this->request->only(['id', 'itemid'], 'post');
  176. $validate = Validate::rule([
  177. 'id|主键ID' => 'require|number|gt:0',
  178. 'itemid|部门ID' => 'require|number|gt:0',
  179. ]);
  180. if ($validate->check($post) == false) return json_show(1004, $validate->getError());
  181. $account = Db::name('account')
  182. ->field('id')
  183. ->where([['id', '=', $post['id']], ['is_del', '=', 0]])
  184. ->findOrEmpty();
  185. if (empty($account)) return json_show(1003, '账户不存在');
  186. $tmp = Db::name('account_item')
  187. ->field('id')
  188. ->where(['account_id' => $post['id']])
  189. ->findOrEmpty();
  190. $date = date('Y-m-d H:i:s');
  191. if (empty($tmp)) $rs = Db::name('account_item')
  192. ->insert([
  193. 'account_id' => $post['id'],
  194. 'itemid' => $post['itemid'],
  195. 'addtime' => $date,
  196. 'updatetime' => $date,
  197. ]);
  198. else $rs = Db::name('account_item')
  199. ->where(['id' => $tmp['id']])
  200. ->update([
  201. 'itemid' => $post['itemid'],
  202. 'updatetime' => $date
  203. ]);
  204. return $rs ? json_show(0, '信息修改成功') : json_show(0, '信息修改失败');;
  205. // $accountinfo = Db::name('user')
  206. // ->field('id')
  207. // ->where([['account_id', '=', $post['id']]])
  208. // ->findOrEmpty();
  209. // if (empty($accountinfo)) return json_show(1003, '账户信息不存在');
  210. // Db::startTrans();
  211. // try {
  212. // Db::name('user')
  213. // ->where($accountinfo)
  214. // ->update([
  215. // 'nickname' => $post['nickname'],
  216. // 'mobile' => $post['mobile'],
  217. // 'email' => $post['email'],
  218. // 'portrait' => $post['portrait'],
  219. // 'sex' => $post['sex'],
  220. // 'updatetime' => date('Y-m-d H:i:s')
  221. // ]);
  222. //
  223. // Db::name('account')
  224. // ->where('id', $post['id'])
  225. // ->update([
  226. // 'id' => $post['id'],
  227. // 'mobile' => $post['mobile'],
  228. // 'username' => $post['mobile'],
  229. // 'updatetime' => date('Y-m-d H:i:s'),
  230. // ]);
  231. // Db::commit();
  232. // return json_show(0, '信息修改成功');
  233. // } catch (Exception $exception) {
  234. // Db::rollback();
  235. // return json_show(1005, '信息修改失败,' . $exception->getMessage());
  236. // }
  237. }
  238. //启禁用
  239. public function status()
  240. {
  241. $post = $this->request->only(['id', 'status'], 'post', 'trim');
  242. $validate = Validate::rule(['id|主键ID' => 'require|number|gt:0', 'status|状态' => 'require|number|in:0,1']);
  243. if ($validate->check($post) == false) return json_show(1004, $validate->getError());
  244. Db::startTrans();
  245. try {
  246. $account = Account::where(['id' => $post['id'], 'is_del' => 0])
  247. ->field('id,status')
  248. ->findOrEmpty();
  249. if (empty($account)) throw new Exception('账户不存在');
  250. if ($account['status'] == $post['status']) throw new Exception('重复操作');
  251. $message = $post['status'] == 1 ? '启用' : '禁用';
  252. Db::name('account')
  253. ->where('status', '<>', $post['status'])
  254. ->where(['id' => $post['id'], 'is_del' => 0])
  255. ->save(['status' => $post['status'], 'updatetime' => date('Y-m-d H:i:s')]);
  256. Db::name('account_company')
  257. ->where('status', '<>', $post['status'])
  258. ->where(['account_id' => $post['id'], 'is_del' => 0])
  259. ->save(['status' => $post['status'], 'updatetime' => date('Y-m-d H:i:s')]);
  260. Db::commit();
  261. return json_show(0, "账户{$message}成功");
  262. } catch (Exception $exception) {
  263. Db::rollback();
  264. return json_show(1004, '操作失败,' . $exception->getMessage());
  265. }
  266. }
  267. //详情
  268. public function info()
  269. {
  270. $id = $this->request->post('id/d', 0, 'trim');
  271. if ($id == 0) return json_show(1003, "参数 id 不能为空");
  272. $list = Db::name('account')
  273. ->alias('a')
  274. ->field('a.id,a.username,a.mobile,a.source,a.status,b.nickname,b.sex,b.email,a.addtime,a.updatetime,c.itemid,d.name item_name,d.companyNo item_companyNo')
  275. ->leftJoin('user b', 'a.id=b.account_id')
  276. ->leftJoin('account_item c', 'c.account_id=a.id')
  277. ->leftJoin('company_item d', 'd.id=c.itemid')
  278. ->append(['plat', 'company_relaton','item_list'])
  279. // ->withAttr('plat', function ($val, $da) {
  280. // return Db::name('account_plat')
  281. // ->alias('a')
  282. // ->leftJoin('platform b', 'a.plat_code=b.plat_code and b.status=1')
  283. // ->where(['a.status' => 1, 'a.is_del' => 0, 'a.account_id' => $da['id']])
  284. // ->column('a.plat_code,plat_name');
  285. // })
  286. ->withAttr('company_relaton', function ($val, $da) {
  287. return Db::name('account_company')
  288. ->where(['account_id' => $da['id'], 'is_del' => 0, 'status' => 1])
  289. ->column('companyCode,companyName,company_type,is_main,status');
  290. })
  291. ->withAttr('item_list',function($val,$da){
  292. return get_part($da['itemid'] ?? 0);
  293. })
  294. ->where(['a.id' => $id, 'a.is_del' => 0])
  295. ->findOrEmpty();
  296. if (empty($list)) return json_show(1004, '未找到用户信息');
  297. // $list['plat'] = Db::name('account_plat')->alias('a')
  298. // ->leftJoin('platform b', 'a.plat_code=b.plat_code and b.status=1')
  299. // ->where(['a.status' => 1, 'a.is_del' => 0, 'a.account_id' => $list['id']])->column('a.plat_code,plat_name');
  300. // $list['company_relaton'] = Db::name('account_company')->where(['account_id' => $list['id'], 'is_del' => 0, 'status' => 1])
  301. // ->column('companyCode,companyName,company_type,is_main,status');
  302. return json_show(0, '获取成功', $list);
  303. }
  304. }