123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190 |
- <?php
- namespace app\controller;
- use app\BaseController;
- use app\model\Account;
- use app\model\AccountCompany;
- use think\Exception;
- use think\facade\Db;
- use think\facade\Validate;
- //【公司账号查询】
- class UserCompany extends BaseController
- {
- //列表
- public function getList()
- {
- $post = $this->request->only(['account_id' => '', 'status' => '', 'page' => 1, 'size' => 10, 'companyCode' => '', 'companyName' => ''], 'post');
- $condition = [['a.is_del', '=', 0]];
- if ($post['account_id'] !== '') $condition[] = ['a.account_id', '=', $post['account_id']];
- if ($post['status'] !== '') $condition[] = ['a.status', '=', $post['status']];
- if ($post['companyCode'] !== '') $condition[] = ['a.companyCode', 'like', '%' . $post['companyCode'] . '%'];
- if ($post['companyName'] !== '') $condition[] = ['a.companyName', 'like', '%' . $post['companyName'] . '%'];
- $count = Db::name('account_company')
- ->alias('a')
- ->leftJoin('user b', 'a.account_id=b.account_id')
- ->leftJoin('account c', 'c.id=a.account_id and c.is_del=0')
- ->where($condition)
- ->count('a.id');
- $list = Db::name('account_company')
- ->alias('a')
- ->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')
- ->leftJoin('user b', 'a.account_id=b.account_id')
- ->leftJoin('account c', 'c.id=a.account_id and c.is_del=0')
- ->where($condition)
- ->page($post['page'], $post['size'])
- ->order('a.addtime desc')
- ->select()
- ->toArray();
- return json_show(0, '获取成功', ['list' => $list, 'count' => $count]);
- }
- //编辑
- public function update()
- {
- $post = $this->request->only(['id', 'nickname', 'mobile', 'email' => '', 'portrait' => '', 'sex' => '',], 'post');
- $validate = Validate::rule([
- 'id|主键ID' => 'require|number|gt:0',
- 'nickname|名称' => 'require|max:255',
- 'mobile|手机号' => 'require|number|length:11|mobile',
- 'email|名称' => 'email',
- 'sex|性别' => 'number|in:0,1,2',
- ]);
- if ($validate->check($post) == false) return json_show(1004, $validate->getError());
- $account = Db::name('account')
- ->field('id')
- ->where([['id', '=', $post['id']], ['is_del', '=', 0]])
- ->findOrEmpty();
- if (empty($account)) return json_show(1003, '账户不存在');
- $accountinfo = Db::name('user')
- ->field('id')
- ->where([['account_id', '=', $post['id']]])
- ->findOrEmpty();
- if (empty($accountinfo)) return json_show(1003, '账户信息不存在');
- Db::startTrans();
- try {
- Db::name('user')
- ->where($accountinfo)
- ->update([
- 'nickname' => $post['nickname'],
- 'mobile' => $post['mobile'],
- 'email' => $post['email'],
- 'portrait' => $post['portrait'],
- 'sex' => $post['sex'],
- 'updatetime' => date('Y-m-d H:i:s')
- ]);
- Db::name('account')
- ->where(['account_id', $post['id']])
- ->update([
- 'id' => $post['id'],
- 'mobile' => $post['mobile'],
- 'username' => $post['mobile'],
- 'updatetime' => date('Y-m-d H:i:s'),
- ]);
- Db::commit();
- return json_show(0, '信息修改成功');
- } catch (Exception $exception) {
- Db::rollback();
- return json_show(1005, '信息修改失败,' . $exception->getMessage());
- }
- }
- //启禁用
- public function status()
- {
- $post = $this->request->only(['id', 'status'], 'post', 'trim');
- $validate = Validate::rule(['id|主键ID' => 'require|number|gt:0', 'status|状态' => 'require|number|in:0,1']);
- if ($validate->check($post) == false) return json_show(1004, $validate->getError());
- $tmp = Db::name('account_company')
- ->field('id,account_id,companyCode companyNo')
- ->where(['id' => $post['id'], 'is_del' => 0])
- ->findOrEmpty();
- if (empty($tmp)) return json_show(1004, '该记录不存在');
- if ($tmp['status'] == $post['status']) return json_show(1004, '重复操作');
- $rs = Db::name('account_company')
- ->where('status', '<>', $post['status'])
- ->where(['id' => $post['id'], 'is_del' => 0])
- ->update(['status' => $post['status'], 'updatetime' => date('Y-m-d H:i:s')]);
- return $rs ? json_show(0, '操作成功', $tmp) : json_show(1004, '操作失败');
- }
- //详情
- public function info()
- {
- $id = $this->request->post('id/d', 0, 'trim');
- if ($id == 0) return json_show(1003, "参数 id 不能为空");
- $rs = Db::name('account_company')
- ->alias('a')
- ->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')
- ->leftJoin('user b', 'a.account_id=b.account_id')
- ->leftJoin('account c', 'c.id=a.account_id and c.is_del=0')
- ->where(['a.id' => $id, 'a.is_del' => 0])
- ->findOrEmpty();
- if (empty($rs)) return json_show(1004, '未找到用户信息');
- return json_show(0, '获取成功', $rs);
- }
- //切换默认公司
- public function changeMain()
- {
- $param = $this->request->only(['account_id','companyCode'], 'post', 'trim');
- $val = Validate::rule([
- 'account_id|账号id' => 'require|number|gt:0',
- 'companyCode|公司编码' => 'require',
- ]);
- if ($val->check($param) == false) return json_show(1004, $val->getError());
- Db::startTrans();
- try {
- $rs = Db::name('account_company')
- ->field('id')
- ->where(['account_id' => $param['account_id'], 'companyCode' => $param['companyCode'], 'is_del' => 0])
- ->findOrEmpty();
- if (empty($rs)) throw new Exception('该账号尚未绑定该公司');
- Db::name('account_company')
- ->where(['account_id' => $param['account_id'], 'is_main' => 1, 'is_del' => 0])
- ->update(['is_main' => 0, 'updatetime' => date('Y-m-d H:i:s')]);
- Db::name('account_company')
- ->where('id', $rs['id'])
- ->update(['is_main' => 1]);
- Db::commit();
- return json_show(0, '切换成功');
- } catch (Exception $exception) {
- Db::rollback();
- return json_show(1005, '切换失败,' . $exception->getMessage());
- }
- }
- }
|