123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320 |
- <?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' => '', 'supplierNo' => '']);
- $db = new DataGroupModel();
- $rs = $db
- ->alias('a')
- ->leftJoin('data_group_userid b', 'b.data_group_id=a.id AND b.is_del=' . DataGroupModel::$delete_正常)
- ->leftJoin('depart_user u', 'u.uid=b.uid AND u.is_del=0')
- ->group('a.id')
- ->where('a.is_del', DataGroupModel::$delete_正常);
- $val = Validate::rule([
- 'page|页码' => 'require|number|gt:0',
- 'size|每页数量' => 'require|number|gt:0|elt:100',
- 'status|状态' => 'number|in:' . DataGroupModel::$status_启用 . ',' . DataGroupModel::$status_禁用,
- 'title|用户组名称' => 'max:100',
- 'nickname|成员名称' => 'max:100',
- 'type|共享类型' => 'number|in:' . DataGroupModel::$type_可编辑 . ',' . DataGroupModel::$type_只读 . ',' . DataGroupModel::$type_不共享,
- ]);
- if (!$val->check($param)) return error_show(1005, $val->getError());
- if ($param['status'] != '') $rs->where('a.status', $param['status']);
- if ($param['title'] != '') $rs->whereLike('a.title', '%' . $param['title'] . '%');
- if ($param['nickname'] != '') {
- $a = $param['nickname'];
- $rs->where("a.id", "IN", function ($query) use ($a) {
- $query->name("data_group_userid")->alias("m")->leftJoin('depart_user f', 'f.uid=m.uid AND f.is_del=0')
- ->where('f.nickname', "like", "%$a%")->where("m.is_del", "=", 0)->field("m.data_group_id");
- });
- }
- if ($param['type'] != '') $rs->where('a.type', $param['type']);
- if ($param['supplierNo'] != '') $rs->where('a.supplierNo', '%' . $param['supplierNo'], '%');
- $count = $rs->count('a.id');
- $list = $rs
- ->field('a.*,GROUP_CONCAT(u.nickname) nickname')
- ->page($param['page'], $param['size'])
- ->order('a.id', 'desc')
- ->select()
- ->toArray();
- return app_show(0, '请求成功', ['count' => $count, 'list' => $list]);
- }
- //添加
- public function addDataGroup()
- {
- $param = $this->request->filter('trim')->only(['title', 'uids', 'type', 'remark' => '', 'supplierNo']);
- $val = Validate::rule([
- 'supplierNo|公司名称' => 'require|length:18',
- '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([
- 'supplierNo' => $param['supplierNo'],
- 'title' => $param['title'],
- 'type' => $param['type'],
- 'remark' => $param['remark'],
- 'is_del' => DataGroupModel::$delete_正常,
- 'status' => DataGroupModel::$status_启用,
- 'createrid' => $this->uid,
- 'creater' => $this->uname,
- 'addtime' => date('Y-m-d H:i:s'),
- 'updatetime' => date('Y-m-d H:i:s'),
- ]);
- //用户组-用户集合
- $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()
- {
- $id = $this->request->filter('trim')->post('id/d', 0);
- $info = DataGroupModel::field(true)
- ->where(['id' => $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,u.nickname,a.is_del')
- ->leftJoin('depart_user u', 'u.uid=a.uid AND u.is_del=0')
- ->where([
- 'a.is_del' => DataGroupModel::$delete_正常,
- // 'a.status' => DataGroupModel::$status_启用,
- 'a.data_group_id' => $data['id']
- ])
- ->select()
- ->toArray();
- })
- ->findOrEmpty()
- ->toArray();
- return app_show(0, '获取详情成功', $info);
- }
- //修改
- public function editDataGroup()
- {
- $param = $this->request->filter('trim')->only(['id', 'title', 'uids', 'type', 'remark']);
- $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,status')
- ->where(['id' => $param['id'], 'is_del' => DataGroupModel::$delete_正常])
- ->findOrEmpty();
- if ($res->isEmpty()) throw new Exception('该记录不存在');
- $param['updatetime'] = date('Y-m-d H:i:s');
- 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' => $res->status,
- 'addtime' => date('Y-m-d H:i:s'),
- 'updatetime' => date('Y-m-d H:i:s'),
- ];
- }
- 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_删除, 'updatetime' => date('Y-m-d H:i:s')]);
- 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, '修改用户组失败,' . $exception->getMessage());
- }
- }
- //删除
- public function deleteDataGroup()
- {
- $param = $this->request->filter('trim')->only(['id']);
- $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_删除, 'updatetime' => date('Y-m-d H:i:s')]);
- Db::name('data_group_userid')
- ->whereIn('data_group_id', $param['id'])
- ->where('is_del', DataGroupModel::$delete_正常)
- ->update(['is_del' => DataGroupModel::$delete_删除, 'updatetime' => date('Y-m-d H:i:s')]);
- 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']);
- $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'], 'updatetime' => date('Y-m-d H:i:s')]);
- Db::name('data_group_userid')
- ->where([
- ['data_group_id', 'in', $param['id']],
- ['is_del', '=', DataGroupModel::$delete_正常],
- ['status', '<>', $param['status']],
- ])->update(['status' => $param['status'], 'updatetime' => date('Y-m-d H:i:s')]);
- Db::commit();
- return app_show(0, $status[$param['status']] . '用户组成功');
- } catch (Exception $exception) {
- Db::rollback();
- return error_show(1005, $status[$param['status']] . '用户组失败');
- }
- }
- //用户组列表(全部)
- public function getDataGroupListAll()
- {
- // $param = $this->request->filter('trim')->only(['status' => '', 'title' => '', 'type' => '']);
- $db = new DataGroupModel();
- // $rs = $db
- // ->alias('a')
- // ->leftJoin('data_group_userid b', 'b.data_group_id=a.id AND b.is_del=' . DataGroupModel::$delete_正常)
- // ->leftJoin('depart_user u', 'u.uid=b.uid AND u.is_del=0')
- // ->group('a.id')
- // ->where('a.is_del', DataGroupModel::$delete_正常);
- // $val = Validate::rule([
- // 'status|状态' => 'number|in:' . DataGroupModel::$status_启用 . ',' . DataGroupModel::$status_禁用,
- // 'title|用户组名称' => 'max:100',
- //// 'nickname|成员名称' => 'max:100',
- // 'type|共享类型' => 'number|in:' . DataGroupModel::$type_可编辑 . ',' . DataGroupModel::$type_只读 . ',' . DataGroupModel::$type_不共享,
- // ]);
- // if (!$val->check($param)) return error_show(1005, $val->getError());
- // 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']);
- $list = $db
- ->where('is_del', DataGroupModel::$delete_正常)
- // ->field('a.*,GROUP_CONCAT(u.nickname) nickname')
- ->field('id,title,status')
- ->order('id', 'desc')
- ->select()
- ->toArray();
- return app_show(0, '请求成功', $list);
- }
- }
|