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); } }