UserCompany.php 6.7 KB

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