package services import ( "eta_gn/eta_api/models" "eta_gn/eta_api/utils" "fmt" "time" ) func GetChartPermissionList(cond string, pars []interface{}) (permissionList []*models.ChartPermission, err error) { ob := new(models.ChartPermission) permissionList, err = ob.GetItemsByCondition(cond, pars) if err != nil { err = fmt.Errorf("获取权限列表失败, Err: %s", err.Error()) return } return } func AddChartPermission(req models.PermissionAddReq) (err error, errMsg string) { ob := new(models.ChartPermission) existCond := ` AND permission_name = ? AND parent_id = ?` existPars := make([]interface{}, 0) existPars = append(existPars, req.PermissionName, req.ParentId) exist, e := ob.GetItemByCondition(existCond, existPars) if e != nil && !utils.IsErrNoRow(e) { errMsg = "操作失败" err = fmt.Errorf("获取重名品种权限失败, Err: " + e.Error()) return } if exist != nil && exist.ChartPermissionId > 0 { errMsg = "品种名称已存在" err = fmt.Errorf("品种名称已存在") return } maxSort, e := ob.GetMaxSort() if e != nil { errMsg = "查询品种排序失败" err = fmt.Errorf("查询品种排序失败, Err: " + e.Error()) return } parentPermissionName := "" if req.ParentId > 0 { parent, e := ob.GetItemById(req.ParentId) if e != nil { errMsg = "查询父级品种失败" err = fmt.Errorf("查询父级品种失败, Err: " + e.Error()) return } if parent.ChartPermissionId == 0 { errMsg = "请选择正确的父级品种" err = fmt.Errorf("请选择正确的父级品种") return } if parent.ParentId != 0 { errMsg = "只能选择一级品种作为父级品种" err = fmt.Errorf("只能选择一级品种作为父级品种") return } parentPermissionName = parent.PermissionName } else { parentPermissionName = req.PermissionName } ob.ChartPermissionName = req.PermissionName ob.PermissionName = req.PermissionName ob.Remark = req.PermissionName ob.ParentId = req.ParentId ob.ProductId = 1 ob.IsPublic = req.IsPublic ob.Enabled = req.Enabled ob.Sort = maxSort + 1 ob.CreatedTime = time.Now() ob.LastUpdatedTime = time.Now() if ob.ProductId == 1 { ob.ProductName = "ficc" } else { ob.ProductName = "权益" } ob.ProductPermissionName = ob.ProductName + ob.PermissionName ob.ClassifyName = parentPermissionName if e = ob.Create(); e != nil { errMsg = "操作失败" err = fmt.Errorf("新增品种权限失败, Err: " + e.Error()) return } if req.ParentId > 0 && req.Enabled == 1 { err = ob.SetEnabledByChartPermissionId(req.ParentId, req.Enabled) if err != nil { errMsg = "更新品种启用禁用状态失败" err = fmt.Errorf("更新品种启用禁用状态失败, Err: " + err.Error()) return } } go func() { _, _ = ChartFiccPermissionSync() }() return } func EditChartPermission(req models.PermissionEditReq) (err error, errMsg string) { ob := new(models.ChartPermission) item, e := ob.GetItemById(req.PermissionId) if e != nil { errMsg = "查询品种信息失败" err = fmt.Errorf("查询品种信息失败, Err: " + e.Error()) return } if item.ChartPermissionId == 0 { errMsg = "请选择正确的品种" err = fmt.Errorf("请选择正确的品种") return } if item.PermissionName != req.PermissionName { existCond := ` AND permission_name = ? AND parent_id = ? AND chart_permission_id !=?` existPars := make([]interface{}, 0) existPars = append(existPars, req.PermissionName, item.ParentId, req.PermissionId) exist, e := ob.GetItemByCondition(existCond, existPars) if e != nil && !utils.IsErrNoRow(e) { errMsg = "操作失败" err = fmt.Errorf("获取重名品种权限失败, Err: " + e.Error()) return } if exist != nil && exist.ChartPermissionId > 0 { errMsg = "品种名称已存在" err = fmt.Errorf("品种名称已存在") return } } parentPermissionName := "" if req.ParentId > 0 { parent, e := ob.GetItemById(req.ParentId) if e != nil { errMsg = "查询父级品种失败" err = fmt.Errorf("查询父级品种失败, Err: " + e.Error()) return } if parent.ChartPermissionId == 0 { errMsg = "请选择正确的父级品种" err = fmt.Errorf("请选择正确的父级品种") return } if parent.ParentId != 0 { errMsg = "只能选择一级品种作为父级品种" err = fmt.Errorf("只能选择一级品种作为父级品种") return } parentPermissionName = parent.PermissionName } else { parentPermissionName = req.PermissionName } updateCol := []string{"PermissionName", "IsPublic", "ParentId", "ClassifyName", "LastUpdatedTime", "ProductPermissionName"} if item.ProductId == 1 { item.Remark = req.PermissionName //remark 在权益品种中用来区分主客观 updateCol = append(updateCol, "Remark") } item.PermissionName = req.PermissionName item.IsPublic = req.IsPublic item.ParentId = req.ParentId item.ClassifyName = parentPermissionName item.ProductPermissionName = item.ProductName + item.PermissionName item.LastUpdatedTime = time.Now() err = item.Update(updateCol) if err != nil { errMsg = "更新失败" err = fmt.Errorf("更新品种失败, Err: " + err.Error()) return } if item.ParentId == 0 { if len(req.PublicPermissionIds) > 0 { err = ob.SetIsPublic(req.PublicPermissionIds, item.ChartPermissionId, 1) if err != nil { errMsg = "更新品种公有权限失败" err = fmt.Errorf("更新品种公有权限失败, Err: " + err.Error()) return } } err = ob.UpdateClassifyNameByParentId(item.ChartPermissionId, req.PermissionName) if err != nil { errMsg = "更新品种启用禁用状态失败" err = fmt.Errorf("更新品种启用禁用状态失败, Err: " + err.Error()) return } } else if item.ParentId > 0 { if item.Enabled == 1 { err = ob.SetEnabledByChartPermissionId(item.ParentId, item.Enabled) if err != nil { errMsg = "更新品种启用禁用状态失败" err = fmt.Errorf("更新品种启用禁用状态失败, Err: " + err.Error()) return } } } go func() { _, _ = ChartFiccPermissionSync() }() return } func SetEnabledChartPermission(req models.PermissionEnabledReq) (err error, errMsg string) { ob := new(models.ChartPermission) item, e := ob.GetItemById(req.PermissionId) if e != nil { errMsg = "查询品种信息失败" err = fmt.Errorf("查询品种信息失败, Err: " + e.Error()) return } if item.ChartPermissionId == 0 { errMsg = "请选择正确的品种" err = fmt.Errorf("请选择正确的品种") return } item.Enabled = req.Enabled item.LastUpdatedTime = time.Now() err = item.Update([]string{"Enabled", "LastUpdatedTime"}) if err != nil { errMsg = "更新失败" err = fmt.Errorf("更新品种失败, Err: " + err.Error()) return } if item.ParentId == 0 { err = ob.SetEnabledByParentId(item.ChartPermissionId, req.Enabled) if err != nil { errMsg = "更新品种启用禁用状态失败" err = fmt.Errorf("更新品种启用禁用状态失败, Err: " + err.Error()) return } } else if item.ParentId > 0 { if req.Enabled == 1 { err = ob.SetEnabledByChartPermissionId(item.ParentId, req.Enabled) if err != nil { errMsg = "更新品种启用禁用状态失败" err = fmt.Errorf("更新品种启用禁用状态失败, Err: " + err.Error()) return } } } go func() { _, _ = ChartFiccPermissionSync() }() return } func MoveChartPermission(req models.PermissionMoveReq) (err error, errMsg string) { ob := new(models.ChartPermission) chartPermissionId := req.PermissionId prevChartPermissionId := req.PrevPermissionId nextChartPermissionId := req.NextPermissionId var ( permissionInfo *models.ChartPermission prevPermission *models.ChartPermission nextPermission *models.ChartPermission prevSort int nextSort int ) permissionInfo, err = ob.GetItemById(chartPermissionId) if err != nil { if utils.IsErrNoRow(err) { errMsg = "当前品种不存在" err = fmt.Errorf("获取品种信息失败,Err:" + err.Error()) return } errMsg = "移动失败" err = fmt.Errorf("获取分类信息失败,Err:" + err.Error()) return } else if permissionInfo.ChartPermissionId == 0 { errMsg = "当前品种不存在" err = fmt.Errorf("获取品种信息失败,Err:" + err.Error()) return } parentChartPermissionId := permissionInfo.ParentId productId := permissionInfo.ProductId if prevChartPermissionId > 0 { prevPermission, err = ob.GetItemById(prevChartPermissionId) if err != nil { errMsg = "移动失败" err = fmt.Errorf("获取上一个兄弟节点分类信息失败,Err:" + err.Error()) return } prevSort = prevPermission.Sort } if nextChartPermissionId > 0 { nextPermission, err = ob.GetItemById(nextChartPermissionId) if err != nil { errMsg = "移动失败" err = fmt.Errorf("获取下一个兄弟节点分类信息失败,Err:" + err.Error()) return } nextSort = nextPermission.Sort } err, errMsg = moveChartPermission(permissionInfo, prevPermission, nextPermission, parentChartPermissionId, prevSort, nextSort, productId) return } func moveChartPermission(permissionInfo, prevPermission, nextPermission *models.ChartPermission, parentId, prevSort, nextSort, productId int) (err error, errMsg string) { ob := new(models.ChartPermission) updateCol := make([]string, 0) if permissionInfo.ParentId != parentId { errMsg = "移动失败" err = fmt.Errorf("不支持目录层级变更") return } if prevSort > 0 { if nextSort > 0 { if prevSort == nextSort || prevSort == permissionInfo.Sort { updateSortStr := `sort + 2` if prevPermission != nil { _ = models.UpdateChartPermissionSortByParentId(parentId, prevPermission.ChartPermissionId, prevPermission.Sort, updateSortStr, productId) } else { _ = models.UpdateChartPermissionSortByParentId(parentId, 0, prevSort, updateSortStr, productId) } } else { if nextSort-prevSort == 1 { updateSortStr := `sort + 1` if prevPermission != nil { _ = models.UpdateChartPermissionSortByParentId(parentId, prevPermission.ChartPermissionId, prevSort, updateSortStr, productId) } else { _ = models.UpdateChartPermissionSortByParentId(parentId, 0, prevSort, updateSortStr, productId) } } } } permissionInfo.Sort = prevSort + 1 permissionInfo.LastUpdatedTime = time.Now() updateCol = append(updateCol, "Sort", "LastUpdatedTime") } else if prevPermission == nil && nextPermission == nil && parentId > 0 { var maxSort int maxSort, err = ob.GetMaxSortByParentId(parentId) if err != nil { errMsg = "移动失败" err = fmt.Errorf("查询组内排序信息失败,Err:" + err.Error()) return } permissionInfo.Sort = maxSort + 1 //那就是排在组内最后一位 permissionInfo.LastUpdatedTime = time.Now() updateCol = append(updateCol, "Sort", "LastUpdatedTime") } else { firstPermission, tmpErr := ob.GetFirstChartPermissionByParentId(parentId) if tmpErr != nil && !utils.IsErrNoRow(tmpErr) { errMsg = "移动失败" err = fmt.Errorf("获取获取当前父级分类下的排序第一条的分类信息失败,Err:" + tmpErr.Error()) return } if firstPermission != nil && firstPermission.ChartPermissionId > 0 && firstPermission.Sort == 0 { updateSortStr := ` sort + 1 ` _ = models.UpdateChartPermissionSortByParentId(parentId, firstPermission.ChartPermissionId-1, 0, updateSortStr, productId) } permissionInfo.Sort = 0 //那就是排在第一位 permissionInfo.LastUpdatedTime = time.Now() updateCol = append(updateCol, "Sort", "LastUpdatedTime") } if len(updateCol) > 0 { err = permissionInfo.Update(updateCol) if err != nil { errMsg = "移动失败" err = fmt.Errorf("修改失败,Err:" + err.Error()) return } } go func() { _, _ = ChartFiccPermissionSync() }() return }