package controllers import ( "encoding/json" "eta/eta_api/models" "eta/eta_api/models/system" "eta/eta_api/services" "eta/eta_api/utils" "strings" "time" ) // 部门组 type SysTeamController struct { BaseAuthController } // @Title 新增分组 // @Description 新增分组接口 // @Param request body system.SysTeamAddReq true "type json string" // @Success 200 新增成功 // @router /team/add [post] func (this *SysTeamController) Add() { br := new(models.BaseResponse).Init() defer func() { this.Data["json"] = br this.ServeJSON() }() var req system.SysTeamAddReq 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 = "大组ID不可为空" return } //构建部门的分组树 teamList, err := system.GetChildSysGroupByGroupId(req.GroupId) teamNameArr := strings.Split(req.TeamName, ",") if err != nil { br.Msg = "获取数据失败,获取当前分组的team列表失败" br.ErrMsg = "获取当前部门的分组列表失败,Err:" + err.Error() return } //现有的分组名称 existTeamMap := make(map[string]bool, len(teamList)) for _, v := range teamList { existTeamMap[v.GroupName] = false } //传入的分组名称 reqTeamMap := make(map[string]int, len(teamNameArr)) for _, v := range teamNameArr { reqTeamMap[v] = 1 } //新增分组名称 newTeamMap := make(map[string]int) groupIds := make([]int, 0) if len(reqTeamMap) > 0 { for k, _ := range reqTeamMap { if _, ok := existTeamMap[k]; !ok { newTeamMap[k] = 1 groupIds = append(groupIds, 0) } else { existTeamMap[k] = true } } } var deleteIds []int var addGroup bool //if req.TeamName == "" { // br.Msg = "分组名称不能为空" // return //} //删除所有team即可 if req.TeamName == "" { addGroup = false for _, node := range teamList { //teamId deleteIds = append(deleteIds, node.GroupId) } } else { addGroup = true for k, v := range existTeamMap { if !v { for _, node := range teamList { if node.GroupName == k { //分组Id deleteIds = append(deleteIds, node.GroupId) } } } } } if addGroup { for k, _ := range newTeamMap { item := new(system.SysGroup) item.GroupName = k item.DepartmentId = req.DepartmentId item.ParentId = req.GroupId 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 || utils.BusinessCode == utils.BusinessCodeSandbox { var syncData system.SyncGroupData syncData.Source = utils.SOURCE_ETA_FLAG syncData.GroupId = int(groupId) _ = utils.Rc.LPush(utils.CACHE_SYNC_GROUP, syncData) } } } 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) } } } br.Ret = 200 br.Success = true br.Msg = "保存成功" } // @Title 修改分组 // @Description 修改分组接口 // @Param request body system.SysTeamEditReq true "type json string" // @Success 200 修改成功 // @router /team/edit [post] func (this *SysTeamController) Edit() { br := new(models.BaseResponse).Init() defer func() { this.Data["json"] = br this.ServeJSON() }() var req system.SysTeamEditReq err := json.Unmarshal(this.Ctx.Input.RequestBody, &req) if err != nil { br.Msg = "参数解析异常!" br.ErrMsg = "参数解析失败,Err:" + err.Error() return } if req.TeamId <= 0 { br.Msg = "参数错误" br.ErrMsg = "参数错误,GroupId 小于等于0 " return } if req.TeamName == "" { br.Msg = "分组名称不能为空" return } item, err := system.GetSysTeamByName(req.TeamName) if err != nil && !utils.IsErrNoRow(err) { br.Msg = "获取数据失败" br.ErrMsg = "获取数据失败,Err:" + err.Error() return } if item != nil { if item.GroupId != req.TeamId && item.GroupId > 0 { br.Msg = "名称已存在,请重新输入" return } } err = system.ModifySysTeam(req.TeamName, req.TeamId) if err != nil { br.Msg = "修改失败" br.ErrMsg = "修改失败,Err:" + err.Error() return } // 同步分组缓存 if utils.BusinessCode == utils.BusinessCodeRelease || utils.BusinessCode == utils.BusinessCodeSandbox { var syncData system.SyncGroupData syncData.Source = utils.SOURCE_ETA_FLAG syncData.GroupId = item.GroupId _ = utils.Rc.LPush(utils.CACHE_SYNC_GROUP, syncData) } br.Ret = 200 br.Success = true br.Msg = "修改成功" } // @Title 删除分组 // @Description 删除分组接口 // @Param request body system.SysTeamDeleteReq true "type json string" // @Success 200 删除成功 // @router /team/delete [post] func (this *SysTeamController) Delete() { br := new(models.BaseResponse).Init() defer func() { this.Data["json"] = br this.ServeJSON() }() var req system.SysTeamDeleteReq err := json.Unmarshal(this.Ctx.Input.RequestBody, &req) if err != nil { br.Msg = "参数解析异常!" br.ErrMsg = "参数解析失败,Err:" + err.Error() return } if req.TeamId <= 0 { br.Msg = "参数错误" br.ErrMsg = "参数错误,GroupId 小于等于0 " return } err = system.DeleteSysGroup(req.TeamId) if err != nil { br.Msg = "删除失败" br.ErrMsg = "删除失败,Err:" + err.Error() return } err = system.ClearSysUserTeam(req.TeamId) if err != nil { br.Msg = "删除失败" br.ErrMsg = "删除失败,情况分组失败,Err:" + err.Error() return } // 同步分组缓存 if utils.BusinessCode == utils.BusinessCodeRelease || utils.BusinessCode == utils.BusinessCodeSandbox { var syncData system.SyncGroupData syncData.Source = utils.SOURCE_ETA_FLAG syncData.GroupId = req.TeamId _ = utils.Rc.LPush(utils.CACHE_SYNC_GROUP, syncData) } br.Ret = 200 br.Success = true br.Msg = "删除成功" }