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/models/system" "eta_gn/eta_api/services/data" "eta_gn/eta_api/utils" "fmt" "strings" "time" ) // ChartFrameworkController 图库框架 type ChartFrameworkController struct { controllers.BaseAuthController } // List // @Title 列表 // @Description 列表 // @Param AdminId query int false "创建人ID" // @Param Visibility query int false "范围: 0-所有; 1-私有; 2-公开" // @Param Keyword query string false "关键词" // @Success 200 Ret=200 获取成功 // @router /list [get] func (this *ChartFrameworkController) 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 } adminId, _ := this.GetInt("AdminId") visibility, _ := this.GetInt("Visibility") if visibility == 1 && adminId <= 0 { adminId = sysUser.AdminId } keyword := this.GetString("Keyword") keyword = strings.TrimSpace(keyword) frameworkOb := new(data_manage.ChartFramework) cond := `` pars := make([]interface{}, 0) if adminId > 0 { cond += fmt.Sprintf(` AND %s = ?`, data_manage.ChartFrameworkColumns.AdminId) pars = append(pars, adminId) } if keyword != "" { cond += fmt.Sprintf(` AND %s LIKE ?`, data_manage.ChartFrameworkColumns.FrameworkName) pars = append(pars, "%"+keyword+"%") } if visibility > 0 { visibilityArr := map[int]int{1: 0, 2: 1} cond += fmt.Sprintf(` AND %s = ?`, data_manage.ChartFrameworkColumns.IsPublic) pars = append(pars, visibilityArr[visibility]) } orderRule := `sort ASC, create_time DESC` list, e := frameworkOb.GetItemsByCondition(cond, pars, []string{}, orderRule) if e != nil { br.Msg = "获取失败" br.ErrMsg = "获取框架列表失败, Err: " + e.Error() return } resp := make([]*data_manage.ChartFrameworkItem, 0) for _, v := range list { t := data_manage.FormatChartFramework2Item(v, make([]*data_manage.ChartFrameworkNodeItem, 0)) resp = append(resp, t) } br.Data = resp br.Ret = 200 br.Success = true br.Msg = "获取成功" } // PublicMenu // @Title 公开框架目录 // @Description 公开框架目录 // @Success 200 Ret=200 获取成功 // @router /public_menu [get] func (this *ChartFrameworkController) PublicMenu() { 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 } frameworkOb := new(data_manage.ChartFramework) cond := fmt.Sprintf(` AND %s = ?`, data_manage.ChartFrameworkColumns.IsPublic) pars := make([]interface{}, 0) pars = append(pars, 1) orderRule := `public_time ASC, sort ASC, create_time DESC` list, e := frameworkOb.GetItemsByCondition(cond, pars, []string{}, orderRule) if e != nil { br.Msg = "获取失败" br.ErrMsg = "获取公开框架失败, Err: " + e.Error() return } // 用户被删除不展示框架 admins, e := system.GetSysAdminList(``, make([]interface{}, 0), []string{}, "") if e != nil { br.Msg = "获取失败" br.ErrMsg = "获取系统用户列表失败, Err: " + e.Error() return } adminExist := make(map[int]bool) for _, v := range admins { adminExist[v.AdminId] = true } userExist := make(map[int]bool) userFrameworks := make(map[int][]*data_manage.ChartFrameworkItem) resp := make([]*data_manage.ChartFrameworkPublicMenuItem, 0) for _, v := range list { if !adminExist[v.AdminId] { continue } if !userExist[v.AdminId] { u := new(data_manage.ChartFrameworkPublicMenuItem) u.AdminId = v.AdminId u.MenuName = fmt.Sprintf("%s的框架", v.AdminName) resp = append(resp, u) userExist[v.AdminId] = true } t := data_manage.FormatChartFramework2Item(v, make([]*data_manage.ChartFrameworkNodeItem, 0)) if userFrameworks[v.AdminId] == nil { userFrameworks[v.AdminId] = make([]*data_manage.ChartFrameworkItem, 0) } userFrameworks[v.AdminId] = append(userFrameworks[v.AdminId], t) } for _, v := range resp { v.Frameworks = userFrameworks[v.AdminId] } br.Data = resp br.Ret = 200 br.Success = true br.Msg = "获取成功" } // Add // @Title 新增框架 // @Description 新增框架 // @Param request body data_manage.ChartFrameworkAddReq true "type json string" // @Success 200 Ret=200 操作成功 // @router /add [post] func (this *ChartFrameworkController) 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.ChartFrameworkAddReq if e := json.Unmarshal(this.Ctx.Input.RequestBody, &req); e != nil { br.Msg = "参数解析异常!" br.ErrMsg = "参数解析失败,Err:" + e.Error() return } req.FrameworkName = strings.TrimSpace(req.FrameworkName) if req.FrameworkName == "" { br.Msg = "框架名称不可为空" return } // 重名校验 { ob := new(data_manage.ChartFramework) cond := fmt.Sprintf(` AND %s = ? AND %s = ?`, data_manage.ChartFrameworkColumns.FrameworkName, data_manage.ChartFrameworkColumns.AdminId) pars := make([]interface{}, 0) pars = append(pars, req.FrameworkName, sysUser.AdminId) exist, e := ob.GetItemByCondition(cond, pars) if e != nil && !utils.IsErrNoRow(e) { br.Msg = "操作失败" br.ErrMsg = "获取重名框架失败, Err: " + e.Error() return } if exist != nil { br.Msg = "框架名称已存在" return } } // 获取图表分类下各自的图表数 chartsNumMap, e := data.GetMyChartClassifyIdNumMap(sysUser.AdminId) if e != nil { br.Msg = "获取失败" br.ErrMsg = "获取失败, GetMyChartClassifyIdNumMap Err: " + e.Error() return } now := time.Now().Local() frameworkCode := utils.MD5(fmt.Sprint(now.UnixMilli())) item := new(data_manage.ChartFramework) item.FrameworkName = req.FrameworkName item.FrameworkCode = frameworkCode item.FrameworkImg = req.FrameworkImg item.FrameworkContent = req.FrameworkContent item.AdminId = sysUser.AdminId item.AdminName = sysUser.RealName item.CreateTime = now item.ModifyTime = now nodes := make([]*data_manage.ChartFrameworkNode, 0) itemNodes := make([]*data_manage.ChartFrameworkNodeItem, 0) if len(req.Nodes) > 0 { for _, v := range req.Nodes { if v.MyChartClassifyId <= 0 { continue } t := new(data_manage.ChartFrameworkNode) t.FrameworkName = req.FrameworkName t.NodeId = v.NodeId t.NodeName = v.NodeName t.MyChartClassifyId = v.MyChartClassifyId t.CreateTime = now nodes = append(nodes, t) num, ok := chartsNumMap[v.MyChartClassifyId] if !ok { num = 0 } // 响应节点数据 td := data_manage.FormatChartFrameworkNode2Item(t, num) itemNodes = append(itemNodes, td) } } if e := item.CreateFrameworkAndNodes(item, nodes); e != nil { br.Msg = "操作失败" br.ErrMsg = "新增框架及节点失败, Err: " + e.Error() return } detail := data_manage.FormatChartFramework2Item(item, itemNodes) br.Data = detail br.Ret = 200 br.Success = true br.Msg = "操作成功" br.IsAddLog = true } // Edit // @Title 编辑框架 // @Description 编辑框架 // @Param request body data_manage.ChartFrameworkEditReq true "type json string" // @Success 200 Ret=200 操作成功 // @router /edit [post] func (this *ChartFrameworkController) 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.ChartFrameworkEditReq if e := json.Unmarshal(this.Ctx.Input.RequestBody, &req); e != nil { br.Msg = "参数解析异常!" br.ErrMsg = "参数解析失败,Err:" + e.Error() return } if req.ChartFrameworkId <= 0 { br.Msg = "参数有误" br.ErrMsg = fmt.Sprintf("参数有误, ChartFrameworkId: %d", req.ChartFrameworkId) return } req.FrameworkName = strings.TrimSpace(req.FrameworkName) if req.FrameworkName == "" { br.Msg = "框架名称不可为空" return } frameworkOb := new(data_manage.ChartFramework) item, e := frameworkOb.GetItemById(req.ChartFrameworkId) if e != nil { if utils.IsErrNoRow(e) { br.Msg = "框架不存在, 请刷新页面" return } br.Msg = "操作失败" br.ErrMsg = "获取框架失败, Err: " + e.Error() return } // 重名校验 { ob := new(data_manage.ChartFramework) cond := fmt.Sprintf(` AND %s <> ? AND %s = ? AND %s = ?`, ob.PrimaryId(), data_manage.ChartFrameworkColumns.FrameworkName, data_manage.ChartFrameworkColumns.AdminId) pars := make([]interface{}, 0) pars = append(pars, req.ChartFrameworkId, req.FrameworkName, sysUser.AdminId) exist, e := ob.GetItemByCondition(cond, pars) if e != nil && !utils.IsErrNoRow(e) { br.Msg = "操作失败" br.ErrMsg = "获取重名框架失败, Err: " + e.Error() return } if exist != nil { br.Msg = "框架名称已存在" return } } // 获取图表分类下各自的图表数 chartsNumMap, e := data.GetMyChartClassifyIdNumMap(sysUser.AdminId) if e != nil { br.Msg = "获取失败" br.ErrMsg = "获取失败, GetMyChartClassifyIdNumMap Err: " + e.Error() return } now := time.Now().Local() item.FrameworkName = req.FrameworkName item.FrameworkImg = req.FrameworkImg item.FrameworkContent = req.FrameworkContent item.ModifyTime = now updateCols := []string{"FrameworkName", "FrameworkImg", "FrameworkContent", "ModifyTime"} nodes := make([]*data_manage.ChartFrameworkNode, 0) itemNodes := make([]*data_manage.ChartFrameworkNodeItem, 0) if len(req.Nodes) > 0 { for _, v := range req.Nodes { if v.MyChartClassifyId <= 0 { continue } t := new(data_manage.ChartFrameworkNode) t.ChartFrameworkId = req.ChartFrameworkId t.FrameworkName = req.FrameworkName t.NodeId = v.NodeId t.NodeName = v.NodeName t.MyChartClassifyId = v.MyChartClassifyId t.CreateTime = now nodes = append(nodes, t) // 响应节点数据 td := data_manage.FormatChartFrameworkNode2Item(t, chartsNumMap[t.MyChartClassifyId]) itemNodes = append(itemNodes, td) } } if e := item.EditFrameworkAndNodes(item, updateCols, nodes); e != nil { br.Msg = "操作失败" br.ErrMsg = "编辑框架及节点失败, Err: " + e.Error() return } detail := data_manage.FormatChartFramework2Item(item, itemNodes) br.Data = detail br.Ret = 200 br.Success = true br.Msg = "操作成功" br.IsAddLog = true } // Remove // @Title 删除框架 // @Description 删除视频 // @Param request body data_manage.ChartFrameworkRemoveReq true "type json string" // @Success 200 Ret=200 操作成功 // @router /remove [post] func (this *ChartFrameworkController) 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.ChartFrameworkRemoveReq if e := json.Unmarshal(this.Ctx.Input.RequestBody, &req); e != nil { br.Msg = "参数解析异常!" br.ErrMsg = "参数解析失败,Err:" + e.Error() return } if req.ChartFrameworkId <= 0 { br.Msg = "参数有误" br.ErrMsg = fmt.Sprintf("参数有误, ChartFrameworkId: %d", req.ChartFrameworkId) return } ob := new(data_manage.ChartFramework) item, e := ob.GetItemById(req.ChartFrameworkId) if e != nil { if utils.IsErrNoRow(e) { br.Ret = 200 br.Success = true br.Msg = "操作成功" return } br.Msg = "操作失败" br.ErrMsg = "获取框架失败, Err: " + e.Error() return } if sysUser.RoleTypeCode != utils.ROLE_TYPE_CODE_ADMIN && item.AdminId != sysUser.AdminId { br.Msg = "无权操作" return } if e := item.RemoveFrameworkAndNodes(req.ChartFrameworkId); e != nil { br.Msg = "操作失败" br.ErrMsg = "删除框架失败, Err: " + e.Error() return } br.Ret = 200 br.Success = true br.Msg = "操作成功" br.IsAddLog = true } // Rename // @Title 重命名框架 // @Description 重命名框架 // @Param request body data_manage.ChartFrameworkRenameReq true "type json string" // @Success 200 Ret=200 操作成功 // @router /rename [post] func (this *ChartFrameworkController) Rename() { 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.ChartFrameworkRenameReq if e := json.Unmarshal(this.Ctx.Input.RequestBody, &req); e != nil { br.Msg = "参数解析异常!" br.ErrMsg = "参数解析失败,Err:" + e.Error() return } if req.ChartFrameworkId <= 0 { br.Msg = "参数有误" br.ErrMsg = fmt.Sprintf("参数有误, ChartFrameworkId: %d", req.ChartFrameworkId) return } req.FrameworkName = strings.TrimSpace(req.FrameworkName) if req.FrameworkName == "" { br.Msg = "框架名称不可为空" return } frameworkOb := new(data_manage.ChartFramework) item, e := frameworkOb.GetItemById(req.ChartFrameworkId) if e != nil { if utils.IsErrNoRow(e) { br.Msg = "框架不存在, 请刷新页面" return } br.Msg = "操作失败" br.ErrMsg = "获取框架失败, Err: " + e.Error() return } // 重名校验 { ob := new(data_manage.ChartFramework) cond := fmt.Sprintf(` AND %s <> ? AND %s = ? AND %s = ?`, ob.PrimaryId(), data_manage.ChartFrameworkColumns.FrameworkName, data_manage.ChartFrameworkColumns.AdminId) pars := make([]interface{}, 0) pars = append(pars, req.ChartFrameworkId, req.FrameworkName, sysUser.AdminId) exist, e := ob.GetItemByCondition(cond, pars) if e != nil && !utils.IsErrNoRow(e) { br.Msg = "操作失败" br.ErrMsg = "获取重名框架失败, Err: " + e.Error() return } if exist != nil { br.Msg = "框架名称已存在" return } } now := time.Now().Local() item.FrameworkName = req.FrameworkName item.ModifyTime = now updateCols := []string{"FrameworkName", "ModifyTime"} if e := item.Update(updateCols); e != nil { br.Msg = "操作失败" br.ErrMsg = "框架重命名失败, Err: " + e.Error() return } br.Ret = 200 br.Success = true br.Msg = "操作成功" } // EditPublic // @Title 公开/隐藏框架 // @Description 公开/隐藏框架 // @Param request body data_manage.ChartFrameworkEditPublicReq true "type json string" // @Success 200 Ret=200 操作成功 // @router /edit_public [post] func (this *ChartFrameworkController) EditPublic() { 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.ChartFrameworkEditPublicReq if e := json.Unmarshal(this.Ctx.Input.RequestBody, &req); e != nil { br.Msg = "参数解析异常!" br.ErrMsg = "参数解析失败,Err:" + e.Error() return } if req.ChartFrameworkId <= 0 { br.Msg = "参数有误" br.ErrMsg = fmt.Sprintf("参数有误, ChartFrameworkId: %d", req.ChartFrameworkId) return } if req.IsPublic != 0 && req.IsPublic != 1 { br.Msg = "参数有误" br.ErrMsg = fmt.Sprintf("参数有误, IsPublic: %d", req.IsPublic) return } frameworkOb := new(data_manage.ChartFramework) item, e := frameworkOb.GetItemById(req.ChartFrameworkId) if e != nil { if utils.IsErrNoRow(e) { br.Msg = "框架不存在, 请刷新页面" return } br.Msg = "操作失败" br.ErrMsg = "获取框架失败, Err: " + e.Error() return } now := time.Now().Local() updateCols := []string{"IsPublic", "PublicTime", "ModifyTime"} item.IsPublic = req.IsPublic if req.IsPublic == 1 { item.PublicTime = time.Now().Local() } else { item.PublicTime = time.Time{} } item.ModifyTime = now if e := item.Update(updateCols); e != nil { br.Msg = "操作失败" br.ErrMsg = "更新框架是否公开失败, Err: " + e.Error() return } br.Ret = 200 br.Success = true br.Msg = "操作成功" } // Move // @Title 移动排序 // @Description 移动排序 // @Param request body data_manage.ChartFrameworkMoveReq true "type json string" // @Success 200 Ret=200 操作成功 // @router /move [post] func (this *ChartFrameworkController) 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.ChartFrameworkMoveReq if e := json.Unmarshal(this.Ctx.Input.RequestBody, &req); e != nil { br.Msg = "参数解析异常!" br.ErrMsg = "参数解析失败,Err:" + e.Error() return } if req.ChartFrameworkId <= 0 { br.Msg = "参数有误" br.ErrMsg = fmt.Sprintf("参数有误, ChartFrameworkId: %d", req.ChartFrameworkId) return } frameworkOb := new(data_manage.ChartFramework) item, e := frameworkOb.GetItemById(req.ChartFrameworkId) if e != nil { if utils.IsErrNoRow(e) { br.Msg = "框架不存在, 请刷新页面" return } br.Msg = "操作失败" br.ErrMsg = "获取框架失败, Err: " + e.Error() return } updateCols := make([]string, 0) // 上一个兄弟节点 if req.PrevChartFrameworkId > 0 { prev, e := frameworkOb.GetItemById(req.PrevChartFrameworkId) if e != nil { br.Msg = "操作失败" br.ErrMsg = "获取上一个兄弟节点失败, Err: " + e.Error() return } // 两个兄弟节点之间 if req.NextChartFrameworkId > 0 { next, e := frameworkOb.GetItemById(req.PrevChartFrameworkId) if e != nil { br.Msg = "操作失败" br.ErrMsg = "获取下一个兄弟节点失败, Err: " + e.Error() return } // 如果上一个与下一个排序权重是一致的, 那么需要将下一个兄弟(以及下个兄弟的同样排序权重)的排序权重+2, 自己变成上一个兄弟的排序权重+1 if prev.Sort == next.Sort || prev.Sort == item.Sort { strUpdate := `sort + 2` _ = data_manage.UpdateChartFrameworkSort(sysUser.AdminId, prev.ChartFrameworkId, prev.Sort, strUpdate) } else { // 如果下一个排序权重正好是上一个节点的下一层, 那么需要再加一层了 if next.Sort-prev.Sort == 1 { //变更兄弟节点的排序 strUpdate := `sort + 1` _ = data_manage.UpdateChartFrameworkSort(sysUser.AdminId, 0, prev.Sort, strUpdate) } } } // 上一个兄弟节点sort+1 item.Sort = prev.Sort + 1 item.ModifyTime = time.Now() updateCols = append(updateCols, "Sort", "ModifyTime") } else { first, err := data_manage.GetFirstChartFramework(sysUser.AdminId) if err != nil && !utils.IsErrNoRow(err) { br.Msg = "操作失败" br.ErrMsg = "获取我的图库框架排首位的数据失败, Err:" + err.Error() return } if first != nil && first.Sort == 0 { strUpdate := ` sort + 1 ` _ = data_manage.UpdateChartFrameworkSort(sysUser.AdminId, first.ChartFrameworkId-1, 0, strUpdate) } // 排首位 item.Sort = 0 item.ModifyTime = time.Now() updateCols = append(updateCols, "Sort", "ModifyTime") } if len(updateCols) > 0 { if e := item.Update(updateCols); e != nil { br.Msg = "操作失败" br.ErrMsg = "更新框架排序失败, Err: " + e.Error() return } } br.Ret = 200 br.Success = true br.Msg = "操作成功" } // Detail // @Title 框架详情 // @Description 框架详情 // @Param ChartFrameworkId query int true "框架ID" // @Success 200 Ret=200 操作成功 // @router /detail [get] func (this *ChartFrameworkController) Detail() { 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 } frameworkId, _ := this.GetInt("ChartFrameworkId") if frameworkId <= 0 { br.Msg = "参数有误" br.ErrMsg = fmt.Sprintf("参数有误, ChartFrameworkId: %d", frameworkId) return } frameworkOb := new(data_manage.ChartFramework) item, e := frameworkOb.GetItemById(frameworkId) if e != nil { if utils.IsErrNoRow(e) { br.Msg = "框架不存在, 请刷新页面" return } br.Msg = "获取失败" br.ErrMsg = "获取框架失败, Err: " + e.Error() return } // 获取节点 nodeOb := new(data_manage.ChartFrameworkNode) nodeCond := ` AND chart_framework_id = ?` nodePars := make([]interface{}, 0) nodePars = append(nodePars, frameworkId) nodes, e := nodeOb.GetItemsByCondition(nodeCond, nodePars, []string{}, "") if e != nil { br.Msg = "获取失败" br.ErrMsg = "获取框架节点失败, Err: " + e.Error() return } // 获取图表分类下各自的图表数 chartsNumMap, e := data.GetMyChartClassifyIdNumMap(sysUser.AdminId) if e != nil { br.Msg = "获取失败" br.ErrMsg = "获取失败, GetMyChartClassifyIdNumMap Err: " + e.Error() return } // 格式化响应数据 itemNodes := make([]*data_manage.ChartFrameworkNodeItem, 0) for _, v := range nodes { if v.NodeId == "" { continue } num, ok := chartsNumMap[v.MyChartClassifyId] if !ok { num = 0 } itemNodes = append(itemNodes, data_manage.FormatChartFrameworkNode2Item(v, num)) } detail := data_manage.FormatChartFramework2Item(item, itemNodes) br.Data = detail br.Ret = 200 br.Success = true br.Msg = "操作成功" }