package controllers import ( "encoding/json" "eta/eta_api/models" "eta/eta_api/models/system" "eta/eta_api/services" "eta/eta_api/services/eta_forum" "eta/eta_api/utils" "strings" "time" ) // 部门组 type SysGroupController struct { BaseAuthController } // @Title 新增分组 // @Description 新增分组接口 // @Param request body system.SysGroupAddReq true "type json string" // @Success 200 新增成功 // @router /group/add [post] func (this *SysGroupController) Add() { br := new(models.BaseResponse).Init() defer func() { this.Data["json"] = br this.ServeJSON() }() var req system.SysGroupAddReq err := json.Unmarshal(this.Ctx.Input.RequestBody, &req) if err != nil { br.Msg = "参数解析异常!" br.ErrMsg = "参数解析失败,Err:" + err.Error() return } if req.DepartmentId <= 0 { br.Msg = "请选择部门" br.ErrMsg = "部门ID不可为空" return } //构建部门的分组树 groupList,err:=system.GetGroupByDepartmentId(req.DepartmentId) if err!=nil{ br.Msg = "获取数据失败,获取当前部门的分组列表失败" br.ErrMsg = "获取当前部门的分组列表失败,Err:" + err.Error() return } //获取传入部门下的分组名称,分组名称为唯一标识,不会重复 groupNameArr := strings.Split(req.GroupName, ",") root := new(services.GroupNode) services.BuildGroupTree(groupList,0,2,root) //现有的分组名称 existGroupMap:=make(map[string]bool,len(root.Child)) for _, v := range root.Child { existGroupMap[v.GroupName] = false } //传入的分组名称 reqGroupMap:=make(map[string]int,len(groupNameArr)) for _, v := range groupNameArr { reqGroupMap[v] = 1 } //新增分组名称 newGroupMap := make(map[string]int) groupIds := make([]int, 0) if len(reqGroupMap)>0{ for k, _ := range reqGroupMap { if _, ok := existGroupMap[k]; !ok { newGroupMap[k] = 1 groupIds = append(groupIds, 0) }else{ existGroupMap[k] = true } } } var deleteIds []int var addGroup bool //删除所有分组即可 if req.GroupName == "" { addGroup=false for _, node := range root.Child { //分组Id deleteIds = append(deleteIds, node.GroupId) //teamId for _, subNode := range node.Child { deleteIds=append(deleteIds, subNode.GroupId) } } }else{ addGroup=true for k, v := range existGroupMap { if !v{ for _, node := range root.Child { if node.GroupName == k{ //分组Id deleteIds = append(deleteIds, node.GroupId) //teamId for _, subNode := range node.Child { deleteIds=append(deleteIds, subNode.GroupId) } } } } } } if addGroup{ //新增分组 for k, _ := range newGroupMap { item := new(system.SysGroup) item.GroupName = k item.DepartmentId = req.DepartmentId item.CreateTime = time.Now() groupId, e := system.AddSysGroup(item) if e != nil { br.Msg = "新增失败" br.ErrMsg = "新增失败,Err:" + e.Error() return } // 同步分组缓存 if utils.BusinessCode == utils.BusinessCodeRelease { var syncData system.SyncGroupData syncData.Source = utils.SOURCE_ETA_FLAG syncData.GroupId = int(groupId) _ = utils.Rc.LPush(utils.CACHE_SYNC_GROUP, syncData) } groupIds = append(groupIds, int(groupId)) } go eta_forum.GroupSave(groupIds) //删除分组合他的子分组,写死只有2层分组,因此逻辑代码也就写死2层,一层分组,一层team } if len(deleteIds)>0 { err =services.DeleteSysGroupByIds(deleteIds) if err != nil { br.Msg = "删除失败" br.ErrMsg = "删除失败,Err:" + err.Error() return } // 同步分组缓存 if utils.BusinessCode == utils.BusinessCodeRelease { for _, groupId := range deleteIds { var syncData system.SyncGroupData syncData.Source = utils.SOURCE_ETA_FLAG syncData.GroupId = groupId _ = utils.Rc.LPush(utils.CACHE_SYNC_GROUP, syncData) go eta_forum.GroupDelete(groupId) } } } br.Ret = 200 br.Success = true br.Msg = "保存成功" } // @Title 修改分组 // @Description 修改分组接口 // @Param request body system.SysGroupEditReq true "type json string" // @Success 200 修改成功 // @router /group/edit [post] func (this *SysGroupController) Edit() { br := new(models.BaseResponse).Init() defer func() { this.Data["json"] = br this.ServeJSON() }() var req system.SysGroupEditReq err := json.Unmarshal(this.Ctx.Input.RequestBody, &req) if err != nil { br.Msg = "参数解析异常!" br.ErrMsg = "参数解析失败,Err:" + err.Error() return } if req.GroupId <= 0 { br.Msg = "参数错误" br.ErrMsg = "参数错误,GroupId 小于等于0 " return } if req.GroupName == "" { br.Msg = "分组名称不能为空" return } item, err := system.GetSysGroupByName(req.GroupName) if err != nil && !utils.IsErrNoRow(err) { br.Msg = "获取数据失败" br.ErrMsg = "获取数据失败,Err:" + err.Error() return } if item != nil { if item.GroupId != req.GroupId && item.GroupId > 0 { br.Msg = "名称已存在,请重新输入" return } } err = system.ModifySysGroup(req.GroupName, req.GroupId) if err != nil { br.Msg = "修改失败" br.ErrMsg = "修改失败,Err:" + err.Error() return } // 同步分组缓存 if utils.BusinessCode == utils.BusinessCodeRelease { var syncData system.SyncGroupData syncData.Source = utils.SOURCE_ETA_FLAG syncData.GroupId = req.GroupId _ = utils.Rc.LPush(utils.CACHE_SYNC_GROUP, syncData) } groupIds := make([]int, 0) groupIds = append(groupIds, req.GroupId) go eta_forum.GroupSave(groupIds) br.Ret = 200 br.Success = true br.Msg = "修改成功" } // @Title 删除分组 // @Description 删除分组接口 // @Param request body system.SysGroupDeleteReq true "type json string" // @Success 200 删除成功 // @router /group/delete [post] func (this *SysGroupController) Delete() { br := new(models.BaseResponse).Init() defer func() { this.Data["json"] = br this.ServeJSON() }() var req system.SysGroupDeleteReq err := json.Unmarshal(this.Ctx.Input.RequestBody, &req) if err != nil { br.Msg = "参数解析异常!" br.ErrMsg = "参数解析失败,Err:" + err.Error() return } if req.GroupId <= 0 { br.Msg = "参数错误" br.ErrMsg = "参数错误,GroupId 小于等于0 " return } err = system.DeleteSysGroup(req.GroupId) if err != nil { br.Msg = "删除失败" br.ErrMsg = "删除失败,Err:" + err.Error() return } err = system.ClearSysUserGroup(req.GroupId) if err != nil { br.Msg = "删除失败" br.ErrMsg = "删除失败,情况分组失败,Err:" + err.Error() return } // 同步分组缓存 if utils.BusinessCode == utils.BusinessCodeRelease { var syncData system.SyncGroupData syncData.Source = utils.SOURCE_ETA_FLAG syncData.GroupId = req.GroupId _ = utils.Rc.LPush(utils.CACHE_SYNC_GROUP, syncData) } go eta_forum.GroupDelete(req.GroupId) br.Ret = 200 br.Success = true br.Msg = "删除成功" } // SetSort // @Title 分组排序 // @Description 分组排序 // @Param request body system.SysGroupEditReq true "type json string" // @Success 200 修改成功 // @router /group/set_sort [post] func (this *SysGroupController) SetSort() { br := new(models.BaseResponse).Init() defer func() { if br.ErrMsg == "" { br.IsSendEmail = false } this.Data["json"] = br this.ServeJSON() }() var req system.SysGroupSortReq err := json.Unmarshal(this.Ctx.Input.RequestBody, &req) if err != nil { br.Msg = "参数解析异常!" br.ErrMsg = "参数解析失败,Err:" + err.Error() return } // 部门排序 if len(req.DepartmentIds) > 0 { updateArr := make([]*system.DepartmentSort, 0) for k, d := range req.DepartmentIds { updateArr = append(updateArr, &system.DepartmentSort{ DepartmentId: d, Sort: k + 1, }) } if e := system.MultiUpdateDepartmentSort(updateArr); e != nil { br.Msg = "操作失败" br.ErrMsg = "批量更新部门排序失败, Err: " + e.Error() return } } // 分组排序/小组排序 if len(req.GroupIds) > 0 || len(req.TeamIds) > 0 { updateArr := make([]*system.GroupSort, 0) if len(req.GroupIds) > 0 { for k, g := range req.GroupIds { updateArr = append(updateArr, &system.GroupSort{ GroupId: g, Sort: k + 1, }) } } if len(req.TeamIds) > 0 { for k, t := range req.TeamIds { updateArr = append(updateArr, &system.GroupSort{ GroupId: t, Sort: k + 1, }) } } if len(updateArr) > 0 { if e := system.MultiUpdateGroupSort(updateArr); e != nil { br.Msg = "操作失败" br.ErrMsg = "批量更新分组排序失败, Err: " + e.Error() return } // 同步分组缓存 if utils.BusinessCode == utils.BusinessCodeRelease { for _, g := range updateArr { var syncData system.SyncGroupData syncData.Source = utils.SOURCE_ETA_FLAG syncData.GroupId = g.GroupId _ = utils.Rc.LPush(utils.CACHE_SYNC_GROUP, syncData) } } } } br.Ret = 200 br.Success = true br.Msg = "操作成功" }