classify.go 15 KB

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