UserCompany.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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(['nickname' => '', 'username' => '', 'status' => '', 'page' => 1, 'size' => 10], 'post');
  16. $condition = [['a.is_del', '=', 0]];
  17. if ($post['nickname'] != '') $condition[] = ['b.nickname', 'like', "%{$post['nickname']}%"];
  18. if ($post['username'] != '') $condition[] = ['c.username', 'like', "%{$post['username']}%"];
  19. if ($post['status'] != '') $condition[] = ['a.status', '=', $post['status']];
  20. $count = Db::name('account_company')
  21. ->alias('a')
  22. ->leftJoin('user b', 'a.account_id=b.account_id')
  23. ->leftJoin('account c', 'c.id=a.account_id and c.is_del=0')
  24. ->where($condition)
  25. ->count('a.id');
  26. $list = Db::name('account_company')
  27. ->alias('a')
  28. ->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')
  29. ->leftJoin('user b', 'a.account_id=b.account_id')
  30. ->leftJoin('account c', 'c.id=a.account_id and c.is_del=0')
  31. ->where($condition)
  32. ->page($post['page'], $post['size'])
  33. ->order('a.addtime desc')
  34. ->select()
  35. ->toArray();
  36. return json_show(0, '获取成功', ['list' => $list, 'count' => $count]);
  37. }
  38. //编辑
  39. public function update()
  40. {
  41. $post = $this->request->only(['id', 'nickname', 'mobile', 'email' => '', 'portrait' => '', 'sex' => '',], 'post');
  42. $validate = Validate::rule([
  43. 'id|主键ID' => 'require|number|gt:0',
  44. 'nickname|名称' => 'require|max:255',
  45. 'mobile|手机号' => 'require|number|length:11|mobile',
  46. 'email|名称' => 'email',
  47. 'sex|性别' => 'number|in:0,1,2',
  48. ]);
  49. if ($validate->check($post) == false) return json_show(1004, $validate->getError());
  50. $account = Db::name('account')
  51. ->field('id')
  52. ->where([['id', '=', $post['id']], ['is_del', '=', 0]])
  53. ->findOrEmpty();
  54. if (empty($account)) return json_show(1003, '账户不存在');
  55. $accountinfo = Db::name('user')
  56. ->field('id')
  57. ->where([['account_id', '=', $post['id']]])
  58. ->findOrEmpty();
  59. if (empty($accountinfo)) return json_show(1003, '账户信息不存在');
  60. Db::startTrans();
  61. try {
  62. Db::name('user')
  63. ->where($accountinfo)
  64. ->update([
  65. 'nickname' => $post['nickname'],
  66. 'mobile' => $post['mobile'],
  67. 'email' => $post['email'],
  68. 'portrait' => $post['portrait'],
  69. 'sex' => $post['sex'],
  70. 'updatetime' => date('Y-m-d H:i:s')
  71. ]);
  72. Db::name('account')
  73. ->where(['account_id', $post['id']])
  74. ->update([
  75. 'id' => $post['id'],
  76. 'mobile' => $post['mobile'],
  77. 'username' => $post['mobile'],
  78. 'updatetime' => date('Y-m-d H:i:s'),
  79. ]);
  80. Db::commit();
  81. return json_show(0, '信息修改成功');
  82. } catch (Exception $exception) {
  83. Db::rollback();
  84. return json_show(1005, '信息修改失败,' . $exception->getMessage());
  85. }
  86. }
  87. //启禁用
  88. public function status()
  89. {
  90. $post = $this->request->only(['id', 'status'], 'post', 'trim');
  91. $validate = Validate::rule(['id|主键ID' => 'require|number|gt:0', 'status|状态' => 'require|number|in:0,1']);
  92. if ($validate->check($post) == false) return json_show(1004, $validate->getError());
  93. Db::name('account_company')
  94. ->where('status', '<>', $post['status'])
  95. ->where(['id' => $post['id'], 'is_del' => 0])
  96. ->save(['status' => $post['status'], 'updatetime' => date('Y-m-d H:i:s')]);
  97. return json_show(0, '操作成功');
  98. }
  99. //详情
  100. public function info()
  101. {
  102. $id = $this->request->post('id/d', 0, 'trim');
  103. if ($id == 0) return json_show(1003, "参数 id 不能为空");
  104. $rs = Db::name('account_company')
  105. ->alias('a')
  106. ->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')
  107. ->leftJoin('user b', 'a.account_id=b.account_id')
  108. ->leftJoin('account c', 'c.id=a.account_id and c.is_del=0')
  109. ->where(['a.id' => $id, 'a.is_del' => 0])
  110. ->findOrEmpty();
  111. if (empty($rs)) return json_show(1004, '未找到用户信息');
  112. return json_show(0, '获取成功', $rs);
  113. }
  114. }