|
@@ -4,6 +4,7 @@ import (
|
|
|
"encoding/json"
|
|
|
"eta_gn/eta_api/models"
|
|
|
"eta_gn/eta_api/models/system"
|
|
|
+ "eta_gn/eta_api/services"
|
|
|
"eta_gn/eta_api/utils"
|
|
|
"strings"
|
|
|
"time"
|
|
@@ -20,6 +21,51 @@ type SysGroupController struct {
|
|
|
// @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
|
|
|
+ //}
|
|
|
+ //if req.GroupName == "" {
|
|
|
+ // br.Msg = "分组名称不能为空"
|
|
|
+ // return
|
|
|
+ //}
|
|
|
+ //groupNameArr := strings.Split(req.GroupName, ",")
|
|
|
+ //for _, v := range groupNameArr {
|
|
|
+ // count, err := system.GetSysGroupCount(req.DepartmentId, v)
|
|
|
+ // if err != nil {
|
|
|
+ // br.Msg = "获取数据失败"
|
|
|
+ // br.ErrMsg = "获取数据失败,Err:" + err.Error()
|
|
|
+ // return
|
|
|
+ // }
|
|
|
+ // if count <= 0 {
|
|
|
+ // item := new(system.SysGroup)
|
|
|
+ // item.GroupName = v
|
|
|
+ // item.DepartmentId = req.DepartmentId
|
|
|
+ // item.CreateTime = time.Now()
|
|
|
+ // _, e := system.AddSysGroup(item)
|
|
|
+ // if e != nil {
|
|
|
+ // br.Msg = "新增失败"
|
|
|
+ // br.ErrMsg = "新增失败,Err:" + e.Error()
|
|
|
+ // return
|
|
|
+ // }
|
|
|
+ // }
|
|
|
+ //}
|
|
|
+ //br.Ret = 200
|
|
|
+ //br.Success = true
|
|
|
+ //br.Msg = "新增成功"
|
|
|
br := new(models.BaseResponse).Init()
|
|
|
defer func() {
|
|
|
this.Data["json"] = br
|
|
@@ -37,21 +83,75 @@ func (this *SysGroupController) Add() {
|
|
|
br.ErrMsg = "部门ID不可为空"
|
|
|
return
|
|
|
}
|
|
|
- if req.GroupName == "" {
|
|
|
- br.Msg = "分组名称不能为空"
|
|
|
+ //构建部门的分组树
|
|
|
+ groupList, err := system.GetSysGroupByDepartmentId(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 {
|
|
|
- count, err := system.GetSysGroupCount(req.DepartmentId, v)
|
|
|
- if err != nil {
|
|
|
- br.Msg = "获取数据失败"
|
|
|
- br.ErrMsg = "获取数据失败,Err:" + err.Error()
|
|
|
- return
|
|
|
+ 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
|
|
|
+ }
|
|
|
}
|
|
|
- if count <= 0 {
|
|
|
+ }
|
|
|
+ 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 = v
|
|
|
+ item.GroupName = k
|
|
|
item.DepartmentId = req.DepartmentId
|
|
|
item.CreateTime = time.Now()
|
|
|
_, e := system.AddSysGroup(item)
|
|
@@ -62,9 +162,17 @@ func (this *SysGroupController) Add() {
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
+ if len(deleteIds) > 0 {
|
|
|
+ err = services.DeleteSysGroupByIds(deleteIds)
|
|
|
+ if err != nil {
|
|
|
+ br.Msg = "删除失败"
|
|
|
+ br.ErrMsg = "删除失败,Err:" + err.Error()
|
|
|
+ return
|
|
|
+ }
|
|
|
+ }
|
|
|
br.Ret = 200
|
|
|
br.Success = true
|
|
|
- br.Msg = "新增成功"
|
|
|
+ br.Msg = "保存成功"
|
|
|
}
|
|
|
|
|
|
// @Title 修改分组
|