sa_compare_classify.go 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. package semantic_analysis
  2. import (
  3. "encoding/json"
  4. "eta/eta_api/controllers"
  5. "eta/eta_api/models"
  6. saModel "eta/eta_api/models/semantic_analysis"
  7. "eta/eta_api/services/alarm_msg"
  8. "eta/eta_api/utils"
  9. "fmt"
  10. "strings"
  11. "time"
  12. )
  13. // SaCompareClassifyController 语义分析-比对分类
  14. type SaCompareClassifyController struct {
  15. controllers.BaseAuthController
  16. }
  17. // Add
  18. // @Title 新增比对分类
  19. // @Description 新增比对分类
  20. // @Param request body saModel.SaCompareClassifyAddReq true "type json string"
  21. // @Success 200 Ret=200 操作成功
  22. // @router /compare_classify/add [post]
  23. func (this *SaCompareClassifyController) Add() {
  24. br := new(models.BaseResponse).Init()
  25. defer func() {
  26. if br.ErrMsg == "" {
  27. br.IsSendEmail = false
  28. }
  29. this.Data["json"] = br
  30. this.ServeJSON()
  31. }()
  32. var req saModel.SaCompareClassifyAddReq
  33. if e := json.Unmarshal(this.Ctx.Input.RequestBody, &req); e != nil {
  34. br.Msg = "参数解析异常!"
  35. br.ErrMsg = "参数解析失败,Err:" + e.Error()
  36. return
  37. }
  38. req.ClassifyName = strings.TrimSpace(req.ClassifyName)
  39. if req.ClassifyName == "" {
  40. br.Msg = "请输入比对分类名称"
  41. return
  42. }
  43. // 重名校验
  44. existItem := new(saModel.SaCompareClassify)
  45. existCond := ` AND %s = ? `
  46. existCond = fmt.Sprintf(existCond, saModel.SaCompareClassifyColumns.ClassifyName)
  47. existPars := make([]interface{}, 0)
  48. existPars = append(existPars, req.ClassifyName)
  49. if e := existItem.GetItemByCondition(existCond, existPars); e != nil && e.Error() != utils.ErrNoRow() {
  50. br.Msg = "操作失败"
  51. br.ErrMsg = "获取重名比对分类失败, Err: " + e.Error()
  52. return
  53. }
  54. if existItem != nil && existItem.SaCompareClassifyId > 0 {
  55. br.Msg = "该比对分类已存在"
  56. return
  57. }
  58. nowTime := time.Now().Local()
  59. newItem := &saModel.SaCompareClassify{
  60. ClassifyName: req.ClassifyName,
  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. // Edit
  76. // @Title 编辑比对分类
  77. // @Description 编辑比对分类
  78. // @Param request body saModel.SaCompareClassifyEditReq true "type json string"
  79. // @Success 200 Ret=200 操作成功
  80. // @router /compare_classify/edit [post]
  81. func (this *SaCompareClassifyController) Edit() {
  82. br := new(models.BaseResponse).Init()
  83. defer func() {
  84. if br.ErrMsg == "" {
  85. br.IsSendEmail = false
  86. }
  87. this.Data["json"] = br
  88. this.ServeJSON()
  89. }()
  90. var req saModel.SaCompareClassifyEditReq
  91. if e := json.Unmarshal(this.Ctx.Input.RequestBody, &req); e != nil {
  92. br.Msg = "参数解析异常!"
  93. br.ErrMsg = "参数解析失败,Err:" + e.Error()
  94. return
  95. }
  96. req.ClassifyName = strings.TrimSpace(req.ClassifyName)
  97. if req.ClassifyName == "" {
  98. br.Msg = "请输入比对分类名称"
  99. return
  100. }
  101. item := new(saModel.SaCompareClassify)
  102. if e := item.GetItemById(req.SaCompareClassifyId); e != nil {
  103. br.Msg = "操作失败"
  104. br.ErrMsg = "获取比对分类信息失败, Err: " + e.Error()
  105. return
  106. }
  107. originName := item.ClassifyName
  108. // 重名校验
  109. existItem := new(saModel.SaCompareClassify)
  110. existCond := fmt.Sprintf(` AND %s = ? AND %s <> ? `, saModel.SaCompareClassifyColumns.ClassifyName, saModel.SaCompareClassifyColumns.SaCompareClassifyId)
  111. existPars := make([]interface{}, 0)
  112. existPars = append(existPars, req.ClassifyName, req.SaCompareClassifyId)
  113. if e := existItem.GetItemByCondition(existCond, existPars); e != nil && e.Error() != utils.ErrNoRow() {
  114. br.Msg = "操作失败"
  115. br.ErrMsg = "获取重名比对分类失败, Err: " + e.Error()
  116. return
  117. }
  118. if existItem != nil && existItem.SaCompareClassifyId > 0 {
  119. br.Msg = "该比对分类已存在"
  120. return
  121. }
  122. item.ClassifyName = req.ClassifyName
  123. item.ModifyTime = time.Now().Local()
  124. updateCols := []string{
  125. saModel.SaCompareClassifyColumns.ClassifyName, saModel.SaCompareClassifyColumns.ModifyTime,
  126. }
  127. if e := item.Update(updateCols); e != nil {
  128. br.Msg = "操作失败"
  129. br.ErrMsg = "比对分类编辑失败, Err: " + e.Error()
  130. return
  131. }
  132. // 更新字段
  133. if originName != req.ClassifyName {
  134. go func() {
  135. if e := saModel.UpdateSaCompareClassifyByClassifyId(item.SaCompareClassifyId, req.ClassifyName); e != nil {
  136. alarm_msg.SendAlarmMsg(fmt.Sprintf("更新语义分析-文档分类信息失败, Err: %s", e.Error()), 3)
  137. }
  138. }()
  139. }
  140. br.Ret = 200
  141. br.Success = true
  142. br.Msg = "操作成功"
  143. }
  144. // Del
  145. // @Title 删除比对分类
  146. // @Description 删除比对分类
  147. // @Param request body saModel.SaCompareClassifyDelReq true "type json string"
  148. // @Success 200 Ret=200 操作成功
  149. // @router /compare_classify/del [post]
  150. func (this *SaCompareClassifyController) Del() {
  151. br := new(models.BaseResponse).Init()
  152. defer func() {
  153. if br.ErrMsg == "" {
  154. br.IsSendEmail = false
  155. }
  156. this.Data["json"] = br
  157. this.ServeJSON()
  158. }()
  159. var req saModel.SaCompareClassifyDelReq
  160. if e := json.Unmarshal(this.Ctx.Input.RequestBody, &req); e != nil {
  161. br.Msg = "参数解析异常!"
  162. br.ErrMsg = "参数解析失败,Err:" + e.Error()
  163. return
  164. }
  165. if req.SaCompareClassifyId <= 0 {
  166. br.Msg = "参数有误"
  167. return
  168. }
  169. item := new(saModel.SaCompareClassify)
  170. e := item.GetItemById(req.SaCompareClassifyId)
  171. if e != nil {
  172. if e.Error() == utils.ErrNoRow() {
  173. br.Msg = "比对分类已被删除, 请刷新页面"
  174. return
  175. }
  176. br.Msg = "操作失败"
  177. br.ErrMsg = "获取比对分类信息失败, Err: " + e.Error()
  178. return
  179. }
  180. // 校验关联比对结果数
  181. useCond := fmt.Sprintf(` AND %s = ?`, saModel.SaCompareColumns.ClassifyId)
  182. usePars := make([]interface{}, 0)
  183. usePars = append(usePars, item.SaCompareClassifyId)
  184. useOB := new(saModel.SaCompare)
  185. count, e := useOB.GetCountByCondition(useCond, usePars)
  186. if e != nil {
  187. br.Msg = "操作失败"
  188. br.ErrMsg = "获取关联比对结果数失败, Err: " + e.Error()
  189. return
  190. }
  191. if count > 0 {
  192. br.Msg = "已关联比对结果, 不可删除"
  193. return
  194. }
  195. if e = item.Del(); e != nil {
  196. br.Msg = "操作失败"
  197. br.ErrMsg = "删除比对分类失败, Err: " + e.Error()
  198. return
  199. }
  200. br.Ret = 200
  201. br.Success = true
  202. br.Msg = "操作成功"
  203. }
  204. // List
  205. // @Title 比对分类列表
  206. // @Description 比对分类列表
  207. // @Param Keyword query string false "关键词-比对标题"
  208. // @Success 200 Ret=200 获取成功
  209. // @router /compare_classify/list [get]
  210. func (this *SaCompareClassifyController) List() {
  211. br := new(models.BaseResponse).Init()
  212. defer func() {
  213. if br.ErrMsg == "" {
  214. br.IsSendEmail = false
  215. }
  216. this.Data["json"] = br
  217. this.ServeJSON()
  218. }()
  219. keyword := this.GetString("Keyword", "")
  220. labelOB := new(saModel.SaCompareClassify)
  221. cond := ``
  222. pars := make([]interface{}, 0)
  223. list, e := labelOB.GetItemsByCondition(cond, pars, []string{}, "")
  224. if e != nil {
  225. br.Msg = "获取失败"
  226. br.ErrMsg = "获取比对分类列表失败, Err: " + e.Error()
  227. return
  228. }
  229. // 分类下的比对
  230. childCond := ``
  231. childPars := make([]interface{}, 0)
  232. if keyword != "" {
  233. kw := "%" + keyword + "%"
  234. childCond += fmt.Sprintf(` AND %s LIKE ?`, saModel.SaCompareColumns.Title)
  235. childPars = append(pars, kw)
  236. }
  237. childOB := new(saModel.SaCompare)
  238. childList, e := childOB.GetItemsByCondition(childCond, childPars, []string{}, "")
  239. if e != nil {
  240. br.Msg = "获取失败"
  241. br.ErrMsg = "获取分类下的比对失败, Err: " + e.Error()
  242. return
  243. }
  244. childMap := make(map[int][]*saModel.SaCompareItem)
  245. for i := range childList {
  246. if childMap[childList[i].ClassifyId] == nil {
  247. childMap[childList[i].ClassifyId] = make([]*saModel.SaCompareItem, 0)
  248. }
  249. childMap[childList[i].ClassifyId] = append(childMap[childList[i].ClassifyId], &saModel.SaCompareItem{
  250. SaCompareId: childList[i].SaCompareId,
  251. ClassifyId: childList[i].ClassifyId,
  252. Title: childList[i].Title,
  253. ResultImg: childList[i].ResultImg,
  254. SysAdminId: childList[i].SysAdminId,
  255. SysAdminName: childList[i].SysAdminName,
  256. CreateTime: childList[i].CreateTime.Format(utils.FormatDate),
  257. })
  258. }
  259. respList := make([]*saModel.SaCompareClassifyItem, 0)
  260. for i := range list {
  261. respList = append(respList, &saModel.SaCompareClassifyItem{
  262. SaCompareClassifyId: list[i].SaCompareClassifyId,
  263. ClassifyName: list[i].ClassifyName,
  264. SysAdminId: list[i].SysAdminId,
  265. SysAdminName: list[i].SysAdminName,
  266. CreateTime: list[i].CreateTime.Format(utils.FormatDateTime),
  267. Children: childMap[list[i].SaCompareClassifyId],
  268. })
  269. }
  270. br.Data = respList
  271. br.Ret = 200
  272. br.Success = true
  273. br.Msg = "获取成功"
  274. }