base_from_sci_hq.go 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656
  1. package data
  2. import (
  3. "errors"
  4. "eta/eta_api/models/data_manage"
  5. "eta/eta_api/utils"
  6. "fmt"
  7. "time"
  8. )
  9. func AddSciHqClassify(classifyName, lang string, parentId int) (ok bool, msg string, err error) {
  10. var count int
  11. switch lang {
  12. case utils.EnLangVersion:
  13. count, err = data_manage.GetBaseFromSciHqClassifyEnCount(classifyName, parentId)
  14. default:
  15. count, err = data_manage.GetBaseFromSciHqClassifyCount(classifyName, parentId)
  16. }
  17. if err != nil {
  18. return
  19. }
  20. if count > 0 {
  21. return false, "分类已存在", nil
  22. }
  23. sort, err := data_manage.GetChildSciHqClassifyMaxSortById(parentId)
  24. if err != nil {
  25. return false, "", err
  26. }
  27. if parentId > 0 {
  28. count, err = data_manage.GetSciHqClassifyCountById(parentId)
  29. if err != nil {
  30. return false, "", err
  31. }
  32. if count == 0 {
  33. return false, "", errors.New("父分类不存在")
  34. }
  35. sciHqClassify := &data_manage.BaseFromSciHqClassify{
  36. ClassifyName: classifyName,
  37. ClassifyNameEn: classifyName,
  38. ParentId: parentId,
  39. Level: 2,
  40. Sort: sort + 1,
  41. ModifyTime: time.Now(),
  42. CreateTime: time.Now(),
  43. }
  44. _, err = sciHqClassify.Add()
  45. if err != nil {
  46. return
  47. }
  48. } else {
  49. sciHqClassify := &data_manage.BaseFromSciHqClassify{
  50. ClassifyName: classifyName,
  51. ClassifyNameEn: classifyName,
  52. ParentId: 0,
  53. Level: 1,
  54. Sort: sort + 1,
  55. ModifyTime: time.Now(),
  56. CreateTime: time.Now(),
  57. }
  58. insertId, er := sciHqClassify.Add()
  59. if er != nil {
  60. return false, "", er
  61. }
  62. sciHqClassifyList := []*data_manage.BaseFromSciHqClassify{
  63. {
  64. ClassifyName: "价格",
  65. ClassifyNameEn: "价格",
  66. ParentId: int(insertId),
  67. Level: 2,
  68. Sort: 1,
  69. ModifyTime: time.Now(),
  70. CreateTime: time.Now(),
  71. },
  72. {
  73. ClassifyName: "供应",
  74. ClassifyNameEn: "供应",
  75. ParentId: int(insertId),
  76. Level: 2,
  77. Sort: 2,
  78. ModifyTime: time.Now(),
  79. CreateTime: time.Now(),
  80. },
  81. {
  82. ClassifyName: "需求",
  83. ClassifyNameEn: "需求",
  84. ParentId: int(insertId),
  85. Level: 2,
  86. Sort: 3,
  87. ModifyTime: time.Now(),
  88. CreateTime: time.Now(),
  89. },
  90. {
  91. ClassifyName: "库存",
  92. ClassifyNameEn: "库存",
  93. ParentId: int(insertId),
  94. Level: 2,
  95. Sort: 4,
  96. ModifyTime: time.Now(),
  97. CreateTime: time.Now(),
  98. },
  99. }
  100. err = data_manage.BatchAddSciHqClassify(sciHqClassifyList)
  101. if err != nil {
  102. return
  103. }
  104. }
  105. return true, "", nil
  106. }
  107. func DelSciHqClassify(classifyId int) (err error) {
  108. classify, err := data_manage.GetSciHqClassifyById(classifyId)
  109. if err != nil {
  110. return
  111. }
  112. var classifyIds []int
  113. classifyIds = append(classifyIds, classify.ClassifyId)
  114. if classify.ParentId == 0 {
  115. tmpClassifyIds, er := data_manage.GetChildSciHqClassifyIdsById(classify.ClassifyId)
  116. if er != nil {
  117. err = er
  118. return
  119. }
  120. classifyIds = append(classifyIds, tmpClassifyIds...)
  121. }
  122. // 获取分类下的所有指标
  123. count, err := data_manage.GetSciHqIndexCountByClassifyIds(classifyIds)
  124. if err != nil {
  125. err = errors.New("获取分类下的指标信息失败,Err:" + err.Error())
  126. return
  127. }
  128. if count > 0 {
  129. err = errors.New("该分类下有指标,不可删除")
  130. return
  131. }
  132. // 删除对应的分类
  133. err = data_manage.DeleteSciHqClassifyByClassifyId(classifyIds)
  134. if err != nil {
  135. return
  136. }
  137. return
  138. }
  139. // ResetSciHqIndex 指标数据清除分类接口
  140. func ResetSciHqIndex(indexId int) (err error) {
  141. sciHqIndex, err := data_manage.GetSciHqIndexByIndexId(indexId)
  142. if err != nil {
  143. return
  144. }
  145. // 更新指标在未分类下的排序,永远排在未分类的最后一个
  146. //移动排序
  147. classifyId := 0
  148. updateCol := make([]string, 0)
  149. var currentSort, prevSort int
  150. currentSort = sciHqIndex.Sort
  151. //未分类的最大的sort值
  152. prevSort, err = data_manage.GetSciHqIndexMaxSortByClassifyId(classifyId)
  153. if err != nil {
  154. err = errors.New("获取上一个兄弟节点指标信息失败,Err:" + err.Error())
  155. return
  156. }
  157. //如果前面一个节点的sort值比当前大,则当前节点往下移动
  158. if prevSort >= currentSort {
  159. //往下移动
  160. err = data_manage.MoveDownSciIndexBySort(classifyId, prevSort, currentSort)
  161. if err != nil {
  162. err = errors.New("向下移动出错:" + err.Error())
  163. return
  164. }
  165. sciHqIndex.Sort = prevSort
  166. }
  167. //更新
  168. sciHqIndex.ModifyTime = time.Now()
  169. sciHqIndex.ClassifyId = classifyId
  170. updateCol = append(updateCol, "Sort", "ModifyTime", "ClassifyId")
  171. err = sciHqIndex.Update(updateCol)
  172. if err != nil {
  173. err = errors.New("移动失败,Err:" + err.Error())
  174. return
  175. }
  176. return
  177. }
  178. // 批量删除卓创红期指标
  179. func BatchDelSciHqData(indexIds []int) (existIndex []*data_manage.BaseFromSciHqIndex, err error) {
  180. sciHqIndexList, err := data_manage.GetSciHqIndexListByIndexIds(indexIds)
  181. if err != nil {
  182. return
  183. }
  184. indexCodes := make([]string, 0)
  185. codeToIndex := make(map[string]*data_manage.BaseFromSciHqIndex)
  186. for _, v := range sciHqIndexList {
  187. indexCodes = append(indexCodes, v.IndexCode)
  188. codeToIndex[v.IndexCode] = v
  189. }
  190. edbInfoList, err := data_manage.GetEdbInfoListByEdbCodes(utils.DATA_SOURCE_SCI_HQ, indexCodes)
  191. if err != nil {
  192. return
  193. }
  194. deleteIndexIds := make([]int, 0)
  195. for _, v := range edbInfoList {
  196. if index, ok := codeToIndex[v.EdbCode]; ok {
  197. existIndex = append(existIndex, index)
  198. delete(codeToIndex, v.EdbCode)
  199. }
  200. }
  201. for _, v := range codeToIndex {
  202. deleteIndexIds = append(deleteIndexIds, v.BaseFromSciHqIndexId)
  203. }
  204. // 删除对应的指标
  205. err = data_manage.DeleteSciHqIndexByIds(deleteIndexIds)
  206. if err != nil {
  207. return
  208. }
  209. return
  210. }
  211. // DelSciHqData 删除卓创红期指标
  212. func DelSciHqData(indexId int) (err error, errMsg string) {
  213. baseFromSciHqIndex, err := data_manage.GetSciHqIndexByIndexId(indexId)
  214. if err != nil {
  215. errMsg = `获取数据失败`
  216. if utils.IsErrNoRow(err) {
  217. errMsg = `该指标未入库`
  218. err = nil
  219. }
  220. return
  221. }
  222. // 获取已经加入到EDB指标库的Sci指标
  223. edbInfo, err := data_manage.GetEdbInfoByEdbCode(utils.DATA_SOURCE_SCI_HQ, baseFromSciHqIndex.IndexCode)
  224. if err != nil && !utils.IsErrNoRow(err) {
  225. errMsg = "删除失败"
  226. err = errors.New("获取分类下的指标信息失败,Err:" + err.Error())
  227. return
  228. }
  229. if edbInfo != nil && edbInfo.EdbInfoId > 0 {
  230. errMsg = "当前指标已被引用,不可删除"
  231. err = nil
  232. return
  233. }
  234. // 删除对应的指标
  235. err = data_manage.DeleteSciHqIndexById(indexId)
  236. if err != nil {
  237. return
  238. }
  239. return
  240. }
  241. // EditSciHqIndex 编辑卓创红期指标
  242. func EditSciHqIndex(indexId, classifyId int) (baseFromSciHqIndex *data_manage.BaseFromSciHqIndex, errMsg string, err error) {
  243. baseFromSciHqIndex, err = data_manage.GetSciHqIndexByIndexId(indexId)
  244. if err != nil {
  245. if utils.IsErrNoRow(err) {
  246. errMsg = `该指标未入库`
  247. err = nil
  248. }
  249. return
  250. }
  251. classify, err := data_manage.GetSciHqClassifyById(classifyId)
  252. if err != nil {
  253. if utils.IsErrNoRow(err) {
  254. errMsg = `该分类不存在或已删除`
  255. err = nil
  256. }
  257. return
  258. }
  259. if classify.Level == 1 { // 顶级分类
  260. errMsg = `不合法的编辑`
  261. err = nil
  262. return
  263. }
  264. maxSort, err := data_manage.GetChildSciHqClassifyMaxSortById(classifyId)
  265. if err != nil {
  266. return
  267. }
  268. baseFromSciHqIndex.ClassifyId = classifyId
  269. baseFromSciHqIndex.Sort = maxSort + 1
  270. baseFromSciHqIndex.ModifyTime = time.Now()
  271. err = baseFromSciHqIndex.Update([]string{"classify_id", "sort", "modify_time"})
  272. return
  273. }
  274. type SciHqIndexSource2EdbReq struct {
  275. EdbCode string
  276. EdbName string
  277. Frequency string
  278. Unit string
  279. ClassifyId int
  280. AdminId int
  281. AdminRealName string
  282. }
  283. // SciHqIndexSource2Edb 新增卓创红期数据从数据源到指标库
  284. func SciHqIndexSource2Edb(req SciHqIndexSource2EdbReq, lang string) (edb *data_manage.EdbInfo, errMsg string, skip bool, err error) {
  285. if req.EdbCode == "" {
  286. err = fmt.Errorf("指标ID为空")
  287. return
  288. }
  289. defer func() {
  290. if err != nil {
  291. tips := fmt.Sprintf("SciHqIndexSource2Edb新增失败, Err: %s", err.Error())
  292. fmt.Println(tips)
  293. utils.FileLog.Info(tips)
  294. }
  295. }()
  296. source := utils.DATA_SOURCE_SCI_HQ
  297. // 是否新增过指标
  298. exist, e := data_manage.GetEdbInfoByEdbCode(source, req.EdbCode)
  299. if e != nil && !utils.IsErrNoRow(e) {
  300. err = fmt.Errorf("获取指标是否存在失败, err: %s", e.Error())
  301. return
  302. }
  303. if exist != nil && exist.EdbInfoId > 0 {
  304. skip = true
  305. return
  306. }
  307. // 开始结束时间
  308. var startDate, endDate string
  309. // 新增指标库
  310. edbInfo, e, msg, _ := EdbInfoAdd(source, utils.DATA_SUB_SOURCE_EDB, req.ClassifyId, req.EdbCode, req.EdbName, req.Frequency, req.Unit, startDate, endDate, req.AdminId, req.AdminRealName, lang)
  311. if e != nil {
  312. errMsg = msg
  313. err = fmt.Errorf("EdbInfo: 新增指标失败, err: %s", e.Error())
  314. return
  315. }
  316. edb = edbInfo
  317. return
  318. }
  319. // MoveSciHqClassify 移动卓创红期分类
  320. func MoveSciHqClassify(classifyId, parentClassifyId, prevClassifyId, nextClassifyId int) (err error, errMsg string) {
  321. //判断分类是否存在
  322. classifyInfo, err := data_manage.GetSciHqClassifyById(classifyId)
  323. if err != nil {
  324. errMsg = "移动失败"
  325. err = errors.New("获取分类信息失败,Err:" + err.Error())
  326. return
  327. }
  328. updateCol := make([]string, 0)
  329. //判断上级id是否一致,如果不一致的话,那么需要移动该分类层级
  330. if classifyInfo.ParentId != parentClassifyId && parentClassifyId != 0 {
  331. parentClassifyInfo, tmpErr := data_manage.GetSciHqClassifyById(parentClassifyId)
  332. if tmpErr != nil {
  333. errMsg = "移动失败"
  334. err = errors.New("获取上级分类信息失败,Err:" + tmpErr.Error())
  335. return
  336. }
  337. classifyInfo.ParentId = parentClassifyInfo.ClassifyId
  338. classifyInfo.Level = parentClassifyInfo.Level + 1
  339. updateCol = append(updateCol, "ParentId", "Level")
  340. }
  341. var prevSort, nextSort int
  342. //var currentSort, prevSort, nextSort int
  343. //currentSort = classifyInfo.Sort
  344. var prevClassify *data_manage.BaseFromSciHqClassify
  345. var nextClassify *data_manage.BaseFromSciHqClassify
  346. //如果有传入 上一个兄弟节点分类id
  347. if prevClassifyId > 0 {
  348. prevClassify, err = data_manage.GetSciHqClassifyById(prevClassifyId)
  349. if err != nil {
  350. if utils.IsErrNoRow(err) {
  351. errMsg = "移动失败"
  352. err = errors.New("上一个兄弟节点分类信息不存在" + err.Error())
  353. return
  354. }
  355. errMsg = "移动失败"
  356. err = errors.New("获取上一个兄弟节点分类信息失败,Err:" + err.Error())
  357. return
  358. }
  359. prevSort = prevClassify.Sort
  360. }
  361. if nextClassifyId > 0 {
  362. //下一个兄弟节点
  363. nextClassify, err = data_manage.GetSciHqClassifyById(nextClassifyId)
  364. if err != nil {
  365. if utils.IsErrNoRow(err) {
  366. errMsg = "移动失败"
  367. err = errors.New("下一个兄弟节点分类信息不存在" + err.Error())
  368. return
  369. }
  370. errMsg = "移动失败"
  371. err = errors.New("获取下一个兄弟节点分类信息失败,Err:" + err.Error())
  372. return
  373. }
  374. nextSort = nextClassify.Sort
  375. }
  376. // 修改历史数据中的排序为0的情况
  377. if prevSort == 0 && nextSort == 0 { //目标是让当前分类处于目录的最顶部
  378. //更新为0排序的数据为当前最小排序
  379. if nextClassify != nil {
  380. minSort, e := data_manage.GetSciHqIndexClassifyMinSort(parentClassifyId)
  381. if e != nil {
  382. errMsg = "移动失败"
  383. err = errors.New("获取下一个兄弟节点分类信息失败,Err:" + e.Error())
  384. return
  385. }
  386. if minSort > 1 {
  387. minSort -= 1
  388. }
  389. nextClassify.Sort = minSort
  390. err = nextClassify.Update([]string{"sort"})
  391. if err != nil {
  392. errMsg = "移动失败"
  393. err = errors.New("更新下一个兄弟节点分类信息失败,Err:" + err.Error())
  394. return
  395. }
  396. nextSort = minSort
  397. }
  398. }
  399. //移到两个排序值中间操作
  400. //if prevSort >= currentSort {
  401. // //往下移动
  402. // err = data_manage.MoveDownSciHqIndexClassifyBySort(parentClassifyId, prevSort, currentSort)
  403. // if err != nil {
  404. // err = errors.New("向下移动出错:" + err.Error())
  405. // return
  406. // }
  407. // classifyInfo.Sort = prevSort
  408. //} else if nextSort <= currentSort && nextSort != 0 {
  409. // //往上移动
  410. // err = data_manage.MoveUpSciHqIndexClassifyBySort(parentClassifyId, nextSort, currentSort)
  411. // if err != nil {
  412. // err = errors.New("向上移动出错:" + err.Error())
  413. // return
  414. // }
  415. // classifyInfo.Sort = nextSort
  416. //}
  417. //classifyInfo.ModifyTime = time.Now()
  418. //updateCol = append(updateCol, "Sort", "ModifyTime")
  419. //err = classifyInfo.Update(updateCol)
  420. //if err != nil {
  421. // errMsg = "移动失败"
  422. // err = errors.New("修改失败,Err:" + err.Error())
  423. // return
  424. //}
  425. if prevSort >= 0 {
  426. //如果是移动在两个兄弟节点之间
  427. if nextSort > 0 {
  428. //下一个兄弟节点
  429. //如果上一个兄弟与下一个兄弟的排序权重是一致的,那么需要将下一个兄弟(以及下个兄弟的同样排序权重)的排序权重+2,自己变成上一个兄弟的排序权重+1
  430. if prevSort == nextSort || prevSort == classifyInfo.Sort {
  431. //变更兄弟节点的排序
  432. updateSortStr := `sort + 2`
  433. //变更分类
  434. if prevClassify != nil {
  435. _ = data_manage.UpdateSciHqClassifySortByParentId(parentClassifyId, prevClassify.ClassifyId, prevClassify.Sort, updateSortStr)
  436. } else {
  437. _ = data_manage.UpdateSciHqClassifySortByParentId(parentClassifyId, 0, prevSort, updateSortStr)
  438. }
  439. } else {
  440. //如果下一个兄弟的排序权重正好是上个兄弟节点的下一层,那么需要再加一层了
  441. if nextSort-prevSort == 1 {
  442. //变更兄弟节点的排序
  443. updateSortStr := `sort + 1`
  444. //变更分类
  445. if prevClassify != nil {
  446. _ = data_manage.UpdateSciHqClassifySortByParentId(parentClassifyId, prevClassify.ClassifyId, prevSort, updateSortStr)
  447. } else {
  448. _ = data_manage.UpdateSciHqClassifySortByParentId(parentClassifyId, 0, prevSort, updateSortStr)
  449. }
  450. }
  451. }
  452. }
  453. classifyInfo.Sort = prevSort + 1
  454. classifyInfo.ModifyTime = time.Now()
  455. updateCol = append(updateCol, "Sort", "ModifyTime")
  456. } else if prevClassify == nil && nextClassify == nil && parentClassifyId > 0 {
  457. //处理只拖动到目录里,默认放到目录底部的情况
  458. var maxSort int
  459. maxSort, err = data_manage.GetSciHqClassifySortMaxByParentId(parentClassifyId)
  460. if err != nil {
  461. errMsg = "移动失败"
  462. err = errors.New("查询组内排序信息失败,Err:" + err.Error())
  463. return
  464. }
  465. classifyInfo.Sort = maxSort + 1 //那就是排在组内最后一位
  466. classifyInfo.ModifyTime = time.Now()
  467. updateCol = append(updateCol, "Sort", "ModifyTime")
  468. } else {
  469. // 拖动到父级分类的第一位
  470. firstClassify, tmpErr := data_manage.GetFirstSciHqClassifyByParentId(parentClassifyId)
  471. if tmpErr != nil && !utils.IsErrNoRow(tmpErr) {
  472. errMsg = "移动失败"
  473. err = errors.New("获取获取当前父级分类下的排序第一条的分类信息失败,Err:" + tmpErr.Error())
  474. return
  475. }
  476. //如果该分类下存在其他分类,且第一个其他分类的排序等于0,那么需要调整排序
  477. if firstClassify != nil && firstClassify.Sort == 0 {
  478. updateSortStr := ` sort + 1 `
  479. _ = data_manage.UpdateSciHqClassifySortByParentId(parentClassifyId, firstClassify.ClassifyId-1, 0, updateSortStr)
  480. }
  481. classifyInfo.Sort = 0 //那就是排在第一位
  482. classifyInfo.ModifyTime = time.Now()
  483. updateCol = append(updateCol, "Sort", "ModifyTime")
  484. }
  485. err = classifyInfo.Update(updateCol)
  486. if err != nil {
  487. errMsg = "移动失败"
  488. err = errors.New("修改失败,Err:" + err.Error())
  489. return
  490. }
  491. return
  492. }
  493. // MoveSciHqData 移动卓创红期指标
  494. func MoveSciHqData(indexId, classifyId, prevIndexId, nextIndexId int) (err error, errMsg string) {
  495. //分类信息
  496. sciHqIndex, err := data_manage.GetSciHqIndexByIndexId(indexId)
  497. if err != nil {
  498. errMsg = `获取数据失败`
  499. if utils.IsErrNoRow(err) {
  500. errMsg = `该指标未入库`
  501. err = nil
  502. }
  503. return
  504. }
  505. //判断分类是否存在
  506. if classifyId > 0 {
  507. _, err = data_manage.GetSciHqClassifyById(classifyId)
  508. if err != nil {
  509. errMsg = "移动失败"
  510. err = errors.New("获取分类信息失败,Err:" + err.Error())
  511. if utils.IsErrNoRow(err) {
  512. errMsg = "找不到该分类"
  513. err = nil
  514. }
  515. return
  516. }
  517. }
  518. //如果改变了分类,那么移动该图表数据
  519. if sciHqIndex.ClassifyId != classifyId {
  520. tmpErr := data_manage.MoveSciHqIndex(indexId, classifyId)
  521. if tmpErr != nil {
  522. errMsg = "移动失败"
  523. err = errors.New("移动失败,Err:" + tmpErr.Error())
  524. return
  525. }
  526. }
  527. //移动排序
  528. updateCol := make([]string, 0)
  529. var currentSort, prevSort, nextSort int
  530. currentSort = sciHqIndex.Sort
  531. //如果有传入 上一个兄弟节点分类id
  532. var prevIndex *data_manage.BaseFromSciHqIndex
  533. var nextIndex *data_manage.BaseFromSciHqIndex
  534. if prevIndexId > 0 {
  535. prevIndex, err = data_manage.GetSciHqIndexByIndexId(prevIndexId)
  536. if err != nil {
  537. if utils.IsErrNoRow(err) {
  538. errMsg = "移动失败"
  539. err = errors.New("上一个兄弟节点指标信息不存在" + err.Error())
  540. return
  541. }
  542. errMsg = "移动失败"
  543. err = errors.New("获取上一个兄弟节点指标信息失败,Err:" + err.Error())
  544. return
  545. }
  546. prevSort = prevIndex.Sort
  547. }
  548. if nextIndexId > 0 {
  549. //下一个兄弟节点
  550. nextIndex, err = data_manage.GetSciHqIndexByIndexId(nextIndexId)
  551. if err != nil {
  552. if utils.IsErrNoRow(err) {
  553. errMsg = "移动失败"
  554. err = errors.New("下一个兄弟节点指标信息不存在" + err.Error())
  555. return
  556. }
  557. errMsg = "移动失败"
  558. err = errors.New("获取下一个兄弟节点指标信息失败,Err:" + err.Error())
  559. return
  560. }
  561. nextSort = nextIndex.Sort
  562. }
  563. // 修改历史数据中的排序为0的情况
  564. if prevSort == 0 && nextSort == 0 { //目标是让当前分类处于目录的最顶部, 但是历史数据里下个节点的sort为0的情况
  565. //更新为0排序的数据为当前最小排序
  566. if nextIndex != nil {
  567. minSort, e := data_manage.GetSciHqIndexMinSortByClassifyId(classifyId)
  568. if e != nil {
  569. errMsg = "移动失败"
  570. err = errors.New("获取下一个兄弟节点分类信息失败,Err:" + e.Error())
  571. return
  572. }
  573. if minSort > 1 {
  574. minSort -= 1
  575. }
  576. nextIndex.Sort = minSort
  577. err = nextIndex.Update([]string{"Sort"})
  578. if err != nil {
  579. errMsg = "移动失败"
  580. err = errors.New("更新下一个兄弟节点分类信息失败,Err:" + err.Error())
  581. return
  582. }
  583. nextSort = minSort
  584. }
  585. }
  586. //移到两个排序值中间操作
  587. if prevSort >= currentSort {
  588. //往下移动
  589. err = data_manage.MoveDownSciHqIndexBySort(classifyId, prevSort, currentSort)
  590. if err != nil {
  591. err = errors.New("向下移动出错:" + err.Error())
  592. return
  593. }
  594. sciHqIndex.Sort = prevSort
  595. } else if nextSort <= currentSort && nextSort != 0 {
  596. //往上移动
  597. err = data_manage.MoveUpSciHqIndexBySort(classifyId, nextSort, currentSort)
  598. if err != nil {
  599. err = errors.New("向上移动出错:" + err.Error())
  600. return
  601. }
  602. sciHqIndex.Sort = nextSort
  603. }
  604. //更新
  605. sciHqIndex.ModifyTime = time.Now()
  606. updateCol = append(updateCol, "Sort", "ModifyTime")
  607. err = sciHqIndex.Update(updateCol)
  608. if err != nil {
  609. errMsg = "移动失败"
  610. err = errors.New("修改失败,Err:" + err.Error())
  611. return
  612. }
  613. return
  614. }