package ai import ( "encoding/json" "eta_gn/eta_api/models" "eta_gn/eta_api/models/ai_summary" "eta_gn/eta_api/models/aimod" "eta_gn/eta_api/models/sandbox" saModel "eta_gn/eta_api/models/semantic_analysis" aiSummaryService "eta_gn/eta_api/services/ai_summary" "eta_gn/eta_api/services/aiser" "eta_gn/eta_api/utils" "fmt" "strconv" "time" "github.com/rdlucklib/rdluck_tools/paging" ) // @router /summary/classify/list [get] func (this *AiController) AiSummaryClassifyItems() { br := new(models.BaseResponse).Init() defer func() { this.Data["json"] = br this.ServeJSON() }() resp := new(ai_summary.AiSummaryClassifyListResp) aiSummaryClassifyId, _ := this.GetInt("AiSummaryClassifyId") isShowMe, _ := this.GetBool("IsShowMe") if isShowMe { errMsg, err := aiSummaryService.GetAiSummaryClassifyListForMe(*this.SysUser, resp, aiSummaryClassifyId) if err != nil { br.Msg = errMsg br.ErrMsg = err.Error() return } br.Ret = 200 br.Success = true br.Msg = "获取成功" br.Data = resp fmt.Println("source my classify") return } rootList, err := ai_summary.GetAiSummaryClassifyAndInfoByParentId(aiSummaryClassifyId) if err != nil && !utils.IsErrNoRow(err) { br.Msg = "获取失败" br.ErrMsg = "获取数据失败,Err:" + err.Error() return } for k := range rootList { rootList[k].UniqueCode = strconv.Itoa(rootList[k].AiSummaryClassifyId) + "_" + strconv.Itoa(rootList[k].AiSummaryId) } classifyAll, err := ai_summary.GetAiSummaryClassifyAndInfoByParentId(aiSummaryClassifyId) if err != nil && !utils.IsErrNoRow(err) { br.Msg = "获取失败" br.ErrMsg = "获取数据失败,Err:" + err.Error() return } for k := range classifyAll { classifyAll[k].UniqueCode = strconv.Itoa(classifyAll[k].AiSummaryClassifyId) + "_" + strconv.Itoa(classifyAll[k].AiSummaryId) } nodeAll := make([]*ai_summary.AiSummaryClassifyItems, 0) for k := range rootList { rootNode := rootList[k] aiSummaryService.AiSummaryClassifyItemsMakeTreeV2(this.SysUser, classifyAll, rootNode) nodeAll = append(nodeAll, rootNode) } resp.AllNodes = nodeAll br.Ret = 200 br.Success = true br.Msg = "获取成功" br.Data = resp } // @router /summary/classify/add [post] func (this *AiController) AddAiSummaryClassify() { br := new(models.BaseResponse).Init() defer func() { this.Data["json"] = br this.ServeJSON() }() var req ai_summary.AddAiSummaryClassifyReq err := json.Unmarshal(this.Ctx.Input.RequestBody, &req) if err != nil { br.Msg = "参数解析异常!" br.ErrMsg = "参数解析失败,Err:" + err.Error() return } if req.ClassifyName == "" { br.Msg = "请输入分类名称" br.IsSendEmail = false return } if req.ParentId < 0 { br.Msg = "参数错误" br.IsSendEmail = false return } count, err := ai_summary.GetAiSummaryClassifyCount(req.ClassifyName, req.ParentId) if err != nil { br.Msg = "判断名称是否已存在失败" br.ErrMsg = "判断名称是否已存在失败,Err:" + err.Error() return } if count > 0 { br.Msg = "分类名称已存在,请重新输入" br.IsSendEmail = false return } maxSort, err := ai_summary.GetAiSummaryClassifyMaxSort(req.ParentId) classify := new(ai_summary.AiSummaryClassify) classify.ParentId = req.ParentId classify.ClassifyName = req.ClassifyName classify.HasData = 0 classify.CreateTime = time.Now() classify.ModifyTime = time.Now() classify.SysUserId = this.SysUser.AdminId classify.SysUserRealName = this.SysUser.RealName classify.Level = req.Level + 1 classify.Sort = maxSort + 1 _, err = ai_summary.AddAiSummaryClassify(classify) if err != nil { br.Msg = "保存分类失败" br.ErrMsg = "保存分类失败,Err:" + err.Error() return } br.Ret = 200 br.Msg = "保存成功" br.Success = true } // @router /summary/classify/edit [post] func (this *AiController) EditAiSummaryClassify() { br := new(models.BaseResponse).Init() defer func() { this.Data["json"] = br this.ServeJSON() }() var req ai_summary.EditAiSummaryClassifyReq err := json.Unmarshal(this.Ctx.Input.RequestBody, &req) if err != nil { br.Msg = "参数解析异常!" br.ErrMsg = "参数解析失败,Err:" + err.Error() return } if req.ClassifyName == "" { br.Msg = "请输入分类名称" br.IsSendEmail = false return } if req.AiSummaryClassifyId <= 0 { br.Msg = "参数错误" br.IsSendEmail = false return } err = ai_summary.EditSandboxClassify(req.AiSummaryClassifyId, req.ClassifyName) if err != nil { br.Msg = "保存失败" br.ErrMsg = "保存失败,Err:" + err.Error() return } br.Ret = 200 br.Msg = "保存成功" br.Success = true br.IsAddLog = true } // @router /summary/classify/delete/check [post] func (this *AiController) DeleteSandboxClassifyCheck() { br := new(models.BaseResponse).Init() defer func() { this.Data["json"] = br this.ServeJSON() }() var req ai_summary.AiSummaryClassifyDeleteCheckReq err := json.Unmarshal(this.Ctx.Input.RequestBody, &req) if err != nil { br.Msg = "参数解析异常!" br.ErrMsg = "参数解析失败,Err:" + err.Error() return } if req.AiSummaryClassifyId < 0 { br.Msg = "参数错误" br.IsSendEmail = false return } var deleteStatus int var tipsMsg string if req.AiSummaryClassifyId > 0 { count, err := ai_summary.GetAiSummaryInfoCountByClassifyId(req.AiSummaryClassifyId) if err != nil { br.Msg = "删除失败" br.ErrMsg = "分类下是否含有指标失败,Err:" + err.Error() return } if count > 0 { deleteStatus = 1 tipsMsg = "该分类下关联沙盘不可删除" } } if deleteStatus != 1 { classifyCount, err := ai_summary.GetAiSummaryInfoCountByClassifyId(req.AiSummaryClassifyId) if err != nil && !utils.IsErrNoRow(err) { br.Msg = "删除失败" br.ErrMsg = "分类下是否含有沙盘失败,Err:" + err.Error() return } if classifyCount > 0 { deleteStatus = 2 tipsMsg = "确认删除当前目录及包含的子目录吗" } } if deleteStatus == 0 { tipsMsg = "可删除,进行删除操作" } resp := new(sandbox.SandboxClassifyDeleteCheckResp) resp.DeleteStatus = deleteStatus resp.TipsMsg = tipsMsg br.Ret = 200 br.Msg = "检测成功" br.Success = true br.Data = resp } // @router /summary/classify/delete [post] func (this *AiController) DeleteAiSummaryClassify() { br := new(models.BaseResponse).Init() defer func() { 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 ai_summary.DeleteAiSummaryClassifyReq err := json.Unmarshal(this.Ctx.Input.RequestBody, &req) if err != nil { br.Msg = "参数解析异常!" br.ErrMsg = "参数解析失败,Err:" + err.Error() return } if req.AiSummaryClassifyId < 0 && req.AiSummaryId <= 0 { br.Msg = "参数错误" br.IsSendEmail = false return } if req.AiSummaryClassifyId > 0 && req.AiSummaryId == 0 { childIds := make([]int, 0) classifyAll, err := ai_summary.GetAiSummaryClassifyAll() if err != nil && !utils.IsErrNoRow(err) { br.Msg = "获取失败" br.ErrMsg = "获取数据失败,Err:" + err.Error() return } classifyAllMap := make(map[int]*ai_summary.AiSummaryClassifyItems) for _, v := range classifyAll { classifyAllMap[v.AiSummaryClassifyId] = v } aiSummaryService.GetSummaryClassifyChildIds(req.AiSummaryClassifyId, classifyAllMap, &childIds) condition := `` pars := make([]interface{}, 0) if len(childIds) > 0 { ids := utils.IntArr2joinString(childIds, ",") condition = ` AND classify_id IN (` + ids + `) ` } else { condition = ` AND classify_id = ? ` pars = append(pars, req.AiSummaryClassifyId) } count, err := ai_summary.GetSummaryCountByCondition(condition, pars) if err != nil { br.Msg = "查询失败" br.ErrMsg = "判断分类下是否有纪要失败,Err:" + err.Error() return } if count > 0 { br.Msg = "该目录下存在纪要,不可删除" br.IsSendEmail = false return } childIds = append(childIds, req.AiSummaryClassifyId) err = ai_summary.DeleteAiSummaryClassifyByIds(childIds) if err != nil { br.Msg = "删除失败" br.ErrMsg = "删除失败,Err:" + err.Error() return } } if req.AiSummaryId > 0 { summaryInfo, err := ai_summary.GetAiSummaryById(req.AiSummaryId) if err != nil { if utils.IsErrNoRow(err) { br.Msg = "纪要已删除,请刷新页面" br.ErrMsg = "指标不存在,Err:" + err.Error() return } else { br.Msg = "删除失败" br.ErrMsg = "删除失败,获取指标信息失败,Err:" + err.Error() return } } if summaryInfo == nil { br.Msg = "纪要已删除,请刷新页面" return } if summaryInfo.SysUserId != sysUser.AdminId { br.Msg = "仅纪要创建人可以删除" return } err = ai_summary.DelAiSummaryById(req.AiSummaryId) if err != nil { br.Msg = err.Error() return } } br.Ret = 200 br.Msg = "删除成功" br.Success = true br.IsAddLog = true } // @router /summary/classify/move [post] func (this *AiController) AiSummaryClassifyMove() { br := new(models.BaseResponse).Init() defer func() { 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 ai_summary.MoveAiSummaryClassifyReq err := json.Unmarshal(this.Ctx.Input.RequestBody, &req) if err != nil { br.Msg = "参数解析异常!" br.ErrMsg = "参数解析失败,Err:" + err.Error() return } if req.AiSummaryClassifyId <= 0 { br.Msg = "参数错误" br.ErrMsg = "分类id小于等于0" return } aiSummaryClassifyInfo, err := ai_summary.GetAiSummaryClassifyById(req.AiSummaryClassifyId) if err != nil { br.Msg = "移动失败" br.ErrMsg = "获取分类信息失败,Err:" + err.Error() return } updateCol := make([]string, 0) if req.AiSummaryId > 0 { count, _ := ai_summary.GetAiSummaryClassifyCountById(req.AiSummaryClassifyId) if count <= 0 { br.Msg = "分类已被删除,不可移动,请刷新页面" return } aiSummaryInfo, err := ai_summary.GetAiSummaryById(req.AiSummaryId) if err != nil { br.Msg = "移动失败" br.ErrMsg = "获取沙盘信息失败,Err:" + err.Error() return } if aiSummaryInfo.ClassifyId != req.ParentClassifyId { err = ai_summary.MoveAiSummary(req.AiSummaryId, req.ParentClassifyId) if err != nil { br.Msg = "移动失败" br.ErrMsg = "移动失败,Err:" + err.Error() return } } updateCol := make([]string, 0) if req.PrevId > 0 { if req.PrevType == 1 { prevClassify, err := ai_summary.GetAiSummaryClassifyById(req.PrevId) if err != nil { br.Msg = "移动失败" br.ErrMsg = "获取上一个兄弟节点分类信息失败,Err:" + err.Error() return } if req.NextId > 0 { if req.NextType == 1 { nextClassify, err := ai_summary.GetAiSummaryClassifyById(req.NextId) if err != nil { br.Msg = "移动失败" br.ErrMsg = "获取下一个兄弟节点分类信息失败,Err:" + err.Error() return } if prevClassify.Sort == nextClassify.Sort || prevClassify.Sort == aiSummaryClassifyInfo.Sort { updateSortStr := `sort + 2` _ = ai_summary.UpdateAiSummaryClassifySortByParentId(prevClassify.ParentId, prevClassify.AiSummaryClassifyId, prevClassify.Sort, updateSortStr) _ = ai_summary.UpdateAiSummarySortByClassifyId(prevClassify.AiSummaryClassifyId, prevClassify.Sort, 0, updateSortStr) } else { if nextClassify.Sort-prevClassify.Sort == 1 { updateSortStr := `sort + 1` _ = ai_summary.UpdateAiSummaryClassifySortByParentId(prevClassify.ParentId, 0, prevClassify.Sort, updateSortStr) _ = ai_summary.UpdateAiSummarySortByClassifyId(prevClassify.AiSummaryClassifyId, prevClassify.Sort, 0, updateSortStr) } } } else { nextChartInfo, err := ai_summary.GetAiSummaryById(req.NextId) if err != nil { br.Msg = "移动失败" br.ErrMsg = "获取下一个兄弟节点分类信息失败,Err:" + err.Error() return } if prevClassify.Sort == nextChartInfo.Sort || prevClassify.Sort == aiSummaryInfo.Sort { updateSortStr := `sort + 2` _ = ai_summary.UpdateAiSummaryClassifySortByParentId(prevClassify.ParentId, prevClassify.AiSummaryClassifyId, prevClassify.Sort, updateSortStr) _ = ai_summary.UpdateAiSummarySortByClassifyId(prevClassify.AiSummaryClassifyId, prevClassify.Sort, 0, updateSortStr) } else { if nextChartInfo.Sort-prevClassify.Sort == 1 { updateSortStr := `sort + 1` _ = ai_summary.UpdateAiSummaryClassifySortByParentId(prevClassify.ParentId, 0, prevClassify.AiSummaryClassifyId, updateSortStr) _ = ai_summary.UpdateAiSummarySortByClassifyId(prevClassify.AiSummaryClassifyId, prevClassify.Sort, 0, updateSortStr) } } } } aiSummaryInfo.Sort = prevClassify.Sort + 1 aiSummaryInfo.ModifyTime = time.Now() updateCol = append(updateCol, "Sort", "ModifyTime") } else { prevAiSumary, err := ai_summary.GetAiSummaryById(req.PrevId) if err != nil { br.Msg = "移动失败" br.ErrMsg = "获取上一个兄弟节点分类信息失败,Err:" + err.Error() return } if req.NextId > 0 { if req.NextType == 1 { nextClassify, err := ai_summary.GetAiSummaryClassifyById(req.NextId) if err != nil { br.Msg = "移动失败" br.ErrMsg = "获取下一个兄弟节点分类信息失败,Err:" + err.Error() return } if prevAiSumary.Sort == nextClassify.Sort || prevAiSumary.Sort == aiSummaryClassifyInfo.Sort { updateSortStr := `sort + 2` _ = ai_summary.UpdateAiSummaryClassifySortByParentId(prevAiSumary.ClassifyId, 0, prevAiSumary.Sort, updateSortStr) _ = ai_summary.UpdateAiSummarySortByClassifyId(prevAiSumary.ClassifyId, prevAiSumary.Sort, prevAiSumary.AiSummaryId, updateSortStr) } else { if nextClassify.Sort-prevAiSumary.Sort == 1 { updateSortStr := `sort + 1` _ = ai_summary.UpdateAiSummaryClassifySortByParentId(prevAiSumary.ClassifyId, 0, prevAiSumary.Sort, updateSortStr) _ = ai_summary.UpdateAiSummarySortByClassifyId(prevAiSumary.ClassifyId, prevAiSumary.Sort, prevAiSumary.AiSummaryId, updateSortStr) } } } else { nextChartInfo, err := ai_summary.GetAiSummaryById(req.NextId) if err != nil { br.Msg = "移动失败" br.ErrMsg = "获取下一个兄弟节点分类信息失败,Err:" + err.Error() return } if prevAiSumary.Sort == nextChartInfo.Sort || prevAiSumary.Sort == aiSummaryInfo.Sort { updateSortStr := `sort + 2` _ = ai_summary.UpdateAiSummaryClassifySortByParentId(prevAiSumary.ClassifyId, 0, prevAiSumary.Sort, updateSortStr) _ = ai_summary.UpdateAiSummarySortByClassifyId(prevAiSumary.ClassifyId, prevAiSumary.Sort, prevAiSumary.AiSummaryId, updateSortStr) } else { if nextChartInfo.Sort-prevAiSumary.Sort == 1 { updateSortStr := `sort + 1` _ = ai_summary.UpdateAiSummaryClassifySortByParentId(prevAiSumary.ClassifyId, 0, prevAiSumary.Sort, updateSortStr) _ = ai_summary.UpdateAiSummarySortByClassifyId(prevAiSumary.ClassifyId, prevAiSumary.Sort, prevAiSumary.AiSummaryId, updateSortStr) } } } } aiSummaryInfo.Sort = prevAiSumary.Sort + 1 aiSummaryInfo.ModifyTime = time.Now() updateCol = append(updateCol, "Sort", "ModifyTime") } } else { firstClassify, err := ai_summary.GetFirstAiSummaryByClassifyId(req.AiSummaryClassifyId) if err != nil && !utils.IsErrNoRow(err) { br.Msg = "移动失败" br.ErrMsg = "获取获取当前父级分类下的排序第一条的分类信息失败,Err:" + err.Error() return } if firstClassify != nil && firstClassify.ClassifyId > 0 && firstClassify.Sort == 0 { updateSortStr := ` sort + 1 ` _ = ai_summary.UpdateAiSummarySortByClassifyId(firstClassify.ClassifyId, 0, firstClassify.AiSummaryId-1, updateSortStr) } aiSummaryInfo.Sort = 0 //那就是排在第一位 aiSummaryInfo.ModifyTime = time.Now() updateCol = append(updateCol, "Sort", "ModifyTime") } if len(updateCol) > 0 { err = aiSummaryInfo.Update(updateCol) if err != nil { br.Msg = "移动失败" br.ErrMsg = "修改失败,Err:" + err.Error() return } } } else { if aiSummaryClassifyInfo.ParentId != req.ParentClassifyId && req.ParentClassifyId != 0 { parentChartClassifyInfo, err := ai_summary.GetAiSummaryClassifyById(req.ParentClassifyId) if err != nil { br.Msg = "移动失败" br.ErrMsg = "获取上级分类信息失败,Err:" + err.Error() return } aiSummaryClassifyInfo.ParentId = parentChartClassifyInfo.AiSummaryClassifyId aiSummaryClassifyInfo.Level = parentChartClassifyInfo.Level + 1 aiSummaryClassifyInfo.ModifyTime = time.Now() updateCol = append(updateCol, "ParentId", "Level", "ModifyTime") } else if aiSummaryClassifyInfo.ParentId != req.ParentClassifyId && req.ParentClassifyId == 0 { aiSummaryClassifyInfo.ParentId = req.ParentClassifyId aiSummaryClassifyInfo.Level = 1 aiSummaryClassifyInfo.ModifyTime = time.Now() updateCol = append(updateCol, "ParentId", "Level", "ModifyTime") } if req.PrevId > 0 { if req.PrevType == 1 { prevClassify, err := ai_summary.GetAiSummaryClassifyById(req.PrevId) if err != nil { br.Msg = "移动失败" br.ErrMsg = "获取上一个兄弟节点分类信息失败,Err:" + err.Error() return } if req.NextId > 0 { if req.NextType == 1 { nextClassify, err := ai_summary.GetAiSummaryClassifyById(req.NextId) if err != nil { br.Msg = "移动失败" br.ErrMsg = "获取下一个兄弟节点分类信息失败,Err:" + err.Error() return } if prevClassify.Sort == nextClassify.Sort || prevClassify.Sort == aiSummaryClassifyInfo.Sort { updateSortStr := `sort + 2` _ = ai_summary.UpdateAiSummaryClassifySortByParentId(prevClassify.ParentId, prevClassify.AiSummaryClassifyId, prevClassify.Sort, updateSortStr) _ = ai_summary.UpdateAiSummarySortByClassifyId(prevClassify.AiSummaryClassifyId, prevClassify.Sort, 0, updateSortStr) } else { if nextClassify.Sort-prevClassify.Sort == 1 { updateSortStr := `sort + 1` _ = ai_summary.UpdateAiSummaryClassifySortByParentId(prevClassify.ParentId, 0, prevClassify.Sort, updateSortStr) _ = ai_summary.UpdateAiSummarySortByClassifyId(prevClassify.AiSummaryClassifyId, prevClassify.Sort, 0, updateSortStr) } } } else { nextChartInfo, err := ai_summary.GetAiSummaryById(req.NextId) if err != nil { br.Msg = "移动失败" br.ErrMsg = "获取下一个兄弟节点分类信息失败,Err:" + err.Error() return } if prevClassify.Sort == nextChartInfo.Sort || prevClassify.Sort == aiSummaryClassifyInfo.Sort { updateSortStr := `sort + 2` _ = ai_summary.UpdateAiSummaryClassifySortByParentId(prevClassify.ParentId, prevClassify.AiSummaryClassifyId, prevClassify.Sort, updateSortStr) _ = ai_summary.UpdateAiSummarySortByClassifyId(prevClassify.AiSummaryClassifyId, prevClassify.Sort, 0, updateSortStr) } else { if nextChartInfo.Sort-prevClassify.Sort == 1 { updateSortStr := `sort + 1` _ = ai_summary.UpdateAiSummaryClassifySortByParentId(prevClassify.ParentId, 0, prevClassify.AiSummaryClassifyId, updateSortStr) _ = ai_summary.UpdateAiSummarySortByClassifyId(prevClassify.AiSummaryClassifyId, prevClassify.Sort, 0, updateSortStr) } } } } aiSummaryClassifyInfo.Sort = prevClassify.Sort + 1 aiSummaryClassifyInfo.ModifyTime = time.Now() updateCol = append(updateCol, "Sort", "ModifyTime") } else { prevAiSumary, err := ai_summary.GetAiSummaryById(req.PrevId) if err != nil { br.Msg = "移动失败" br.ErrMsg = "获取上一个兄弟节点分类信息失败,Err:" + err.Error() return } if req.NextId > 0 { if req.NextType == 1 { nextClassify, err := ai_summary.GetAiSummaryClassifyById(req.NextId) if err != nil { br.Msg = "移动失败" br.ErrMsg = "获取下一个兄弟节点分类信息失败,Err:" + err.Error() return } if prevAiSumary.Sort == nextClassify.Sort || prevAiSumary.Sort == aiSummaryClassifyInfo.Sort { updateSortStr := `sort + 2` _ = ai_summary.UpdateAiSummaryClassifySortByParentId(prevAiSumary.ClassifyId, 0, prevAiSumary.Sort, updateSortStr) _ = ai_summary.UpdateAiSummarySortByClassifyId(prevAiSumary.ClassifyId, prevAiSumary.Sort, prevAiSumary.AiSummaryId, updateSortStr) } else { if nextClassify.Sort-prevAiSumary.Sort == 1 { updateSortStr := `sort + 1` _ = ai_summary.UpdateAiSummaryClassifySortByParentId(prevAiSumary.ClassifyId, 0, prevAiSumary.Sort, updateSortStr) _ = ai_summary.UpdateAiSummarySortByClassifyId(prevAiSumary.ClassifyId, prevAiSumary.Sort, prevAiSumary.AiSummaryId, updateSortStr) } } } else { nextChartInfo, err := ai_summary.GetAiSummaryById(req.NextId) if err != nil { br.Msg = "移动失败" br.ErrMsg = "获取下一个兄弟节点分类信息失败,Err:" + err.Error() return } if prevAiSumary.Sort == nextChartInfo.Sort || prevAiSumary.Sort == aiSummaryClassifyInfo.Sort { updateSortStr := `sort + 2` _ = ai_summary.UpdateAiSummaryClassifySortByParentId(prevAiSumary.ClassifyId, 0, prevAiSumary.Sort, updateSortStr) _ = ai_summary.UpdateAiSummarySortByClassifyId(prevAiSumary.ClassifyId, prevAiSumary.Sort, prevAiSumary.AiSummaryId, updateSortStr) } else { if nextChartInfo.Sort-prevAiSumary.Sort == 1 { updateSortStr := `sort + 1` _ = ai_summary.UpdateAiSummaryClassifySortByParentId(prevAiSumary.ClassifyId, 0, prevAiSumary.Sort, updateSortStr) _ = ai_summary.UpdateAiSummarySortByClassifyId(prevAiSumary.ClassifyId, prevAiSumary.Sort, prevAiSumary.AiSummaryId, updateSortStr) } } } } aiSummaryClassifyInfo.Sort = prevAiSumary.Sort + 1 aiSummaryClassifyInfo.ModifyTime = time.Now() updateCol = append(updateCol, "Sort", "ModifyTime") } } else { firstClassify, err := ai_summary.GetFirstAiSummaryClassifyByParentId(aiSummaryClassifyInfo.ParentId) if err != nil && !utils.IsErrNoRow(err) { br.Msg = "移动失败" br.ErrMsg = "获取获取当前父级分类下的排序第一条的分类信息失败,Err:" + err.Error() return } if firstClassify != nil && firstClassify.AiSummaryClassifyId > 0 && firstClassify.Sort == 0 { updateSortStr := ` sort + 1 ` _ = ai_summary.UpdateAiSummaryClassifySortByParentId(firstClassify.ParentId, firstClassify.AiSummaryClassifyId-1, 0, updateSortStr) } aiSummaryClassifyInfo.Sort = 0 //那就是排在第一位 aiSummaryClassifyInfo.ModifyTime = time.Now() updateCol = append(updateCol, "Sort", "ModifyTime") } if len(updateCol) > 0 { err = aiSummaryClassifyInfo.Update(updateCol) if err != nil { br.Msg = "移动失败" br.ErrMsg = "修改失败,Err:" + err.Error() return } } } br.Ret = 200 br.Success = true br.Msg = "移动成功" } // @router /summary/list [get] func (this *AiController) AiSummaryList() { br := new(models.BaseResponse).Init() defer func() { this.Data["json"] = br this.ServeJSON() }() sysUser := this.SysUser if sysUser == nil { br.Msg = "请登录" br.ErrMsg = "请登录,SysUser Is Empty" br.Ret = 408 return } aiSummaryClassifyId, _ := this.GetInt("AiSummaryClassifyId") pageSize, _ := this.GetInt("PageSize") currentIndex, _ := this.GetInt("CurrentIndex") keyWord := this.GetString("KeyWord") var total int page := paging.GetPaging(currentIndex, pageSize, total) var startSize int if pageSize <= 0 { pageSize = utils.PageSize20 } if currentIndex <= 0 { currentIndex = 1 } startSize = paging.StartIndex(currentIndex, pageSize) classifyAll, err := ai_summary.GetAiSummaryClassifyAll() if err != nil && !utils.IsErrNoRow(err) { br.Msg = "获取失败" br.ErrMsg = "获取数据失败,Err:" + err.Error() return } classifyAllMap := make(map[int]*ai_summary.AiSummaryClassifyItems) for _, v := range classifyAll { classifyAllMap[v.AiSummaryClassifyId] = v } var condition string var pars []interface{} if aiSummaryClassifyId > 0 { sandboxClassifyIds, err := ai_summary.GetAiSummaryClassify(aiSummaryClassifyId) if err != nil && !utils.IsErrNoRow(err) { br.Msg = "获取图表信息失败" br.ErrMsg = "获取信息失败,GetChartClassify,Err:" + err.Error() return } condition += " AND ai_summary_classify_id IN(" + sandboxClassifyIds + ") " } if keyWord != "" { condition += ` AND ( title LIKE '%` + keyWord + `%' )` } isShowMe, _ := this.GetBool("IsShowMe") if isShowMe { condition += ` AND sys_user_id = ? ` pars = append(pars, sysUser.AdminId) } list, err := ai_summary.GetAiSummaryListByCondition(condition, pars, startSize, pageSize) if err != nil && !utils.IsErrNoRow(err) { br.Success = true br.Msg = "获取纪要信息失败" br.ErrMsg = "获取纪要信息失败,Err:" + err.Error() return } for i, v := range list { parentIds := make([]int, 0) aiSummaryService.GetSummaryParentIds(v.ClassifyId, classifyAllMap, &parentIds) ids := utils.IntArr2joinString(parentIds, ",") list[i].ParentIds = ids } resp := new(ai_summary.AiSummaryListResp) if list == nil || len(list) <= 0 || (err != nil && utils.IsErrNoRow(err)) { items := make([]*ai_summary.AiSummaryItems, 0) resp.Paging = page resp.List = items br.Ret = 200 br.Success = true br.Msg = "获取成功" return } dataCount, err := ai_summary.GetAiSummaryListCountByCondition(condition, pars) if err != nil && !utils.IsErrNoRow(err) { br.Msg = "获取指标信息失败" br.ErrMsg = "获取指标数据总数失败,Err:" + err.Error() return } page = paging.GetPaging(currentIndex, pageSize, dataCount) resp.Paging = page resp.List = list br.Ret = 200 br.Success = true br.Msg = "获取成功" br.Data = resp } // @router /summary/detail [get] func (this *AiController) AiSummaryDetail() { br := new(models.BaseResponse).Init() defer func() { this.Data["json"] = br this.ServeJSON() }() sysUser := this.SysUser if sysUser == nil { br.Msg = "请登录" br.ErrMsg = "请登录,SysUser Is Empty" br.Ret = 408 return } aiSummaryId, _ := this.GetInt("AiSummaryId") detail, err := ai_summary.GetAiSummaryItemById(aiSummaryId) if err != nil && !utils.IsErrNoRow(err) { br.Success = true br.Msg = "获取沙盘信息失败" br.ErrMsg = "获取沙盘信息失败,Err:" + err.Error() return } resp := ai_summary.AiSummaryDetailResp{ AiSummaryItems: detail, } if detail.SaDocId > 0 { item := new(saModel.SaDoc) e := item.GetItemById(detail.SaDocId) if e != nil { if utils.IsErrNoRow(e) { br.Msg = "文档已被删除, 请刷新页面" return } br.Msg = "获取失败" br.ErrMsg = "获取文档信息失败, Err: " + e.Error() return } resp.SaDocTitle = item.Title resp.SaDocClassifyId = item.ClassifyId } br.Data = resp br.Ret = 200 br.Success = true br.Msg = "获取成功" } // @router /summary/add [post] func (this *AiController) AddAiSummary() { br := new(models.BaseResponse).Init() defer func() { this.Data["json"] = br this.ServeJSON() }() var req ai_summary.AddAiSummaryReq err := json.Unmarshal(this.Ctx.Input.RequestBody, &req) if err != nil { br.Msg = "参数解析异常!" br.ErrMsg = "参数解析失败,Err:" + err.Error() return } if req.Title == "" { br.Msg = "请输入名称" br.IsSendEmail = false return } if req.SummaryContent == "" { br.Msg = "纪要内容为空" br.IsSendEmail = false return } if req.ClassifyId < 0 { br.Msg = "分类id错误" br.IsSendEmail = false return } maxSort, err := ai_summary.GetAiSummaryMaxSort(req.ClassifyId) summary := &ai_summary.AiSummary{ SaDocId: req.SaDocId, OriginContent: req.OriginContent, SummaryContent: req.SummaryContent, ClassifyId: req.ClassifyId, SysUserId: this.SysUser.AdminId, SysUserRealName: this.SysUser.RealName, Title: req.Title, CreateTime: time.Now(), ModifyTime: time.Now(), OpenaiFileName: req.OpenaiFileName, OpenaiFilePath: req.OpenaiFilePath, OriginTitle: req.OriginTitle, Sort: maxSort + 1, } id, err := ai_summary.AddAiSummary(summary) if err != nil { br.Msg = "保存分类失败" br.ErrMsg = "保存分类失败,Err:" + err.Error() return } classify, err := ai_summary.GetAiSummaryClassifyById(req.ClassifyId) if err != nil { br.Msg = "获取分类信息失败" br.ErrMsg = "获取分类信息失败,Err:" + err.Error() return } resp := ai_summary.AiSummaryAddResp{ AiSummaryId: int(id), UniqueCode: strconv.Itoa(req.ClassifyId) + "_" + strconv.Itoa(int(id)), ParentId: classify.ParentId, } br.Ret = 200 br.Msg = "保存成功" br.Success = true br.Data = resp } // @router /summary/generate [post] func (this *AiController) GenerateAiSummary() { br := new(models.BaseResponse).Init() defer func() { this.Data["json"] = br this.ServeJSON() }() var req ai_summary.GenerateAiSummaryReq err := json.Unmarshal(this.Ctx.Input.RequestBody, &req) if err != nil { br.Msg = "参数解析异常!" br.ErrMsg = "参数解析失败,Err:" + err.Error() return } req.Prompt, err = aiser.FormatAskContent(req.Prompt) if err != nil { br.Msg = "提示词错误" br.ErrMsg = "提示词错误,FormatAskContent,Err:" + err.Error() return } if req.Prompt == "" { br.Msg = "请输入提示词" br.IsSendEmail = false return } req.OriginContent, err = aiser.FormatAskContent(req.OriginContent) if err != nil { br.Msg = "文档内容错误" br.ErrMsg = "文档内容错误,FormatAskContent,Err:" + err.Error() return } if req.OriginContent == "" && len(req.OpenaiFileId) == 0 && req.SaDocId <= 0 { br.Msg = "请选择文档或输入内容" br.IsSendEmail = false return } if req.SaDocId > 0 { // 获取段落 sectionOB := new(saModel.SaDocSection) sectionCond := fmt.Sprintf(` AND %s = ?`, saModel.SaDocSectionColumns.DocId) sectionPars := make([]interface{}, 0) sectionPars = append(sectionPars, req.SaDocId) sectionList, e := sectionOB.GetItemsByCondition(sectionCond, sectionPars, []string{}, "") if e != nil { br.Msg = "获取失败" br.ErrMsg = "获取段落信息失败, Err: " + e.Error() return } secIds := make([]int, 0) for _, s := range sectionList { secIds = append(secIds, s.SaDocSectionId) } for _, v := range sectionList { req.OriginContent += v.Content + "
" } } if utils.Re == nil { key := "CACHE_CHAT_" + strconv.Itoa(this.SysUser.AdminId) cacheVal, err := utils.Rc.RedisInt(key) fmt.Println("RedisString:", cacheVal, "err:", err) if err != nil && (err.Error() != "redigo: nil returned" && err.Error() != "redis: nil") { br.Msg = "获取数据失败!" br.ErrMsg = "获取数据失败,Err:" + err.Error() return } putVal := 0 if cacheVal <= 0 { putVal = utils.AiChatLimit } else { putVal = cacheVal - 1 } if putVal <= 0 { br.Msg = "您今日" + strconv.Itoa(utils.AiChatLimit) + "次问答已达上限,请明天再来!" br.ErrMsg = "您今日" + strconv.Itoa(utils.AiChatLimit) + "次问答已达上限,请明天再来!" return } lastSecond := utils.GetTodayLastSecond() utils.Rc.Put(key, putVal, lastSecond) } resp := new(aimod.ChatResp) if req.OriginContent != "" { ask := req.Prompt + req.OriginContent var answer string historyList, err := aimod.GetAiChatList(req.AiChatTopicId) if err != nil && !utils.IsErrNoRow(err) { br.Msg = "获取主题历史数据失败!" br.ErrMsg = "获取主题历史数据失败,Err:" + err.Error() return } historyChatList := make([]aimod.HistoryChat, 0) for _, v := range historyList { historyChat := new(aimod.HistoryChat) historyChat.Ask = v.Ask historyChat.Answer = v.Answer historyChatList = append(historyChatList, *historyChat) } answer, err = aiser.ChatAutoMsg(ask, historyChatList, req.Model) if err != nil { br.Msg = "获取数据失败!" br.ErrMsg = "获取数据失败,ChatAutoMsg,Err:" + err.Error() return } resp.Ask = ask resp.Answer = answer resp.Model = req.Model if req.AiChatTopicId <= 0 { //新增 name, err := aiser.QueryAskContent(ask) if err != nil { br.Msg = "获取数据失败!" br.ErrMsg = "获取数据失败,QueryAskContent,Err:" + err.Error() return } topic := new(aimod.AiChatTopic) topic.TopicName = name topic.SysUserId = this.SysUser.AdminId topic.SysUserRealName = this.SysUser.RealName topic.CreateTime = time.Now() topic.ModifyTime = time.Now() topicId, err := aimod.AddAiChatTopic(topic) if err != nil { br.Msg = "获取数据失败!" br.ErrMsg = "生成话题失败,Err:" + err.Error() return } resp.AiChatTopicId = int(topicId) chatItem := new(aimod.AiChat) chatItem.AiChatTopicId = resp.AiChatTopicId chatItem.Ask = ask chatItem.AskUuid = utils.MD5(ask) chatItem.Answer = answer chatItem.Model = EnabledModelsForMap[req.Model] chatItem.SysUserId = this.SysUser.AdminId chatItem.SysUserRealName = this.SysUser.RealName chatItem.CreateTime = time.Now() chatItem.ModifyTime = time.Now() _, err = aimod.AddAiChat(chatItem) if err != nil { br.Msg = "获取数据失败!" br.ErrMsg = "生成话题记录失败,Err:" + err.Error() return } } else { resp.AiChatTopicId = req.AiChatTopicId chatItem := new(aimod.AiChat) chatItem.AiChatTopicId = resp.AiChatTopicId chatItem.Ask = ask chatItem.AskUuid = utils.MD5(ask) chatItem.Answer = answer chatItem.Model = EnabledModelsForMap[req.Model] chatItem.SysUserId = this.SysUser.AdminId chatItem.SysUserRealName = this.SysUser.RealName chatItem.CreateTime = time.Now() chatItem.ModifyTime = time.Now() _, err = aimod.AddAiChat(chatItem) if err != nil { br.Msg = "获取数据失败!" br.ErrMsg = "生成话题记录失败,Err:" + err.Error() return } } } else if len(req.OpenaiFileId) > 0 { ask := req.Prompt askUuid := utils.MD5(ask) var assistantId, threadId string if req.AiChatTopicId > 0 { aiChatTopicObj := new(aimod.AiChatTopic) aiChatTopicObj.AiChatTopicId = req.AiChatTopicId topic, err := aiChatTopicObj.GetAiChatTopicById() if err != nil { if utils.IsErrNoRow(err) { br.Msg = "获取数据失败!" br.ErrMsg = "获取数据失败,主题不存在,Err:" + err.Error() return } br.Msg = "获取数据失败!" br.ErrMsg = "获取数据失败,GetAiChatTopicById,Err:" + err.Error() return } assistantId = topic.AssistantId threadId = topic.ThreadId } var answer string frList := make([]aimod.HistoryChat, 0) tmpFileIdList := make([]string, 0) tmpFileIdList = append(tmpFileIdList, req.OpenaiFileId...) { frItem := new(aimod.HistoryChat) frItem.Ask = ask frItem.Answer = "" frItem.OpenaiFileId = tmpFileIdList frList = append(frList, *frItem) } fileRetrieveResp, err := aiser.FileRetrieve(assistantId, threadId, req.Model, frList, req.OpenaiFileId) if err != nil { br.Msg = "获取数据失败!" br.ErrMsg = "获取数据失败,FileRetrieve,Err:" + err.Error() return } if fileRetrieveResp != nil { if fileRetrieveResp.Ret == 200 { assistantId = fileRetrieveResp.Data.AssistantId threadId = fileRetrieveResp.Data.ThreadId answer = fileRetrieveResp.Data.Answer } else { br.Msg = "获取数据失败!" br.ErrMsg = fileRetrieveResp.Msg return } } if req.AiChatTopicId <= 0 { //新增 name, err := aiser.QueryAskContent(ask) if err != nil { br.Msg = "获取数据失败!" br.ErrMsg = "获取数据失败,QueryAskContent,Err:" + err.Error() return } topic := new(aimod.AiChatTopic) topic.TopicName = name topic.SysUserId = this.SysUser.AdminId topic.SysUserRealName = this.SysUser.RealName topic.CreateTime = time.Now() topic.ModifyTime = time.Now() topic.AssistantId = assistantId topic.ThreadId = threadId topicId, err := aimod.AddAiChatTopic(topic) if err != nil { br.Msg = "获取数据失败!" br.ErrMsg = "生成话题失败,Err:" + err.Error() return } chatItem := new(aimod.AiChat) chatItem.AiChatTopicId = int(topicId) chatItem.Ask = ask chatItem.AskUuid = utils.MD5(ask) chatItem.Answer = answer chatItem.Model = EnabledModelsForMap[req.Model] chatItem.SysUserId = this.SysUser.AdminId chatItem.SysUserRealName = this.SysUser.RealName chatItem.CreateTime = time.Now() chatItem.ModifyTime = time.Now() _, err = aimod.AddAiChat(chatItem) if err != nil { br.Msg = "获取数据失败!" br.ErrMsg = "生成话题记录失败,Err:" + err.Error() return } req.AiChatTopicId = int(topicId) } else { chatItem := new(aimod.AiChat) chatItem.AiChatTopicId = req.AiChatTopicId chatItem.Ask = ask chatItem.AskUuid = askUuid chatItem.Answer = answer chatItem.Model = EnabledModelsForMap[req.Model] chatItem.SysUserId = this.SysUser.AdminId chatItem.SysUserRealName = this.SysUser.RealName chatItem.CreateTime = time.Now() chatItem.ModifyTime = time.Now() _, err = aimod.AddAiChat(chatItem) if err != nil { br.Msg = "获取数据失败!" br.ErrMsg = "生成话题记录失败,Err:" + err.Error() return } aiChatTopicObj := new(aimod.AiChatTopic) err = aiChatTopicObj.UpdateAssistantAndThreadId(assistantId, threadId, req.AiChatTopicId) if err != nil { br.Msg = "获取失败!" br.ErrMsg = "修改助手标识失败,Err:" + err.Error() return } } resp.Model = aimod.ModelViewMap[req.Model] resp.AiChatTopicId = req.AiChatTopicId resp.Ask = ask resp.Answer = answer } br.Ret = 200 br.Success = true br.Msg = "获取成功" br.Data = resp } // @router /summary/classifyList [get] func (this *AiController) AiSummaryClassifyList() { br := new(models.BaseResponse).Init() defer func() { this.Data["json"] = br this.ServeJSON() }() resp := new(ai_summary.AiSummaryClassifyListResp) rootList, err := ai_summary.GetAiSummaryClassifyByParentId(0) if err != nil && !utils.IsErrNoRow(err) { br.Msg = "获取失败" br.ErrMsg = "获取数据失败,Err:" + err.Error() return } classifyAll, err := ai_summary.GetAiSummaryClassifyAll() if err != nil && !utils.IsErrNoRow(err) { br.Msg = "获取失败" br.ErrMsg = "获取数据失败,Err:" + err.Error() return } nodeAll := make([]*ai_summary.AiSummaryClassifyItems, 0) for k := range rootList { rootNode := rootList[k] aiSummaryService.AiSummaryClassifyItemsMakeTree(this.SysUser, classifyAll, rootNode) nodeAll = append(nodeAll, rootNode) } resp.AllNodes = nodeAll br.Ret = 200 br.Success = true br.Msg = "获取成功" br.Data = resp }