package data_manage import ( "encoding/json" "eta_gn/eta_api/controllers" "eta_gn/eta_api/models" "eta_gn/eta_api/models/data_manage" "eta_gn/eta_api/services/data" "eta_gn/eta_api/utils" "fmt" "sort" "strconv" "strings" "time" ) type EdbCollectClassifyController struct { controllers.BaseAuthController } // @router /edb_collect/classify/list [get] func (this *EdbCollectClassifyController) List() { br := new(models.BaseResponse).Init() defer func() { if br.ErrMsg == "" { br.IsSendEmail = false } this.Data["json"] = br this.ServeJSON() }() sysUser := this.SysUser if sysUser == nil { br.Msg = "请登录" br.ErrMsg = "请登录,SysUser Is Empty" br.Ret = 408 return } parentId, _ := this.GetInt("ParentId") resp := make([]*data_manage.EdbCollectClassifyListItem, 0) classifyOb := new(data_manage.EdbCollectClassify) { cond := fmt.Sprintf(" AND %s = ? AND %s = ?", classifyOb.Cols().ParentId, classifyOb.Cols().SysUserId) pars := make([]interface{}, 0) pars = append(pars, parentId, sysUser.AdminId) list, e := classifyOb.GetItemsByCondition(cond, pars, []string{}, fmt.Sprintf("%s ASC", classifyOb.Cols().Sort)) if e != nil { br.Msg = "获取失败" br.ErrMsg = fmt.Sprintf("获取子分类失败, %v", e) return } for _, v := range list { resp = append(resp, &data_manage.EdbCollectClassifyListItem{ NodeType: 1, ClassifyId: v.EdbCollectClassifyId, ClassifyName: v.ClassifyName, ParentId: v.ParentId, Level: v.Level, Sort: v.Sort, UniqueCode: v.UniqueCode, }) } } { list, e := data_manage.GetCollectEdbInfoByClassifyId(parentId) if e != nil { br.Msg = "获取失败" br.ErrMsg = fmt.Sprintf("获取分类下指标失败, %v", e) return } for _, v := range list { resp = append(resp, &data_manage.EdbCollectClassifyListItem{ NodeType: 2, EdbInfoId: v.EdbInfoId, EdbCode: v.EdbCode, EdbName: v.EdbName, ParentId: parentId, Sort: v.Sort, UniqueCode: v.UniqueCode, }) } } sort.Slice(resp, func(i, j int) bool { return resp[i].Sort < resp[j].Sort }) br.Data = resp br.Ret = 200 br.Success = true br.Msg = "获取成功" } // @router /edb_collect/classify/tree [get] func (this *EdbCollectClassifyController) Tree() { br := new(models.BaseResponse).Init() defer func() { if br.ErrMsg == "" { br.IsSendEmail = false } this.Data["json"] = br this.ServeJSON() }() sysUser := this.SysUser if sysUser == nil { br.Msg = "请登录" br.ErrMsg = "请登录,SysUser Is Empty" br.Ret = 408 return } classifyOb := new(data_manage.EdbCollectClassify) cond := fmt.Sprintf(" AND %s = ?", classifyOb.Cols().SysUserId) pars := make([]interface{}, 0) pars = append(pars, sysUser.AdminId) list, e := classifyOb.GetItemsByCondition(cond, pars, []string{}, fmt.Sprintf("%s ASC, %s ASC", classifyOb.Cols().ParentId, classifyOb.Cols().Sort)) if e != nil { br.Msg = "获取失败" br.ErrMsg = fmt.Sprintf("获取分类列表失败, %v", e) return } items := make([]*data_manage.EdbCollectClassifyItem, 0) for _, v := range list { items = append(items, v.Format2Item()) } tree := data.GetEdbCollectClassifyTreeRecursive(items, 0) br.Data = tree br.Ret = 200 br.Success = true br.Msg = "获取成功" } // @router /edb_collect/classify/add [post] func (this *EdbCollectClassifyController) Add() { br := new(models.BaseResponse).Init() defer func() { if br.ErrMsg == "" { br.IsSendEmail = false } this.Data["json"] = br this.ServeJSON() }() sysUser := this.SysUser if sysUser == nil { br.Msg = "请登录" br.ErrMsg = "请登录,SysUser Is Empty" br.Ret = 408 return } var req data_manage.EdbCollectClassifyAddReq if e := json.Unmarshal(this.Ctx.Input.RequestBody, &req); e != nil { br.Msg = "参数解析异常" br.ErrMsg = fmt.Sprintf("参数解析异常: %v", e) return } req.ClassifyName = strings.TrimSpace(req.ClassifyName) if req.ClassifyName == "" { br.Msg = "请输入名称" return } classifyOb := new(data_manage.EdbCollectClassify) var levelPath string var rootId int if req.ParentId > 0 { parent, e := classifyOb.GetItemById(req.ParentId) if e != nil { br.Msg = "上级分类有误" br.ErrMsg = fmt.Sprintf("获取上级分类失败, %v", e) return } levelPath = parent.LevelPath rootId = parent.RootId } sortMax, e := classifyOb.GetSortMax(req.ParentId) if e != nil { br.Msg = "操作失败" br.ErrMsg = fmt.Sprintf("获取分类最大排序失败, %v", e) return } timestamp := strconv.FormatInt(time.Now().UnixNano(), 10) classifyOb.ParentId = req.ParentId classifyOb.ClassifyName = req.ClassifyName classifyOb.Level = req.Level + 1 classifyOb.Sort = sortMax + 1 classifyOb.SysUserId = sysUser.AdminId classifyOb.SysUserRealName = sysUser.RealName classifyOb.UniqueCode = utils.MD5(classifyOb.TableName() + "_" + timestamp) classifyOb.CreateTime = time.Now().Local() classifyOb.ModifyTime = time.Now().Local() if e = classifyOb.Create(); e != nil { br.Msg = "操作失败" br.ErrMsg = fmt.Sprintf("新增分类失败, %v", e) return } if req.ParentId > 0 { classifyOb.LevelPath = fmt.Sprintf("%s,%d", levelPath, classifyOb.EdbCollectClassifyId) classifyOb.RootId = rootId } else { classifyOb.LevelPath = fmt.Sprint(classifyOb.EdbCollectClassifyId) classifyOb.RootId = classifyOb.EdbCollectClassifyId } if e = classifyOb.Update([]string{classifyOb.Cols().LevelPath, classifyOb.Cols().RootId}); e != nil { br.Msg = "操作失败" br.ErrMsg = fmt.Sprintf("更新分类失败, %v", e) return } br.Data = classifyOb.Format2Item() br.Ret = 200 br.Success = true br.Msg = "操作成功" } // @router /edb_collect/classify/edit [post] func (this *EdbCollectClassifyController) Edit() { br := new(models.BaseResponse).Init() defer func() { if br.ErrMsg == "" { br.IsSendEmail = false } this.Data["json"] = br this.ServeJSON() }() sysUser := this.SysUser if sysUser == nil { br.Msg = "请登录" br.ErrMsg = "请登录,SysUser Is Empty" br.Ret = 408 return } var req data_manage.EdbCollectClassifyEditReq if e := json.Unmarshal(this.Ctx.Input.RequestBody, &req); e != nil { br.Msg = "参数解析异常" br.ErrMsg = fmt.Sprintf("参数解析异常: %v", e) return } if req.ClassifyId < 0 { br.Msg = "请选择分类" return } req.ClassifyName = strings.TrimSpace(req.ClassifyName) if req.ClassifyName == "" { br.Msg = "请输入分类名称" return } classifyOb := new(data_manage.EdbCollectClassify) classifyItem, e := classifyOb.GetItemById(req.ClassifyId) if e != nil { if utils.IsErrNoRow(e) { br.Msg = "分类不存在, 请刷新页面" return } br.Msg = "操作失败" br.ErrMsg = fmt.Sprintf("获取分类失败, %v", e) return } classifyItem.ClassifyName = req.ClassifyName classifyItem.ModifyTime = time.Now().Local() updateCols := []string{classifyOb.Cols().ClassifyName, classifyOb.Cols().ModifyTime} if e = classifyItem.Update(updateCols); e != nil { br.Msg = "操作失败" br.ErrMsg = fmt.Sprintf("更新分类失败, %v", e) return } br.Data = classifyItem.Format2Item() br.Ret = 200 br.Success = true br.Msg = "操作成功" } // @router /edb_collect/classify/remove [post] func (this *EdbCollectClassifyController) Remove() { br := new(models.BaseResponse).Init() defer func() { if br.ErrMsg == "" { br.IsSendEmail = false } this.Data["json"] = br this.ServeJSON() }() sysUser := this.SysUser if sysUser == nil { br.Msg = "请登录" br.ErrMsg = "请登录,SysUser Is Empty" br.Ret = 408 return } var req data_manage.EdbCollectClassifyRemoveReq if e := json.Unmarshal(this.Ctx.Input.RequestBody, &req); e != nil { br.Msg = "参数解析异常" br.ErrMsg = fmt.Sprintf("参数解析异常, %v", e) return } if req.ClassifyId < 0 { br.Msg = "请选择分类" return } classifyOb := new(data_manage.EdbCollectClassify) _, e := classifyOb.GetItemById(req.ClassifyId) if e != nil { if utils.IsErrNoRow(e) { br.Ret = 200 br.Success = true br.Msg = "操作成功" return } br.Msg = "操作失败" br.ErrMsg = fmt.Sprintf("获取分类失败, %v", e) return } classifyIds, e := data.GetEdbCollectClassifyChildIds(req.ClassifyId, sysUser.AdminId, true) if e != nil { br.Msg = "操作失败" br.ErrMsg = fmt.Sprintf("获取子分类IDs失败, %v", e) return } if len(classifyIds) == 0 { br.Ret = 200 br.Success = true br.Msg = "操作成功" return } if e = classifyOb.RemoveClassifyAndCollect(classifyIds); e != nil { br.Msg = "操作失败" br.ErrMsg = fmt.Sprintf("移除分类及收藏失败, %v", e) return } br.Ret = 200 br.Success = true br.Msg = "操作成功" } // @router /edb_collect/classify/move [post] func (this *EdbCollectClassifyController) Move() { br := new(models.BaseResponse).Init() defer func() { if br.ErrMsg == "" { br.IsSendEmail = false } this.Data["json"] = br this.ServeJSON() }() sysUser := this.SysUser if sysUser == nil { br.Msg = "请登录" br.ErrMsg = "请登录,SysUser Is Empty" br.Ret = 408 return } var req data_manage.EdbCollectClassifyMoveReq if e := json.Unmarshal(this.Ctx.Input.RequestBody, &req); e != nil { br.Msg = "参数解析异常!" br.ErrMsg = fmt.Sprintf("参数解析失败, %v", e) return } classifyOb := new(data_manage.EdbCollectClassify) item, e := classifyOb.GetItemById(req.ClassifyId) if e != nil { if utils.IsErrNoRow(e) { br.Msg = "分类不存在, 请刷新页面" return } br.Msg = "操作失败" br.ErrMsg = fmt.Sprintf("获取分类失败, %v", e) return } updateCol := make([]string, 0) if req.PrevClassifyId > 0 { prevClassify, err := data_manage.GetEdbCollectClassifyById(sysUser.AdminId, req.PrevClassifyId) if err != nil { br.Msg = "移动失败" br.ErrMsg = "获取上一个兄弟节点分类信息失败,Err:" + err.Error() return } if req.NextClassifyId > 0 { nextClassify, err := data_manage.GetEdbCollectClassifyById(sysUser.AdminId, req.NextClassifyId) if err != nil { br.Msg = "移动失败" br.ErrMsg = "获取下一个兄弟节点分类信息失败,Err:" + err.Error() return } if prevClassify.Sort == nextClassify.Sort || prevClassify.Sort == item.Sort { updateSortStr := `sort + 2` _ = data_manage.UpdateEdbCollectClassifySortByClassifyId(sysUser.AdminId, prevClassify.EdbCollectClassifyId, prevClassify.Sort, updateSortStr) } else { if nextClassify.Sort-prevClassify.Sort == 1 { updateSortStr := `sort + 1` _ = data_manage.UpdateEdbCollectClassifySortByClassifyId(sysUser.AdminId, 0, prevClassify.Sort, updateSortStr) } } } item.Sort = prevClassify.Sort + 1 item.ModifyTime = time.Now() updateCol = append(updateCol, "Sort", "ModifyTime") } else { firstClassify, err := data_manage.GetFirstEdbCollectClassifyByAdminId(sysUser.AdminId) if err != nil && !utils.IsErrNoRow(err) { br.Msg = "移动失败" br.ErrMsg = "获取获取当前账号下的排序第一条的分类信息失败,Err:" + err.Error() return } if firstClassify != nil && firstClassify.EdbCollectClassifyId > 0 && firstClassify.Sort == 0 { updateSortStr := ` sort + 1 ` _ = data_manage.UpdateEdbCollectClassifySortByClassifyId(sysUser.AdminId, firstClassify.EdbCollectClassifyId-1, 0, updateSortStr) } item.Sort = 0 //那就是排在第一位 item.ModifyTime = time.Now() updateCol = append(updateCol, "Sort", "ModifyTime") } if len(updateCol) > 0 { if e = item.Update(updateCol); e != nil { br.Msg = "操作失败" br.ErrMsg = fmt.Sprintf("更新分类失败, %v", e) return } } br.Ret = 200 br.Success = true br.Msg = "操作成功" }