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/utils" "fmt" "strings" "time" ) // AiPredictModelFrameworkController 模型框架 type AiPredictModelFrameworkController 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 /framework/list [get] func (c *AiPredictModelFrameworkController) List() { br := new(models.BaseResponse).Init() defer func() { if br.ErrMsg == "" { br.IsSendEmail = false } c.Data["json"] = br c.ServeJSON() }() sysUser := c.SysUser if sysUser == nil { br.Msg = "请登录" br.ErrMsg = "请登录,SysUser Is Empty" br.Ret = 408 return } adminId, _ := c.GetInt("AdminId") keyword := c.GetString("Keyword") keyword = strings.TrimSpace(keyword) frameworkOb := new(aiPredictModel.AiPredictModelFramework) cond := `` pars := make([]interface{}, 0) if adminId > 0 { cond += fmt.Sprintf(` AND %s = ?`, aiPredictModel.AiPredictModelFrameworkColumns.AdminId) pars = append(pars, adminId) } if keyword != "" { cond += fmt.Sprintf(` AND %s LIKE ?`, aiPredictModel.AiPredictModelFrameworkColumns.FrameworkName) pars = append(pars, "%"+keyword+"%") } 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([]*aiPredictModel.AiPredictModelFrameworkItem, 0) for _, v := range list { t := aiPredictModel.FormatAiPredictModelFramework2Item(v, make([]*aiPredictModel.AiPredictModelFrameworkNodeItem, 0)) if t.AdminId == sysUser.AdminId || utils.IsAdminRole(sysUser.RoleTypeCode) { t.Button.OpButton = true t.Button.DeleteButton = true t.Button.MoveButton = true } resp = append(resp, t) } br.Data = resp br.Ret = 200 br.Success = true br.Msg = "获取成功" } // Add // @Title 新增框架 // @Description 新增框架 // @Param request body aiPredictModel.AiPredictModelFrameworkAddReq true "type json string" // @Success 200 Ret=200 操作成功 // @router /framework/add [post] func (c *AiPredictModelFrameworkController) Add() { br := new(models.BaseResponse).Init() defer func() { if br.ErrMsg == "" { br.IsSendEmail = false } c.Data["json"] = br c.ServeJSON() }() sysUser := c.SysUser if sysUser == nil { br.Msg = "请登录" br.ErrMsg = "请登录,SysUser Is Empty" br.Ret = 408 return } // 参数校验 var req aiPredictModel.AiPredictModelFrameworkAddReq if e := json.Unmarshal(c.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(aiPredictModel.AiPredictModelFramework) cond := fmt.Sprintf(` AND %s = ? AND %s = ?`, aiPredictModel.AiPredictModelFrameworkColumns.FrameworkName, aiPredictModel.AiPredictModelFrameworkColumns.AdminId) pars := make([]interface{}, 0) pars = append(pars, req.FrameworkName, sysUser.AdminId) exist, e := ob.GetItemByCondition(cond, pars) if e != nil && e.Error() != utils.ErrNoRow() { br.Msg = "操作失败" br.ErrMsg = "获取重名框架失败, Err: " + e.Error() return } if exist != nil { br.Msg = "框架名称已存在,请重新输入" return } } now := time.Now().Local() frameworkCode := utils.MD5(fmt.Sprint(now.UnixMilli())) item := new(aiPredictModel.AiPredictModelFramework) 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([]*aiPredictModel.AiPredictModelFrameworkNode, 0) itemNodes := make([]*aiPredictModel.AiPredictModelFrameworkNodeItem, 0) if len(req.Nodes) > 0 { for _, v := range req.Nodes { if v.AiPredictModelIndexId <= 0 { continue } t := new(aiPredictModel.AiPredictModelFrameworkNode) t.FrameworkName = req.FrameworkName t.NodeId = v.NodeId t.NodeName = v.NodeName t.AiPredictModelIndexId = v.AiPredictModelIndexId t.CreateTime = now nodes = append(nodes, t) num := 1 // 响应节点数据 td := aiPredictModel.FormatAiPredictModelFrameworkNode2Item(t, num, map[int]*aiPredictModel.AiPredictModelIndex{}) itemNodes = append(itemNodes, td) } } if e := item.CreateFrameworkAndNodes(item, nodes); e != nil { br.Msg = "操作失败" br.ErrMsg = "新增框架及节点失败, Err: " + e.Error() return } detail := aiPredictModel.FormatAiPredictModelFramework2Item(item, itemNodes) br.Data = detail br.Ret = 200 br.Success = true br.Msg = "操作成功" br.IsAddLog = true } // Edit // @Title 编辑框架 // @Description 编辑框架 // @Param request body aiPredictModel.AiPredictModelFrameworkEditReq true "type json string" // @Success 200 Ret=200 操作成功 // @router /framework/edit [post] func (c *AiPredictModelFrameworkController) Edit() { br := new(models.BaseResponse).Init() defer func() { if br.ErrMsg == "" { br.IsSendEmail = false } c.Data["json"] = br c.ServeJSON() }() sysUser := c.SysUser if sysUser == nil { br.Msg = "请登录" br.ErrMsg = "请登录,SysUser Is Empty" br.Ret = 408 return } // 参数校验 var req aiPredictModel.AiPredictModelFrameworkEditReq if e := json.Unmarshal(c.Ctx.Input.RequestBody, &req); e != nil { br.Msg = "参数解析异常!" br.ErrMsg = "参数解析失败,Err:" + e.Error() return } if req.AiPredictModelFrameworkId <= 0 { br.Msg = "参数有误" br.ErrMsg = fmt.Sprintf("参数有误, AiPredictModelFrameworkId: %d", req.AiPredictModelFrameworkId) return } req.FrameworkName = strings.TrimSpace(req.FrameworkName) if req.FrameworkName == "" { br.Msg = "框架名称不可为空" return } frameworkOb := new(aiPredictModel.AiPredictModelFramework) item, e := frameworkOb.GetItemById(req.AiPredictModelFrameworkId) if e != nil { if e.Error() == utils.ErrNoRow() { br.Msg = "框架不存在, 请刷新页面" return } br.Msg = "操作失败" br.ErrMsg = "获取框架失败, Err: " + e.Error() return } // 操作权限校验 if item.AdminId != sysUser.AdminId && !utils.IsAdminRole(sysUser.RoleTypeCode) { br.Msg = "您没有权限操作该框架" return } // 重名校验 { ob := new(aiPredictModel.AiPredictModelFramework) cond := fmt.Sprintf(` AND %s <> ? AND %s = ? AND %s = ?`, ob.PrimaryId(), aiPredictModel.AiPredictModelFrameworkColumns.FrameworkName, aiPredictModel.AiPredictModelFrameworkColumns.AdminId) pars := make([]interface{}, 0) pars = append(pars, req.AiPredictModelFrameworkId, req.FrameworkName, sysUser.AdminId) exist, e := ob.GetItemByCondition(cond, pars) if e != nil && e.Error() != utils.ErrNoRow() { br.Msg = "操作失败" br.ErrMsg = "获取重名框架失败, Err: " + e.Error() return } if exist != nil { br.Msg = "框架名称已存在,请重新输入" 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([]*aiPredictModel.AiPredictModelFrameworkNode, 0) itemNodes := make([]*aiPredictModel.AiPredictModelFrameworkNodeItem, 0) if len(req.Nodes) > 0 { for _, v := range req.Nodes { if v.AiPredictModelIndexId <= 0 { continue } t := new(aiPredictModel.AiPredictModelFrameworkNode) t.AiPredictModelFrameworkId = req.AiPredictModelFrameworkId t.FrameworkName = req.FrameworkName t.NodeId = v.NodeId t.NodeName = v.NodeName t.AiPredictModelIndexId = v.AiPredictModelIndexId t.CreateTime = now nodes = append(nodes, t) // 响应节点数据 td := aiPredictModel.FormatAiPredictModelFrameworkNode2Item(t, 1, map[int]*aiPredictModel.AiPredictModelIndex{}) itemNodes = append(itemNodes, td) } } if e := item.EditFrameworkAndNodes(item, updateCols, nodes); e != nil { br.Msg = "操作失败" br.ErrMsg = "编辑框架及节点失败, Err: " + e.Error() return } detail := aiPredictModel.FormatAiPredictModelFramework2Item(item, itemNodes) br.Data = detail br.Ret = 200 br.Success = true br.Msg = "操作成功" br.IsAddLog = true } // Remove // @Title 删除框架 // @Description 删除视频 // @Param request body aiPredictModel.AiPredictModelFrameworkRemoveReq true "type json string" // @Success 200 Ret=200 操作成功 // @router /framework/remove [post] func (c *AiPredictModelFrameworkController) Remove() { br := new(models.BaseResponse).Init() defer func() { if br.ErrMsg == "" { br.IsSendEmail = false } c.Data["json"] = br c.ServeJSON() }() sysUser := c.SysUser if sysUser == nil { br.Msg = "请登录" br.ErrMsg = "请登录,SysUser Is Empty" br.Ret = 408 return } // 参数校验 var req aiPredictModel.AiPredictModelFrameworkRemoveReq if e := json.Unmarshal(c.Ctx.Input.RequestBody, &req); e != nil { br.Msg = "参数解析异常!" br.ErrMsg = "参数解析失败,Err:" + e.Error() return } if req.AiPredictModelFrameworkId <= 0 { br.Msg = "参数有误" br.ErrMsg = fmt.Sprintf("参数有误, AiPredictModelFrameworkId: %d", req.AiPredictModelFrameworkId) return } ob := new(aiPredictModel.AiPredictModelFramework) item, e := ob.GetItemById(req.AiPredictModelFrameworkId) if e != nil { if e.Error() == utils.ErrNoRow() { 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.AiPredictModelFrameworkId); 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 aiPredictModel.AiPredictModelFrameworkRenameReq true "type json string" // @Success 200 Ret=200 操作成功 // @router /framework/rename [post] func (c *AiPredictModelFrameworkController) Rename() { br := new(models.BaseResponse).Init() defer func() { if br.ErrMsg == "" { br.IsSendEmail = false } c.Data["json"] = br c.ServeJSON() }() sysUser := c.SysUser if sysUser == nil { br.Msg = "请登录" br.ErrMsg = "请登录,SysUser Is Empty" br.Ret = 408 return } // 参数校验 var req aiPredictModel.AiPredictModelFrameworkRenameReq if e := json.Unmarshal(c.Ctx.Input.RequestBody, &req); e != nil { br.Msg = "参数解析异常!" br.ErrMsg = "参数解析失败,Err:" + e.Error() return } if req.AiPredictModelFrameworkId <= 0 { br.Msg = "参数有误" br.ErrMsg = fmt.Sprintf("参数有误, AiPredictModelFrameworkId: %d", req.AiPredictModelFrameworkId) return } req.FrameworkName = strings.TrimSpace(req.FrameworkName) if req.FrameworkName == "" { br.Msg = "框架名称不可为空" return } frameworkOb := new(aiPredictModel.AiPredictModelFramework) item, e := frameworkOb.GetItemById(req.AiPredictModelFrameworkId) if e != nil { if e.Error() == utils.ErrNoRow() { br.Msg = "框架不存在, 请刷新页面" return } br.Msg = "操作失败" br.ErrMsg = "获取框架失败, Err: " + e.Error() return } // 操作权限校验 if item.AdminId != sysUser.AdminId && !utils.IsAdminRole(sysUser.RoleTypeCode) { br.Msg = "您没有权限操作该框架" return } // 重名校验 { ob := new(aiPredictModel.AiPredictModelFramework) cond := fmt.Sprintf(` AND %s <> ? AND %s = ? AND %s = ?`, ob.PrimaryId(), aiPredictModel.AiPredictModelFrameworkColumns.FrameworkName, aiPredictModel.AiPredictModelFrameworkColumns.AdminId) pars := make([]interface{}, 0) pars = append(pars, req.AiPredictModelFrameworkId, req.FrameworkName, sysUser.AdminId) exist, e := ob.GetItemByCondition(cond, pars) if e != nil && e.Error() != utils.ErrNoRow() { 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 = "操作成功" } // Move // @Title 移动排序 // @Description 移动排序 // @Param request body aiPredictModel.AiPredictModelFrameworkMoveReq true "type json string" // @Success 200 Ret=200 操作成功 // @router /framework/move [post] func (c *AiPredictModelFrameworkController) Move() { br := new(models.BaseResponse).Init() defer func() { if br.ErrMsg == "" { br.IsSendEmail = false } c.Data["json"] = br c.ServeJSON() }() sysUser := c.SysUser if sysUser == nil { br.Msg = "请登录" br.ErrMsg = "请登录,SysUser Is Empty" br.Ret = 408 return } // 参数校验 var req aiPredictModel.AiPredictModelFrameworkMoveReq if e := json.Unmarshal(c.Ctx.Input.RequestBody, &req); e != nil { br.Msg = "参数解析异常!" br.ErrMsg = "参数解析失败,Err:" + e.Error() return } if req.AiPredictModelFrameworkId <= 0 { br.Msg = "参数有误" br.ErrMsg = fmt.Sprintf("参数有误, AiPredictModelFrameworkId: %d", req.AiPredictModelFrameworkId) return } frameworkOb := new(aiPredictModel.AiPredictModelFramework) item, e := frameworkOb.GetItemById(req.AiPredictModelFrameworkId) if e != nil { if e.Error() == utils.ErrNoRow() { br.Msg = "框架不存在, 请刷新页面" return } br.Msg = "操作失败" br.ErrMsg = "获取框架失败, Err: " + e.Error() return } updateCols := make([]string, 0) // 上一个兄弟节点 if req.PrevAiPredictModelFrameworkId > 0 { prev, e := frameworkOb.GetItemById(req.PrevAiPredictModelFrameworkId) if e != nil { br.Msg = "操作失败" br.ErrMsg = "获取上一个兄弟节点失败, Err: " + e.Error() return } // 两个兄弟节点之间 if req.NextAiPredictModelFrameworkId > 0 { next, e := frameworkOb.GetItemById(req.PrevAiPredictModelFrameworkId) 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` _ = aiPredictModel.UpdateAiPredictModelFrameworkSort(sysUser.AdminId, prev.AiPredictModelFrameworkId, prev.Sort, strUpdate) } else { // 如果下一个排序权重正好是上一个节点的下一层, 那么需要再加一层了 if next.Sort-prev.Sort == 1 { //变更兄弟节点的排序 strUpdate := `sort + 1` _ = aiPredictModel.UpdateAiPredictModelFrameworkSort(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 := aiPredictModel.GetFirstAiPredictModelFramework() if err != nil && err.Error() != utils.ErrNoRow() { br.Msg = "操作失败" br.ErrMsg = "获取我的模型框架排首位的数据失败, Err:" + err.Error() return } if first != nil && first.Sort == 0 { strUpdate := ` sort + 1 ` _ = aiPredictModel.UpdateAiPredictModelFrameworkSort(sysUser.AdminId, first.AiPredictModelFrameworkId-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 AiPredictModelFrameworkId query int true "框架ID" // @Success 200 Ret=200 操作成功 // @router /framework/detail [get] func (c *AiPredictModelFrameworkController) Detail() { br := new(models.BaseResponse).Init() defer func() { if br.ErrMsg == "" { br.IsSendEmail = false } c.Data["json"] = br c.ServeJSON() }() sysUser := c.SysUser if sysUser == nil { br.Msg = "请登录" br.ErrMsg = "请登录,SysUser Is Empty" br.Ret = 408 return } frameworkId, _ := c.GetInt("AiPredictModelFrameworkId") if frameworkId <= 0 { br.Msg = "参数有误" br.ErrMsg = fmt.Sprintf("参数有误, AiPredictModelFrameworkId: %d", frameworkId) return } frameworkOb := new(aiPredictModel.AiPredictModelFramework) item, e := frameworkOb.GetItemById(frameworkId) if e != nil { if e.Error() == utils.ErrNoRow() { br.Msg = "框架不存在, 请刷新页面" return } br.Msg = "获取失败" br.ErrMsg = "获取框架失败, Err: " + e.Error() return } // 获取节点 nodeOb := new(aiPredictModel.AiPredictModelFrameworkNode) nodeCond := ` AND ai_predict_model_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 } // 模型标的map aiPredictModelIndexMap := make(map[int]*aiPredictModel.AiPredictModelIndex) { aiPredictModelIndexIdList := make([]interface{}, 0) for _, v := range nodes { if v.AiPredictModelIndexId > 0 { aiPredictModelIndexIdList = append(aiPredictModelIndexIdList, v.AiPredictModelIndexId) } } indexIdNum := len(aiPredictModelIndexIdList) if indexIdNum > 0 { indexObj := aiPredictModel.AiPredictModelIndex{} indexList, e := indexObj.GetItemsByCondition(` AND ai_predict_model_index_id in (`+utils.GetOrmInReplace(indexIdNum)+`)`, aiPredictModelIndexIdList, []string{}, "") if e != nil { if e.Error() == utils.ErrNoRow() { br.Msg = "框架不存在, 请刷新页面" return } br.Msg = "获取失败" br.ErrMsg = "获取标的列表失败, Err: " + e.Error() return } for _, v := range indexList { aiPredictModelIndexMap[v.AiPredictModelIndexId] = v } } } // 格式化响应数据 itemNodes := make([]*aiPredictModel.AiPredictModelFrameworkNodeItem, 0) for _, v := range nodes { if v.NodeId == "" { continue } num := 1 itemNodes = append(itemNodes, aiPredictModel.FormatAiPredictModelFrameworkNode2Item(v, num, aiPredictModelIndexMap)) } detail := aiPredictModel.FormatAiPredictModelFramework2Item(item, itemNodes) br.Data = detail br.Ret = 200 br.Success = true br.Msg = "操作成功" }