package ai_predict_model import ( "encoding/json" "eta/eta_api/controllers" "eta/eta_api/models" aiPredictModel "eta/eta_api/models/ai_predict_model" "eta/eta_api/models/data_manage" "eta/eta_api/services" "eta/eta_api/utils" "fmt" "sort" "strconv" "strings" "time" ) // AiPredictModelClassifyController AI预测模型-分类 type AiPredictModelClassifyController struct { controllers.BaseAuthController } // List // @Title 分类列表 // @Description 分类列表 // @Param ParentId query bool false "父级ID" // @Success 200 {object} data_manage.ChartClassifyListResp // @router /classify/list [get] func (this *AiPredictModelClassifyController) List() { br := new(models.BaseResponse).Init() defer func() { if br.ErrMsg == "" { br.IsSendEmail = false } this.Data["json"] = br this.ServeJSON() }() parentId, _ := this.GetInt("ParentId") resp := new(aiPredictModel.AiPredictModelClassifyListResp) // (懒加载)仅查询直属分类 classifyOb := new(aiPredictModel.AiPredictModelClassify) { cond := fmt.Sprintf(" AND %s = ?", classifyOb.Cols().ParentId) pars := make([]interface{}, 0) pars = append(pars, parentId) 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.AllNodes = append(resp.AllNodes, &aiPredictModel.AiPredictModelClassifyListItem{ NodeName: v.ClassifyName, ClassifyId: v.AiPredictModelClassifyId, ClassifyName: v.ClassifyName, ParentId: v.ParentId, Level: v.Level, Sort: v.Sort, UniqueCode: v.UniqueCode, }) } } // 非顶级目录查询指标 indexOb := new(aiPredictModel.AiPredictModelIndex) if parentId > 0 { parentClassify, e := classifyOb.GetItemById(parentId) if e != nil { br.Msg = "父级分类不存在, 请刷新页面" return } cond := fmt.Sprintf(" AND %s = ?", indexOb.Cols().ClassifyId) pars := make([]interface{}, 0) pars = append(pars, parentId) list, e := indexOb.GetItemsByCondition(cond, pars, []string{}, fmt.Sprintf("%s ASC", indexOb.Cols().Sort)) if e != nil { br.Msg = "获取失败" br.ErrMsg = fmt.Sprintf("获取分类下指标失败, %v", e) return } for _, v := range list { resp.AllNodes = append(resp.AllNodes, &aiPredictModel.AiPredictModelClassifyListItem{ NodeType: 1, NodeName: v.IndexName, ClassifyId: parentClassify.AiPredictModelClassifyId, ClassifyName: parentClassify.ClassifyName, IndexId: v.AiPredictModelIndexId, IndexCode: v.IndexCode, IndexName: v.IndexName, ParentId: parentId, Sort: v.Sort, UniqueCode: v.IndexCode, }) } } sort.Slice(resp.AllNodes, func(i, j int) bool { return resp.AllNodes[i].Sort < resp.AllNodes[j].Sort }) br.Data = resp br.Ret = 200 br.Success = true br.Msg = "获取成功" } // Add // @Title 新增分类 // @Description 新增分类 // @Param request body aiPredictModel.AiPredictModelClassifyAddReq true "type json string" // @Success 200 Ret=200 保存成功 // @router /classify/add [post] func (this *AiPredictModelClassifyController) 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 aiPredictModel.AiPredictModelClassifyAddReq 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 } if req.ParentId < 0 { br.Msg = "请选择上级分类" return } if req.Level > 5 { br.Msg = "目前只支持6级目录" return } // 校验分类名称 classifyOb := new(aiPredictModel.AiPredictModelClassify) { cond := fmt.Sprintf(" AND %s = ?", classifyOb.Cols().ParentId) if this.Lang == utils.EnLangVersion { cond += fmt.Sprintf(" AND %s = ?", classifyOb.Cols().ClassifyNameEn) } else { cond += fmt.Sprintf(" AND %s = ?", classifyOb.Cols().ClassifyName) } pars := make([]interface{}, 0) pars = append(pars, req.ParentId, req.ClassifyName) 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.ClassifyNameEn = 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.AiPredictModelClassifyId) classifyOb.RootId = rootId } else { classifyOb.LevelPath = fmt.Sprint(classifyOb.AiPredictModelClassifyId) classifyOb.RootId = classifyOb.AiPredictModelClassifyId } if e = classifyOb.Update([]string{classifyOb.Cols().LevelPath, classifyOb.Cols().RootId}); e != nil { br.Msg = "操作失败" br.ErrMsg = fmt.Sprintf("更新分类失败, %v", e) return } br.Ret = 200 br.Msg = "操作成功" br.Success = true } // Edit // @Title 修改分类 // @Description 修改分类 // @Param request body aiPredictModel.AiPredictModelClassifyEditReq true "type json string" // @Success 200 Ret=200 修改成功 // @router /classify/edit [post] func (this *AiPredictModelClassifyController) 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 aiPredictModel.AiPredictModelClassifyEditReq 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(aiPredictModel.AiPredictModelClassify) classifyItem, e := classifyOb.GetItemById(req.ClassifyId) if e != nil { if e.Error() == utils.ErrNoRow() { br.Msg = "分类不存在, 请刷新页面" return } br.Msg = "操作失败" br.ErrMsg = fmt.Sprintf("获取分类失败, %v", e) return } // 校验分类名称 { cond := fmt.Sprintf(" AND %s <> ?", classifyOb.Cols().PrimaryId) if this.Lang == utils.EnLangVersion { cond += fmt.Sprintf(" AND %s = ?", classifyOb.Cols().ClassifyNameEn) } else { cond += fmt.Sprintf(" AND %s = ?", classifyOb.Cols().ClassifyName) } pars := make([]interface{}, 0) pars = append(pars, req.ClassifyId, req.ClassifyName) 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.ClassifyNameEn = req.ClassifyName classifyItem.ModifyTime = time.Now().Local() updateCols := []string{classifyOb.Cols().ClassifyName, classifyOb.Cols().ClassifyNameEn, classifyOb.Cols().ModifyTime} if e = classifyItem.Update(updateCols); e != nil { br.Msg = "操作失败" br.ErrMsg = fmt.Sprintf("更新分类失败, %v", e) return } br.Ret = 200 br.Msg = "操作成功" br.Success = true } // RemoveCheck // @Title 删除校验 // @Description 删除校验 // @Param request body aiPredictModel.AiPredictModelClassifyRemoveReq true "type json string" // @Success 200 Ret=200 检测成功 // @router /classify/remove_check [post] func (this *AiPredictModelClassifyController) RemoveCheck() { br := new(models.BaseResponse).Init() defer func() { if br.ErrMsg == "" { br.IsSendEmail = false } this.Data["json"] = br this.ServeJSON() }() var req aiPredictModel.AiPredictModelClassifyRemoveReq if e := json.Unmarshal(this.Ctx.Input.RequestBody, &req); e != nil { br.Msg = "参数解析异常" br.ErrMsg = fmt.Sprintf("参数解析异常, %v", e) return } if req.ClassifyId < 0 && req.IndexId <= 0 { br.Msg = "参数错误" return } var deleteStatus int var tipsMsg string // 删除分类 if req.ClassifyId > 0 && req.IndexId == 0 { count, err := aiPredictModel.GetAiPredictModelIndexCountByClassifyId(req.ClassifyId) if err != nil { br.Msg = "删除失败" br.ErrMsg = "分类下是否含有图表失败,Err:" + err.Error() return } if count > 0 { deleteStatus = 1 tipsMsg = "该分类下关联图表不可删除" } } if deleteStatus != 1 && req.IndexId == 0 { classifyCount, err := aiPredictModel.GetAiPredictModelClassifyCountByClassifyId(req.ClassifyId) if err != nil && err.Error() != utils.ErrNoRow() { br.Msg = "删除失败" br.ErrMsg = "分类下是否含有图表失败,Err:" + err.Error() return } if classifyCount > 0 { deleteStatus = 2 tipsMsg = "确认删除当前目录及包含的子目录吗" } } if deleteStatus == 0 { tipsMsg = "可删除,进行删除操作" } resp := new(data_manage.ChartClassifyDeleteCheckResp) resp.DeleteStatus = deleteStatus resp.TipsMsg = tipsMsg br.Ret = 200 br.Msg = "检测成功" br.Success = true br.Data = resp } // Remove // @Title 删除分类/标的 // @Description 删除分类/标的 // @Param request body aiPredictModel.AiPredictModelClassifyRemoveReq true "type json string" // @Success 200 Ret=200 删除成功 // @router /classify/remove [post] func (this *AiPredictModelClassifyController) Remove() { br := new(models.BaseResponse).Init() defer func() { if br.ErrMsg == "" { br.IsSendEmail = false } this.Data["json"] = br this.ServeJSON() }() var req aiPredictModel.AiPredictModelClassifyRemoveReq if e := json.Unmarshal(this.Ctx.Input.RequestBody, &req); e != nil { br.Msg = "参数解析异常" br.ErrMsg = fmt.Sprintf("参数解析异常, %v", e) return } if req.ClassifyId < 0 && req.IndexId <= 0 { br.Msg = "参数错误" return } // 删除分类 if req.ClassifyId > 0 && req.IndexId == 0 { count, err := aiPredictModel.GetAiPredictModelIndexCountByClassifyId(req.ClassifyId) if err != nil && err.Error() != utils.ErrNoRow() { br.Msg = "删除失败" br.ErrMsg = "判断名称是否已存在失败,Err:" + err.Error() return } if count > 0 { br.Msg = "该目录下存在关联指标,不可删除" br.IsSendEmail = false return } err = aiPredictModel.RemoveAiPredictModelClassify(req.ClassifyId) if err != nil { br.Msg = "删除失败" br.ErrMsg = "删除失败,Err:" + err.Error() return } } // 删除标的 if req.IndexId > 0 { indexOb := new(aiPredictModel.AiPredictModelIndex) _, e := indexOb.GetItemById(req.IndexId) if e != nil { if e.Error() == utils.ErrNoRow() { br.Ret = 200 br.Msg = "删除成功" br.Success = true return } br.Msg = "操作失败" br.ErrMsg = fmt.Sprintf("获取标的信息失败, %v", e) return } // 删除标的及数据 if e = indexOb.RemoveIndexAndData(req.IndexId); e != nil { br.Msg = "操作失败" br.ErrMsg = fmt.Sprintf("删除标的及数据失败, %v", e) return } } br.Ret = 200 br.Msg = "删除成功" br.Success = true br.IsAddLog = true } // Move // @Title 移动 // @Description 移动 // @Success 200 {object} aiPredictModel.AiPredictModelClassifyMoveReq // @router /classify/move [post] func (this *AiPredictModelClassifyController) 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 aiPredictModel.AiPredictModelClassifyMoveReq if e := json.Unmarshal(this.Ctx.Input.RequestBody, &req); e != nil { br.Msg = "参数解析异常" br.ErrMsg = fmt.Sprintf("参数解析异常, %v", e) return } if req.ClassifyId <= 0 && req.ItemId <= 0 { br.Msg = "请选择分类或指标" return } err, errMsg := services.AiPredictModelMoveClassify(req, sysUser) if errMsg != `` { br.Msg = errMsg br.ErrMsg = errMsg if err != nil { br.ErrMsg = err.Error() } else { br.IsSendEmail = false } return } br.Ret = 200 br.Success = true br.Msg = "移动成功" }