sa_compare_classify.go 7.6 KB

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