base_from_sci.go 20 KB

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