sa_doc_classify.go 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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 SaDocClassifyController struct {
  14. controllers.BaseAuthController
  15. }
  16. // @router /doc_classify/add [post]
  17. func (this *SaDocClassifyController) 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.SaDocClassifyAddReq
  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.SaDocClassify)
  38. existCond := ` AND %s = ? `
  39. existCond = fmt.Sprintf(existCond, saModel.SaDocClassifyColumns.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.SaDocClassifyId > 0 {
  48. br.Msg = "该文档分类已存在"
  49. return
  50. }
  51. nowTime := time.Now().Local()
  52. newItem := &saModel.SaDocClassify{
  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 /doc_classify/edit [post]
  69. func (this *SaDocClassifyController) 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.SaDocClassifyEditReq
  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. existItem := new(saModel.SaDocClassify)
  90. existCond := fmt.Sprintf(` AND %s = ? AND %s <> ? `, saModel.SaDocClassifyColumns.ClassifyName, saModel.SaDocClassifyColumns.SaDocClassifyId)
  91. existPars := make([]interface{}, 0)
  92. existPars = append(existPars, req.ClassifyName, req.SaDocClassifyId)
  93. if e := existItem.GetItemByCondition(existCond, existPars); e != nil && !utils.IsErrNoRow(e) {
  94. br.Msg = "操作失败"
  95. br.ErrMsg = "获取重名文档分类失败, Err: " + e.Error()
  96. return
  97. }
  98. if existItem != nil && existItem.SaDocClassifyId > 0 {
  99. br.Msg = "该文档分类已存在"
  100. return
  101. }
  102. item := new(saModel.SaDocClassify)
  103. if e := item.GetItemById(req.SaDocClassifyId); e != nil {
  104. br.Msg = "操作失败"
  105. br.ErrMsg = "获取文档分类信息失败, Err: " + e.Error()
  106. return
  107. }
  108. originName := item.ClassifyName
  109. item.ClassifyName = req.ClassifyName
  110. item.ModifyTime = time.Now().Local()
  111. updateCols := []string{
  112. saModel.SaDocClassifyColumns.ClassifyName, saModel.SaDocClassifyColumns.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.UpdateSaDocClassifyByClassifyId(item.SaDocClassifyId, 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 /doc_classify/del [post]
  131. func (this *SaDocClassifyController) 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.SaDocClassifyDelReq
  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.SaDocClassifyId <= 0 {
  147. br.Msg = "参数有误"
  148. return
  149. }
  150. item := new(saModel.SaDocClassify)
  151. e := item.GetItemById(req.SaDocClassifyId)
  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.SaDocColumns.ClassifyId)
  162. usePars := make([]interface{}, 0)
  163. usePars = append(usePars, item.SaDocClassifyId)
  164. useOB := new(saModel.SaDoc)
  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 /doc_classify/list [get]
  185. func (this *SaDocClassifyController) 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. docKeyword := this.GetString("DocKeyword", "")
  195. labelOB := new(saModel.SaDocClassify)
  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. docCond := ``
  205. docPars := make([]interface{}, 0)
  206. if docKeyword != "" {
  207. kw := "%" + docKeyword + "%"
  208. docCond += fmt.Sprintf(` AND a.%s LIKE ?`, saModel.SaDocColumns.Title)
  209. docPars = append(pars, kw)
  210. }
  211. docList, e := saModel.GetSaDocsWithUseNumByCondition(docCond, docPars)
  212. if e != nil {
  213. br.Msg = "获取失败"
  214. br.ErrMsg = "获取分类下的文档失败, Err: " + e.Error()
  215. return
  216. }
  217. docMap := make(map[int][]*saModel.SaDocClassifyChildrenItem)
  218. for i := range docList {
  219. if docMap[docList[i].ClassifyId] == nil {
  220. docMap[docList[i].ClassifyId] = make([]*saModel.SaDocClassifyChildrenItem, 0)
  221. }
  222. docMap[docList[i].ClassifyId] = append(docMap[docList[i].ClassifyId], &saModel.SaDocClassifyChildrenItem{
  223. SaDocId: docList[i].SaDocId,
  224. Title: docList[i].Title,
  225. Theme: docList[i].Theme,
  226. CreateTime: docList[i].CreateTime,
  227. UseNum: docList[i].UseNum,
  228. })
  229. }
  230. respList := make([]*saModel.SaDocClassifyItem, 0)
  231. for i := range list {
  232. respList = append(respList, &saModel.SaDocClassifyItem{
  233. SaDocClassifyId: list[i].SaDocClassifyId,
  234. ClassifyName: list[i].ClassifyName,
  235. CoverImg: list[i].CoverImg,
  236. SysAdminId: list[i].SysAdminId,
  237. SysAdminName: list[i].SysAdminName,
  238. CreateTime: list[i].CreateTime.Format(utils.FormatDateTime),
  239. Children: docMap[list[i].SaDocClassifyId],
  240. })
  241. }
  242. br.Data = respList
  243. br.Ret = 200
  244. br.Success = true
  245. br.Msg = "获取成功"
  246. }