knowledge_classify.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  1. package controllers
  2. import (
  3. "encoding/json"
  4. "eta_gn/eta_api/models"
  5. "eta_gn/eta_api/models/knowledge"
  6. knowledgeServ "eta_gn/eta_api/services/knowledge"
  7. )
  8. // 分类
  9. type KnowledgeClassifyController struct {
  10. BaseAuthController
  11. }
  12. // @Title 新增分类接口
  13. // @Description 新增分类
  14. // @Param request body knowledge.ClassifyAddReq true "type json string"
  15. // @Success 200 新增成功
  16. // @router /classify/add [post]
  17. func (this *KnowledgeClassifyController) Add() {
  18. br := new(models.BaseResponse).Init()
  19. defer func() {
  20. this.Data["json"] = br
  21. this.ServeJSON()
  22. }()
  23. var req knowledge.ClassifyAddReq
  24. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  25. if err != nil {
  26. br.Msg = "参数解析异常!"
  27. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  28. return
  29. }
  30. if req.ClassifyName == "" {
  31. br.Msg = "分类名称不可为空"
  32. return
  33. }
  34. // 新增分类
  35. err, errMsg, isSentEmail := knowledgeServ.AddKnowledgeClassify(req.ClassifyName, req.ParentId, req.ResourceType)
  36. if err != nil {
  37. br.Msg = "添加失败"
  38. if errMsg != "" {
  39. br.Msg = errMsg
  40. }
  41. br.ErrMsg = "添加失败,Err:" + err.Error()
  42. br.IsSendEmail = isSentEmail
  43. return
  44. }
  45. br.Ret = 200
  46. br.Success = true
  47. br.Msg = "新增成功"
  48. }
  49. // CheckDeleteClassify
  50. // @Title 删除分类-检测接口
  51. // @Description 删除分类-信息检测,是否符合删除条件
  52. // @Param ClassifyId query int true "分类ID"
  53. // @Success 200 {object} knowledge.CheckDeleteClassifyResp
  54. // @router /classify/checkDelete [get]
  55. func (this *KnowledgeClassifyController) CheckDeleteClassify() {
  56. br := new(models.BaseResponse).Init()
  57. defer func() {
  58. this.Data["json"] = br
  59. this.ServeJSON()
  60. }()
  61. classifyId, err := this.GetInt("ClassifyId")
  62. if classifyId <= 0 {
  63. br.Msg = "参数错误"
  64. return
  65. }
  66. knowledgeObj := new(knowledge.KnowledgeClassify)
  67. classify, err := knowledgeObj.GetClassifyById(classifyId)
  68. if err != nil {
  69. br.Msg = "获取信息失败"
  70. br.ErrMsg = "获取信息失败,Err:" + err.Error()
  71. return
  72. }
  73. resp := new(knowledge.CheckDeleteClassifyResp)
  74. if classify == nil {
  75. br.Msg = "分类不存在"
  76. resp.Code = 1
  77. resp.Msg = "分类不存在"
  78. br.Data = resp
  79. br.Ret = 200
  80. br.Success = true
  81. return
  82. }
  83. //判断分类是否关联了报告
  84. if classify.ParentId > 0 {
  85. count, err := knowledgeObj.GetReportCountByClassifyId(classifyId)
  86. if err != nil {
  87. br.Msg = "获取信息失败"
  88. br.ErrMsg = "获取信息失败,Err:" + err.Error()
  89. return
  90. }
  91. if count > 0 {
  92. resp.Code = 2
  93. resp.Msg = "该分类有关联报告,不允许删除"
  94. br.Data = resp
  95. br.Ret = 200
  96. br.Msg = "该分类有关联报告,不允许删除"
  97. br.Success = true
  98. return
  99. }
  100. } else {
  101. subCount, err := knowledgeObj.GetClassifySubCountByClassifyId(classifyId)
  102. if err != nil {
  103. br.Msg = "获取信息失败"
  104. br.ErrMsg = "获取信息失败,Err:" + err.Error()
  105. return
  106. }
  107. if subCount > 0 {
  108. resp.Code = 3
  109. resp.Msg = "二级分类有关联报告,不允许删除"
  110. br.Data = resp
  111. br.Ret = 200
  112. br.Msg = "二级分类有关联报告,不允许删除"
  113. br.Success = true
  114. return
  115. }
  116. subTotal, err := knowledgeObj.GetClassifySubCountByParentId(classifyId)
  117. if err != nil {
  118. br.Msg = "获取信息失败"
  119. br.ErrMsg = "获取信息失败,Err:" + err.Error()
  120. return
  121. }
  122. if subTotal > 0 {
  123. resp.Code = 4
  124. resp.Msg = "请先删除该分类下关联分类"
  125. br.Data = resp
  126. br.Ret = 200
  127. br.Msg = "请先删除该分类下关联分类"
  128. br.Success = true
  129. return
  130. }
  131. }
  132. resp.Code = 0
  133. resp.Msg = "检测完成,可进行删除操作"
  134. br.Ret = 200
  135. br.Data = resp
  136. br.Success = true
  137. br.Msg = "检测成功"
  138. }
  139. // @Title 删除分类接口
  140. // @Description 删除分类
  141. // @Param request body knowledge.DeleteClassifyReq true "type json string"
  142. // @Success 200 Ret=200,删除成功
  143. // @router /classify/delete [post]
  144. func (this *KnowledgeClassifyController) Delete() {
  145. br := new(models.BaseResponse).Init()
  146. defer func() {
  147. this.Data["json"] = br
  148. this.ServeJSON()
  149. }()
  150. var req knowledge.DeleteClassifyReq
  151. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  152. if err != nil {
  153. br.Msg = "参数解析异常!"
  154. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  155. return
  156. }
  157. if req.ClassifyId <= 0 {
  158. br.Msg = "参数错误"
  159. return
  160. }
  161. //todo 是否需要删除接口
  162. /*br.Msg = "报告分类不允许删除"
  163. br.IsSendEmail = false
  164. return*/
  165. knowledgeObj := new(knowledge.KnowledgeClassify)
  166. item, err := knowledgeObj.GetClassifyById(req.ClassifyId)
  167. if err != nil {
  168. br.Msg = "获取信息失败"
  169. br.ErrMsg = "获取信息失败,Err:" + err.Error()
  170. return
  171. }
  172. if item == nil {
  173. br.Msg = "分类不存在"
  174. return
  175. }
  176. err = knowledgeObj.Delete(req.ClassifyId)
  177. if err != nil {
  178. br.Msg = "删除失败"
  179. br.ErrMsg = "删除失败,Err:" + err.Error()
  180. return
  181. }
  182. // 被删除是二级分类且关联电话会时, 同步FICC活动分类
  183. //if item.ParentId > 0 && item.RelateTel == 1 {
  184. // go func() {
  185. // _ = yb.SyncClassifyAndFiccActivityType()
  186. // }()
  187. //}
  188. br.Ret = 200
  189. br.Success = true
  190. br.Msg = "删除成功"
  191. }
  192. // @Title 修改分类接口
  193. // @Description 修改分类
  194. // @Param request body knowledge.EditClassifyReq true "type json string"
  195. // @Success 200 Ret=200,修改成功
  196. // @router /classify/edit [post]
  197. func (this *KnowledgeClassifyController) Edit() {
  198. br := new(models.BaseResponse).Init()
  199. defer func() {
  200. this.Data["json"] = br
  201. this.ServeJSON()
  202. }()
  203. var req knowledge.EditClassifyReq
  204. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  205. if err != nil {
  206. br.Msg = "参数解析异常!"
  207. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  208. return
  209. }
  210. if req.ClassifyId <= 0 {
  211. br.Msg = "参数错误"
  212. return
  213. }
  214. if req.ClassifyName == "" {
  215. br.Msg = "分类名称不可为空"
  216. return
  217. }
  218. // 修改分类
  219. err, errMsg, isSentEmail := knowledgeServ.EditKnowledgeClassify(req.ClassifyId, req.ParentId, req.ClassifyName)
  220. if err != nil {
  221. br.Msg = "修改失败"
  222. if errMsg != "" {
  223. br.Msg = errMsg
  224. }
  225. br.ErrMsg = "修改失败,Err:" + err.Error()
  226. br.IsSendEmail = isSentEmail
  227. return
  228. }
  229. br.Ret = 200
  230. br.Success = true
  231. br.Msg = "修改成功"
  232. }
  233. // ParentClassify
  234. // @Title 获取父级分类接口
  235. // @Description 获取父级分类
  236. // @Success 200 {object} knowledge.Classify
  237. // @router /classify/parent [get]
  238. func (this *KnowledgeClassifyController) ParentClassify() {
  239. br := new(models.BaseResponse).Init()
  240. defer func() {
  241. this.Data["json"] = br
  242. this.ServeJSON()
  243. }()
  244. knowledgeObj := new(knowledge.KnowledgeClassify)
  245. items, err := knowledgeObj.GetAllClassify()
  246. if err != nil {
  247. br.Msg = "获取失败"
  248. br.ErrMsg = "获取失败,Err:" + err.Error()
  249. return
  250. }
  251. classifyIds := make([]int, 0)
  252. for i := range items {
  253. classifyIds = append(classifyIds, items[i].ClassifyId)
  254. }
  255. classifyLen := len(classifyIds)
  256. if classifyLen == 0 {
  257. br.Ret = 200
  258. br.Success = true
  259. br.Msg = "获取成功"
  260. return
  261. }
  262. resp := make([]*knowledge.KnowledgeClassifyItem, 0)
  263. for i := range items {
  264. resp = append(resp, &knowledge.KnowledgeClassifyItem{
  265. KnowledgeClassify: *items[i],
  266. })
  267. }
  268. resp = knowledgeServ.GetClassifyTreeRecursive(resp, 0)
  269. br.Data = resp
  270. br.Ret = 200
  271. br.Success = true
  272. br.Msg = "获取成功"
  273. }
  274. // @Title 根据id获取分类详情接口
  275. // @Description 根据id获取分类详情
  276. // @Param request body knowledge.FindByIdClassifyReq true "type json string"
  277. // @Success 200 {object} knowledge.Classify
  278. // @router /classify/findById [get]
  279. func (this *KnowledgeClassifyController) FindByIdClassify() {
  280. br := new(models.BaseResponse).Init()
  281. defer func() {
  282. this.Data["json"] = br
  283. this.ServeJSON()
  284. }()
  285. var req knowledge.FindByIdClassifyReq
  286. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  287. if err != nil {
  288. br.Msg = "参数解析异常!"
  289. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  290. return
  291. }
  292. knowledgeObj := new(knowledge.KnowledgeClassify)
  293. items, err := knowledgeObj.GetClassifyById(req.ClassifyId)
  294. if err != nil {
  295. br.Msg = "获取失败"
  296. br.ErrMsg = "获取失败,Err:" + err.Error()
  297. return
  298. }
  299. br.Data = items
  300. br.Ret = 200
  301. br.Success = true
  302. br.Msg = "获取成功"
  303. }
  304. // @Title 获取分类列表
  305. // @Description 获取分类列表
  306. // @Param KeyWord query string true "检索关键词"
  307. // @Param CompanyType query string false "产品类型,枚举值:'ficc','权益';不传默认返回全部"
  308. // @Param HideDayWeek query int false "是否隐藏晨周报"
  309. // @Success 200 {object} knowledge.Classify
  310. // @router /classify/list [get]
  311. func (this *KnowledgeClassifyController) ListClassify() {
  312. br := new(models.BaseResponse).Init()
  313. defer func() {
  314. this.Data["json"] = br
  315. this.ServeJSON()
  316. }()
  317. keyWord := this.GetString("KeyWord")
  318. reqEnabled, _ := this.GetInt("Enabled", -1)
  319. enabled := -1
  320. if reqEnabled == 1 {
  321. enabled = reqEnabled
  322. }
  323. knowledgeObj := new(knowledge.KnowledgeClassify)
  324. list, err := knowledgeObj.GetClassifyListByKeyword(keyWord, enabled)
  325. if err != nil {
  326. br.Msg = "获取失败"
  327. br.ErrMsg = "获取失败,Err:" + err.Error()
  328. return
  329. }
  330. if keyWord != `` {
  331. idMap := make(map[int]bool)
  332. currParentClassifyIdList := make([]int, 0)
  333. for _, v := range list {
  334. idMap[v.ClassifyId] = true
  335. if v.ParentId > 0 {
  336. currParentClassifyIdList = append(currParentClassifyIdList, v.ParentId)
  337. }
  338. }
  339. findList := list
  340. list = make([]*knowledge.KnowledgeClassifyItem, 0)
  341. tmpList, tmpErr := knowledgeServ.GetParentClassifyListByParentIdList(currParentClassifyIdList)
  342. if tmpErr != nil {
  343. br.Msg = "获取失败"
  344. br.ErrMsg = "获取上级分类信息失败,Err:" + tmpErr.Error()
  345. return
  346. }
  347. for _, v := range tmpList {
  348. if _, ok := idMap[v.ClassifyId]; !ok {
  349. list = append(list, v)
  350. }
  351. }
  352. list = append(list, findList...)
  353. }
  354. classifyIdList := make([]int, 0)
  355. for i := range list {
  356. classifyIdList = append(classifyIdList, list[i].ClassifyId)
  357. }
  358. parentIdLen := len(classifyIdList)
  359. if parentIdLen == 0 {
  360. resp := &knowledge.KnowledgeClassifyListResp{
  361. List: list,
  362. }
  363. br.Data = resp
  364. br.Ret = 200
  365. br.Success = true
  366. br.Msg = "获取成功"
  367. return
  368. }
  369. // 先将分类列表排序
  370. knowledgeServ.SortClassifyListBySortAndCreateTime(list)
  371. // 接着转换结构
  372. list = knowledgeServ.GetClassifyListTreeRecursive(list, 0)
  373. resp := new(knowledge.KnowledgeClassifyListResp)
  374. resp.List = list
  375. br.Data = resp
  376. br.Ret = 200
  377. br.Success = true
  378. br.Msg = "获取成功"
  379. }
  380. // Move
  381. // @Title 移动分类接口
  382. // @Description 移动分类
  383. // @Param request body knowledge.ClassifyMoveReq true "type json string"
  384. // @Success 200 新增成功
  385. // @router /classify/move [post]
  386. func (this *KnowledgeClassifyController) Move() {
  387. br := new(models.BaseResponse).Init()
  388. defer func() {
  389. this.Data["json"] = br
  390. this.ServeJSON()
  391. }()
  392. var req knowledge.ClassifyMoveReq
  393. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  394. if err != nil {
  395. br.Msg = "参数解析异常!"
  396. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  397. return
  398. }
  399. if req.ClassifyId <= 0 {
  400. br.Msg = "请选择分类"
  401. return
  402. }
  403. e, msg := knowledgeServ.MoveKnowledgeClassify(req)
  404. if e != nil {
  405. br.Msg = msg
  406. br.ErrMsg = "移动分类失败, Err: " + e.Error()
  407. return
  408. }
  409. br.Ret = 200
  410. br.Success = true
  411. br.Msg = "操作成功"
  412. }