sa_label.go 8.5 KB

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