package semantic_analysis import ( "encoding/json" "eta/eta_api/controllers" "eta/eta_api/models" saModel "eta/eta_api/models/semantic_analysis" "eta/eta_api/services" "eta/eta_api/utils" "fmt" "github.com/rdlucklib/rdluck_tools/paging" "html" "strconv" "strings" "time" ) // SaCompareController 语义分析-文档比对 type SaCompareController struct { controllers.BaseAuthController } // Save // @Title 保存比对 // @Description 保存比对 // @Param request body saModel.SaCompareSaveReq true "type json string" // @Success 200 Ret=200 操作成功 // @router /compare/save [post] func (this *SaCompareController) Save() { br := new(models.BaseResponse).Init() defer func() { if br.ErrMsg == "" { br.IsSendEmail = false } this.Data["json"] = br this.ServeJSON() }() var req saModel.SaCompareSaveReq if e := json.Unmarshal(this.Ctx.Input.RequestBody, &req); e != nil { br.Msg = "参数解析异常!" br.ErrMsg = "参数解析失败,Err:" + e.Error() return } req.Title = strings.TrimSpace(req.Title) if req.Title == "" { br.Msg = "请输入比对名称" return } if req.ClassifyId <= 0 { br.Msg = "请选择比对分类" return } if len(req.CompareList) == 0 { br.Msg = "请选择比对文档" return } //secIds := make([]int, 0) for _, d := range req.CompareList { if d.SaDocId <= 0 { br.Msg = "文档ID有误" return } //for _, s := range d.SectionList { // if !utils.InArrayByInt(secIds, s.SaDocSectionId) { // secIds = append(secIds, s.SaDocSectionId) // } //} } // 标题去重 existItem := new(saModel.SaCompare) existCond := fmt.Sprintf(` AND %s = ?`, saModel.SaCompareColumns.Title) existPars := make([]interface{}, 0) existPars = append(existPars, req.Title) e := existItem.GetItemByCondition(existCond, existPars) if e != nil && e.Error() != utils.ErrNoRow() { br.Msg = "获取重名标题的比对失败" br.ErrMsg = "获取重名标题比对失败, Err: " + e.Error() return } if existItem != nil && existItem.SaCompareId > 0 { if existItem.SaCompareId != req.SaCompareId { br.Msg = "标题已存在" return } } nowTime := time.Now().Local() compareClassifyItem := new(saModel.SaCompareClassify) if e := compareClassifyItem.GetItemById(req.ClassifyId); e != nil { br.Msg = "操作失败" br.ErrMsg = "获取比对分类信息失败, Err: " + e.Error() return } compareItem := new(saModel.SaCompare) updateCols := make([]string, 0) if req.SaCompareId > 0 { // 编辑 if e := compareItem.GetItemById(req.SaCompareId); e != nil { br.Msg = "操作失败" br.ErrMsg = "获取比对信息失败, Err: " + e.Error() return } compareItem.ClassifyId = req.ClassifyId compareItem.ClassifyName = compareClassifyItem.ClassifyName compareItem.Title = req.Title compareItem.ModifyTime = nowTime updateCols = []string{ saModel.SaCompareColumns.ClassifyId, saModel.SaCompareColumns.ClassifyName, saModel.SaCompareColumns.Title, saModel.SaCompareColumns.ModifyTime, } } else { // 新增 compareItem.ClassifyId = req.ClassifyId compareItem.ClassifyName = compareClassifyItem.ClassifyName compareItem.Title = req.Title compareItem.SysAdminId = this.SysUser.AdminId compareItem.SysAdminName = this.SysUser.RealName compareItem.CreateTime = nowTime compareItem.ModifyTime = nowTime } // 标签匹配 labelOB := new(saModel.SaLabel) labelCond := `` labelPars := make([]interface{}, 0) labelFields := []string{saModel.SaLabelColumns.SaLabelId, saModel.SaLabelColumns.LabelName} labelItems, e := labelOB.GetItemsByCondition(labelCond, labelPars, labelFields, "") if e != nil { br.Msg = "操作失败" br.ErrMsg = "获取标签列表失败, Err: " + e.Error() return } labelMap := make(map[int]string) for _, l := range labelItems { labelMap[l.SaLabelId] = l.LabelName } // 段落和标签 compareDocs := make([]*saModel.SaCompareDoc, 0) compareLabels := make([]*saModel.SaCompareLabel, 0) for _, v := range req.CompareList { compareDocs = append(compareDocs, &saModel.SaCompareDoc{ DocId: v.SaDocId, }) for _, s := range v.SectionList { for _, l := range s.LabelIds { compareLabels = append(compareLabels, &saModel.SaCompareLabel{ DocId: v.SaDocId, SectionId: s.SaDocSectionId, LabelId: l, LabelName: labelMap[l], IsPart: s.IsPart, StartIndex: s.StartIndex, EndIndex: s.EndIndex, Content: html.EscapeString(s.Content), SysAdminId: this.SysUser.AdminId, SysAdminName: this.SysUser.RealName, CreateTime: nowTime, }) } } } // 新增/更新比对 if req.SaCompareId > 0 { if e := saModel.UpdateSaCompare(compareItem, compareDocs, compareLabels, updateCols); e != nil { br.Msg = "保存失败" br.ErrMsg = "更新比对失败, Err: " + e.Error() return } } else { if e := saModel.CreateSaCompare(compareItem, compareDocs, compareLabels); e != nil { br.Msg = "保存失败" br.ErrMsg = "新增比对失败, Err: " + e.Error() return } } // 获取表格数据 compLabCond := fmt.Sprintf(` AND a.%s = ?`, saModel.SaCompareLabelColumns.CompareId) compLabPars := make([]interface{}, 0) compLabPars = append(compLabPars, compareItem.SaCompareId) compLabs, e := saModel.GetSaCompareLabelByCond(compLabCond, compLabPars) if e != nil { br.Msg = "操作失败" br.ErrMsg = "获取比对标签列表失败, ErrMsg: " + e.Error() return } resp, e := services.FormatCompareLabels2TableData(compLabs) if e != nil { br.Msg = "操作失败" br.ErrMsg = "获取比对表格数据失败, ErrMsg: " + e.Error() return } resp.SaCompareId = compareItem.SaCompareId br.Data = resp br.Ret = 200 br.Success = true br.Msg = "操作成功" } // PageList // @Title 文档比对列表 // @Description 文档比对列表 // @Param Keyword query string false "关键词" // @Param ClassifyId query int false "分类ID" // @Success 200 Ret=200 获取成功 // @router /compare/page_list [get] func (this *SaCompareController) PageList() { br := new(models.BaseResponse).Init() defer func() { if br.ErrMsg == "" { br.IsSendEmail = false } this.Data["json"] = br this.ServeJSON() }() keyword := this.GetString("Keyword", "") classifyId, _ := this.GetInt("ClassifyId", 0) var startSize int pageSize, _ := this.GetInt("PageSize") currentIndex, _ := this.GetInt("CurrentIndex") if pageSize <= 0 { pageSize = utils.PageSize20 } if currentIndex <= 0 { currentIndex = 1 } startSize = paging.StartIndex(currentIndex, pageSize) itemOB := new(saModel.SaCompare) cond := `` pars := make([]interface{}, 0) if keyword != "" { kw := "%" + keyword + "%" cond += fmt.Sprintf(` AND %s LIKE ?`, saModel.SaCompareColumns.Title) pars = append(pars, kw) } if classifyId > 0 { cond += fmt.Sprintf(` AND %s = ?`, saModel.SaCompareColumns.ClassifyId) pars = append(pars, classifyId) } total, list, e := itemOB.GetPageItemsByCondition(startSize, pageSize, cond, pars, []string{}, "") if e != nil { br.Msg = "获取失败" br.ErrMsg = "获取文档列表失败, Err: " + e.Error() return } respList := make([]*saModel.SaCompareItem, 0) for i := range list { v := new(saModel.SaCompareItem) v.SaCompareId = list[i].SaCompareId v.ClassifyId = list[i].ClassifyId v.ClassifyName = list[i].ClassifyName v.Title = list[i].Title v.ResultImg = list[i].ResultImg v.SysAdminId = list[i].SysAdminId v.SysAdminName = list[i].SysAdminName v.CreateTime = list[i].CreateTime.Format(utils.FormatDateTime) respList = append(respList, v) } page := paging.GetPaging(currentIndex, pageSize, total) resp := &saModel.SaComparePageListResp{ Paging: page, List: respList, } br.Data = resp br.Ret = 200 br.Success = true br.Msg = "获取成功" } // UpdateResultImg // @Title 更新比对结果图片 // @Description 更新比对结果图片 // @Param request body saModel.SaCompareUpdateResultImgReq true "type json string" // @Success 200 Ret=200 操作成功 // @router /compare/update_result_img [post] func (this *SaCompareController) UpdateResultImg() { br := new(models.BaseResponse).Init() defer func() { if br.ErrMsg == "" { br.IsSendEmail = false } this.Data["json"] = br this.ServeJSON() }() var req saModel.SaCompareUpdateResultImgReq if e := json.Unmarshal(this.Ctx.Input.RequestBody, &req); e != nil { br.Msg = "参数解析异常!" br.ErrMsg = "参数解析失败,Err:" + e.Error() return } if req.SaCompareId <= 0 { br.Msg = "参数有误" return } if req.ResultImg == "" { br.Msg = "请选择结果图片" return } compareItem := new(saModel.SaCompare) if e := compareItem.GetItemById(req.SaCompareId); e != nil { br.Msg = "操作失败" br.ErrMsg = "获取比对信息失败, Err: " + e.Error() return } updateCols := []string{saModel.SaCompareColumns.ResultImg, saModel.SaCompareColumns.ModifyTime} compareItem.ResultImg = req.ResultImg compareItem.ModifyTime = time.Now().Local() if e := compareItem.Update(updateCols); e != nil { br.Msg = "操作失败" br.ErrMsg = "更新语义分析图片失败, Err: " + e.Error() return } br.Ret = 200 br.Success = true br.Msg = "操作成功" } // Detail // @Title 比对详情 // @Description 比对详情 // @Param CompareId query int true "比对ID" // @Success 200 Ret=200 获取成功 // @router /compare/detail [get] func (this *SaCompareController) Detail() { br := new(models.BaseResponse).Init() defer func() { if br.ErrMsg == "" { br.IsSendEmail = false } this.Data["json"] = br this.ServeJSON() }() compareId, _ := this.GetInt("CompareId", 0) if compareId <= 0 { br.Msg = "参数有误" return } compareItem := new(saModel.SaCompare) if e := compareItem.GetItemById(compareId); e != nil { br.Msg = "获取失败" br.ErrMsg = "获取比对信息失败, Err: " + e.Error() return } // 比对文档IDs compDocOB := new(saModel.SaCompareDoc) compDocCond := fmt.Sprintf(` AND %s = ?`, saModel.SaCompareDocColumns.CompareId) compDocPars := make([]interface{}, 0) compDocPars = append(compDocPars, compareItem.SaCompareId) compDocs, e := compDocOB.GetItemsByCondition(compDocCond, compDocPars, []string{saModel.SaCompareDocColumns.DocId}, "") if e != nil { br.Msg = "获取失败" br.ErrMsg = "获取比对文档失败, Err: " + e.Error() return } compDocIds := make([]int, 0) for _, d := range compDocs { compDocIds = append(compDocIds, d.DocId) } // 比对详情 detail := new(saModel.SaCompareDetail) detail.SaCompareId = compareItem.SaCompareId detail.ClassifyId = compareItem.ClassifyId detail.ClassifyName = compareItem.ClassifyName detail.Title = compareItem.Title detail.ResultImg = compareItem.ResultImg detail.SysAdminId = compareItem.SysAdminId detail.SysAdminName = compareItem.SysAdminName detail.CreateTime = compareItem.CreateTime.Format(utils.FormatDateTime) // 文档详情 docDetail, e := services.GetSaCompareDetailByDocIds(compDocIds, compareId, this.SysUser.AdminId) if e != nil { br.Msg = "获取失败" br.ErrMsg = "获取比对详情信息失败, ErrMsg: " + e.Error() return } detail.HeadLabel = docDetail.HeadLabel detail.DocList = docDetail.DocList detail.KeywordsList = docDetail.KeywordsList br.Data = detail br.Ret = 200 br.Success = true br.Msg = "获取成功" } // SelectDocs // @Title 选择比对文档 // @Description 选择比对文档 // @Param DocIds query string true "文档IDs" // @Success 200 Ret=200 获取成功 // @router /compare/select_docs [get] func (this *SaCompareController) SelectDocs() { br := new(models.BaseResponse).Init() defer func() { if br.ErrMsg == "" { br.IsSendEmail = false } this.Data["json"] = br this.ServeJSON() }() docIdStr := this.GetString("DocIds", "") if docIdStr == "" { br.Msg = "请选择文档" return } docIds := make([]int, 0) docIdArr := strings.Split(docIdStr, ",") if len(docIdArr) == 0 { br.Msg = "请选择文档" return } if len(docIdArr) > 10 { br.Msg = "最多支持选择10个文档" return } for i := range docIdArr { d, e := strconv.Atoi(docIdArr[i]) if e != nil { br.Msg = "文档ID有误" return } docIds = append(docIds, d) } resp, e := services.GetSaCompareDetailByDocIds(docIds, 0, this.SysUser.AdminId) if e != nil { br.Msg = "获取失败" br.ErrMsg = "获取选择的文档详情失败, ErrMsg: " + e.Error() return } br.Data = resp br.Ret = 200 br.Success = true br.Msg = "获取成功" } // Del // @Title 删除语义分析 // @Description 删除语义分析 // @Param request body saModel.SaCompareDelReq true "type json string" // @Success 200 Ret=200 操作成功 // @router /compare/del [post] func (this *SaCompareController) Del() { br := new(models.BaseResponse).Init() defer func() { if br.ErrMsg == "" { br.IsSendEmail = false } this.Data["json"] = br this.ServeJSON() }() var req saModel.SaCompareDelReq if e := json.Unmarshal(this.Ctx.Input.RequestBody, &req); e != nil { br.Msg = "参数解析异常!" br.ErrMsg = "参数解析失败,Err:" + e.Error() return } if req.SaCompareId <= 0 { br.Msg = "参数有误" return } // TODO:可能会有只让自己或者管理员删除的需求,目前无 item := new(saModel.SaCompare) e := item.GetItemById(req.SaCompareId) if e != nil { if e.Error() == utils.ErrNoRow() { br.Msg = "文档比对已被删除, 请刷新页面" return } br.Msg = "操作失败" br.ErrMsg = "获取语义分析比对信息失败, Err: " + e.Error() return } // 删除比对及相关 if e = saModel.DelSaCompare(item.SaCompareId); e != nil { br.Msg = "操作失败" br.ErrMsg = "删除语义分析比对失败, Err: " + e.Error() return } br.Ret = 200 br.Success = true br.Msg = "操作成功" } // SaveKeywords // @Title 保存关键词 // @Description 保存比对 // @Param request body saModel.SaCompareSearchKeywordSaveReq true "type json string" // @Success 200 Ret=200 操作成功 // @router /compare/save_keywords [post] func (this *SaCompareController) SaveKeywords() { br := new(models.BaseResponse).Init() defer func() { if br.ErrMsg == "" { br.IsSendEmail = false } this.Data["json"] = br this.ServeJSON() }() var req saModel.SaCompareSearchKeywordSaveReq if e := json.Unmarshal(this.Ctx.Input.RequestBody, &req); e != nil { br.Msg = "参数解析异常!" br.ErrMsg = "参数解析失败,Err:" + e.Error() return } if req.SaCompareId <= 0 { br.Msg = "参数异常" return } keywords := make([]string, 0) for k := range req.Keywords { v := strings.TrimSpace(req.Keywords[k]) if v != "" { keywords = append(keywords, v) } } keywordOB := new(saModel.SaCompareSearchKeyword) if e := keywordOB.DelAndCreateSearchKeywords(req.SaCompareId, keywords); e != nil { br.Msg = "操作失败" br.ErrMsg = "保存搜索关键词失败, Err: " + e.Error() return } br.Ret = 200 br.Success = true br.Msg = "操作成功" } // Move // @Title 移动比对 // @Description 移动比对 // @Param request body saModel.SaCompareMoveReq true "type json string" // @Success 200 Ret=200 操作成功 // @router /compare/move [post] func (this *SaCompareController) Move() { br := new(models.BaseResponse).Init() defer func() { if br.ErrMsg == "" { br.IsSendEmail = false } this.Data["json"] = br this.ServeJSON() }() var req saModel.SaCompareMoveReq if e := json.Unmarshal(this.Ctx.Input.RequestBody, &req); e != nil { br.Msg = "参数解析异常!" br.ErrMsg = "参数解析失败,Err:" + e.Error() return } if req.SaCompareClassifyId <= 0 { br.Msg = "请选择分类" return } if req.SaCompareId <= 0 { br.Msg = "参数异常" return } item := new(saModel.SaCompare) e := item.GetItemById(req.SaCompareId) if e != nil { if e.Error() == utils.ErrNoRow() { br.Msg = "比对已被删除, 请刷新页面" return } br.Msg = "操作失败" br.ErrMsg = "获取比对信息失败, Err: " + e.Error() return } // 分类 classifyItem := new(saModel.SaCompareClassify) if e = classifyItem.GetItemById(req.SaCompareClassifyId); e != nil { br.Msg = "分类信息有误" br.ErrMsg = "获取比对分类信息失败, Err: " + e.Error() return } // CV的指标库指标的移动, 没有技术, 全是感情~ if req.PrevSaCompareId > 0 { prevCompare := new(saModel.SaCompare) if e = prevCompare.GetItemById(req.PrevSaCompareId); e != nil { if e.Error() == utils.ErrNoRow() { br.Msg = "上一个比对已被删除, 请刷新页面" return } br.Msg = "操作失败" br.ErrMsg = "获取上一个比对信息失败, Err: " + e.Error() return } // 如果是在两个比对之间 if req.NextSaCompareId > 0 { nextCompare := new(saModel.SaCompare) if e = nextCompare.GetItemById(req.NextSaCompareId); e != nil { if e.Error() == utils.ErrNoRow() { br.Msg = "下一个比对已被删除, 请刷新页面" return } br.Msg = "操作失败" br.ErrMsg = "获取下一个比对信息失败, Err: " + e.Error() return } // 判断上一个比对与当前比对/下一个比对的位置 if prevCompare.Sort == item.Sort || prevCompare.Sort == nextCompare.Sort { _ = saModel.UpdateSaCompareSort(prevCompare.ClassifyId, prevCompare.Sort, prevCompare.SaCompareId, `sort + 2`) } else { if nextCompare.Sort-prevCompare.Sort == 1 { _ = saModel.UpdateSaCompareSort(prevCompare.ClassifyId, prevCompare.Sort, prevCompare.SaCompareId, `sort + 1`) } } } item.Sort = prevCompare.Sort + 1 } else { firstCompare, e := saModel.GetFirstSortSaCompare(req.SaCompareClassifyId) if e != nil && e.Error() != utils.ErrNoRow() { br.Msg = "操作失败" br.ErrMsg = "获取首个比对失败, Err: " + e.Error() return } if firstCompare != nil && firstCompare.Sort == 0 { _ = saModel.UpdateSaCompareSort(firstCompare.ClassifyId, 0, firstCompare.SaCompareId-1, `sort + 1`) } item.Sort = 0 } // 更新 item.ClassifyId = classifyItem.SaCompareClassifyId item.ClassifyName = classifyItem.ClassifyName item.ModifyTime = time.Now().Local() updateCols := []string{ saModel.SaCompareColumns.ClassifyId, saModel.SaCompareColumns.ClassifyName, saModel.SaCompareColumns.Sort, saModel.SaCompareColumns.ModifyTime, } if e = item.Update(updateCols); e != nil { br.Msg = "操作失败" br.ErrMsg = "移动比对失败, Err: " + e.Error() return } br.Ret = 200 br.Success = true br.Msg = "操作成功" } // Search // @Title 文档对比搜索(从es获取) // @Description 图表模糊搜索(从es获取) // @Param Keyword query string true "文档对比标题" // @Success 200 {object} saModel.CompareListByEsResp // @router /compare/search [get] func (this *SaCompareController) Search() { 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 } pageSize, _ := this.GetInt("PageSize") currentIndex, _ := this.GetInt("CurrentIndex") var startSize int if pageSize <= 0 { pageSize = utils.PageSize20 } if currentIndex <= 0 { currentIndex = 1 } startSize = paging.StartIndex(currentIndex, pageSize) keyword := this.GetString("Keyword") var searchList []*saModel.SaCompareElastic var total int var err error var list []*saModel.SaCompare saCompare := new(saModel.SaCompare) existCond := fmt.Sprintf(` AND result_img != ""`) existPars := make([]interface{}, 0) if keyword != "" { existCond += ` AND ( title LIKE ? )` existPars = append(existPars, utils.GetLikeKeyword(keyword)) } total, list, err = saCompare.GetPageItemsByCondition(startSize, pageSize, existCond, existPars, []string{}, "") if err != nil && err.Error() != utils.ErrNoRow() { br.Msg = "获取失败" br.ErrMsg = "获取图表信息失败,Err:" + err.Error() return } for _, v := range list { tmp := new(saModel.SaCompareElastic) tmp.SaCompareId = v.SaCompareId tmp.ResultImg = v.ResultImg tmp.CreateTime = v.CreateTime.Format(utils.FormatDateTime) tmp.ModifyTime = v.ModifyTime.Format(utils.FormatDateTime) tmp.SysAdminId = v.SysAdminId tmp.SysAdminName = v.SysAdminName tmp.ClassifyId = v.ClassifyId tmp.ClassifyName = v.ClassifyName tmp.Title = v.Title searchList = append(searchList, tmp) } page := paging.GetPaging(currentIndex, pageSize, total) resp := saModel.CompareListByEsResp{ Paging: page, List: searchList, } br.Ret = 200 br.Success = true br.Msg = "获取成功" br.Data = resp }