UserCompany.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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 UserCompany extends BaseController
  11. {
  12. //列表
  13. public function getList()
  14. {
  15. $post = $this->request->only(['account_id' => '', 'status' => '', 'page' => 1, 'size' => 10, 'companyCode' => '', 'companyName' => ''], 'post');
  16. $condition = [['a.is_del', '=', 0]];
  17. if ($post['account_id'] !== '') $condition[] = ['a.account_id', '=', $post['account_id']];
  18. if ($post['status'] !== '') $condition[] = ['a.status', '=', $post['status']];
  19. if ($post['companyCode'] !== '') $condition[] = ['a.companyCode', 'like', '%' . $post['companyCode'] . '%'];
  20. if ($post['companyName'] !== '') $condition[] = ['a.companyName', 'like', '%' . $post['companyName'] . '%'];
  21. $count = Db::name('account_company')
  22. ->alias('a')
  23. ->leftJoin('user b', 'a.account_id=b.account_id')
  24. ->leftJoin('account c', 'c.id=a.account_id and c.is_del=0')
  25. ->where($condition)
  26. ->count('a.id');
  27. $list = Db::name('account_company')
  28. ->alias('a')
  29. ->field('a.id,a.account_id,a.companyCode,a.companyName,a.company_type,a.is_main,a.status com_status,c.username,c.mobile,c.source,c.status account_status,b.nickname,b.sex,b.email,c.addtime,c.updatetime,c.level')
  30. ->leftJoin('user b', 'a.account_id=b.account_id')
  31. ->leftJoin('account c', 'c.id=a.account_id and c.is_del=0')
  32. ->where($condition)
  33. ->page($post['page'], $post['size'])
  34. ->order('a.addtime desc')
  35. ->select()
  36. ->toArray();
  37. return json_show(0, '获取成功', ['list' => $list, 'count' => $count]);
  38. }
  39. //编辑
  40. public function update()
  41. {
  42. $post = $this->request->only(['id', 'nickname', 'mobile', 'email' => '', 'portrait' => '', 'sex' => '',], 'post');
  43. $validate = Validate::rule([
  44. 'id|主键ID' => 'require|number|gt:0',
  45. 'nickname|名称' => 'require|max:255',
  46. 'mobile|手机号' => 'require|number|length:11|mobile',
  47. 'email|名称' => 'email',
  48. 'sex|性别' => 'number|in:0,1,2',
  49. ]);
  50. if ($validate->check($post) == false) return json_show(1004, $validate->getError());
  51. $account = Db::name('account')
  52. ->field('id')
  53. ->where([['id', '=', $post['id']], ['is_del', '=', 0]])
  54. ->findOrEmpty();
  55. if (empty($account)) return json_show(1003, '账户不存在');
  56. $accountinfo = Db::name('user')
  57. ->field('id')
  58. ->where([['account_id', '=', $post['id']]])
  59. ->findOrEmpty();
  60. if (empty($accountinfo)) return json_show(1003, '账户信息不存在');
  61. Db::startTrans();
  62. try {
  63. Db::name('user')
  64. ->where($accountinfo)
  65. ->update([
  66. 'nickname' => $post['nickname'],
  67. 'mobile' => $post['mobile'],
  68. 'email' => $post['email'],
  69. 'portrait' => $post['portrait'],
  70. 'sex' => $post['sex'],
  71. 'updatetime' => date('Y-m-d H:i:s')
  72. ]);
  73. Db::name('account')
  74. ->where(['account_id', $post['id']])
  75. ->update([
  76. 'id' => $post['id'],
  77. 'mobile' => $post['mobile'],
  78. 'username' => $post['mobile'],
  79. 'updatetime' => date('Y-m-d H:i:s'),
  80. ]);
  81. Db::commit();
  82. return json_show(0, '信息修改成功');
  83. } catch (Exception $exception) {
  84. Db::rollback();
  85. return json_show(1005, '信息修改失败,' . $exception->getMessage());
  86. }
  87. }
  88. //启禁用
  89. public function status()
  90. {
  91. $post = $this->request->only(['id', 'status'], 'post', 'trim');
  92. $validate = Validate::rule(['id|主键ID' => 'require|number|gt:0', 'status|状态' => 'require|number|in:0,1']);
  93. if ($validate->check($post) == false) return json_show(1004, $validate->getError());
  94. $tmp = Db::name('account_company')
  95. ->field('id,account_id,companyCode companyNo')
  96. ->where(['id' => $post['id'], 'is_del' => 0])
  97. ->findOrEmpty();
  98. if (empty($tmp)) return json_show(1004, '该记录不存在');
  99. if ($tmp['status'] == $post['status']) return json_show(1004, '重复操作');
  100. $rs = Db::name('account_company')
  101. ->where('status', '<>', $post['status'])
  102. ->where(['id' => $post['id'], 'is_del' => 0])
  103. ->update(['status' => $post['status'], 'updatetime' => date('Y-m-d H:i:s')]);
  104. return $rs ? json_show(0, '操作成功', $tmp) : json_show(1004, '操作失败');
  105. }
  106. //详情
  107. public function info()
  108. {
  109. $id = $this->request->post('id/d', 0, 'trim');
  110. if ($id == 0) return json_show(1003, "参数 id 不能为空");
  111. $rs = Db::name('account_company')
  112. ->alias('a')
  113. ->field('a.id,a.account_id,a.companyCode,a.companyName,a.company_type,a.is_main,a.status com_status,c.username,c.mobile,c.source,c.status account_status,b.nickname,b.sex,b.email,c.addtime,c.updatetime,c.level')
  114. ->leftJoin('user b', 'a.account_id=b.account_id')
  115. ->leftJoin('account c', 'c.id=a.account_id and c.is_del=0')
  116. ->where(['a.id' => $id, 'a.is_del' => 0])
  117. ->findOrEmpty();
  118. if (empty($rs)) return json_show(1004, '未找到用户信息');
  119. return json_show(0, '获取成功', $rs);
  120. }
  121. //切换默认公司
  122. public function changeMain()
  123. {
  124. $param = $this->request->only(['account_id','companyCode'], 'post', 'trim');
  125. $val = Validate::rule([
  126. 'account_id|账号id' => 'require|number|gt:0',
  127. 'companyCode|公司编码' => 'require',
  128. ]);
  129. if ($val->check($param) == false) return json_show(1004, $val->getError());
  130. Db::startTrans();
  131. try {
  132. $rs = Db::name('account_company')
  133. ->field('id')
  134. ->where(['account_id' => $param['account_id'], 'companyCode' => $param['companyCode'], 'is_del' => 0])
  135. ->findOrEmpty();
  136. if (empty($rs)) throw new Exception('该账号尚未绑定该公司');
  137. Db::name('account_company')
  138. ->where(['account_id' => $param['account_id'], 'is_main' => 1, 'is_del' => 0])
  139. ->update(['is_main' => 0, 'updatetime' => date('Y-m-d H:i:s')]);
  140. Db::name('account_company')
  141. ->where('id', $rs['id'])
  142. ->update(['is_main' => 1]);
  143. Db::commit();
  144. return json_show(0, '切换成功');
  145. } catch (Exception $exception) {
  146. Db::rollback();
  147. return json_show(1005, '切换失败,' . $exception->getMessage());
  148. }
  149. }
  150. }