classify.go 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546
  1. package knowledge
  2. import (
  3. "errors"
  4. "eta_gn/eta_api/models/knowledge"
  5. "eta_gn/eta_api/services/alarm_msg"
  6. "eta_gn/eta_api/utils"
  7. "fmt"
  8. "sort"
  9. "time"
  10. )
  11. // MoveKnowledgeClassify 移动分类
  12. func MoveKnowledgeClassify(req knowledge.ClassifyMoveReq) (err error, errMsg string) {
  13. knowledgeObj := new(knowledge.KnowledgeClassify)
  14. classifyId := req.ClassifyId
  15. prevClassifyId := req.PrevClassifyId
  16. nextClassifyId := req.NextClassifyId
  17. //如果有传入 上一个兄弟节点分类id
  18. var (
  19. classifyInfo *knowledge.KnowledgeClassify
  20. prevClassify *knowledge.KnowledgeClassify
  21. nextClassify *knowledge.KnowledgeClassify
  22. prevSort int
  23. nextSort int
  24. )
  25. // 移动对象为分类, 判断权限
  26. classifyInfo, err = knowledgeObj.GetClassifyById(classifyId)
  27. if err != nil {
  28. if utils.IsErrNoRow(err) {
  29. errMsg = "分类不存在, 请刷新页面"
  30. err = fmt.Errorf("获取分类信息失败,Err:" + err.Error())
  31. return
  32. }
  33. errMsg = "移动失败"
  34. err = fmt.Errorf("获取分类信息失败,Err:" + err.Error())
  35. return
  36. } else if classifyInfo.ClassifyId == 0 {
  37. errMsg = "分类不存在, 请刷新页面"
  38. err = fmt.Errorf("获取分类信息失败")
  39. return
  40. }
  41. parentClassifyId := classifyInfo.ParentId
  42. if prevClassifyId > 0 {
  43. prevClassify, err = knowledgeObj.GetClassifyById(prevClassifyId)
  44. if err != nil {
  45. if utils.IsErrNoRow(err) {
  46. errMsg = "上一个分类不存在, 请刷新页面"
  47. err = fmt.Errorf("获取分类信息失败,Err:" + err.Error())
  48. return
  49. }
  50. errMsg = "移动失败"
  51. err = fmt.Errorf("获取上一个兄弟节点分类信息失败,Err:" + err.Error())
  52. return
  53. }
  54. if prevClassify.ParentId != parentClassifyId {
  55. errMsg = "禁止拖动到其他节点"
  56. err = fmt.Errorf(errMsg)
  57. return
  58. }
  59. prevSort = prevClassify.Sort
  60. }
  61. if nextClassifyId > 0 {
  62. //下一个兄弟节点
  63. nextClassify, err = knowledgeObj.GetClassifyById(nextClassifyId)
  64. if err != nil {
  65. if utils.IsErrNoRow(err) {
  66. errMsg = "下一个分类不存在, 请刷新页面"
  67. err = fmt.Errorf("获取分类信息失败,Err:" + err.Error())
  68. return
  69. }
  70. errMsg = "移动失败"
  71. err = fmt.Errorf("获取下一个兄弟节点分类信息失败,Err:" + err.Error())
  72. return
  73. }
  74. if nextClassify.ParentId != parentClassifyId {
  75. errMsg = "禁止拖动到其他节点"
  76. err = fmt.Errorf(errMsg)
  77. return
  78. }
  79. nextSort = nextClassify.Sort
  80. }
  81. err, errMsg = moveKnowledgeClassify(classifyInfo, prevClassify, nextClassify, parentClassifyId, prevSort, nextSort, classifyInfo.ResourceType)
  82. return
  83. }
  84. // moveKnowledgeClassify 移动分类
  85. func moveKnowledgeClassify(classifyInfo, prevClassify, nextClassify *knowledge.KnowledgeClassify, parentId, prevSort, nextSort, resourceType int) (err error, errMsg string) {
  86. knowledgeObj := new(knowledge.KnowledgeClassify)
  87. updateCol := make([]string, 0)
  88. //判断上级id是否一致,如果不一致的话,那么需要移动该分类层级
  89. if classifyInfo.ParentId != parentId {
  90. errMsg = "移动失败"
  91. err = fmt.Errorf("不支持目录层级变更")
  92. return
  93. }
  94. if prevSort > 0 {
  95. //如果是移动在两个兄弟节点之间
  96. if nextSort > 0 {
  97. //下一个兄弟节点
  98. //如果上一个兄弟与下一个兄弟的排序权重是一致的,那么需要将下一个兄弟(以及下个兄弟的同样排序权重)的排序权重+2,自己变成上一个兄弟的排序权重+1
  99. if prevSort == nextSort || prevSort == classifyInfo.Sort {
  100. //变更兄弟节点的排序
  101. updateSortStr := `sort + 2`
  102. //变更分类
  103. if prevClassify != nil {
  104. _ = knowledgeObj.UpdateClassifySortByParentId(parentId, prevClassify.ClassifyId, prevClassify.Sort, updateSortStr, resourceType)
  105. } else {
  106. _ = knowledgeObj.UpdateClassifySortByParentId(parentId, 0, prevSort, updateSortStr, resourceType)
  107. }
  108. } else {
  109. //如果下一个兄弟的排序权重正好是上个兄弟节点的下一层,那么需要再加一层了
  110. if nextSort-prevSort == 1 {
  111. //变更兄弟节点的排序
  112. updateSortStr := `sort + 1`
  113. //变更分类
  114. if prevClassify != nil {
  115. _ = knowledgeObj.UpdateClassifySortByParentId(parentId, prevClassify.ClassifyId, prevSort, updateSortStr, resourceType)
  116. } else {
  117. _ = knowledgeObj.UpdateClassifySortByParentId(parentId, 0, prevSort, updateSortStr, resourceType)
  118. }
  119. }
  120. }
  121. }
  122. classifyInfo.Sort = prevSort + 1
  123. classifyInfo.ModifyTime = time.Now()
  124. updateCol = append(updateCol, "Sort", "ModifyTime")
  125. } else if prevClassify == nil && nextClassify == nil && parentId > 0 {
  126. //处理只拖动到目录里,默认放到目录底部的情况
  127. var maxSort int
  128. maxSort, err = knowledgeObj.GetMaxSortByParentId(parentId)
  129. if err != nil {
  130. errMsg = "移动失败"
  131. err = fmt.Errorf("查询组内排序信息失败,Err:" + err.Error())
  132. return
  133. }
  134. classifyInfo.Sort = maxSort + 1 //那就是排在组内最后一位
  135. classifyInfo.ModifyTime = time.Now()
  136. updateCol = append(updateCol, "Sort", "ModifyTime")
  137. } else {
  138. // 拖动到父级分类的第一位
  139. firstPermission, tmpErr := knowledgeObj.GetFirstClassifyByParentId(parentId)
  140. if tmpErr != nil && !utils.IsErrNoRow(tmpErr) {
  141. errMsg = "移动失败"
  142. err = fmt.Errorf("获取获取当前父级分类下的排序第一条的分类信息失败,Err:" + tmpErr.Error())
  143. return
  144. }
  145. //如果该分类下存在其他分类,且第一个其他分类的排序等于0,那么需要调整排序
  146. if firstPermission != nil && firstPermission.ClassifyId > 0 && firstPermission.Sort == 0 {
  147. updateSortStr := ` sort + 1 `
  148. _ = knowledgeObj.UpdateClassifySortByParentId(parentId, firstPermission.ClassifyId-1, 0, updateSortStr, resourceType)
  149. }
  150. classifyInfo.Sort = 0 //那就是排在第一位
  151. classifyInfo.ModifyTime = time.Now()
  152. updateCol = append(updateCol, "Sort", "ModifyTime")
  153. }
  154. //更新
  155. if len(updateCol) > 0 {
  156. err = classifyInfo.Update(updateCol)
  157. if err != nil {
  158. errMsg = "移动失败"
  159. err = fmt.Errorf("修改失败,Err:" + err.Error())
  160. return
  161. }
  162. }
  163. return
  164. }
  165. // AddKnowledgeClassify
  166. // @Description: 添加报告分类
  167. // @author: Roc
  168. // @datetime 2024-06-17 11:01:21
  169. // @param classifyName string
  170. // @param parentId int
  171. // @param chartPermissionIdList []int
  172. // @return err error
  173. // @return errMsg string
  174. // @return isSendEmail bool
  175. func AddKnowledgeClassify(classifyName string, parentId, resourceType int) (err error, errMsg string, isSendEmail bool) {
  176. isSendEmail = true
  177. errMsg = `添加失败`
  178. knowledgeObj := new(knowledge.KnowledgeClassify)
  179. _, err = knowledgeObj.GetClassifyByName(classifyName, parentId, resourceType)
  180. if err != nil && !utils.IsErrNoRow(err) {
  181. errMsg = "获取分类信息失败"
  182. return
  183. }
  184. if err == nil {
  185. errMsg = "分类名称:" + classifyName + "已存在"
  186. isSendEmail = false
  187. err = errors.New(errMsg)
  188. return
  189. } else {
  190. err = nil
  191. }
  192. level := 1
  193. // 父级分类
  194. var parentClassifyItem *knowledge.KnowledgeClassify
  195. // 父级分类下的子分类数量
  196. var childClassifyCount int
  197. if parentId > 0 {
  198. // 获取父级分类信息
  199. parentClassifyItem, err = knowledgeObj.GetClassifyById(parentId)
  200. if err != nil {
  201. errMsg = "获取父级分类信息失败"
  202. if utils.IsErrNoRow(err) {
  203. errMsg = "父级分类不存在"
  204. }
  205. return
  206. }
  207. if parentClassifyItem.ResourceType != resourceType {
  208. errMsg = "父级分类与当前分类类型不一致"
  209. err = fmt.Errorf(errMsg)
  210. return
  211. }
  212. level = parentClassifyItem.Level + 1
  213. if level > 3 {
  214. errMsg = "分类层级不可超过三级"
  215. isSendEmail = false
  216. return
  217. }
  218. // 获取父级分类下的子分类数量
  219. childClassifyCount, err = knowledgeObj.GetCountClassifyChildByParentId(parentId)
  220. if err != nil {
  221. errMsg = "获取父级分类的子分类信息失败"
  222. return
  223. }
  224. }
  225. nowTime := time.Now().Local()
  226. classify := new(knowledge.KnowledgeClassify)
  227. maxSort, err := classify.GetMaxSort()
  228. if err != nil {
  229. errMsg = "操作失败"
  230. err = errors.New("查询品种排序失败, Err: " + err.Error())
  231. return
  232. }
  233. classify.ClassifyName = classifyName
  234. classify.ParentId = parentId
  235. classify.CreateTime = nowTime
  236. classify.ModifyTime = nowTime
  237. classify.Sort = maxSort + 1
  238. classify.Enabled = 1
  239. classify.Level = level
  240. classify.ResourceType = resourceType
  241. err = knowledgeObj.Add(classify)
  242. if err != nil {
  243. return
  244. }
  245. fmt.Println("classify.ClassifyId:")
  246. fmt.Println(classify.ClassifyId)
  247. // 如果父级分类不为空的话,那么就标记有子级分类,同时
  248. if parentClassifyItem != nil {
  249. /*parentClassifyItem.HasChild = 1
  250. err = parentClassifyItem.Update([]string{"HasChild"})
  251. if err != nil {
  252. return
  253. }
  254. */
  255. // 如果以前没有子级分类,那么就继承父级分类下的章节类型(创建新的章节与分类的关系)
  256. if childClassifyCount <= 0 {
  257. moveResourceByAddClassify(parentClassifyItem, classify, resourceType)
  258. }
  259. }
  260. return
  261. }
  262. // 关于研报分类,因只允许报告或品种关联在最小分类下,所以当某个父分类(非三级分类)已关联报告或品种,需要在该父分类下增加子分类,则第一个子分类添加成功时,默认把该父分类关联的品种和报告转移至新创建的子分类(第一个子分类)下
  263. // moveReportByAddClassify
  264. // @Description: 报告和章节的转移
  265. // @author: Roc
  266. // @datetime 2024-06-17 16:29:56
  267. // @param parentClassifyInfo *knowledge.KnowledgeClassify
  268. // @param currClassifyInfo *knowledge.KnowledgeClassify
  269. // @return err error
  270. func moveResourceByAddClassify(parentClassifyInfo, currClassifyInfo *knowledge.KnowledgeClassify, resourceType int) (err error) {
  271. defer func() {
  272. if err != nil {
  273. msg := fmt.Sprint("历史事件更改分类失败,父级分类ID:", parentClassifyInfo.ClassifyId, ";当前分类ID:", currClassifyInfo.ClassifyId, ";错误信息:", err.Error())
  274. utils.FileLog.Error(msg)
  275. go alarm_msg.SendAlarmMsg(msg, 3)
  276. }
  277. }()
  278. if currClassifyInfo.Level > 3 {
  279. err = errors.New("父级分类不支持三级分类以上")
  280. return
  281. }
  282. // 报告的分类归属调整,转为下一级的分类
  283. var condition string
  284. pars := make([]interface{}, 0)
  285. condition += ` AND resource_type = ? `
  286. pars = append(pars, resourceType)
  287. // 查询是需要转移的资源数量
  288. condition += ` AND classify_id = ? `
  289. pars = append(pars, parentClassifyInfo.ClassifyId)
  290. idList, err := knowledge.GetKnowledgeResourceIdList(condition, pars)
  291. if err != nil {
  292. err = fmt.Errorf("查询需要转移的资源数量失败,Err: %s", err.Error())
  293. return
  294. }
  295. if len(idList) == 0 { //没有需要转移的资源
  296. return
  297. }
  298. newClassifyId := currClassifyInfo.ClassifyId
  299. if currClassifyInfo.Level == 2 {
  300. //查询排名第一的子分类
  301. classifyObj := new(knowledge.KnowledgeClassify)
  302. childClassify, e := classifyObj.GetFirstClassifyByParentId(currClassifyInfo.ClassifyId)
  303. if e == nil {
  304. newClassifyId = childClassify.ClassifyId
  305. }
  306. }
  307. resourceObj := new(knowledge.KnowledgeResource)
  308. // 批量事件的分类ID
  309. err = resourceObj.UpdateClassifyIdByIds(idList, newClassifyId, resourceType)
  310. return
  311. }
  312. func EditKnowledgeClassify(classifyId, parentId int, classifyName string, resourceType int) (err error, errMsg string, isSendEmail bool) {
  313. isSendEmail = true
  314. errMsg = `修改失败`
  315. knowledgeObj := new(knowledge.KnowledgeClassify)
  316. item, err := knowledgeObj.GetClassifyById(classifyId)
  317. if err != nil {
  318. errMsg = "获取分类信息失败"
  319. if utils.IsErrNoRow(err) {
  320. errMsg = "分类不存在, 或已被删除"
  321. isSendEmail = false
  322. }
  323. return
  324. }
  325. if item.ResourceType != resourceType {
  326. errMsg = "分类类型错误"
  327. err = errors.New("分类类型错误")
  328. return
  329. }
  330. if (parentId == 0 && item.ParentId > 0) || (parentId > 0 && item.ParentId == 0) {
  331. err = errors.New("不允许修改分类层级")
  332. errMsg = "不允许修改分类层级"
  333. return
  334. }
  335. // 父级分类
  336. var parentClassifyItem *knowledge.KnowledgeClassify
  337. // 父级分类下的子分类数量
  338. var childClassifyCount int
  339. if parentId > 0 {
  340. // 获取父级分类信息
  341. parentClassifyItem, err = knowledgeObj.GetClassifyById(parentId)
  342. if err != nil {
  343. errMsg = "获取父级分类信息失败"
  344. if utils.IsErrNoRow(err) {
  345. errMsg = "父级分类不存在"
  346. }
  347. return
  348. }
  349. if parentClassifyItem.ResourceType != resourceType {
  350. errMsg = "父级分类与当前分类类型不一致"
  351. err = fmt.Errorf(errMsg)
  352. return
  353. }
  354. if parentClassifyItem.Level+1 != item.Level {
  355. errMsg = "不允许修改分类层级"
  356. err = errors.New("不允许修改分类层级")
  357. return
  358. }
  359. level := parentClassifyItem.Level + 1
  360. if level > 3 {
  361. errMsg = "分类层级不可超过三级"
  362. isSendEmail = false
  363. return
  364. }
  365. // 获取父级分类下的子分类数量
  366. childClassifyCount, err = knowledgeObj.GetCountClassifyChildByParentId(parentId)
  367. if err != nil {
  368. errMsg = "获取父级分类的子分类信息失败"
  369. return
  370. }
  371. }
  372. //originName := item.ClassifyName
  373. // 重名校验
  374. existName, e := knowledgeObj.GetClassifyByName(classifyName, parentId, item.ResourceType)
  375. if e != nil && !utils.IsErrNoRow(e) {
  376. errMsg = "分类名称已存在"
  377. err = errors.New("获取重名分类失败, Err: " + e.Error())
  378. return
  379. }
  380. if existName != nil && existName.ClassifyId > 0 && existName.ClassifyId != item.ClassifyId {
  381. errMsg = "分类名称:" + classifyName + "已存在"
  382. err = errors.New(errMsg)
  383. isSendEmail = false
  384. return
  385. }
  386. item.ClassifyName = classifyName
  387. item.ParentId = parentId
  388. item.ModifyTime = time.Now().Local()
  389. cols := make([]string, 0)
  390. cols = append(cols, "ParentId", "ClassifyName", "ModifyTime")
  391. err = item.Update(cols)
  392. if err != nil {
  393. return
  394. }
  395. // 修改父级分类下相关的事件,找到当前分类的最小分类,把上一级的报告挪到最小分类上
  396. // 如果父级分类不为空的话,那么就标记有子级分类,同时
  397. if parentClassifyItem != nil {
  398. /*parentClassifyItem.HasChild = 1
  399. err = parentClassifyItem.Update([]string{"HasChild"})
  400. if err != nil {
  401. return
  402. }*/
  403. // 如果以前没有子级分类,那么就继承父级分类下的章节类型(创建新的章节与分类的关系)
  404. if childClassifyCount <= 0 {
  405. moveResourceByAddClassify(parentClassifyItem, item, resourceType)
  406. }
  407. }
  408. return
  409. }
  410. // GetClassifyTreeRecursive 递归获取分类树形结构
  411. func GetClassifyTreeRecursive(list []*knowledge.KnowledgeClassifyItem, parentId int) []*knowledge.KnowledgeClassifyItem {
  412. res := make([]*knowledge.KnowledgeClassifyItem, 0)
  413. for _, v := range list {
  414. if v.ParentId == parentId {
  415. v.Child = GetClassifyTreeRecursive(list, v.ClassifyId)
  416. res = append(res, v)
  417. }
  418. }
  419. return res
  420. }
  421. // GetParentClassifyListByParentIdList
  422. // @Description: 递归获取父级分类信息,正常来讲只有三次
  423. // @author: Roc
  424. // @datetime 2024-06-19 13:23:33
  425. // @param parentClassifyIdList []int
  426. // @return list []*knowledge.KnowledgeClassifyItem
  427. // @return err error
  428. func GetParentClassifyListByParentIdList(parentClassifyIdList []int) (list []*knowledge.KnowledgeClassifyItem, err error) {
  429. knowledgeObj := new(knowledge.KnowledgeClassify)
  430. num := len(parentClassifyIdList)
  431. if num <= 0 {
  432. return
  433. }
  434. list, err = knowledgeObj.GetClassifyListByParentIdList(parentClassifyIdList)
  435. if err != nil {
  436. return
  437. }
  438. // 是否还有上级
  439. {
  440. currParentClassifyIdList := make([]int, 0)
  441. for _, v := range list {
  442. if v.ParentId > 0 {
  443. currParentClassifyIdList = append(currParentClassifyIdList, v.ParentId)
  444. }
  445. }
  446. if len(currParentClassifyIdList) > 0 {
  447. tmpList, tmpErr := GetParentClassifyListByParentIdList(currParentClassifyIdList)
  448. if tmpErr != nil {
  449. err = tmpErr
  450. return
  451. }
  452. list = append(tmpList, list...)
  453. }
  454. }
  455. return
  456. }
  457. // GetClassifyListTreeRecursive
  458. // @Description: 递归获取分类树形结构
  459. // @author: Roc
  460. // @datetime 2024-06-19 13:23:28
  461. // @param list []*knowledge.KnowledgeClassifyItem
  462. // @param parentId int
  463. // @return []*knowledge.KnowledgeClassifyItem
  464. func GetClassifyListTreeRecursive(list []*knowledge.KnowledgeClassifyItem, parentId int) []*knowledge.KnowledgeClassifyItem {
  465. res := make([]*knowledge.KnowledgeClassifyItem, 0)
  466. for _, v := range list {
  467. if v.ParentId == parentId {
  468. v.Child = GetClassifyListTreeRecursive(list, v.ClassifyId)
  469. res = append(res, v)
  470. }
  471. }
  472. // 前端的JP需要我这么返回
  473. if len(res) <= 0 {
  474. res = nil
  475. }
  476. return res
  477. }
  478. // BySortAndCreateTime 用来排序,先按Sort字段升序排序,若Sort相同,则按照CreateTime字段升序排序。
  479. type BySortAndCreateTime []*knowledge.KnowledgeClassifyItem
  480. func (a BySortAndCreateTime) Len() int {
  481. return len(a)
  482. }
  483. func (a BySortAndCreateTime) Swap(i, j int) {
  484. a[i], a[j] = a[j], a[i]
  485. }
  486. func (a BySortAndCreateTime) Less(i, j int) bool {
  487. if a[i].Sort == a[j].Sort {
  488. return a[i].CreateTime.Before(a[j].CreateTime)
  489. }
  490. return a[i].Sort < a[j].Sort
  491. }
  492. // SortClassifyListBySortAndCreateTime sorts the ClassifyList slice by Sort and then CreateTime in ascending order.
  493. func SortClassifyListBySortAndCreateTime(classifyList []*knowledge.KnowledgeClassifyItem) {
  494. sort.Sort(BySortAndCreateTime(classifyList))
  495. }