classify.go 12 KB

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