|
@@ -4,10 +4,164 @@
|
|
|
namespace app\admin\controller;
|
|
|
|
|
|
|
|
|
+use app\admin\model\DataGroupUserid;
|
|
|
use think\App;
|
|
|
+use think\facade\Validate;
|
|
|
class UserGroup extends Base{
|
|
|
public function __construct(App $app) {
|
|
|
parent::__construct($app);
|
|
|
- $this->model=new \app\admin\model\UserGroup();
|
|
|
+ $this->model=new \app\admin\model\DataGroup();
|
|
|
+ }
|
|
|
+ //用户组列表
|
|
|
+ public function list(){
|
|
|
+ $param = $this->request->param(['page' => 1, 'size' => 10, 'status' => '', 'title' => '', 'uid' => '', 'type'
|
|
|
+ => '', 'supplierNo' => ''],"post","trim");
|
|
|
+ $where=[["is_del","=","0"]];
|
|
|
+ if($param['status']!='') $where[]=["status","=",$param['status']];
|
|
|
+ if($param['title']!='') $where[]=["title","like","%".$param['title']."%"];
|
|
|
+ if($param['uid']!='') $where[]=["id","in",function()use($param){
|
|
|
+ return DataGroupUserid::GroupIdByUid($param['uid']);
|
|
|
+ }];
|
|
|
+ if($param['type']!='') $where[]=["type","=",$param['type']];
|
|
|
+ $list= $this->model->with(["userGroup"=>["userInfo"]])->where($where)->order("id desc")->paginate
|
|
|
+ (["list_rows"=>$param["size"], "page"=>$param["page"]]);
|
|
|
+ foreach ($list->items() as &$item){
|
|
|
+ $item->nickname=array_column($item->userGroup->toArray(),"nickname");
|
|
|
+ }
|
|
|
+ return success("获取成功",["list"=>$list->items(),"count"=>$list->total()]);
|
|
|
+ }
|
|
|
+ /**
|
|
|
+ * @param title|用户组名称:require|max:255,
|
|
|
+ * @param type|共享类型:require|number|1,2,3,
|
|
|
+ * @param uids|组成员:require,
|
|
|
+ * @param remark|备注:max:255
|
|
|
+ * @return \think\response\Json
|
|
|
+ */
|
|
|
+ public function create(){
|
|
|
+ $param = $this->request->param(['title' => '', 'type' => '', 'uids'=>[],'remark'=>""],"post","trim");
|
|
|
+ $valid =Validate::rule([
|
|
|
+ "title|用户组名称"=>"require",
|
|
|
+ 'type|共享类型'=>"require|number|1,2,3",
|
|
|
+ 'uids|组成员'=>"require|array",
|
|
|
+ 'remark|备注'=>"max:255"
|
|
|
+ ]);
|
|
|
+ if(!$valid->check($param)) return error($valid->getError());
|
|
|
+ $this->model->startTrans();
|
|
|
+ try{
|
|
|
+ $data=[
|
|
|
+ "title"=>$param['title'],
|
|
|
+ "type"=>$param['type'],
|
|
|
+ "remark"=>$param['remark'],
|
|
|
+ "createrid"=>$this->uid,
|
|
|
+ "creater"=>$this->uname,
|
|
|
+ ];
|
|
|
+ $create= $this->model->create($data);
|
|
|
+ if($create){
|
|
|
+ $dataGroupUserid=new DataGroupUserid();
|
|
|
+ $dataGroupUserid->saveAll(array_map(function($item)use($create){
|
|
|
+ return ["data_group_id"=>$create->id,"uid"=>$item];
|
|
|
+ },$param['uids']));
|
|
|
+ }
|
|
|
+ $this->model->commit();
|
|
|
+ }catch (\Exception $e){
|
|
|
+ $this->model->rollback();
|
|
|
+ return error($e->getMessage());
|
|
|
+ }
|
|
|
+ return success("添加成功");
|
|
|
+ }
|
|
|
+ /**
|
|
|
+ * @param id|用户组id:require|number
|
|
|
+ * @return \think\Response|\think\response\Json|void
|
|
|
+ *
|
|
|
+ */
|
|
|
+ public function info(){
|
|
|
+ $id=$this->request->param("id","0","intval");
|
|
|
+ $info= $this->model->with(["userGroup"=>["userInfo"]])->findOrEmpty($id);
|
|
|
+ if($info->isEmpty()) return error("数据不存在");
|
|
|
+ return success("获取成功",$info);
|
|
|
+ }
|
|
|
+ /**
|
|
|
+ * @param id|用户组id:require|number
|
|
|
+ * title|用户组名称:require|max:255,
|
|
|
+ * type|共享类型:require|number|1,2,3,
|
|
|
+ * uids|组成员:require,
|
|
|
+ * remark|备注:max:255
|
|
|
+ * @return \think\Response|\think\response\Json|void
|
|
|
+ * @throws \think\exception\PDOException
|
|
|
+ */
|
|
|
+ public function save(){
|
|
|
+ $param = $this->request->param(['id' => '', 'title' => '', 'type' => '', 'uids'=>[],'remark'=>""],"post","trim");
|
|
|
+ $valid =Validate::rule([
|
|
|
+ "id|用户组id"=>"require|number|gt:0",
|
|
|
+ "title|用户组名称"=>"require",
|
|
|
+ 'type|共享类型'=>"require|number|1,2,3",
|
|
|
+ 'uids|组成员'=>"require|array",
|
|
|
+ 'remark|备注'=>"max:255"
|
|
|
+ ]);
|
|
|
+ if(!$valid->check($param)) return error($valid->getError());
|
|
|
+ $info= $this->model->with(["userGroup"])->findOrEmpty($param['id']);
|
|
|
+ if($info->isEmpty()) return error("数据不存在");
|
|
|
+ $this->model->startTrans();
|
|
|
+ try{
|
|
|
+ $info->title=$param['title'];
|
|
|
+ $info->type=$param['type'];
|
|
|
+ $info->remark=$param['remark'];
|
|
|
+ if($info->save()){
|
|
|
+ $info->userGroup->where("uid","not in",$param['uids'])->save(["is_del"=>1]);
|
|
|
+ $add =array_diff($param['uids'],array_column($info->userGroup->toArray(),"uid"));
|
|
|
+ $dataGroupUserid=new DataGroupUserid();
|
|
|
+ if(!empty($add)) $dataGroupUserid->saveAll(array_map(function($item)use($info){
|
|
|
+ return ["data_group_id"=>$info->id,"uid"=>$item];
|
|
|
+ },$add));
|
|
|
+ }
|
|
|
+ $this->model->commit();
|
|
|
+ }catch (\Exception $e){
|
|
|
+ $this->model->rollback();
|
|
|
+ return error($e->getMessage());
|
|
|
+ }
|
|
|
+ return success("保存成功");
|
|
|
+ }
|
|
|
+ /**
|
|
|
+ * @param id|用户组id:require|number
|
|
|
+ * @return \think\Response|\think\response\Json|void
|
|
|
+ * @throws \think\exception\PDOException
|
|
|
+ */
|
|
|
+ public function delete(){
|
|
|
+ $id=$this->request->param("id","0","intval");
|
|
|
+ $info= $this->model->with(["userGroup"])->findOrEmpty($id);
|
|
|
+ if($info->isEmpty()) return error("数据不存在");
|
|
|
+ $info->is_del=1;
|
|
|
+ if($info->save()){
|
|
|
+ $info->userGroup->save(["is_del"=>1]);
|
|
|
+ };
|
|
|
+ return success("删除成功");
|
|
|
+ }
|
|
|
+ /**
|
|
|
+ * @param id|用户组id:require|number
|
|
|
+ * @param status|状态:require|number|in:0,1
|
|
|
+ * @return \think\Response|\think\response\Json|void
|
|
|
+ * @throws \think\exception\PDOException
|
|
|
+ */
|
|
|
+ public function status(){
|
|
|
+ $param = $this->request->param(['id' => '', 'status' => ''],"post","intval");
|
|
|
+ $valid =Validate::rule([
|
|
|
+ "id|用户组id"=>"require|number|gt:0",
|
|
|
+ "status|状态"=>"require|number|in:0,1"
|
|
|
+ ]);
|
|
|
+ if(!$valid->check($param)) return error($valid->getError());
|
|
|
+ $info= $this->model->with(["userGroup"])->findOrEmpty($param['id']);
|
|
|
+ if($info->isEmpty()) return error("数据不存在");
|
|
|
+ $info->status=$param['status'];
|
|
|
+ if($info->save()){
|
|
|
+ $info->userGroup->save(["status"=>$param['status']]);
|
|
|
+ };
|
|
|
+ return success("修改成功");
|
|
|
+ }
|
|
|
+ /**
|
|
|
+ * @return \think\Response|\think\response\Json|void
|
|
|
+ */
|
|
|
+ public function getGroupAll(){
|
|
|
+ $list= $this->model->where("is_del",0)->order("id","desc")->select();
|
|
|
+ return success("获取成功",$list);
|
|
|
}
|
|
|
}
|