classify.go 15 KB

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