123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478 |
- package services
- import (
- "eta/eta_api/models"
- "eta/eta_api/models/company"
- "eta/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 && e.Error() != utils.ErrNoRow() {
- 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 && e.Error() != utils.ErrNoRow() {
- 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
- }
- // SetEnabledChartPermission 设置启用禁用
- 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
- }
- // MoveChartPermission 移动品种
- func MoveChartPermission(req models.PermissionMoveReq) (err error, errMsg string) {
- ob := new(models.ChartPermission)
- chartPermissionId := req.PermissionId
- prevChartPermissionId := req.PrevPermissionId
- nextChartPermissionId := req.NextPermissionId
- //如果有传入 上一个兄弟节点分类id
- var (
- permissionInfo *models.ChartPermission
- prevPermission *models.ChartPermission
- nextPermission *models.ChartPermission
- prevSort int
- nextSort int
- )
- // 移动对象为分类, 判断权限
- permissionInfo, err = ob.GetItemById(chartPermissionId)
- if err != nil {
- if err.Error() == utils.ErrNoRow() {
- 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
- }
- // moveChartPermission 移动指标分类
- func moveChartPermission(permissionInfo, prevPermission, nextPermission *models.ChartPermission, parentId, prevSort, nextSort, productId int) (err error, errMsg string) {
- ob := new(models.ChartPermission)
- updateCol := make([]string, 0)
- //判断上级id是否一致,如果不一致的话,那么需要移动该分类层级
- if permissionInfo.ParentId != parentId {
- errMsg = "移动失败"
- err = fmt.Errorf("不支持目录层级变更")
- return
- }
- if prevSort > 0 {
- //如果是移动在两个兄弟节点之间
- if nextSort > 0 {
- //下一个兄弟节点
- //如果上一个兄弟与下一个兄弟的排序权重是一致的,那么需要将下一个兄弟(以及下个兄弟的同样排序权重)的排序权重+2,自己变成上一个兄弟的排序权重+1
- 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 && tmpErr.Error() != utils.ErrNoRow() {
- errMsg = "移动失败"
- err = fmt.Errorf("获取获取当前父级分类下的排序第一条的分类信息失败,Err:" + tmpErr.Error())
- return
- }
- //如果该分类下存在其他分类,且第一个其他分类的排序等于0,那么需要调整排序
- 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
- }
- // GetChartPermissionEnabledList 查询已启用的FICC权限列表
- func GetChartPermissionEnabledList() (list []*company.PermissionSetList, err error) {
- dataList, err := GetChartPermissionList(" AND enabled = 1", []interface{}{})
- if err != nil {
- return
- }
- //enabledList := make([]*models.ChartPermission, 0)
- parentList := make([]*models.ChartPermission, 0)
- enabledMap := make(map[int][]*company.PermissionSetItem)
- listMap := make(map[int]*models.ChartPermission)
- for _, v := range dataList {
- listMap[v.ChartPermissionId] = v
- if v.ParentId == 0 {
- parentList = append(parentList, v)
- }
- if v.Enabled == 1 && v.ParentId > 0 && v.PermissionType == 0 {
- //enabledList = append(enabledList, v)
- item := &company.PermissionSetItem{
- ChartPermissionId: v.ChartPermissionId,
- PermissionName: v.PermissionName,
- PermissionType: v.PermissionType,
- }
- enabledMap[v.ParentId] = append(enabledMap[v.ParentId], item)
- }
- }
- for _, v := range parentList {
- items, ok := enabledMap[v.ChartPermissionId]
- if !ok {
- continue
- }
- checkList := make([]int, 0)
- p := new(company.PermissionSetList)
- p.PermissionName = v.PermissionName
- p.Child = items
- if v.PermissionName == "宏观经济" {
- checkList = append(checkList, 1)
- }
- p.CheckList = checkList
- list = append(list, p)
- }
- return
- }
|