123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289 |
- <?php
- namespace app\admin\controller;
- //用户组
- use think\Exception;
- use think\facade\Db;
- use think\facade\Validate;
- use app\admin\model\DataGroup as DataGroupModel;
- class DataGroup extends Base
- {
- //用户组列表
- public function getDataGroupList()
- {
- $param = $this->request->filter('trim')->only(['page' => 1, 'size' => 10, 'status' => '', 'title' => '', 'nickname' => '', 'type' => '', 'token'], 'post');
- $db = new DataGroupModel();
- $val = Validate::rule([
- 'page|页码' => 'require|number|gt:0',
- 'size|每页数量' => 'require|number|gt:0|elt:100',
- 'status|状态' => 'number|in:' . $db::$status_启用 . ',' . $db::$status_禁用,
- 'title|用户组名称' => 'max:100',
- 'nickname|成员名称' => 'max:100',
- 'type|共享类型' => 'number|in:' . $db::$type_可编辑 . ',' . $db::$type_只读 . ',' . $db::$type_不共享,
- ]);
- if (!$val->check($param)) return error_show(1005, $val->getError());
- $rs = $db
- ->alias('a')
- ->leftJoin('data_group_userid b', 'b.data_group_id=a.id AND b.is_del=' . $db::$delete_正常)
- ->group('a.id')
- ->where('a.is_del', $db::$delete_正常);
- if ($param['status'] != '') $rs->where('a.status', $param['status']);
- if ($param['title'] != '') $rs->whereLike('a.title', '%' . $param['title'] . '%');
- if ($param['nickname'] != '') $rs->whereLike('u.nickname', '%' . $param['nickname'] . '%');
- if ($param['type'] != '') $rs->where('a.type', $param['type']);
- $count = $rs->count('a.id');
- $list = $rs
- ->field('a.*,GROUP_CONCAT(b.uid) uid,"" nickname')
- ->page($param['page'], $param['size'])
- ->order('a.id', 'desc')
- ->withAttr('nickname', function ($val, $da) use ($param) {
- //从外部获取符合条件的用户信息,并补充到列表中
- $temp = GetUserlist($param['token'], ['id' => explode(',', $da['uid'])]);
- if ($temp['code'] == 0) return implode(',', array_column($temp['data'], 'nickname'));
- else return '';
- })
- ->select()
- ->toArray();
- return app_show(0, '请求成功', ['count' => $count, 'list' => $list]);
- }
- //添加
- public function addDataGroup()
- {
- $param = $this->request->filter('trim')->only(['title', 'uids', 'type', 'remark' => ''], 'post');
- $val = Validate::rule([
- 'title|用户组名称' => 'require|max:100',
- 'uids|组成员' => 'require|array|max:100',
- 'type|共享类型' => 'require|number|in:' . DataGroupModel::$type_可编辑 . ',' . DataGroupModel::$type_只读 . ',' . DataGroupModel::$type_不共享,
- 'remark|备注' => 'max:100',
- ]);
- if (!$val->check($param)) return error_show(1005, $val->getError());
- Db::startTrans();
- try {
- //添加用户组
- $group_id = Db::name('data_group')->insertGetId([
- 'title' => $param['title'],
- 'type' => $param['type'],
- 'remark' => $param['remark'],
- 'is_del' => DataGroupModel::$delete_正常,
- 'status' => DataGroupModel::$status_启用,
- 'createrid' => $this->uid,
- 'creater' => $this->uname,
- ]);
- //用户组-用户集合
- $insert_data = [];
- foreach ($param['uids'] as $uids) {
- $insert_data[] = [
- 'data_group_id' => $group_id,
- 'uid' => $uids['uid'],
- 'is_del' => DataGroupModel::$delete_正常,
- 'status' => DataGroupModel::$status_启用,
- ];
- }
- if ($insert_data) Db::name('data_group_userid')->insertAll($insert_data);
- Db::commit();
- return app_show(0, '添加用户组成功');
- } catch (Exception $exception) {
- Db::rollback();
- return error_show(1005, '添加用户组失败');
- }
- }
- //获取详情
- public function getDataGroupDetail()
- {
- $param = $this->request->filter('trim')->only(['id', 'token'], 'post');
- $val = Validate::rule([
- 'id|ID' => 'require|number|gt:0',
- 'token' => 'require',
- ]);
- if (!$val->check($param)) return error_show(1005, $val->getError());
- $info = DataGroupModel::field(true)
- ->where(['id' => $param['id'], 'is_del' => DataGroupModel::$delete_正常])
- ->append(['user_list'])
- ->withAttr('user_list', function ($val, $data) {
- return Db::name('data_group_userid')
- ->alias('a')
- ->field('a.id,a.uid,a.is_del,"" nickname')
- ->where([
- 'a.is_del' => DataGroupModel::$delete_正常,
- 'a.status' => DataGroupModel::$status_启用,
- 'a.data_group_id' => $data['id']
- ])->select()
- ->toArray();
- })
- ->findOrEmpty()
- ->toArray();
- //从外部获取符合条件的用户信息,并补充到列表中
- $uids = array_column($info['user_list'], 'uid');
- if ($uids) {
- $temp = GetUserlist($param['token'], ['id' => $uids]);
- if ($temp['code'] == 0) {
- $nickname_uid = array_column($temp['data'], 'nickname', 'id');
- foreach ($info['user_list'] as &$value) {
- $value['nickname'] = $nickname_uid[$value['uid']] ?? '';
- }
- }
- }
- return app_show(0, '获取详情成功', $info);
- }
- //修改
- public function editDataGroup()
- {
- $param = $this->request->filter('trim')->only(['id', 'title', 'uids', 'type', 'remark'], 'post');
- $val = Validate::rule([
- 'id' => 'require|number|gt:0',
- 'title|用户组名称' => 'require|max:100',
- 'uids|组成员' => 'require|array|max:100',
- 'type|共享类型' => 'require|number|in:' . DataGroupModel::$type_可编辑 . ',' . DataGroupModel::$type_只读 . ',' . DataGroupModel::$type_不共享,
- 'remark|备注' => 'max:100',
- ]);
- if (!$val->check($param)) return error_show(1005, $val->getError());
- Db::startTrans();
- try {
- $res = DataGroupModel::field('id')
- ->where(['id' => $param['id'], 'is_del' => DataGroupModel::$delete_正常])
- ->findOrEmpty()
- ->isEmpty();
- if ($res) throw new Exception('该记录不存在');
- DataGroupModel::where(['id' => $param['id'], 'is_del' => DataGroupModel::$delete_正常])->strict(false)->save($param);
- //用户组-用户集合
- $insert_data = $delete_data = [];
- foreach ($param['uids'] as $uid) {
- if (isset($uid['id']) && ((int)$uid['id']) != 0) {
- if ($uid['is_del'] == DataGroupModel::$delete_删除) $delete_data[] = $uid['id'];
- } else $insert_data[] = [
- 'data_group_id' => $param['id'],
- 'uid' => $uid['uid'],
- 'is_del' => DataGroupModel::$delete_正常,
- 'status' => DataGroupModel::$status_启用,
- ];
- }
- if ($delete_data) Db::name('data_group_userid')
- ->whereIn('id', $delete_data)
- ->where(['is_del' => DataGroupModel::$delete_正常, 'data_group_id' => $param['id']])
- ->update(['is_del' => DataGroupModel::$delete_删除]);
- if ($insert_data) Db::name('data_group_userid')->insertAll($insert_data);
- Db::commit();
- return app_show(0, '修改用户组成功');
- } catch (Exception $exception) {
- Db::rollback();
- return error_show(1005, '修改用户组失败');
- }
- }
- //删除
- public function deleteDataGroup()
- {
- $param = $this->request->filter('trim')->only(['id'], 'post');
- $val = Validate::rule(['id' => 'require|array|max:100']);
- if (!$val->check($param)) return error_show(1005, $val->getError());
- Db::startTrans();
- try {
- Db::name('data_group')
- ->whereIn('id', $param['id'])
- ->where(['is_del' => DataGroupModel::$delete_正常])
- ->update(['is_del' => DataGroupModel::$delete_删除]);
- Db::name('data_group_userid')
- ->whereIn('data_group_id', $param['id'])
- ->where('is_del', DataGroupModel::$delete_正常)
- ->update(['is_del' => DataGroupModel::$delete_删除]);
- Db::commit();
- return app_show(0, '删除用户组成功');
- } catch (Exception $exception) {
- Db::rollback();
- return error_show(1005, '删除用户组失败');
- }
- }
- //启/禁用用户组
- public function statusDataGroup()
- {
- $param = $this->request->filter('trim')->only(['id', 'status'], 'post');
- $val = Validate::rule([
- 'id' => 'require|array|max:100',
- 'status|状态' => 'require|number|in:' . DataGroupModel::$status_启用 . ',' . DataGroupModel::$status_禁用,
- ]);
- if (!$val->check($param)) return error_show(1005, $val->getError());
- $status = [0 => '禁用', 1 => '启用'];
- Db::startTrans();
- try {
- Db::name('data_group')
- ->where([
- ['id', 'in', $param['id']],
- ['is_del', '=', DataGroupModel::$delete_正常],
- ['status', '<>', $param['status']],
- ])->update(['status' => $param['status']]);
- Db::name('data_group_userid')
- ->where([
- ['data_group_id', 'in', $param['id']],
- ['is_del', '=', DataGroupModel::$delete_正常],
- ['status', '<>', $param['status']],
- ])->update(['status' => $param['status']]);
- Db::commit();
- return app_show(0, $status[$param['status']] . '用户组成功');
- } catch (Exception $exception) {
- Db::rollback();
- return error_show(1005, $status[$param['status']] . '用户组成功');
- }
- }
- }
|