sa_label.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  1. package semantic_analysis
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "github.com/rdlucklib/rdluck_tools/paging"
  6. "eta/eta_api/controllers"
  7. "eta/eta_api/models"
  8. saModel "eta/eta_api/models/semantic_analysis"
  9. "eta/eta_api/services"
  10. "eta/eta_api/services/alarm_msg"
  11. "eta/eta_api/utils"
  12. "strings"
  13. "time"
  14. )
  15. // SaLabelController 语义分析-标签
  16. type SaLabelController struct {
  17. controllers.BaseAuthController
  18. }
  19. // Add
  20. // @Title 新增标签
  21. // @Description 新增标签
  22. // @Param request body saModel.SaLabelAddReq true "type json string"
  23. // @Success 200 Ret=200 操作成功
  24. // @router /label/add [post]
  25. func (this *SaLabelController) Add() {
  26. br := new(models.BaseResponse).Init()
  27. defer func() {
  28. if br.ErrMsg == "" {
  29. br.IsSendEmail = false
  30. }
  31. this.Data["json"] = br
  32. this.ServeJSON()
  33. }()
  34. var req saModel.SaLabelAddReq
  35. if e := json.Unmarshal(this.Ctx.Input.RequestBody, &req); e != nil {
  36. br.Msg = "参数解析异常!"
  37. br.ErrMsg = "参数解析失败,Err:" + e.Error()
  38. return
  39. }
  40. req.LabelName = strings.TrimSpace(req.LabelName)
  41. if req.LabelName == "" {
  42. br.Msg = "请输入标签名称"
  43. return
  44. }
  45. nameRune := []rune(req.LabelName)
  46. if len(nameRune) > 30 {
  47. br.Msg = "名称不得超过30个字符"
  48. return
  49. }
  50. // 重名校验
  51. existItem := new(saModel.SaLabel)
  52. existCond := ` AND %s = ? `
  53. existCond = fmt.Sprintf(existCond, saModel.SaLabelColumns.LabelName)
  54. existPars := make([]interface{}, 0)
  55. existPars = append(existPars, req.LabelName)
  56. if e := existItem.GetItemByCondition(existCond, existPars); e != nil && e.Error() != utils.ErrNoRow() {
  57. br.Msg = "操作失败"
  58. br.ErrMsg = "获取重名标签失败, Err: " + e.Error()
  59. return
  60. }
  61. if existItem != nil && existItem.SaLabelId > 0 {
  62. br.Msg = "该标签已存在"
  63. return
  64. }
  65. nowTime := time.Now().Local()
  66. newItem := &saModel.SaLabel{
  67. LabelName: req.LabelName,
  68. SysAdminId: this.SysUser.AdminId,
  69. SysAdminName: this.SysUser.RealName,
  70. CreateTime: nowTime,
  71. ModifyTime: nowTime,
  72. }
  73. if e := newItem.Create(); e != nil {
  74. br.Msg = "操作失败"
  75. br.ErrMsg = "新增标签失败, Err: " + e.Error()
  76. return
  77. }
  78. br.Ret = 200
  79. br.Success = true
  80. br.Msg = "操作成功"
  81. }
  82. // Edit
  83. // @Title 编辑标签
  84. // @Description 编辑标签
  85. // @Param request body saModel.SaLabelEditReq true "type json string"
  86. // @Success 200 Ret=200 操作成功
  87. // @router /label/edit [post]
  88. func (this *SaLabelController) Edit() {
  89. br := new(models.BaseResponse).Init()
  90. defer func() {
  91. if br.ErrMsg == "" {
  92. br.IsSendEmail = false
  93. }
  94. this.Data["json"] = br
  95. this.ServeJSON()
  96. }()
  97. var req saModel.SaLabelEditReq
  98. if e := json.Unmarshal(this.Ctx.Input.RequestBody, &req); e != nil {
  99. br.Msg = "参数解析异常!"
  100. br.ErrMsg = "参数解析失败,Err:" + e.Error()
  101. return
  102. }
  103. req.LabelName = strings.TrimSpace(req.LabelName)
  104. if req.LabelName == "" {
  105. br.Msg = "请输入标签名称"
  106. return
  107. }
  108. nameRune := []rune(req.LabelName)
  109. if len(nameRune) > 30 {
  110. br.Msg = "名称不得超过30个字符"
  111. return
  112. }
  113. item := new(saModel.SaLabel)
  114. if e := item.GetItemById(req.SaLabelId); e != nil {
  115. br.Msg = "操作失败"
  116. br.ErrMsg = "获取标签信息失败, Err: " + e.Error()
  117. return
  118. }
  119. originName := item.LabelName
  120. // 重名校验
  121. existItem := new(saModel.SaLabel)
  122. existCond := fmt.Sprintf(` AND %s = ? AND %s <> ? `, saModel.SaLabelColumns.LabelName, saModel.SaLabelColumns.SaLabelId)
  123. existPars := make([]interface{}, 0)
  124. existPars = append(existPars, req.LabelName, req.SaLabelId)
  125. if e := existItem.GetItemByCondition(existCond, existPars); e != nil && e.Error() != utils.ErrNoRow() {
  126. br.Msg = "操作失败"
  127. br.ErrMsg = "获取重名标签失败, Err: " + e.Error()
  128. return
  129. }
  130. if existItem != nil && existItem.SaLabelId > 0 {
  131. br.Msg = "该标签已存在"
  132. return
  133. }
  134. item.LabelName = req.LabelName
  135. item.ModifyTime = time.Now().Local()
  136. if e := item.Update([]string{saModel.SaLabelColumns.LabelName, saModel.SaLabelColumns.ModifyTime}); e != nil {
  137. br.Msg = "操作失败"
  138. br.ErrMsg = "标签编辑失败, Err: " + e.Error()
  139. return
  140. }
  141. // 更新字段
  142. if originName != req.LabelName {
  143. go func() {
  144. if e := saModel.UpdateSaCompareLabelByLabelId(item.SaLabelId, req.LabelName); e != nil {
  145. alarm_msg.SendAlarmMsg(fmt.Sprintf("更新语义分析-比对标签信息失败, Err: %s", e.Error()), 3)
  146. }
  147. }()
  148. }
  149. br.Ret = 200
  150. br.Success = true
  151. br.Msg = "操作成功"
  152. }
  153. // Del
  154. // @Title 删除标签
  155. // @Description 删除标签
  156. // @Param request body saModel.SaLabelDelReq true "type json string"
  157. // @Success 200 Ret=200 操作成功
  158. // @router /label/del [post]
  159. func (this *SaLabelController) Del() {
  160. br := new(models.BaseResponse).Init()
  161. defer func() {
  162. if br.ErrMsg == "" {
  163. br.IsSendEmail = false
  164. }
  165. this.Data["json"] = br
  166. this.ServeJSON()
  167. }()
  168. var req saModel.SaLabelDelReq
  169. if e := json.Unmarshal(this.Ctx.Input.RequestBody, &req); e != nil {
  170. br.Msg = "参数解析异常!"
  171. br.ErrMsg = "参数解析失败,Err:" + e.Error()
  172. return
  173. }
  174. if req.SaLabelId <= 0 {
  175. br.Msg = "参数有误"
  176. return
  177. }
  178. item := new(saModel.SaLabel)
  179. e := item.GetItemById(req.SaLabelId)
  180. if e != nil {
  181. if e.Error() == utils.ErrNoRow() {
  182. br.Msg = "标签已被删除, 请刷新页面"
  183. return
  184. }
  185. br.Msg = "操作失败"
  186. br.ErrMsg = "获取标签信息失败, Err: " + e.Error()
  187. return
  188. }
  189. // 校验标签是否已引用
  190. useCond := fmt.Sprintf(` AND %s = ?`, saModel.SaCompareLabelColumns.LabelId)
  191. usePars := make([]interface{}, 0)
  192. usePars = append(usePars, item.SaLabelId)
  193. useOB := new(saModel.SaCompareLabel)
  194. count, e := useOB.GetCountByCondition(useCond, usePars)
  195. if e != nil {
  196. br.Msg = "操作失败"
  197. br.ErrMsg = "获取标签引用数失败, Err: " + e.Error()
  198. return
  199. }
  200. if count > 0 {
  201. br.Msg = "该标签已被引用, 不可删除"
  202. return
  203. }
  204. if e = item.Del(); e != nil {
  205. br.Msg = "操作失败"
  206. br.ErrMsg = "删除标签失败, Err: " + e.Error()
  207. return
  208. }
  209. br.Ret = 200
  210. br.Success = true
  211. br.Msg = "操作成功"
  212. }
  213. // PageList
  214. // @Title 标签列表
  215. // @Description 标签列表
  216. // @Param Keyword query string false "关键词"
  217. // @Success 200 Ret=200 获取成功
  218. // @router /label/page_list [get]
  219. func (this *SaLabelController) PageList() {
  220. br := new(models.BaseResponse).Init()
  221. defer func() {
  222. if br.ErrMsg == "" {
  223. br.IsSendEmail = false
  224. }
  225. this.Data["json"] = br
  226. this.ServeJSON()
  227. }()
  228. keyword := this.GetString("Keyword", "")
  229. cond := ``
  230. pars := make([]interface{}, 0)
  231. if keyword != "" {
  232. kw := "%" + keyword + "%"
  233. cond += fmt.Sprintf(` AND %s LIKE ?`, saModel.SaLabelColumns.LabelName)
  234. pars = append(pars, kw)
  235. }
  236. var startSize int
  237. pageSize, _ := this.GetInt("PageSize")
  238. currentIndex, _ := this.GetInt("CurrentIndex")
  239. if pageSize <= 0 {
  240. pageSize = utils.PageSize20
  241. }
  242. if currentIndex <= 0 {
  243. currentIndex = 1
  244. }
  245. startSize = paging.StartIndex(currentIndex, pageSize)
  246. labelOB := new(saModel.SaLabel)
  247. total, list, e := labelOB.GetPageItemsByCondition(startSize, pageSize, cond, pars, []string{}, "")
  248. if e != nil {
  249. br.Msg = "获取失败"
  250. br.ErrMsg = "获取标签列表失败, Err: " + e.Error()
  251. return
  252. }
  253. labelIds := make([]int, 0)
  254. for i := range list {
  255. labelIds = append(labelIds, list[i].SaLabelId)
  256. }
  257. // 标签段落引用数
  258. useMap := make(map[int]int)
  259. if len(labelIds) > 0 {
  260. useOB := new(saModel.SaCompareLabel)
  261. useCond := fmt.Sprintf(` AND %s IN (%s)`, saModel.SaCompareLabelColumns.LabelId, utils.GetOrmInReplace(len(labelIds)))
  262. usePars := make([]interface{}, 0)
  263. usePars = append(usePars, labelIds)
  264. countList, e := useOB.GetGroupCountByCondition(useCond, usePars, saModel.SaCompareLabelColumns.LabelId)
  265. if e != nil && e.Error() != utils.ErrNoRow() {
  266. br.Msg = "获取失败"
  267. br.ErrMsg = "获取标签段落引用数失败, Err: " + e.Error()
  268. return
  269. }
  270. for i := range countList {
  271. useMap[countList[i].LabelId] = countList[i].UseNum
  272. }
  273. }
  274. respList := make([]*saModel.SaLabelItem, 0)
  275. for i := range list {
  276. respList = append(respList, &saModel.SaLabelItem{
  277. SaLabelId: list[i].SaLabelId,
  278. LabelName: list[i].LabelName,
  279. UserNum: useMap[list[i].SaLabelId],
  280. SysAdminId: list[i].SysAdminId,
  281. SysAdminName: list[i].SysAdminName,
  282. CreateTime: list[i].CreateTime.Format(utils.FormatDateTime),
  283. })
  284. }
  285. page := paging.GetPaging(currentIndex, pageSize, total)
  286. resp := &saModel.SaLabelPageListResp{
  287. Paging: page,
  288. List: respList,
  289. }
  290. br.Ret = 200
  291. br.Success = true
  292. br.Msg = "获取成功"
  293. br.Data = resp
  294. }
  295. // UseDetail
  296. // @Title 段落引用详情
  297. // @Description 段落引用详情
  298. // @Param LabelId query int true "标签ID"
  299. // @Success 200 Ret=200 获取成功
  300. // @router /label/use_detail [get]
  301. func (this *SaLabelController) UseDetail() {
  302. br := new(models.BaseResponse).Init()
  303. defer func() {
  304. if br.ErrMsg == "" {
  305. br.IsSendEmail = false
  306. }
  307. this.Data["json"] = br
  308. this.ServeJSON()
  309. }()
  310. labelId, _ := this.GetInt("LabelId", 0)
  311. if labelId <= 0 {
  312. br.Msg = "参数有误"
  313. return
  314. }
  315. // 获取标签引用
  316. //compLabOB := new(saModel.SaCompareLabel)
  317. //compLabCond := fmt.Sprintf(" AND %s = ?", saModel.SaCompareLabelColumns.LabelId)
  318. //compLabPars := make([]interface{}, 0)
  319. //compLabPars = append(compLabPars, labelId)
  320. //compLabs, e := compLabOB.GetItemsByCondition(compLabCond, compLabPars, []string{}, "")
  321. //if e != nil {
  322. // br.Msg = "获取失败"
  323. // br.ErrMsg = "获取标签引用列表失败, Err: " + e.Error()
  324. // return
  325. //}
  326. //
  327. //// 引用文档及标题
  328. //docIds := make([]int, 0)
  329. //secIds := make([]int, 0)
  330. //labelIds := make([]int, 0)
  331. //contentMap := make(map[string]int)
  332. //for _, c := range compLabs {
  333. // if !utils.InArrayByInt(docIds, c.DocId) {
  334. // docIds = append(docIds, c.DocId)
  335. // }
  336. // if !utils.InArrayByInt(secIds, c.SectionId) {
  337. // secIds = append(secIds, c.SectionId)
  338. // }
  339. // // 标签ID-文档ID-段落ID作为key进行后续匹配
  340. // k := fmt.Sprintf("%d-%d-%d", labelId, c.DocId, c.SectionId)
  341. // contentMap[k] = c.SectionId
  342. //}
  343. //labelIds = append(labelIds, labelId)
  344. // 获取表格数据
  345. compLabCond := fmt.Sprintf(` AND a.%s = ?`, saModel.SaCompareLabelColumns.LabelId)
  346. compLabPars := make([]interface{}, 0)
  347. compLabPars = append(compLabPars, labelId)
  348. compLabs, e := saModel.GetSaCompareLabelByCond(compLabCond, compLabPars)
  349. if e != nil {
  350. br.Msg = "操作失败"
  351. br.ErrMsg = "获取比对标签列表失败, ErrMsg: " + e.Error()
  352. return
  353. }
  354. resp, e := services.FormatCompareLabels2TableData(compLabs)
  355. if e != nil {
  356. br.Msg = "操作失败"
  357. br.ErrMsg = "获取标签引用表格数据失败, ErrMsg: " + e.Error()
  358. return
  359. }
  360. br.Data = resp
  361. br.Ret = 200
  362. br.Success = true
  363. br.Msg = "获取成功"
  364. }