classify.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  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. func MoveKnowledgeClassify(req knowledge.ClassifyMoveReq) (err error, errMsg string) {
  12. knowledgeObj := new(knowledge.KnowledgeClassify)
  13. classifyId := req.ClassifyId
  14. prevClassifyId := req.PrevClassifyId
  15. nextClassifyId := req.NextClassifyId
  16. var (
  17. classifyInfo *knowledge.KnowledgeClassify
  18. prevClassify *knowledge.KnowledgeClassify
  19. nextClassify *knowledge.KnowledgeClassify
  20. prevSort int
  21. nextSort int
  22. )
  23. classifyInfo, err = knowledgeObj.GetClassifyById(classifyId)
  24. if err != nil {
  25. if utils.IsErrNoRow(err) {
  26. errMsg = "分类不存在, 请刷新页面"
  27. err = fmt.Errorf("获取分类信息失败,Err:" + err.Error())
  28. return
  29. }
  30. errMsg = "移动失败"
  31. err = fmt.Errorf("获取分类信息失败,Err:" + err.Error())
  32. return
  33. } else if classifyInfo.ClassifyId == 0 {
  34. errMsg = "分类不存在, 请刷新页面"
  35. err = fmt.Errorf("获取分类信息失败")
  36. return
  37. }
  38. parentClassifyId := classifyInfo.ParentId
  39. if prevClassifyId > 0 {
  40. prevClassify, err = knowledgeObj.GetClassifyById(prevClassifyId)
  41. if err != nil {
  42. if utils.IsErrNoRow(err) {
  43. errMsg = "上一个分类不存在, 请刷新页面"
  44. err = fmt.Errorf("获取分类信息失败,Err:" + err.Error())
  45. return
  46. }
  47. errMsg = "移动失败"
  48. err = fmt.Errorf("获取上一个兄弟节点分类信息失败,Err:" + err.Error())
  49. return
  50. }
  51. if prevClassify.ParentId != parentClassifyId {
  52. errMsg = "禁止拖动到其他节点"
  53. err = fmt.Errorf(errMsg)
  54. return
  55. }
  56. prevSort = prevClassify.Sort
  57. }
  58. if nextClassifyId > 0 {
  59. nextClassify, err = knowledgeObj.GetClassifyById(nextClassifyId)
  60. if err != nil {
  61. if utils.IsErrNoRow(err) {
  62. errMsg = "下一个分类不存在, 请刷新页面"
  63. err = fmt.Errorf("获取分类信息失败,Err:" + err.Error())
  64. return
  65. }
  66. errMsg = "移动失败"
  67. err = fmt.Errorf("获取下一个兄弟节点分类信息失败,Err:" + err.Error())
  68. return
  69. }
  70. if nextClassify.ParentId != parentClassifyId {
  71. errMsg = "禁止拖动到其他节点"
  72. err = fmt.Errorf(errMsg)
  73. return
  74. }
  75. nextSort = nextClassify.Sort
  76. }
  77. err, errMsg = moveKnowledgeClassify(classifyInfo, prevClassify, nextClassify, parentClassifyId, prevSort, nextSort, classifyInfo.ResourceType)
  78. return
  79. }
  80. func moveKnowledgeClassify(classifyInfo, prevClassify, nextClassify *knowledge.KnowledgeClassify, parentId, prevSort, nextSort, resourceType int) (err error, errMsg string) {
  81. knowledgeObj := new(knowledge.KnowledgeClassify)
  82. updateCol := make([]string, 0)
  83. if classifyInfo.ParentId != parentId {
  84. errMsg = "移动失败"
  85. err = fmt.Errorf("不支持目录层级变更")
  86. return
  87. }
  88. if prevSort > 0 {
  89. if nextSort > 0 {
  90. if prevSort == nextSort || prevSort == classifyInfo.Sort {
  91. updateSortStr := `sort + 2`
  92. if prevClassify != nil {
  93. _ = knowledgeObj.UpdateClassifySortByParentId(parentId, prevClassify.ClassifyId, prevClassify.Sort, updateSortStr, resourceType)
  94. } else {
  95. _ = knowledgeObj.UpdateClassifySortByParentId(parentId, 0, prevSort, updateSortStr, resourceType)
  96. }
  97. } else {
  98. if nextSort-prevSort == 1 {
  99. updateSortStr := `sort + 1`
  100. if prevClassify != nil {
  101. _ = knowledgeObj.UpdateClassifySortByParentId(parentId, prevClassify.ClassifyId, prevSort, updateSortStr, resourceType)
  102. } else {
  103. _ = knowledgeObj.UpdateClassifySortByParentId(parentId, 0, prevSort, updateSortStr, resourceType)
  104. }
  105. }
  106. }
  107. }
  108. classifyInfo.Sort = prevSort + 1
  109. classifyInfo.ModifyTime = time.Now()
  110. updateCol = append(updateCol, "Sort", "ModifyTime")
  111. } else if prevClassify == nil && nextClassify == nil && parentId > 0 {
  112. var maxSort int
  113. maxSort, err = knowledgeObj.GetMaxSortByParentId(parentId)
  114. if err != nil {
  115. errMsg = "移动失败"
  116. err = fmt.Errorf("查询组内排序信息失败,Err:" + err.Error())
  117. return
  118. }
  119. classifyInfo.Sort = maxSort + 1 //那就是排在组内最后一位
  120. classifyInfo.ModifyTime = time.Now()
  121. updateCol = append(updateCol, "Sort", "ModifyTime")
  122. } else {
  123. firstPermission, tmpErr := knowledgeObj.GetFirstClassifyByParentId(parentId)
  124. if tmpErr != nil && !utils.IsErrNoRow(tmpErr) {
  125. errMsg = "移动失败"
  126. err = fmt.Errorf("获取获取当前父级分类下的排序第一条的分类信息失败,Err:" + tmpErr.Error())
  127. return
  128. }
  129. if firstPermission != nil && firstPermission.ClassifyId > 0 && firstPermission.Sort == 0 {
  130. updateSortStr := ` sort + 1 `
  131. _ = knowledgeObj.UpdateClassifySortByParentId(parentId, firstPermission.ClassifyId-1, 0, updateSortStr, resourceType)
  132. }
  133. classifyInfo.Sort = 0 //那就是排在第一位
  134. classifyInfo.ModifyTime = time.Now()
  135. updateCol = append(updateCol, "Sort", "ModifyTime")
  136. }
  137. if len(updateCol) > 0 {
  138. err = classifyInfo.Update(updateCol)
  139. if err != nil {
  140. errMsg = "移动失败"
  141. err = fmt.Errorf("修改失败,Err:" + err.Error())
  142. return
  143. }
  144. }
  145. return
  146. }
  147. func AddKnowledgeClassify(classifyName string, parentId, resourceType int) (err error, errMsg string, isSendEmail bool) {
  148. isSendEmail = true
  149. errMsg = `添加失败`
  150. knowledgeObj := new(knowledge.KnowledgeClassify)
  151. _, err = knowledgeObj.GetClassifyByName(classifyName, parentId, resourceType)
  152. if err != nil && !utils.IsErrNoRow(err) {
  153. errMsg = "获取分类信息失败"
  154. return
  155. }
  156. if err == nil {
  157. errMsg = "分类名称:" + classifyName + "已存在"
  158. isSendEmail = false
  159. err = errors.New(errMsg)
  160. return
  161. } else {
  162. err = nil
  163. }
  164. level := 1
  165. var parentClassifyItem *knowledge.KnowledgeClassify
  166. var childClassifyCount int
  167. if parentId > 0 {
  168. parentClassifyItem, err = knowledgeObj.GetClassifyById(parentId)
  169. if err != nil {
  170. errMsg = "获取父级分类信息失败"
  171. if utils.IsErrNoRow(err) {
  172. errMsg = "父级分类不存在"
  173. }
  174. return
  175. }
  176. if parentClassifyItem.ResourceType != resourceType {
  177. errMsg = "父级分类与当前分类类型不一致"
  178. err = fmt.Errorf(errMsg)
  179. return
  180. }
  181. level = parentClassifyItem.Level + 1
  182. if level > 3 {
  183. errMsg = "分类层级不可超过三级"
  184. isSendEmail = false
  185. return
  186. }
  187. childClassifyCount, err = knowledgeObj.GetCountClassifyChildByParentId(parentId)
  188. if err != nil {
  189. errMsg = "获取父级分类的子分类信息失败"
  190. return
  191. }
  192. }
  193. nowTime := time.Now().Local()
  194. classify := new(knowledge.KnowledgeClassify)
  195. maxSort, err := classify.GetMaxSort()
  196. if err != nil {
  197. errMsg = "操作失败"
  198. err = errors.New("查询品种排序失败, Err: " + err.Error())
  199. return
  200. }
  201. classify.ClassifyName = classifyName
  202. classify.ParentId = parentId
  203. classify.CreateTime = nowTime
  204. classify.ModifyTime = nowTime
  205. classify.Sort = maxSort + 1
  206. classify.Enabled = 1
  207. classify.Level = level
  208. classify.ResourceType = resourceType
  209. err = knowledgeObj.Add(classify)
  210. if err != nil {
  211. return
  212. }
  213. fmt.Println("classify.ClassifyId:")
  214. fmt.Println(classify.ClassifyId)
  215. if parentClassifyItem != nil {
  216. if childClassifyCount <= 0 {
  217. moveResourceByAddClassify(parentClassifyItem, classify, resourceType)
  218. }
  219. }
  220. return
  221. }
  222. func moveResourceByAddClassify(parentClassifyInfo, currClassifyInfo *knowledge.KnowledgeClassify, resourceType int) (err error) {
  223. defer func() {
  224. if err != nil {
  225. msg := fmt.Sprint("历史事件更改分类失败,父级分类ID:", parentClassifyInfo.ClassifyId, ";当前分类ID:", currClassifyInfo.ClassifyId, ";错误信息:", err.Error())
  226. utils.FileLog.Error(msg)
  227. go alarm_msg.SendAlarmMsg(msg, 3)
  228. }
  229. }()
  230. if currClassifyInfo.Level > 3 {
  231. err = errors.New("父级分类不支持三级分类以上")
  232. return
  233. }
  234. var condition string
  235. pars := make([]interface{}, 0)
  236. condition += ` AND resource_type = ? `
  237. pars = append(pars, resourceType)
  238. condition += ` AND classify_id = ? `
  239. pars = append(pars, parentClassifyInfo.ClassifyId)
  240. idList, err := knowledge.GetKnowledgeResourceIdList(condition, pars)
  241. if err != nil {
  242. err = fmt.Errorf("查询需要转移的资源数量失败,Err: %s", err.Error())
  243. return
  244. }
  245. if len(idList) == 0 { //没有需要转移的资源
  246. return
  247. }
  248. newClassifyId := currClassifyInfo.ClassifyId
  249. if currClassifyInfo.Level == 2 {
  250. classifyObj := new(knowledge.KnowledgeClassify)
  251. childClassify, e := classifyObj.GetFirstClassifyByParentId(currClassifyInfo.ClassifyId)
  252. if e == nil {
  253. newClassifyId = childClassify.ClassifyId
  254. }
  255. }
  256. resourceObj := new(knowledge.KnowledgeResource)
  257. err = resourceObj.UpdateClassifyIdByIds(idList, newClassifyId, resourceType)
  258. return
  259. }
  260. func EditKnowledgeClassify(classifyId, parentId int, classifyName string, resourceType int) (err error, errMsg string, isSendEmail bool) {
  261. isSendEmail = true
  262. errMsg = `修改失败`
  263. knowledgeObj := new(knowledge.KnowledgeClassify)
  264. item, err := knowledgeObj.GetClassifyById(classifyId)
  265. if err != nil {
  266. errMsg = "获取分类信息失败"
  267. if utils.IsErrNoRow(err) {
  268. errMsg = "分类不存在, 或已被删除"
  269. isSendEmail = false
  270. }
  271. return
  272. }
  273. if item.ResourceType != resourceType {
  274. errMsg = "分类类型错误"
  275. err = errors.New("分类类型错误")
  276. return
  277. }
  278. if (parentId == 0 && item.ParentId > 0) || (parentId > 0 && item.ParentId == 0) {
  279. err = errors.New("不允许修改分类层级")
  280. errMsg = "不允许修改分类层级"
  281. return
  282. }
  283. var parentClassifyItem *knowledge.KnowledgeClassify
  284. var childClassifyCount int
  285. if parentId > 0 {
  286. parentClassifyItem, err = knowledgeObj.GetClassifyById(parentId)
  287. if err != nil {
  288. errMsg = "获取父级分类信息失败"
  289. if utils.IsErrNoRow(err) {
  290. errMsg = "父级分类不存在"
  291. }
  292. return
  293. }
  294. if parentClassifyItem.ResourceType != resourceType {
  295. errMsg = "父级分类与当前分类类型不一致"
  296. err = fmt.Errorf(errMsg)
  297. return
  298. }
  299. if parentClassifyItem.Level+1 != item.Level {
  300. errMsg = "不允许修改分类层级"
  301. err = errors.New("不允许修改分类层级")
  302. return
  303. }
  304. level := parentClassifyItem.Level + 1
  305. if level > 3 {
  306. errMsg = "分类层级不可超过三级"
  307. isSendEmail = false
  308. return
  309. }
  310. childClassifyCount, err = knowledgeObj.GetCountClassifyChildByParentId(parentId)
  311. if err != nil {
  312. errMsg = "获取父级分类的子分类信息失败"
  313. return
  314. }
  315. }
  316. existName, e := knowledgeObj.GetClassifyByName(classifyName, parentId, item.ResourceType)
  317. if e != nil && !utils.IsErrNoRow(e) {
  318. errMsg = "分类名称已存在"
  319. err = errors.New("获取重名分类失败, Err: " + e.Error())
  320. return
  321. }
  322. if existName != nil && existName.ClassifyId > 0 && existName.ClassifyId != item.ClassifyId {
  323. errMsg = "分类名称:" + classifyName + "已存在"
  324. err = errors.New(errMsg)
  325. isSendEmail = false
  326. return
  327. }
  328. item.ClassifyName = classifyName
  329. item.ParentId = parentId
  330. item.ModifyTime = time.Now().Local()
  331. cols := make([]string, 0)
  332. cols = append(cols, "ParentId", "ClassifyName", "ModifyTime")
  333. err = item.Update(cols)
  334. if err != nil {
  335. return
  336. }
  337. if parentClassifyItem != nil {
  338. if childClassifyCount <= 0 {
  339. moveResourceByAddClassify(parentClassifyItem, item, resourceType)
  340. }
  341. }
  342. return
  343. }
  344. func GetClassifyTreeRecursive(list []*knowledge.KnowledgeClassifyItem, parentId int) []*knowledge.KnowledgeClassifyItem {
  345. res := make([]*knowledge.KnowledgeClassifyItem, 0)
  346. for _, v := range list {
  347. if v.ParentId == parentId {
  348. v.Child = GetClassifyTreeRecursive(list, v.ClassifyId)
  349. res = append(res, v)
  350. }
  351. }
  352. return res
  353. }
  354. func GetParentClassifyListByParentIdList(parentClassifyIdList []int) (list []*knowledge.KnowledgeClassifyItem, err error) {
  355. knowledgeObj := new(knowledge.KnowledgeClassify)
  356. num := len(parentClassifyIdList)
  357. if num <= 0 {
  358. return
  359. }
  360. list, err = knowledgeObj.GetClassifyListByParentIdList(parentClassifyIdList)
  361. if err != nil {
  362. return
  363. }
  364. {
  365. currParentClassifyIdList := make([]int, 0)
  366. for _, v := range list {
  367. if v.ParentId > 0 {
  368. currParentClassifyIdList = append(currParentClassifyIdList, v.ParentId)
  369. }
  370. }
  371. if len(currParentClassifyIdList) > 0 {
  372. tmpList, tmpErr := GetParentClassifyListByParentIdList(currParentClassifyIdList)
  373. if tmpErr != nil {
  374. err = tmpErr
  375. return
  376. }
  377. list = append(tmpList, list...)
  378. }
  379. }
  380. return
  381. }
  382. func GetClassifyListTreeRecursive(list []*knowledge.KnowledgeClassifyItem, parentId int) []*knowledge.KnowledgeClassifyItem {
  383. res := make([]*knowledge.KnowledgeClassifyItem, 0)
  384. for _, v := range list {
  385. if v.ParentId == parentId {
  386. v.Child = GetClassifyListTreeRecursive(list, v.ClassifyId)
  387. res = append(res, v)
  388. }
  389. }
  390. if len(res) <= 0 {
  391. res = nil
  392. }
  393. return res
  394. }
  395. type BySortAndCreateTime []*knowledge.KnowledgeClassifyItem
  396. func (a BySortAndCreateTime) Len() int {
  397. return len(a)
  398. }
  399. func (a BySortAndCreateTime) Swap(i, j int) {
  400. a[i], a[j] = a[j], a[i]
  401. }
  402. func (a BySortAndCreateTime) Less(i, j int) bool {
  403. if a[i].Sort == a[j].Sort {
  404. return a[i].CreateTime.Before(a[j].CreateTime)
  405. }
  406. return a[i].Sort < a[j].Sort
  407. }
  408. func SortClassifyListBySortAndCreateTime(classifyList []*knowledge.KnowledgeClassifyItem) {
  409. sort.Sort(BySortAndCreateTime(classifyList))
  410. }