base_from_sci.go 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695
  1. package data
  2. import (
  3. "errors"
  4. "eta/eta_api/models/data_manage"
  5. "eta/eta_api/models/system"
  6. "eta/eta_api/utils"
  7. "time"
  8. )
  9. // AddSciClassify 添加SCI分类
  10. func AddSciClassify(classifyName string, parentId, sysUserId int, sysUserName string) (classifyInfo *data_manage.BaseFromSciClassify, err error, errMsg string) {
  11. count, err := data_manage.GetBaseFromSciClassifyCount(classifyName, parentId)
  12. if err != nil {
  13. errMsg = `判断名称是否已存在失败`
  14. return
  15. }
  16. if count > 0 {
  17. errMsg = `分类名称已存在,请重新输入`
  18. return
  19. }
  20. level := 1
  21. if parentId > 0 {
  22. parentClassify, e := data_manage.GetBaseFromSciClassifyById(parentId)
  23. if e != nil {
  24. if e.Error() == utils.ErrNoRow() {
  25. err = e
  26. errMsg = `一级分类不存在`
  27. return
  28. }
  29. err = e
  30. errMsg = `查询一级分类失败`
  31. return
  32. }
  33. level = parentClassify.Level + 1
  34. }
  35. classifyInfo = &data_manage.BaseFromSciClassify{
  36. ClassifyName: classifyName,
  37. ClassifyNameEn: classifyName,
  38. ParentId: parentId,
  39. SysUserId: sysUserId,
  40. SysUserRealName: sysUserName,
  41. Sort: 0,
  42. Level: level,
  43. ModifyTime: time.Now(),
  44. CreateTime: time.Now(),
  45. }
  46. classifyId, err := data_manage.AddBaseFromSciClassify(classifyInfo)
  47. if err != nil {
  48. errMsg = "保存分类失败"
  49. return
  50. }
  51. classifyInfo.ClassifyId = int(classifyId)
  52. //更新sort为classifyId
  53. err = data_manage.UpdateBaseFromSciClassifySort(int(classifyId))
  54. return
  55. }
  56. // EditSciClassify 编辑Sci分类
  57. func EditSciClassify(classifyId int, classifyName, lang string, sysUser *system.Admin) (err error, errMsg string) {
  58. item, err := data_manage.GetBaseFromSciClassifyById(classifyId)
  59. if err != nil {
  60. errMsg = `保存失败`
  61. return
  62. }
  63. switch lang {
  64. case utils.LangEn:
  65. // 名字相同,那么就直接返回
  66. if item.ClassifyNameEn == classifyName {
  67. return
  68. }
  69. // 判断名称是否已存在
  70. count, tmpErr := data_manage.GetBaseFromSciClassifyEnCount(classifyName, item.ParentId)
  71. if tmpErr != nil {
  72. err = tmpErr
  73. errMsg = "判断名称是否已存在失败"
  74. return
  75. }
  76. if count > 0 {
  77. errMsg = "分类名称已存在,请重新输入"
  78. err = errors.New(errMsg)
  79. return
  80. }
  81. // 修改数据
  82. err = data_manage.EditBaseFromSciClassifyEn(classifyId, classifyName)
  83. if err != nil {
  84. errMsg = "保存失败"
  85. }
  86. default:
  87. // 名字相同,那么就直接返回
  88. if item.ClassifyName == classifyName {
  89. return
  90. }
  91. // 判断名称是否已存在
  92. count, tmpErr := data_manage.GetBaseFromSciClassifyCount(classifyName, item.ParentId)
  93. if tmpErr != nil {
  94. err = tmpErr
  95. errMsg = "判断名称是否已存在失败"
  96. return
  97. }
  98. if count > 0 {
  99. errMsg = "分类名称已存在,请重新输入"
  100. err = errors.New(errMsg)
  101. return
  102. }
  103. // 修改数据
  104. err = data_manage.EditBaseFromSciClassify(classifyId, classifyName)
  105. if err != nil {
  106. errMsg = "保存失败"
  107. }
  108. }
  109. return
  110. }
  111. // MoveSciClassify 移动Sci分类
  112. func MoveSciClassify(classifyId, parentClassifyId, prevClassifyId, nextClassifyId int) (err error, errMsg string) {
  113. //判断分类是否存在
  114. classifyInfo, err := data_manage.GetBaseFromSciClassifyById(classifyId)
  115. if err != nil {
  116. errMsg = "移动失败"
  117. err = errors.New("获取分类信息失败,Err:" + err.Error())
  118. return
  119. }
  120. updateCol := make([]string, 0)
  121. //判断上级id是否一致,如果不一致的话,那么需要移动该分类层级
  122. if classifyInfo.ParentId != parentClassifyId && parentClassifyId != 0 {
  123. parentClassifyInfo, tmpErr := data_manage.GetBaseFromSciClassifyById(parentClassifyId)
  124. if tmpErr != nil {
  125. errMsg = "移动失败"
  126. err = errors.New("获取上级分类信息失败,Err:" + tmpErr.Error())
  127. return
  128. }
  129. classifyInfo.ParentId = parentClassifyInfo.ClassifyId
  130. classifyInfo.Level = parentClassifyInfo.Level + 1
  131. updateCol = append(updateCol, "ParentId", "Level")
  132. }
  133. var currentSort, prevSort, nextSort int
  134. currentSort = classifyInfo.Sort
  135. var prevClassify *data_manage.BaseFromSciClassify
  136. var nextClassify *data_manage.BaseFromSciClassify
  137. //如果有传入 上一个兄弟节点分类id
  138. if prevClassifyId > 0 {
  139. prevClassify, err = data_manage.GetBaseFromSciClassifyById(prevClassifyId)
  140. if err != nil {
  141. if err.Error() == utils.ErrNoRow() {
  142. errMsg = "移动失败"
  143. err = errors.New("上一个兄弟节点分类信息不存在" + err.Error())
  144. return
  145. }
  146. errMsg = "移动失败"
  147. err = errors.New("获取上一个兄弟节点分类信息失败,Err:" + err.Error())
  148. return
  149. }
  150. prevSort = prevClassify.Sort
  151. }
  152. if nextClassifyId > 0 {
  153. //下一个兄弟节点
  154. nextClassify, err = data_manage.GetBaseFromSciClassifyById(nextClassifyId)
  155. if err != nil {
  156. if err.Error() == utils.ErrNoRow() {
  157. errMsg = "移动失败"
  158. err = errors.New("下一个兄弟节点分类信息不存在" + err.Error())
  159. return
  160. }
  161. errMsg = "移动失败"
  162. err = errors.New("获取下一个兄弟节点分类信息失败,Err:" + err.Error())
  163. return
  164. }
  165. nextSort = nextClassify.Sort
  166. }
  167. // 修改历史数据中的排序为0的情况
  168. if prevSort == 0 && nextSort == 0 { //目标是让当前分类处于目录的最顶部
  169. //更新为0排序的数据为当前最小排序
  170. if nextClassify != nil {
  171. minSort, e := data_manage.GetSciIndexClassifyMinSort(parentClassifyId)
  172. if e != nil {
  173. errMsg = "移动失败"
  174. err = errors.New("获取下一个兄弟节点分类信息失败,Err:" + e.Error())
  175. return
  176. }
  177. if minSort > 1 {
  178. minSort -= 1
  179. }
  180. nextClassify.Sort = minSort
  181. err = nextClassify.Update([]string{"Sort"})
  182. if err != nil {
  183. errMsg = "移动失败"
  184. err = errors.New("更新下一个兄弟节点分类信息失败,Err:" + err.Error())
  185. return
  186. }
  187. nextSort = minSort
  188. }
  189. }
  190. //移到两个排序值中间操作
  191. if prevSort >= currentSort {
  192. //往下移动
  193. err = data_manage.MoveDownSciIndexClassifyBySort(parentClassifyId, prevSort, currentSort)
  194. if err != nil {
  195. err = errors.New("向下移动出错:" + err.Error())
  196. return
  197. }
  198. classifyInfo.Sort = prevSort
  199. } else if nextSort <= currentSort && nextSort != 0 {
  200. //往上移动
  201. err = data_manage.MoveUpSciIndexClassifyBySort(parentClassifyId, nextSort, currentSort)
  202. if err != nil {
  203. err = errors.New("向上移动出错:" + err.Error())
  204. return
  205. }
  206. classifyInfo.Sort = nextSort
  207. }
  208. classifyInfo.ModifyTime = time.Now()
  209. updateCol = append(updateCol, "Sort", "ModifyTime")
  210. err = classifyInfo.Update(updateCol)
  211. if err != nil {
  212. errMsg = "移动失败"
  213. err = errors.New("修改失败,Err:" + err.Error())
  214. return
  215. }
  216. return
  217. }
  218. // DelSciClassify 删除SCI分类
  219. func DelSciClassify(classifyId int, sysUser *system.Admin) (err error, errMsg string) {
  220. //判断分类是否存在
  221. classifyInfo, err := data_manage.GetBaseFromSciClassifyById(classifyId)
  222. if err != nil {
  223. errMsg = "删除失败"
  224. err = errors.New("获取分类信息失败,Err:" + err.Error())
  225. return
  226. }
  227. // 需要删除的分类id集合
  228. classifyIdList := make([]int, 0)
  229. classifyIdList = append(classifyIdList, classifyId)
  230. // 查找所有的子集id
  231. if classifyInfo.ParentId <= 0 {
  232. childrenClassifyInfoList, tmpErr := data_manage.GetBaseFromSciClassifyByParentId(classifyId)
  233. if tmpErr != nil {
  234. errMsg = "删除失败"
  235. err = errors.New("获取分类信息失败,Err:" + tmpErr.Error())
  236. return
  237. }
  238. for _, v := range childrenClassifyInfoList {
  239. classifyIdList = append(classifyIdList, v.ClassifyId)
  240. }
  241. }
  242. // 获取分类下的所有指标
  243. count, err := data_manage.GetBaseFromSciIndexCountByClassifyIds(classifyIdList)
  244. if err != nil {
  245. errMsg = "删除失败"
  246. err = errors.New("获取分类下的指标信息失败,Err:" + err.Error())
  247. return
  248. }
  249. if count > 0 {
  250. errMsg = "该分类下有指标,不可删除"
  251. err = errors.New("该分类下有指标,不可删除")
  252. return
  253. }
  254. // 删除对应的分类
  255. err = data_manage.DeleteSciClassifyByClassifyId(classifyIdList)
  256. if err != nil {
  257. return
  258. }
  259. return
  260. }
  261. // MoveSciData 移动SCI指标
  262. func MoveSciData(indexId, classifyId, prevIndexId, nextIndexId int) (err error, errMsg string) {
  263. //分类信息
  264. sciIndex, err := data_manage.GetBaseFromSciIndexByIndexId(indexId)
  265. if err != nil {
  266. errMsg = `获取数据失败`
  267. if err.Error() == utils.ErrNoRow() {
  268. errMsg = `该指标未入库`
  269. err = nil
  270. }
  271. return
  272. }
  273. //判断分类是否存在
  274. if classifyId > 0 {
  275. _, err = data_manage.GetBaseFromSciClassifyById(classifyId)
  276. if err != nil {
  277. errMsg = "移动失败"
  278. err = errors.New("获取分类信息失败,Err:" + err.Error())
  279. if err.Error() == utils.ErrNoRow() {
  280. errMsg = "找不到该分类"
  281. err = nil
  282. }
  283. return
  284. }
  285. }
  286. //如果改变了分类,那么移动该图表数据
  287. if sciIndex.ClassifyId != classifyId {
  288. tmpErr := data_manage.MoveBaseFromSciIndex(indexId, classifyId)
  289. if tmpErr != nil {
  290. errMsg = "移动失败"
  291. err = errors.New("移动失败,Err:" + tmpErr.Error())
  292. return
  293. }
  294. }
  295. //移动排序
  296. updateCol := make([]string, 0)
  297. //如果有传入 上一个兄弟节点分类id
  298. if prevIndexId > 0 {
  299. prevIndex, tmpErr := data_manage.GetBaseFromSciIndexByIndexId(prevIndexId)
  300. if tmpErr != nil {
  301. errMsg = "移动失败"
  302. err = errors.New("获取上一个兄弟节点指标信息失败,Err:" + tmpErr.Error())
  303. return
  304. }
  305. //如果是移动在两个兄弟节点之间
  306. if nextIndexId > 0 {
  307. //下一个兄弟节点
  308. nextIndex, tmpErr := data_manage.GetBaseFromSciIndexByIndexId(nextIndexId)
  309. if tmpErr != nil {
  310. errMsg = "移动失败"
  311. err = errors.New("获取下一个兄弟节点指标信息失败,Err:" + tmpErr.Error())
  312. return
  313. }
  314. //如果上一个兄弟与下一个兄弟的排序权重是一致的,那么需要将下一个兄弟(以及下个兄弟的同样排序权重)的排序权重+2,自己变成上一个兄弟的排序权重+1
  315. if prevIndex.Sort == nextIndex.Sort || prevIndex.Sort == sciIndex.Sort {
  316. //变更兄弟节点的排序
  317. updateSortStr := `sort + 2`
  318. err = data_manage.UpdateBaseFromSciIndexByClassifyId(prevIndex.ClassifyId, prevIndex.Sort, prevIndex.BaseFromSciIndexId, updateSortStr)
  319. if err != nil {
  320. errMsg = "移动失败"
  321. err = errors.New("变更兄弟节点排序指标信息失败,Err:" + err.Error())
  322. return
  323. }
  324. } else {
  325. //如果下一个兄弟的排序权重正好是上个兄弟节点的下一层,那么需要再加一层了
  326. if nextIndex.Sort-prevIndex.Sort == 1 {
  327. //变更兄弟节点的排序
  328. updateSortStr := `sort + 1`
  329. err = data_manage.UpdateBaseFromSciIndexByClassifyId(prevIndex.ClassifyId, prevIndex.Sort, prevIndex.BaseFromSciIndexId, updateSortStr)
  330. if err != nil {
  331. errMsg = "移动失败"
  332. err = errors.New("变更兄弟节点排序指标信息失败,Err:" + err.Error())
  333. return
  334. }
  335. }
  336. }
  337. }
  338. sciIndex.Sort = prevIndex.Sort + 1
  339. sciIndex.ModifyTime = time.Now()
  340. updateCol = append(updateCol, "Sort", "ModifyTime")
  341. } else {
  342. firstClassify, tmpErr := data_manage.GetFirstBaseFromSciIndexByClassifyId(classifyId)
  343. if tmpErr != nil && tmpErr.Error() != utils.ErrNoRow() {
  344. errMsg = "移动失败"
  345. err = errors.New("获取获取当前父级分类下的排序第一条的分类信息失败,Err:" + tmpErr.Error())
  346. return
  347. }
  348. //如果该分类下存在其他分类,且第一个其他分类的排序等于0,那么需要调整排序
  349. if firstClassify != nil && firstClassify.Sort == 0 {
  350. updateSortStr := ` sort + 1 `
  351. _ = data_manage.UpdateBaseFromSciIndexByClassifyId(firstClassify.ClassifyId, 0, firstClassify.BaseFromSciIndexId-1, updateSortStr)
  352. }
  353. sciIndex.Sort = 0 //那就是排在第一位
  354. sciIndex.ModifyTime = time.Now()
  355. updateCol = append(updateCol, "Sort", "ModifyTime")
  356. }
  357. //更新
  358. if len(updateCol) > 0 {
  359. err = sciIndex.Update(updateCol)
  360. if err != nil {
  361. errMsg = "移动失败"
  362. err = errors.New("修改失败,Err:" + err.Error())
  363. return
  364. }
  365. }
  366. return
  367. }
  368. // MoveSciData2 移动SCI指标
  369. func MoveSciData2(indexId, classifyId, prevIndexId, nextIndexId int) (err error, errMsg string) {
  370. //分类信息
  371. sciIndex, err := data_manage.GetBaseFromSciIndexByIndexId(indexId)
  372. if err != nil {
  373. errMsg = `获取数据失败`
  374. if err.Error() == utils.ErrNoRow() {
  375. errMsg = `该指标未入库`
  376. err = nil
  377. }
  378. return
  379. }
  380. //判断分类是否存在
  381. if classifyId > 0 {
  382. _, err = data_manage.GetBaseFromSciClassifyById(classifyId)
  383. if err != nil {
  384. errMsg = "移动失败"
  385. err = errors.New("获取分类信息失败,Err:" + err.Error())
  386. if err.Error() == utils.ErrNoRow() {
  387. errMsg = "找不到该分类"
  388. err = nil
  389. }
  390. return
  391. }
  392. }
  393. //如果改变了分类,那么移动该图表数据
  394. if sciIndex.ClassifyId != classifyId {
  395. tmpErr := data_manage.MoveBaseFromSciIndex(indexId, classifyId)
  396. if tmpErr != nil {
  397. errMsg = "移动失败"
  398. err = errors.New("移动失败,Err:" + tmpErr.Error())
  399. return
  400. }
  401. }
  402. //移动排序
  403. updateCol := make([]string, 0)
  404. var currentSort, prevSort, nextSort int
  405. currentSort = sciIndex.Sort
  406. //如果有传入 上一个兄弟节点分类id
  407. var prevIndex *data_manage.BaseFromSciIndex
  408. var nextIndex *data_manage.BaseFromSciIndex
  409. if prevIndexId > 0 {
  410. prevIndex, err = data_manage.GetBaseFromSciIndexByIndexId(prevIndexId)
  411. if err != nil {
  412. if err.Error() == utils.ErrNoRow() {
  413. errMsg = "移动失败"
  414. err = errors.New("上一个兄弟节点指标信息不存在" + err.Error())
  415. return
  416. }
  417. errMsg = "移动失败"
  418. err = errors.New("获取上一个兄弟节点指标信息失败,Err:" + err.Error())
  419. return
  420. }
  421. prevSort = prevIndex.Sort
  422. }
  423. if nextIndexId > 0 {
  424. //下一个兄弟节点
  425. nextIndex, err = data_manage.GetBaseFromSciIndexByIndexId(nextIndexId)
  426. if err != nil {
  427. if err.Error() == utils.ErrNoRow() {
  428. errMsg = "移动失败"
  429. err = errors.New("下一个兄弟节点指标信息不存在" + err.Error())
  430. return
  431. }
  432. errMsg = "移动失败"
  433. err = errors.New("获取下一个兄弟节点指标信息失败,Err:" + err.Error())
  434. return
  435. }
  436. nextSort = nextIndex.Sort
  437. }
  438. // 修改历史数据中的排序为0的情况
  439. if prevSort == 0 && nextSort == 0 { //目标是让当前分类处于目录的最顶部, 但是历史数据里下个节点的sort为0的情况
  440. //更新为0排序的数据为当前最小排序
  441. if nextIndex != nil {
  442. minSort, e := data_manage.GetBaseFromSciIndexMinSortByClassifyId(classifyId)
  443. if e != nil {
  444. errMsg = "移动失败"
  445. err = errors.New("获取下一个兄弟节点分类信息失败,Err:" + e.Error())
  446. return
  447. }
  448. if minSort > 1 {
  449. minSort -= 1
  450. }
  451. nextIndex.Sort = minSort
  452. err = nextIndex.Update([]string{"Sort"})
  453. if err != nil {
  454. errMsg = "移动失败"
  455. err = errors.New("更新下一个兄弟节点分类信息失败,Err:" + err.Error())
  456. return
  457. }
  458. nextSort = minSort
  459. }
  460. }
  461. //移到两个排序值中间操作
  462. if prevSort >= currentSort {
  463. //往下移动
  464. err = data_manage.MoveDownSciIndexBySort(classifyId, prevSort, currentSort)
  465. if err != nil {
  466. err = errors.New("向下移动出错:" + err.Error())
  467. return
  468. }
  469. sciIndex.Sort = prevSort
  470. } else if nextSort <= currentSort && nextSort != 0 {
  471. //往上移动
  472. err = data_manage.MoveUpSciIndexBySort(classifyId, nextSort, currentSort)
  473. if err != nil {
  474. err = errors.New("向上移动出错:" + err.Error())
  475. return
  476. }
  477. sciIndex.Sort = nextSort
  478. }
  479. //更新
  480. sciIndex.ModifyTime = time.Now()
  481. updateCol = append(updateCol, "Sort", "ModifyTime")
  482. err = sciIndex.Update(updateCol)
  483. if err != nil {
  484. errMsg = "移动失败"
  485. err = errors.New("修改失败,Err:" + err.Error())
  486. return
  487. }
  488. return
  489. }
  490. // EditSciIndex 编辑SCI指标
  491. func EditSciIndex(indexId, classifyId int, sysUser *system.Admin) (baseFromSciIndex *data_manage.BaseFromSciIndex, err error, errMsg string) {
  492. baseFromSciIndex, err = data_manage.GetBaseFromSciIndexByIndexId(indexId)
  493. if err != nil {
  494. errMsg = `获取数据失败`
  495. if err.Error() == utils.ErrNoRow() {
  496. errMsg = `该指标未入库`
  497. err = nil
  498. }
  499. return
  500. }
  501. baseFromSciIndex.ClassifyId = classifyId
  502. baseFromSciIndex.ModifyTime = time.Now()
  503. err = baseFromSciIndex.Update([]string{"ClassifyId", "ModifyTime"})
  504. if err != nil {
  505. errMsg = "保存分类失败"
  506. }
  507. return
  508. }
  509. func InitBaseFromSciClassify() (err error) {
  510. //查询历史数据所有分类
  511. typeList, err := data_manage.GetBaseFromSciIndexType()
  512. if err != nil {
  513. return
  514. }
  515. classifyList := make([]*data_manage.BaseFromSciClassify, 0)
  516. classifyMap := make(map[string][]string, 0)
  517. parentClassifyMap := make(map[string]struct{}, 0)
  518. now := time.Now()
  519. for _, v := range typeList {
  520. if _, ok := parentClassifyMap[v.Type2]; !ok {
  521. parentClassifyMap[v.Type2] = struct{}{}
  522. tmp := new(data_manage.BaseFromSciClassify)
  523. tmp.ClassifyName = v.Type2
  524. tmp.Level = 1
  525. tmp.ModifyTime = now
  526. tmp.CreateTime = now
  527. classifyList = append(classifyList, tmp)
  528. }
  529. classifyMap[v.Type2] = append(classifyMap[v.Type2], v.Type3)
  530. }
  531. //添加一级分类
  532. err = data_manage.AddSciClassifyMulti(classifyList)
  533. if err != nil {
  534. err = errors.New("新增分类出错:" + err.Error())
  535. return
  536. }
  537. //查询分类表的所有父级
  538. rootList, err := data_manage.GetBaseFromSciClassifyByParentId(0)
  539. if err != nil {
  540. return
  541. }
  542. classifyIdMap := make(map[string]int)
  543. for _, v := range rootList {
  544. classifyIdMap[v.ClassifyName] = v.ClassifyId
  545. }
  546. classifyList2 := make([]*data_manage.BaseFromSciClassify, 0)
  547. for _, v := range typeList {
  548. if parentId, ok := classifyIdMap[v.Type2]; ok {
  549. tmp := new(data_manage.BaseFromSciClassify)
  550. tmp.ParentId = parentId
  551. tmp.ClassifyName = v.Type3
  552. tmp.Level = 2
  553. tmp.ModifyTime = now
  554. tmp.CreateTime = now
  555. classifyList2 = append(classifyList2, tmp)
  556. }
  557. }
  558. //添加二级分类
  559. err = data_manage.AddSciClassifyMulti(classifyList2)
  560. if err != nil {
  561. err = errors.New("新增二级分类出错:" + err.Error())
  562. return
  563. }
  564. //初始化sort字段
  565. err = data_manage.InitSciClassifySort()
  566. if err != nil {
  567. return
  568. }
  569. //更新index表里的分类id
  570. err = data_manage.InitSciIndexClassifyId()
  571. return
  572. }
  573. // DelSciData 删除SCI指标
  574. func DelSciData(indexId int) (err error, errMsg string) {
  575. baseFromSciIndex, err := data_manage.GetBaseFromSciIndexByIndexId(indexId)
  576. if err != nil {
  577. errMsg = `获取数据失败`
  578. if err.Error() == utils.ErrNoRow() {
  579. errMsg = `该指标未入库`
  580. err = nil
  581. }
  582. return
  583. }
  584. // 获取已经加入到EDB指标库的Sci指标
  585. edbInfo, err := data_manage.GetEdbInfoByEdbCode(utils.DATA_SOURCE_SCI, baseFromSciIndex.IndexCode)
  586. if err != nil && err.Error() != utils.ErrNoRow() {
  587. errMsg = "删除失败"
  588. err = errors.New("获取分类下的指标信息失败,Err:" + err.Error())
  589. return
  590. }
  591. if edbInfo != nil {
  592. errMsg = "当前指标已被引用,不可删除"
  593. err = nil
  594. return
  595. }
  596. // 删除对应的分类和指标
  597. err = data_manage.DeleteBaseFromSciIndexById(indexId)
  598. if err != nil {
  599. return
  600. }
  601. return
  602. }
  603. // ResetSciData 指标数据清除分类接口
  604. func ResetSciData(indexId int) (err error, errMsg string) {
  605. sciIndex, err := data_manage.GetBaseFromSciIndexByIndexId(indexId)
  606. if err != nil {
  607. errMsg = `获取数据失败`
  608. if err.Error() == utils.ErrNoRow() {
  609. errMsg = `该指标未入库`
  610. err = nil
  611. }
  612. return
  613. }
  614. // 更新指标在未分类下的排序,永远排在未分类的最后一个
  615. //移动排序
  616. classifyId := 0
  617. updateCol := make([]string, 0)
  618. var currentSort, prevSort int
  619. currentSort = sciIndex.Sort
  620. //未分类的最大的sort值
  621. prevSort, err = data_manage.GetBaseFromSciIndexMaxSortByClassifyId(classifyId)
  622. if err != nil {
  623. errMsg = "移动失败"
  624. err = errors.New("获取上一个兄弟节点指标信息失败,Err:" + err.Error())
  625. return
  626. }
  627. //如果前面一个节点的sort值比当前大,则当前节点往下移动
  628. if prevSort >= currentSort {
  629. //往下移动
  630. err = data_manage.MoveDownSciIndexBySort(classifyId, prevSort, currentSort)
  631. if err != nil {
  632. err = errors.New("向下移动出错:" + err.Error())
  633. return
  634. }
  635. sciIndex.Sort = prevSort
  636. }
  637. //更新
  638. sciIndex.ModifyTime = time.Now()
  639. sciIndex.ClassifyId = classifyId
  640. updateCol = append(updateCol, "Sort", "ModifyTime", "ClassifyId")
  641. err = sciIndex.Update(updateCol)
  642. if err != nil {
  643. errMsg = "移动失败"
  644. err = errors.New("修改失败,Err:" + err.Error())
  645. return
  646. }
  647. return
  648. }