base_from_baiinfo.go 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710
  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. // AddBaiinfoClassify 添加SCI分类
  10. func AddBaiinfoClassify(classifyName string, parentId, sysUserId int, sysUserName, lang string) (classifyInfo *data_manage.BaseFromBaiinfoClassify, err error, errMsg string) {
  11. // 校验分类名称相同的数量
  12. {
  13. var count int
  14. switch lang {
  15. case utils.EnLangVersion:
  16. count, err = data_manage.GetBaseFromBaiinfoClassifyEnCount(classifyName, parentId)
  17. default:
  18. count, err = data_manage.GetBaseFromBaiinfoClassifyCount(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.GetBaseFromBaiinfoClassifyById(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.BaseFromBaiinfoClassify{
  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.AddBaseFromBaiinfoClassify(classifyInfo)
  57. if err != nil {
  58. errMsg = "保存分类失败"
  59. return
  60. }
  61. classifyInfo.ClassifyId = int(classifyId)
  62. //更新sort为classifyId
  63. err = data_manage.UpdateBaseFromBaiinfoClassifySort(int(classifyId))
  64. return
  65. }
  66. // EditBaiinfoClassify 编辑Baiinfo分类
  67. func EditBaiinfoClassify(classifyId int, classifyName, lang string, sysUser *system.Admin) (err error, errMsg string) {
  68. item, err := data_manage.GetBaseFromBaiinfoClassifyById(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.GetBaseFromBaiinfoClassifyEnCount(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.EditBaseFromBaiinfoClassifyEn(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.GetBaseFromBaiinfoClassifyCount(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.EditBaseFromBaiinfoClassify(classifyId, classifyName)
  115. if err != nil {
  116. errMsg = "保存失败"
  117. }
  118. }
  119. return
  120. }
  121. // MoveBaiinfoClassify 移动Baiinfo分类
  122. func MoveBaiinfoClassify(classifyId, parentClassifyId, prevClassifyId, nextClassifyId int) (err error, errMsg string) {
  123. //判断分类是否存在
  124. classifyInfo, err := data_manage.GetBaseFromBaiinfoClassifyById(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.GetBaseFromBaiinfoClassifyById(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.BaseFromBaiinfoClassify
  146. var nextClassify *data_manage.BaseFromBaiinfoClassify
  147. //如果有传入 上一个兄弟节点分类id
  148. if prevClassifyId > 0 {
  149. prevClassify, err = data_manage.GetBaseFromBaiinfoClassifyById(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.GetBaseFromBaiinfoClassifyById(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.GetBaiinfoIndexClassifyMinSort(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.MoveDownBaiinfoIndexClassifyBySort(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.MoveUpBaiinfoIndexClassifyBySort(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. // DelBaiinfoClassify 删除SCI分类
  229. func DelBaiinfoClassify(classifyId int, sysUser *system.Admin) (err error, errMsg string) {
  230. //判断分类是否存在
  231. classifyInfo, err := data_manage.GetBaseFromBaiinfoClassifyById(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.GetBaseFromBaiinfoClassifyByParentId(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.GetBaseFromBaiinfoIndexCountByClassifyIds(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.DeleteBaiinfoClassifyByClassifyId(classifyIdList)
  266. if err != nil {
  267. return
  268. }
  269. return
  270. }
  271. // MoveBaiinfoData 移动SCI指标
  272. func MoveBaiinfoData(indexId, classifyId, prevIndexId, nextIndexId int) (err error, errMsg string) {
  273. //分类信息
  274. baiinfoIndex, err := data_manage.GetBaseFromBaiinfoIndexByIndexId(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.GetBaseFromBaiinfoClassifyById(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 baiinfoIndex.ClassifyId != classifyId {
  298. tmpErr := data_manage.MoveBaseFromBaiinfoIndex(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.GetBaseFromBaiinfoIndexByIndexId(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.GetBaseFromBaiinfoIndexByIndexId(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 == baiinfoIndex.Sort {
  326. //变更兄弟节点的排序
  327. updateSortStr := `sort + 2`
  328. err = data_manage.UpdateBaseFromBaiinfoIndexByClassifyId(prevIndex.ClassifyId, prevIndex.Sort, prevIndex.BaseFromBaiinfoIndexId, 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.UpdateBaseFromBaiinfoIndexByClassifyId(prevIndex.ClassifyId, prevIndex.Sort, prevIndex.BaseFromBaiinfoIndexId, updateSortStr)
  340. if err != nil {
  341. errMsg = "移动失败"
  342. err = errors.New("变更兄弟节点排序指标信息失败,Err:" + err.Error())
  343. return
  344. }
  345. }
  346. }
  347. }
  348. baiinfoIndex.Sort = prevIndex.Sort + 1
  349. baiinfoIndex.ModifyTime = time.Now()
  350. updateCol = append(updateCol, "Sort", "ModifyTime")
  351. } else {
  352. firstClassify, tmpErr := data_manage.GetFirstBaseFromBaiinfoIndexByClassifyId(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.UpdateBaseFromBaiinfoIndexByClassifyId(firstClassify.ClassifyId, 0, firstClassify.BaseFromBaiinfoIndexId-1, updateSortStr)
  362. }
  363. baiinfoIndex.Sort = 0 //那就是排在第一位
  364. baiinfoIndex.ModifyTime = time.Now()
  365. updateCol = append(updateCol, "Sort", "ModifyTime")
  366. }
  367. //更新
  368. if len(updateCol) > 0 {
  369. err = baiinfoIndex.Update(updateCol)
  370. if err != nil {
  371. errMsg = "移动失败"
  372. err = errors.New("修改失败,Err:" + err.Error())
  373. return
  374. }
  375. }
  376. return
  377. }
  378. // MoveBaiinfoData2 移动SCI指标
  379. func MoveBaiinfoData2(indexId, classifyId, prevIndexId, nextIndexId int) (err error, errMsg string) {
  380. //分类信息
  381. baiinfoIndex, err := data_manage.GetBaseFromBaiinfoIndexByIndexId(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.GetBaseFromBaiinfoClassifyById(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 baiinfoIndex.ClassifyId != classifyId {
  405. tmpErr := data_manage.MoveBaseFromBaiinfoIndex(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 = baiinfoIndex.Sort
  416. //如果有传入 上一个兄弟节点分类id
  417. var prevIndex *data_manage.BaseFromBaiinfoIndex
  418. var nextIndex *data_manage.BaseFromBaiinfoIndex
  419. if prevIndexId > 0 {
  420. prevIndex, err = data_manage.GetBaseFromBaiinfoIndexByIndexId(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.GetBaseFromBaiinfoIndexByIndexId(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.GetBaseFromBaiinfoIndexMinSortByClassifyId(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.MoveDownBaiinfoIndexBySort(classifyId, prevSort, currentSort)
  475. if err != nil {
  476. err = errors.New("向下移动出错:" + err.Error())
  477. return
  478. }
  479. baiinfoIndex.Sort = prevSort
  480. } else if nextSort <= currentSort && nextSort != 0 {
  481. //往上移动
  482. err = data_manage.MoveUpBaiinfoIndexBySort(classifyId, nextSort, currentSort)
  483. if err != nil {
  484. err = errors.New("向上移动出错:" + err.Error())
  485. return
  486. }
  487. baiinfoIndex.Sort = nextSort
  488. }
  489. //更新
  490. baiinfoIndex.ModifyTime = time.Now()
  491. updateCol = append(updateCol, "Sort", "ModifyTime")
  492. err = baiinfoIndex.Update(updateCol)
  493. if err != nil {
  494. errMsg = "移动失败"
  495. err = errors.New("修改失败,Err:" + err.Error())
  496. return
  497. }
  498. return
  499. }
  500. // EditBaiinfoIndex 编辑SCI指标
  501. func EditBaiinfoIndex(indexId, classifyId int, frequency, unit string, sysUser *system.Admin) (baseFromBaiinfoIndex *data_manage.BaseFromBaiinfoIndex, err error, errMsg string) {
  502. baseFromBaiinfoIndex, err = data_manage.GetBaseFromBaiinfoIndexByIndexId(indexId)
  503. if err != nil {
  504. errMsg = `获取数据失败`
  505. if err.Error() == utils.ErrNoRow() {
  506. errMsg = `该指标未入库`
  507. err = nil
  508. }
  509. return
  510. }
  511. baseFromBaiinfoIndex.ClassifyId = classifyId
  512. baseFromBaiinfoIndex.ModifyTime = time.Now()
  513. baseFromBaiinfoIndex.Frequency = frequency
  514. baseFromBaiinfoIndex.Unit = unit
  515. err = baseFromBaiinfoIndex.Update([]string{"ClassifyId", "ModifyTime", "frequency", "unit"})
  516. if err != nil {
  517. errMsg = "保存分类失败"
  518. }
  519. return
  520. }
  521. func InitBaseFromBaiinfoClassify() (err error) {
  522. //查询历史数据所有分类
  523. typeList, err := data_manage.GetBaseFromBaiinfoIndexType()
  524. if err != nil {
  525. return
  526. }
  527. classifyList := make([]*data_manage.BaseFromBaiinfoClassify, 0)
  528. classifyMap := make(map[string][]string, 0)
  529. parentClassifyMap := make(map[string]struct{}, 0)
  530. now := time.Now()
  531. for _, v := range typeList {
  532. if _, ok := parentClassifyMap[v.Type2]; !ok {
  533. parentClassifyMap[v.Type2] = struct{}{}
  534. tmp := new(data_manage.BaseFromBaiinfoClassify)
  535. tmp.ClassifyName = v.Type2
  536. tmp.Level = 1
  537. tmp.ModifyTime = now
  538. tmp.CreateTime = now
  539. classifyList = append(classifyList, tmp)
  540. }
  541. classifyMap[v.Type2] = append(classifyMap[v.Type2], v.Type3)
  542. }
  543. //添加一级分类
  544. err = data_manage.AddBaiinfoClassifyMulti(classifyList)
  545. if err != nil {
  546. err = errors.New("新增分类出错:" + err.Error())
  547. return
  548. }
  549. //查询分类表的所有父级
  550. rootList, err := data_manage.GetBaseFromBaiinfoClassifyByParentId(0)
  551. if err != nil {
  552. return
  553. }
  554. classifyIdMap := make(map[string]int)
  555. for _, v := range rootList {
  556. classifyIdMap[v.ClassifyName] = v.ClassifyId
  557. }
  558. classifyList2 := make([]*data_manage.BaseFromBaiinfoClassify, 0)
  559. for _, v := range typeList {
  560. if parentId, ok := classifyIdMap[v.Type2]; ok {
  561. tmp := new(data_manage.BaseFromBaiinfoClassify)
  562. tmp.ParentId = parentId
  563. tmp.ClassifyName = v.Type3
  564. tmp.Level = 2
  565. tmp.ModifyTime = now
  566. tmp.CreateTime = now
  567. classifyList2 = append(classifyList2, tmp)
  568. }
  569. }
  570. //添加二级分类
  571. err = data_manage.AddBaiinfoClassifyMulti(classifyList2)
  572. if err != nil {
  573. err = errors.New("新增二级分类出错:" + err.Error())
  574. return
  575. }
  576. //初始化sort字段
  577. err = data_manage.InitBaiinfoClassifySort()
  578. if err != nil {
  579. return
  580. }
  581. //更新index表里的分类id
  582. err = data_manage.InitBaiinfoIndexClassifyId()
  583. return
  584. }
  585. // DelBaiinfoData 删除SCI指标
  586. func DelBaiinfoData(indexId int) (err error, errMsg string) {
  587. baseFromBaiinfoIndex, err := data_manage.GetBaseFromBaiinfoIndexByIndexId(indexId)
  588. if err != nil {
  589. errMsg = `获取数据失败`
  590. if err.Error() == utils.ErrNoRow() {
  591. errMsg = `该指标未入库`
  592. err = nil
  593. }
  594. return
  595. }
  596. // 获取已经加入到EDB指标库的Baiinfo指标
  597. edbInfo, err := data_manage.GetEdbInfoByEdbCode(utils.DATA_SOURCE_BAIINFO, baseFromBaiinfoIndex.IndexCode)
  598. if err != nil && err.Error() != utils.ErrNoRow() {
  599. errMsg = "删除失败"
  600. err = errors.New("获取分类下的指标信息失败,Err:" + err.Error())
  601. return
  602. }
  603. if edbInfo != nil {
  604. errMsg = "当前指标已被引用,不可删除"
  605. err = nil
  606. return
  607. }
  608. // 删除对应的分类和指标
  609. err = data_manage.DeleteBaseFromBaiinfoIndexById(indexId)
  610. if err != nil {
  611. return
  612. }
  613. return
  614. }
  615. // ResetBaiinfoData 指标数据清除分类接口
  616. func ResetBaiinfoData(indexId int) (err error, errMsg string) {
  617. baiinfoIndex, err := data_manage.GetBaseFromBaiinfoIndexByIndexId(indexId)
  618. if err != nil {
  619. errMsg = `获取数据失败`
  620. if err.Error() == utils.ErrNoRow() {
  621. errMsg = `该指标未入库`
  622. err = nil
  623. }
  624. return
  625. }
  626. // 更新指标在未分类下的排序,永远排在未分类的最后一个
  627. //移动排序
  628. classifyId := 0
  629. updateCol := make([]string, 0)
  630. var currentSort, prevSort int
  631. currentSort = baiinfoIndex.Sort
  632. //未分类的最大的sort值
  633. prevSort, err = data_manage.GetBaseFromBaiinfoIndexMaxSortByClassifyId(classifyId)
  634. if err != nil {
  635. errMsg = "移动失败"
  636. err = errors.New("获取上一个兄弟节点指标信息失败,Err:" + err.Error())
  637. return
  638. }
  639. //如果前面一个节点的sort值比当前大,则当前节点往下移动
  640. if prevSort >= currentSort {
  641. //往下移动
  642. err = data_manage.MoveDownBaiinfoIndexBySort(classifyId, prevSort, currentSort)
  643. if err != nil {
  644. err = errors.New("向下移动出错:" + err.Error())
  645. return
  646. }
  647. baiinfoIndex.Sort = prevSort
  648. }
  649. //更新
  650. baiinfoIndex.ModifyTime = time.Now()
  651. baiinfoIndex.ClassifyId = classifyId
  652. updateCol = append(updateCol, "Sort", "ModifyTime", "ClassifyId")
  653. err = baiinfoIndex.Update(updateCol)
  654. if err != nil {
  655. errMsg = "移动失败"
  656. err = errors.New("修改失败,Err:" + err.Error())
  657. return
  658. }
  659. return
  660. }