123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515 |
- 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"
- )
- // ChartCollectClassifyController 图表收藏分类
- type ChartCollectClassifyController struct {
- controllers.BaseAuthController
- }
- // List
- // @Title 分类列表-含图表
- // @Description 分类列表-含图表
- // @Param ParentId query int false "父级ID"
- // @Success 200 {object} data_manage.ChartCollectClassifyListItem
- // @router /chart_collect/classify/list [get]
- func (this *ChartCollectClassifyController) 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.ChartCollectClassifyListItem, 0)
- // 查询分类
- classifyOb := new(data_manage.ChartCollectClassify)
- {
- 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.ChartCollectClassifyListItem{
- NodeType: 1,
- ClassifyId: v.ChartCollectClassifyId,
- ClassifyName: v.ClassifyName,
- ParentId: v.ParentId,
- Level: v.Level,
- Sort: v.Sort,
- UniqueCode: v.UniqueCode,
- })
- }
- }
- // 查询分类下收藏的图表
- {
- list, e := data_manage.GetCollectChartInfoByClassifyId(parentId)
- if e != nil {
- br.Msg = "获取失败"
- br.ErrMsg = fmt.Sprintf("获取分类下图表失败, %v", e)
- return
- }
- for _, v := range list {
- resp = append(resp, &data_manage.ChartCollectClassifyListItem{
- NodeType: 2,
- ChartInfoId: v.ChartInfoId,
- ChartName: v.ChartName,
- 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 = "获取成功"
- }
- // Tree
- // @Title 分类树
- // @Description 分类树
- // @Success 200 {object} data_manage.ChartCollectClassifyItem
- // @router /chart_collect/classify/tree [get]
- func (this *ChartCollectClassifyController) 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.ChartCollectClassify)
- 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.ChartCollectClassifyItem, 0)
- for _, v := range list {
- items = append(items, v.Format2Item())
- }
- tree := data.GetChartCollectClassifyTreeRecursive(items, 0)
- br.Data = tree
- br.Ret = 200
- br.Success = true
- br.Msg = "获取成功"
- }
- // Add
- // @Title 新增分类
- // @Description 新增分类
- // @Param request body data_manage.ChartCollectClassifyAddReq true "type json string"
- // @Success Ret=200 保存成功
- // @router /chart_collect/classify/add [post]
- func (this *ChartCollectClassifyController) 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.ChartCollectClassifyAddReq
- 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.ChartCollectClassify)
- //校验分类名称
- {
- cond := fmt.Sprintf(" AND %s = ? AND %s = ? AND %s = ? ", classifyOb.Cols().ParentId, classifyOb.Cols().ClassifyName, classifyOb.Cols().SysUserId)
- pars := make([]interface{}, 0)
- pars = append(pars, req.ParentId, req.ClassifyName, this.SysUser.AdminId)
- count, e := classifyOb.GetCountByCondition(cond, pars)
- if e != nil {
- br.Msg = "操作失败"
- br.ErrMsg = fmt.Sprintf("获取分类名称重复数失败, %v", e)
- return
- }
- if count > 0 {
- br.Msg = "分类名称已存在"
- return
- }
- }
- // 层级路径
- 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.ChartCollectClassifyId)
- classifyOb.RootId = rootId
- } else {
- classifyOb.LevelPath = fmt.Sprint(classifyOb.ChartCollectClassifyId)
- classifyOb.RootId = classifyOb.ChartCollectClassifyId
- }
- 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 = "操作成功"
- }
- // Edit
- // @Title 编辑分类
- // @Description 编辑分类
- // @Param request body data_manage.ChartCollectClassifyEditReq true "type json string"
- // @Success Ret=200 保存成功
- // @router /chart_collect/classify/edit [post]
- func (this *ChartCollectClassifyController) 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.ChartCollectClassifyEditReq
- 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.ChartCollectClassify)
- 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
- }
- // 校验分类名称
- {
- cond := fmt.Sprintf(" AND %s != ? AND %s = ? AND %s = ? AND %s = ? ", classifyOb.Cols().PrimaryId, classifyOb.Cols().ParentId, classifyOb.Cols().ClassifyName, classifyOb.Cols().SysUserId)
- pars := make([]interface{}, 0)
- pars = append(pars, classifyItem.ChartCollectClassifyId, classifyItem.ParentId, req.ClassifyName, this.SysUser.AdminId)
- count, e := classifyOb.GetCountByCondition(cond, pars)
- if e != nil {
- br.Msg = "操作失败"
- br.ErrMsg = fmt.Sprintf("获取分类名称重复数失败, %v", e)
- return
- }
- if count > 0 {
- br.Msg = "分类名称已存在"
- 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 = "操作成功"
- }
- // Remove
- // @Title 删除分类
- // @Description 删除分类
- // @Param request body data_manage.ChartCollectClassifyRemoveReq true "type json string"
- // @Success 200 string "操作成功"
- // @router /chart_collect/classify/remove [post]
- func (this *ChartCollectClassifyController) 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.ChartCollectClassifyRemoveReq
- 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.ChartCollectClassify)
- _, 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
- }
- // 获取子分类IDs
- classifyIds, e := data.GetChartCollectClassifyChildIds(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 = "操作成功"
- }
- // Move
- // @Title 移动分类
- // @Description 移动分类
- // @Param request body data_manage.ChartCollectClassifyMoveReq true "type json string"
- // @Success 200 string "操作成功"
- // @router /chart_collect/classify/move [post]
- func (this *ChartCollectClassifyController) 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.ChartCollectClassifyMoveReq
- if e := json.Unmarshal(this.Ctx.Input.RequestBody, &req); e != nil {
- br.Msg = "参数解析异常!"
- br.ErrMsg = fmt.Sprintf("参数解析失败, %v", e)
- return
- }
- classifyOb := new(data_manage.ChartCollectClassify)
- 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)
- //如果有传入 上一个兄弟节点分类id
- if req.PrevClassifyId > 0 {
- prevClassify, err := data_manage.GetChartCollectClassifyById(sysUser.AdminId, req.PrevClassifyId)
- if err != nil {
- br.Msg = "移动失败"
- br.ErrMsg = "获取上一个兄弟节点分类信息失败,Err:" + err.Error()
- return
- }
- //如果是移动在两个兄弟节点之间
- if req.NextClassifyId > 0 {
- //下一个兄弟节点
- nextClassify, err := data_manage.GetChartCollectClassifyById(sysUser.AdminId, req.NextClassifyId)
- if err != nil {
- br.Msg = "移动失败"
- br.ErrMsg = "获取下一个兄弟节点分类信息失败,Err:" + err.Error()
- return
- }
- //如果上一个兄弟与下一个兄弟的排序权重是一致的,那么需要将下一个兄弟(以及下个兄弟的同样排序权重)的排序权重+2,自己变成上一个兄弟的排序权重+1
- if prevClassify.Sort == nextClassify.Sort || prevClassify.Sort == item.Sort {
- //变更兄弟节点的排序
- updateSortStr := `sort + 2`
- _ = data_manage.UpdateChartCollectClassifySortByClassifyId(sysUser.AdminId, prevClassify.ChartCollectClassifyId, prevClassify.Sort, updateSortStr)
- } else {
- //如果下一个兄弟的排序权重正好是上个兄弟节点的下一层,那么需要再加一层了
- if nextClassify.Sort-prevClassify.Sort == 1 {
- //变更兄弟节点的排序
- updateSortStr := `sort + 1`
- _ = data_manage.UpdateChartCollectClassifySortByClassifyId(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.GetFirstChartCollectClassifyByAdminId(sysUser.AdminId)
- if err != nil && !utils.IsErrNoRow(err) {
- br.Msg = "移动失败"
- br.ErrMsg = "获取获取当前账号下的排序第一条的分类信息失败,Err:" + err.Error()
- return
- }
- //如果该分类下存在其他分类,且第一个其他分类的排序等于0,那么需要调整排序
- if firstClassify != nil && firstClassify.ChartCollectClassifyId > 0 && firstClassify.Sort == 0 {
- updateSortStr := ` sort + 1 `
- _ = data_manage.UpdateChartCollectClassifySortByClassifyId(sysUser.AdminId, firstClassify.ChartCollectClassifyId-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 = "操作成功"
- }
|