classify.go 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535
  1. package help_doc
  2. import (
  3. "encoding/json"
  4. "hongze/hz_crm_api/controllers"
  5. "hongze/hz_crm_api/models"
  6. "hongze/hz_crm_api/models/help_doc"
  7. "hongze/hz_crm_api/services/data"
  8. "hongze/hz_crm_api/utils"
  9. "time"
  10. )
  11. // HelpDocClassifyController 帮助文档分类
  12. type HelpDocClassifyController struct {
  13. controllers.BaseAuthController
  14. }
  15. // ListClassify
  16. // @Title 获取分类列表
  17. // @Description 获取分类列表
  18. // @Param KeyWord query string true "检索关键词"
  19. // @Success 200 {object} models.HelpDocClassifyListResp
  20. // @router /classify/list [get]
  21. func (this *HelpDocClassifyController) ListClassify() {
  22. br := new(models.BaseResponse).Init()
  23. defer func() {
  24. this.Data["json"] = br
  25. this.ServeJSON()
  26. }()
  27. keyWord := this.GetString("KeyWord")
  28. resp := new(help_doc.HelpDocClassifyListResp)
  29. rootList, err := help_doc.GetHelpDocClassifyByParentId(0, "")
  30. if err != nil {
  31. br.Msg = "获取失败"
  32. br.ErrMsg = "获取数据失败,Err:" + err.Error()
  33. return
  34. }
  35. docAll, err := help_doc.GetAllHelpDoc()
  36. if err != nil {
  37. br.Msg = "获取失败"
  38. br.ErrMsg = "获取数据失败,Err:" + err.Error()
  39. return
  40. }
  41. classifyMap := make(map[int]int,0)
  42. for _, v := range docAll {
  43. classifyMap[v.ClassifyId] = 1
  44. }
  45. classifyAll := make([]*help_doc.HelpDocClassifyItems, 0)
  46. newClassifyAll := make([]*help_doc.HelpDocClassifyItems, 0)
  47. newRootList := make([]help_doc.HelpDocClassifyItems, 0)
  48. //parentMap := make(map[int]int)
  49. classifyAllMap := make(map[int]*help_doc.HelpDocClassifyItems, 0)
  50. rootMap := make(map[*help_doc.HelpDocClassifyItems]int, 0)
  51. if keyWord != "" {
  52. classifyAll, err = help_doc.GetAllHelpDocClassifyByKeyword("")
  53. if err != nil {
  54. br.Msg = "获取失败"
  55. br.ErrMsg = "获取数据失败,Err:" + err.Error()
  56. return
  57. }
  58. for _, v := range classifyAll {
  59. classifyAllMap[v.ClassifyId] = v
  60. }
  61. keywordClassify, err := help_doc.GetAllHelpDocClassifyByKeyword(keyWord)
  62. if err != nil {
  63. br.Msg = "获取失败"
  64. br.ErrMsg = "获取数据失败,Err:" + err.Error()
  65. return
  66. }
  67. for _, v := range keywordClassify {
  68. if v.Level == 3 {
  69. ppid := classifyAllMap[v.ParentId].ParentId
  70. //newRootList = append(newRootList, *classifyAllMap[ppid])
  71. rootMap[classifyAllMap[ppid]] = 1
  72. } else if v.Level == 2{
  73. pid := classifyAllMap[v.ParentId].ClassifyId
  74. //newRootList = append(newRootList, *classifyAllMap[pid])
  75. rootMap[classifyAllMap[pid]] = 1
  76. } else if v.Level == 1 {
  77. //newRootList = append(newRootList, *v)
  78. rootMap[v] = 1
  79. }
  80. }
  81. // 重新组合 newRootList
  82. //for _, v := range rootList {
  83. // if root,ok := rootMap[v.ClassifyId]; ok{
  84. // newRootList = append(newRootList, *root)
  85. // }
  86. //}
  87. for v, _ := range rootMap {
  88. newRootList = append(newRootList, *v)
  89. }
  90. // 重新组合 newClassifyAll
  91. for _, v := range keywordClassify {
  92. if v.Level != 1 {
  93. newClassifyAll = append(newClassifyAll, v)
  94. }
  95. }
  96. } else {
  97. classifyAll, err = help_doc.GetHelpDocClassifyAll("")
  98. if err != nil {
  99. br.Msg = "获取失败"
  100. br.ErrMsg = "获取数据失败,Err:" + err.Error()
  101. return
  102. }
  103. newRootList = rootList
  104. newClassifyAll = classifyAll
  105. }
  106. for k := range newClassifyAll {
  107. _,ok := classifyMap[newClassifyAll[k].ClassifyId]
  108. newClassifyAll[k].Disabled = ok
  109. }
  110. //rootListAll := make([]help_doc.HelpDocClassifyItems, 0)
  111. //rootListAll = rootList
  112. nodeAll := make([]*help_doc.HelpDocClassifyItems, 0)
  113. for k := range newRootList {
  114. rootNode := newRootList[k]
  115. data.HelpDocClassifyItemsMakeTree(newClassifyAll, &rootNode)
  116. nodeAll = append(nodeAll, &rootNode)
  117. }
  118. //添加分类用,只有一二级目录
  119. classifyLevel, err := help_doc.GetHelpDocClassifyLevel()
  120. if err != nil {
  121. br.Msg = "获取失败"
  122. br.ErrMsg = "获取数据失败,Err:" + err.Error()
  123. return
  124. }
  125. for k := range classifyLevel {
  126. _,ok := classifyMap[classifyLevel[k].ClassifyId]
  127. classifyLevel[k].Disabled = ok
  128. }
  129. //rootListLevel := make([]help_doc.HelpDocClassifyItems, 0)
  130. //rootListLevel = rootList
  131. nodeAllTwoLevel := make([]*help_doc.HelpDocClassifyItems, 0)
  132. for k := range rootList {
  133. rootNode := rootList[k]
  134. data.HelpDocClassifyItemsMakeTree(classifyLevel, &rootNode)
  135. nodeAllTwoLevel = append(nodeAllTwoLevel, &rootNode)
  136. }
  137. resp.AllNodes = nodeAll
  138. resp.TwoLevelNodes = nodeAllTwoLevel
  139. br.Ret = 200
  140. br.Success = true
  141. br.Msg = "获取成功"
  142. br.Data = resp
  143. }
  144. // @Title 新增分类
  145. // @Description 新增分类接口
  146. // @Param ClassifyName int true "分类名称"
  147. // @Param ParentId query int true "父级Id 添加父级时为0"
  148. // @Param Sort query string false "排序"
  149. // @Success 200 新增成功
  150. // @router /classify/add [post]
  151. func (this *HelpDocClassifyController) AddClassify() {
  152. br := new(models.BaseResponse).Init()
  153. defer func() {
  154. this.Data["json"] = br
  155. this.ServeJSON()
  156. }()
  157. var req help_doc.AddHelpDocClassifyReq
  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.HelpDocClassifyName == "" {
  165. br.Msg = "请输入分类名称"
  166. br.IsSendEmail = false
  167. return
  168. }
  169. if req.ParentId < 0 {
  170. br.Msg = "参数错误"
  171. br.IsSendEmail = false
  172. return
  173. }
  174. count, err := help_doc.GetHelpDocClassifyCount(req.HelpDocClassifyName, req.ParentId)
  175. if err != nil {
  176. br.Msg = "判断名称是否已存在失败"
  177. br.ErrMsg = "判断名称是否已存在失败,Err:" + err.Error()
  178. return
  179. }
  180. if count > 0 {
  181. br.Msg = "分类名称已存在,请重新输入"
  182. br.IsSendEmail = false
  183. return
  184. }
  185. //获取该层级下最大的排序数
  186. maxSort, err := help_doc.GetHelpDocClassifyMaxSort(req.ParentId)
  187. classify := new(help_doc.HelpDocClassify)
  188. classify.ParentId = req.ParentId
  189. classify.ClassifyName = req.HelpDocClassifyName
  190. classify.CreateTime = time.Now()
  191. classify.ModifyTime = time.Now()
  192. classify.SysUserId = this.SysUser.AdminId
  193. classify.SysUserRealName = this.SysUser.RealName
  194. classify.Level = req.Level + 1
  195. classify.Sort = maxSort + 1
  196. _, err = help_doc.AddHelpDocClassify(classify)
  197. if err != nil {
  198. br.Msg = "保存分类失败"
  199. br.ErrMsg = "保存分类失败,Err:" + err.Error()
  200. return
  201. }
  202. br.Ret = 200
  203. br.Msg = "保存成功"
  204. br.Success = true
  205. }
  206. // @Title 编辑分类
  207. // @Description 编辑分类接口
  208. // @Param ClassifyId int true "分类Id"
  209. // @Param ClassifyName string true "分类名称"
  210. // @Param ParentId query int true "父级Id 添加父级时为0"
  211. // @Param Sort query string false "排序"
  212. // @Success 200 保存成功
  213. // @router /classify/edit [post]
  214. func (this *HelpDocClassifyController) EditClassify() {
  215. br := new(models.BaseResponse).Init()
  216. defer func() {
  217. this.Data["json"] = br
  218. this.ServeJSON()
  219. }()
  220. var req help_doc.EditHelpDocClassifyReq
  221. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  222. if err != nil {
  223. br.Msg = "参数解析异常!"
  224. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  225. return
  226. }
  227. if req.HelpDocClassifyName == "" {
  228. br.Msg = "请输入分类名称"
  229. br.IsSendEmail = false
  230. return
  231. }
  232. if req.HelpDocClassifyId <= 0 {
  233. br.Msg = "参数错误"
  234. br.IsSendEmail = false
  235. return
  236. }
  237. item, err := help_doc.GetHelpDocClassifyById(req.HelpDocClassifyId)
  238. if err != nil {
  239. br.Msg = "保存失败"
  240. br.Msg = "获取分类信息失败,Err:" + err.Error()
  241. return
  242. }
  243. if item.ClassifyName != req.HelpDocClassifyName {
  244. count, err := help_doc.GetHelpDocClassifyCount(req.HelpDocClassifyName, item.ParentId)
  245. if err != nil {
  246. br.Msg = "判断名称是否已存在失败"
  247. br.ErrMsg = "判断名称是否已存在失败,Err:" + err.Error()
  248. return
  249. }
  250. if count > 0 {
  251. br.Msg = "分类名称已存在,请重新输入"
  252. br.IsSendEmail = false
  253. return
  254. }
  255. err = help_doc.EditHelpDocClassify(req.HelpDocClassifyId, req.HelpDocClassifyName)
  256. if err != nil {
  257. br.Msg = "保存失败"
  258. br.ErrMsg = "保存失败,Err:" + err.Error()
  259. return
  260. }
  261. }
  262. br.Ret = 200
  263. br.Msg = "保存成功"
  264. br.Success = true
  265. br.IsAddLog = true
  266. }
  267. // @Title 删除分类列表
  268. // @Description 删除分类列表
  269. // @Param ClassifyId int true "分类名称"
  270. // @Success 200 删除成功
  271. // @router /classify/delete [get]
  272. func (this *HelpDocClassifyController) DelClassify() {
  273. br := new(models.BaseResponse).Init()
  274. defer func() {
  275. this.Data["json"] = br
  276. this.ServeJSON()
  277. }()
  278. sysUser := this.SysUser
  279. if sysUser == nil {
  280. br.Msg = "请登录"
  281. br.ErrMsg = "请登录,SysUser Is Empty"
  282. br.Ret = 408
  283. return
  284. }
  285. classifyId, _ := this.GetInt("ClassifyId")
  286. _, err := help_doc.GetHelpDocClassifyById(classifyId)
  287. if err != nil {
  288. if err.Error() == utils.ErrNoRow() {
  289. br.Msg = "当前分类不存在"
  290. br.ErrMsg = "当前分类不存在"
  291. return
  292. }
  293. br.Msg = "获取分类信息失败"
  294. br.ErrMsg = "获取分类信息失败,Err:" + err.Error()
  295. return
  296. }
  297. count, err := help_doc.GetHelpDocClassifyChildCounts(classifyId)
  298. if err != nil && err.Error() != utils.ErrNoRow() {
  299. br.Msg = "获取信息失败"
  300. br.ErrMsg = "获取失败,Err:" + err.Error()
  301. return
  302. }
  303. if count > 0 {
  304. br.Msg = "请先删除该分类下关联分类"
  305. br.Ret = 403
  306. return
  307. }
  308. reportCount, e := help_doc.GetHelpDocCounts(classifyId)
  309. if e != nil && e.Error() != utils.ErrNoRow() {
  310. br.Msg = "获取信息失败"
  311. br.ErrMsg = "获取失败,Err:" + e.Error()
  312. return
  313. }
  314. if reportCount > 0 {
  315. br.Msg = "该分类已关联文章,不允许删除!"
  316. br.Ret = 403
  317. return
  318. }
  319. if err = help_doc.DeleteHelpDocClassify(classifyId); err != nil {
  320. br.Msg = "删除失败"
  321. br.ErrMsg = "删除分类失败, Err: " + err.Error()
  322. return
  323. }
  324. br.Ret = 200
  325. br.Success = true
  326. br.Msg = "删除成功"
  327. }
  328. // ChartClassifyMove
  329. // @Title 分类移动接口
  330. // @Description 分类移动接口
  331. // @Success 200 {object} data_manage.MoveChartClassifyReq
  332. // @router /classify/move [post]
  333. func (this *HelpDocClassifyController) ClassifyMove() {
  334. br := new(models.BaseResponse).Init()
  335. defer func() {
  336. this.Data["json"] = br
  337. this.ServeJSON()
  338. }()
  339. sysUser := this.SysUser
  340. if sysUser == nil {
  341. br.Msg = "请登录"
  342. br.ErrMsg = "请登录,SysUser Is Empty"
  343. br.Ret = 408
  344. return
  345. }
  346. var req help_doc.MoveClassifyReq
  347. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  348. if err != nil {
  349. br.Msg = "参数解析异常!"
  350. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  351. return
  352. }
  353. if req.ClassifyId <= 0 {
  354. br.Msg = "参数错误"
  355. br.ErrMsg = "分类id小于等于0"
  356. return
  357. }
  358. //判断分类是否存在
  359. classifyInfo, err := help_doc.GetHelpDocClassifyById(req.ClassifyId)
  360. if err != nil {
  361. br.Msg = "移动失败"
  362. br.ErrMsg = "获取分类信息失败,Err:" + err.Error()
  363. return
  364. }
  365. updateCol := make([]string, 0)
  366. //判断上级id是否一致,如果不一致的话,那么需要移动该分类层级
  367. if classifyInfo.ParentId != req.ParentClassifyId && req.ParentClassifyId != 0 {
  368. parentClassifyInfo, err := help_doc.GetHelpDocClassifyById(req.ParentClassifyId)
  369. if err != nil {
  370. br.Msg = "移动失败"
  371. br.ErrMsg = "获取上级分类信息失败,Err:" + err.Error()
  372. return
  373. }
  374. classifyInfo.ParentId = parentClassifyInfo.ClassifyId
  375. classifyInfo.Level = parentClassifyInfo.Level + 1
  376. classifyInfo.ModifyTime = time.Now()
  377. updateCol = append(updateCol, "ParentId", "Level", "ModifyTime")
  378. }
  379. //如果有传入 上一个兄弟节点分类id
  380. if req.PrevClassifyId > 0 {
  381. //上一个兄弟节点
  382. prevClassify, err := help_doc.GetHelpDocClassifyById(req.PrevClassifyId)
  383. if err != nil {
  384. br.Msg = "移动失败"
  385. br.ErrMsg = "获取上一个兄弟节点分类信息失败,Err:" + err.Error()
  386. return
  387. }
  388. //如果是移动在两个兄弟节点之间
  389. if req.NextClassifyId > 0 {
  390. //下一个兄弟节点
  391. nextClassify, err := help_doc.GetHelpDocClassifyById(req.NextClassifyId)
  392. if err != nil {
  393. br.Msg = "移动失败"
  394. br.ErrMsg = "获取下一个兄弟节点分类信息失败,Err:" + err.Error()
  395. return
  396. }
  397. //如果上一个兄弟与下一个兄弟的排序权重是一致的,那么需要将下一个兄弟(以及下个兄弟的同样排序权重)的排序权重+2,自己变成上一个兄弟的排序权重+1
  398. if prevClassify.Sort == nextClassify.Sort || prevClassify.Sort == classifyInfo.Sort {
  399. //变更兄弟节点的排序
  400. updateSortStr := `sort + 2`
  401. _ = help_doc.UpdateHelpDocClassifySortByParentId(prevClassify.ParentId, prevClassify.ClassifyId, prevClassify.Sort, updateSortStr)
  402. } else {
  403. //如果下一个兄弟的排序权重正好是上个兄弟节点的下一层,那么需要再加一层了
  404. if nextClassify.Sort-prevClassify.Sort == 1 {
  405. //变更兄弟节点的排序
  406. updateSortStr := `sort + 1`
  407. _ = help_doc.UpdateHelpDocClassifySortByParentId(prevClassify.ParentId, 0, prevClassify.Sort, updateSortStr)
  408. }
  409. }
  410. }
  411. classifyInfo.Sort = prevClassify.Sort + 1
  412. classifyInfo.ModifyTime = time.Now()
  413. updateCol = append(updateCol, "Sort", "ModifyTime")
  414. } else {
  415. firstClassify, err := help_doc.GetFirstHelpDocClassifyByParentId(classifyInfo.ParentId)
  416. if err != nil && err.Error() != utils.ErrNoRow() {
  417. br.Msg = "移动失败"
  418. br.ErrMsg = "获取获取当前父级分类下的排序第一条的分类信息失败,Err:" + err.Error()
  419. return
  420. }
  421. //如果该分类下存在其他分类,且第一个其他分类的排序等于0,那么需要调整排序
  422. if firstClassify != nil && firstClassify.Sort == 0 {
  423. updateSortStr := ` sort + 1 `
  424. _ = help_doc.UpdateHelpDocClassifySortByParentId(firstClassify.ParentId, firstClassify.ClassifyId-1, 0, updateSortStr)
  425. }
  426. classifyInfo.Sort = 0 //那就是排在第一位
  427. classifyInfo.ModifyTime = time.Now()
  428. updateCol = append(updateCol, "Sort", "ModifyTime")
  429. }
  430. //更新
  431. if len(updateCol) > 0 {
  432. err = classifyInfo.Update(updateCol)
  433. if err != nil {
  434. br.Msg = "移动失败"
  435. br.ErrMsg = "修改失败,Err:" + err.Error()
  436. return
  437. }
  438. }
  439. br.Ret = 200
  440. br.Success = true
  441. br.Msg = "移动成功"
  442. }
  443. // @Title 编辑分类可见权限
  444. // @Description 编辑分类可见权限接口
  445. // @Param ClassifyId int true "分类Id"
  446. // @Param ClassifyName string true "分类名称"
  447. // @Param ParentId query int true "父级Id 添加父级时为0"
  448. // @Param Sort query string false "排序"
  449. // @Success 200 保存成功
  450. // @router /classify/visible/edit [post]
  451. func (this *HelpDocClassifyController) EditClassifyVisible() {
  452. br := new(models.BaseResponse).Init()
  453. defer func() {
  454. this.Data["json"] = br
  455. this.ServeJSON()
  456. }()
  457. var req help_doc.EditHelpDocClassifyVisibleReq
  458. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  459. if err != nil {
  460. br.Msg = "参数解析异常!"
  461. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  462. return
  463. }
  464. if req.HelpDocClassifyId <= 0 {
  465. br.Msg = "参数错误"
  466. br.IsSendEmail = false
  467. return
  468. }
  469. //item, err := help_doc.GetHelpDocClassifyById(req.HelpDocClassifyId)
  470. //if err != nil {
  471. // br.Msg = "保存失败"
  472. // br.Msg = "获取分类信息失败,Err:" + err.Error()
  473. // return
  474. //}
  475. err = help_doc.EditHelpDocClassifyVisible(req.HelpDocClassifyId, req.VisibleBusinessIds)
  476. if err != nil {
  477. br.Msg = "保存失败"
  478. br.ErrMsg = "保存失败,Err:" + err.Error()
  479. return
  480. }
  481. br.Ret = 200
  482. br.Msg = "保存成功"
  483. br.Success = true
  484. br.IsAddLog = true
  485. }