sa_doc_classify.go 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. package semantic_analysis
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "eta/eta_api/controllers"
  6. "eta/eta_api/models"
  7. saModel "eta/eta_api/models/semantic_analysis"
  8. "eta/eta_api/services/alarm_msg"
  9. "eta/eta_api/utils"
  10. "strings"
  11. "time"
  12. )
  13. // SaDocClassifyController 语义分析-文档分类
  14. type SaDocClassifyController struct {
  15. controllers.BaseAuthController
  16. }
  17. // Add
  18. // @Title 新增文档分类
  19. // @Description 新增文档分类
  20. // @Param request body saModel.SaDocClassifyAddReq true "type json string"
  21. // @Success 200 Ret=200 操作成功
  22. // @router /doc_classify/add [post]
  23. func (this *SaDocClassifyController) 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.SaDocClassifyAddReq
  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.SaDocClassify)
  45. existCond := ` AND %s = ? `
  46. existCond = fmt.Sprintf(existCond, saModel.SaDocClassifyColumns.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.SaDocClassifyId > 0 {
  55. br.Msg = "该文档分类已存在"
  56. return
  57. }
  58. nowTime := time.Now().Local()
  59. newItem := &saModel.SaDocClassify{
  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.SaDocClassifyEditReq true "type json string"
  79. // @Success 200 Ret=200 操作成功
  80. // @router /doc_classify/edit [post]
  81. func (this *SaDocClassifyController) 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.SaDocClassifyEditReq
  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. // 重名校验
  102. existItem := new(saModel.SaDocClassify)
  103. existCond := fmt.Sprintf(` AND %s = ? AND %s <> ? `, saModel.SaDocClassifyColumns.ClassifyName, saModel.SaDocClassifyColumns.SaDocClassifyId)
  104. existPars := make([]interface{}, 0)
  105. existPars = append(existPars, req.ClassifyName, req.SaDocClassifyId)
  106. if e := existItem.GetItemByCondition(existCond, existPars); e != nil && e.Error() != utils.ErrNoRow() {
  107. br.Msg = "操作失败"
  108. br.ErrMsg = "获取重名文档分类失败, Err: " + e.Error()
  109. return
  110. }
  111. if existItem != nil && existItem.SaDocClassifyId > 0 {
  112. br.Msg = "该文档分类已存在"
  113. return
  114. }
  115. item := new(saModel.SaDocClassify)
  116. if e := item.GetItemById(req.SaDocClassifyId); e != nil {
  117. br.Msg = "操作失败"
  118. br.ErrMsg = "获取文档分类信息失败, Err: " + e.Error()
  119. return
  120. }
  121. originName := item.ClassifyName
  122. item.ClassifyName = req.ClassifyName
  123. item.ModifyTime = time.Now().Local()
  124. updateCols := []string{
  125. saModel.SaDocClassifyColumns.ClassifyName, saModel.SaDocClassifyColumns.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.UpdateSaDocClassifyByClassifyId(item.SaDocClassifyId, 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.SaDocClassifyDelReq true "type json string"
  148. // @Success 200 Ret=200 操作成功
  149. // @router /doc_classify/del [post]
  150. func (this *SaDocClassifyController) 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.SaDocClassifyDelReq
  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.SaDocClassifyId <= 0 {
  166. br.Msg = "参数有误"
  167. return
  168. }
  169. item := new(saModel.SaDocClassify)
  170. e := item.GetItemById(req.SaDocClassifyId)
  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.SaDocColumns.ClassifyId)
  182. usePars := make([]interface{}, 0)
  183. usePars = append(usePars, item.SaDocClassifyId)
  184. useOB := new(saModel.SaDoc)
  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 DocKeyword query string false "文档关键词"
  208. // @Success 200 Ret=200 获取成功
  209. // @router /doc_classify/list [get]
  210. func (this *SaDocClassifyController) 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. docKeyword := this.GetString("DocKeyword", "")
  220. labelOB := new(saModel.SaDocClassify)
  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. docCond := ``
  231. docPars := make([]interface{}, 0)
  232. if docKeyword != "" {
  233. kw := "%" + docKeyword + "%"
  234. docCond += fmt.Sprintf(` AND a.%s LIKE ?`, saModel.SaDocColumns.Title)
  235. docPars = append(pars, kw)
  236. }
  237. docList, e := saModel.GetSaDocsWithUseNumByCondition(docCond, docPars)
  238. if e != nil {
  239. br.Msg = "获取失败"
  240. br.ErrMsg = "获取分类下的文档失败, Err: " + e.Error()
  241. return
  242. }
  243. docMap := make(map[int][]*saModel.SaDocClassifyChildrenItem)
  244. for i := range docList {
  245. if docMap[docList[i].ClassifyId] == nil {
  246. docMap[docList[i].ClassifyId] = make([]*saModel.SaDocClassifyChildrenItem, 0)
  247. }
  248. docMap[docList[i].ClassifyId] = append(docMap[docList[i].ClassifyId], &saModel.SaDocClassifyChildrenItem{
  249. SaDocId: docList[i].SaDocId,
  250. Title: docList[i].Title,
  251. Theme: docList[i].Theme,
  252. CreateTime: docList[i].CreateTime,
  253. UseNum: docList[i].UseNum,
  254. })
  255. }
  256. respList := make([]*saModel.SaDocClassifyItem, 0)
  257. for i := range list {
  258. respList = append(respList, &saModel.SaDocClassifyItem{
  259. SaDocClassifyId: list[i].SaDocClassifyId,
  260. ClassifyName: list[i].ClassifyName,
  261. CoverImg: list[i].CoverImg,
  262. SysAdminId: list[i].SysAdminId,
  263. SysAdminName: list[i].SysAdminName,
  264. CreateTime: list[i].CreateTime.Format(utils.FormatDateTime),
  265. Children: docMap[list[i].SaDocClassifyId],
  266. })
  267. }
  268. br.Data = respList
  269. br.Ret = 200
  270. br.Success = true
  271. br.Msg = "获取成功"
  272. }