base_from_sci_hq.go 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583
  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 err.Error() == utils.ErrNoRow() {
  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 && err.Error() != utils.ErrNoRow() {
  225. errMsg = "删除失败"
  226. err = errors.New("获取分类下的指标信息失败,Err:" + err.Error())
  227. return
  228. }
  229. if edbInfo != nil {
  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 err.Error() == utils.ErrNoRow() {
  246. errMsg = `该指标未入库`
  247. err = nil
  248. }
  249. return
  250. }
  251. classify, err := data_manage.GetSciHqClassifyById(classifyId)
  252. if err != nil {
  253. if err.Error() == utils.ErrNoRow() {
  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 && e.Error() != utils.ErrNoRow() {
  300. err = fmt.Errorf("获取指标是否存在失败, err: %s", e.Error())
  301. return
  302. }
  303. if exist != nil {
  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 currentSort, prevSort, nextSort int
  342. currentSort = classifyInfo.Sort
  343. var prevClassify *data_manage.BaseFromSciHqClassify
  344. var nextClassify *data_manage.BaseFromSciHqClassify
  345. //如果有传入 上一个兄弟节点分类id
  346. if prevClassifyId > 0 {
  347. prevClassify, err = data_manage.GetSciHqClassifyById(prevClassifyId)
  348. if err != nil {
  349. if err.Error() == utils.ErrNoRow() {
  350. errMsg = "移动失败"
  351. err = errors.New("上一个兄弟节点分类信息不存在" + err.Error())
  352. return
  353. }
  354. errMsg = "移动失败"
  355. err = errors.New("获取上一个兄弟节点分类信息失败,Err:" + err.Error())
  356. return
  357. }
  358. prevSort = prevClassify.Sort
  359. }
  360. if nextClassifyId > 0 {
  361. //下一个兄弟节点
  362. nextClassify, err = data_manage.GetSciHqClassifyById(nextClassifyId)
  363. if err != nil {
  364. if err.Error() == utils.ErrNoRow() {
  365. errMsg = "移动失败"
  366. err = errors.New("下一个兄弟节点分类信息不存在" + err.Error())
  367. return
  368. }
  369. errMsg = "移动失败"
  370. err = errors.New("获取下一个兄弟节点分类信息失败,Err:" + err.Error())
  371. return
  372. }
  373. nextSort = nextClassify.Sort
  374. }
  375. // 修改历史数据中的排序为0的情况
  376. if prevSort == 0 && nextSort == 0 { //目标是让当前分类处于目录的最顶部
  377. //更新为0排序的数据为当前最小排序
  378. if nextClassify != nil {
  379. minSort, e := data_manage.GetSciHqIndexClassifyMinSort(parentClassifyId)
  380. if e != nil {
  381. errMsg = "移动失败"
  382. err = errors.New("获取下一个兄弟节点分类信息失败,Err:" + e.Error())
  383. return
  384. }
  385. if minSort > 1 {
  386. minSort -= 1
  387. }
  388. nextClassify.Sort = minSort
  389. err = nextClassify.Update([]string{"sort"})
  390. if err != nil {
  391. errMsg = "移动失败"
  392. err = errors.New("更新下一个兄弟节点分类信息失败,Err:" + err.Error())
  393. return
  394. }
  395. nextSort = minSort
  396. }
  397. }
  398. //移到两个排序值中间操作
  399. if prevSort >= currentSort {
  400. //往下移动
  401. err = data_manage.MoveDownSciHqIndexClassifyBySort(parentClassifyId, prevSort, currentSort)
  402. if err != nil {
  403. err = errors.New("向下移动出错:" + err.Error())
  404. return
  405. }
  406. classifyInfo.Sort = prevSort
  407. } else if nextSort <= currentSort && nextSort != 0 {
  408. //往上移动
  409. err = data_manage.MoveUpSciHqIndexClassifyBySort(parentClassifyId, nextSort, currentSort)
  410. if err != nil {
  411. err = errors.New("向上移动出错:" + err.Error())
  412. return
  413. }
  414. classifyInfo.Sort = nextSort
  415. }
  416. classifyInfo.ModifyTime = time.Now()
  417. updateCol = append(updateCol, "Sort", "ModifyTime")
  418. err = classifyInfo.Update(updateCol)
  419. if err != nil {
  420. errMsg = "移动失败"
  421. err = errors.New("修改失败,Err:" + err.Error())
  422. return
  423. }
  424. return
  425. }
  426. // MoveSciHqData 移动卓创红期指标
  427. func MoveSciHqData(indexId, classifyId, prevIndexId, nextIndexId int) (err error, errMsg string) {
  428. //分类信息
  429. sciHqIndex, err := data_manage.GetSciHqIndexByIndexId(indexId)
  430. if err != nil {
  431. errMsg = `获取数据失败`
  432. if err.Error() == utils.ErrNoRow() {
  433. errMsg = `该指标未入库`
  434. err = nil
  435. }
  436. return
  437. }
  438. //判断分类是否存在
  439. if classifyId > 0 {
  440. _, err = data_manage.GetSciHqClassifyById(classifyId)
  441. if err != nil {
  442. errMsg = "移动失败"
  443. err = errors.New("获取分类信息失败,Err:" + err.Error())
  444. if err.Error() == utils.ErrNoRow() {
  445. errMsg = "找不到该分类"
  446. err = nil
  447. }
  448. return
  449. }
  450. }
  451. //如果改变了分类,那么移动该图表数据
  452. if sciHqIndex.ClassifyId != classifyId {
  453. tmpErr := data_manage.MoveSciHqIndex(indexId, classifyId)
  454. if tmpErr != nil {
  455. errMsg = "移动失败"
  456. err = errors.New("移动失败,Err:" + tmpErr.Error())
  457. return
  458. }
  459. }
  460. //移动排序
  461. updateCol := make([]string, 0)
  462. var currentSort, prevSort, nextSort int
  463. currentSort = sciHqIndex.Sort
  464. //如果有传入 上一个兄弟节点分类id
  465. var prevIndex *data_manage.BaseFromSciHqIndex
  466. var nextIndex *data_manage.BaseFromSciHqIndex
  467. if prevIndexId > 0 {
  468. prevIndex, err = data_manage.GetSciHqIndexByIndexId(prevIndexId)
  469. if err != nil {
  470. if err.Error() == utils.ErrNoRow() {
  471. errMsg = "移动失败"
  472. err = errors.New("上一个兄弟节点指标信息不存在" + err.Error())
  473. return
  474. }
  475. errMsg = "移动失败"
  476. err = errors.New("获取上一个兄弟节点指标信息失败,Err:" + err.Error())
  477. return
  478. }
  479. prevSort = prevIndex.Sort
  480. }
  481. if nextIndexId > 0 {
  482. //下一个兄弟节点
  483. nextIndex, err = data_manage.GetSciHqIndexByIndexId(nextIndexId)
  484. if err != nil {
  485. if err.Error() == utils.ErrNoRow() {
  486. errMsg = "移动失败"
  487. err = errors.New("下一个兄弟节点指标信息不存在" + err.Error())
  488. return
  489. }
  490. errMsg = "移动失败"
  491. err = errors.New("获取下一个兄弟节点指标信息失败,Err:" + err.Error())
  492. return
  493. }
  494. nextSort = nextIndex.Sort
  495. }
  496. // 修改历史数据中的排序为0的情况
  497. if prevSort == 0 && nextSort == 0 { //目标是让当前分类处于目录的最顶部, 但是历史数据里下个节点的sort为0的情况
  498. //更新为0排序的数据为当前最小排序
  499. if nextIndex != nil {
  500. minSort, e := data_manage.GetSciHqIndexMinSortByClassifyId(classifyId)
  501. if e != nil {
  502. errMsg = "移动失败"
  503. err = errors.New("获取下一个兄弟节点分类信息失败,Err:" + e.Error())
  504. return
  505. }
  506. if minSort > 1 {
  507. minSort -= 1
  508. }
  509. nextIndex.Sort = minSort
  510. err = nextIndex.Update([]string{"Sort"})
  511. if err != nil {
  512. errMsg = "移动失败"
  513. err = errors.New("更新下一个兄弟节点分类信息失败,Err:" + err.Error())
  514. return
  515. }
  516. nextSort = minSort
  517. }
  518. }
  519. //移到两个排序值中间操作
  520. if prevSort >= currentSort {
  521. //往下移动
  522. err = data_manage.MoveDownSciHqIndexBySort(classifyId, prevSort, currentSort)
  523. if err != nil {
  524. err = errors.New("向下移动出错:" + err.Error())
  525. return
  526. }
  527. sciHqIndex.Sort = prevSort
  528. } else if nextSort <= currentSort && nextSort != 0 {
  529. //往上移动
  530. err = data_manage.MoveUpSciHqIndexBySort(classifyId, nextSort, currentSort)
  531. if err != nil {
  532. err = errors.New("向上移动出错:" + err.Error())
  533. return
  534. }
  535. sciHqIndex.Sort = nextSort
  536. }
  537. //更新
  538. sciHqIndex.ModifyTime = time.Now()
  539. updateCol = append(updateCol, "Sort", "ModifyTime")
  540. err = sciHqIndex.Update(updateCol)
  541. if err != nil {
  542. errMsg = "移动失败"
  543. err = errors.New("修改失败,Err:" + err.Error())
  544. return
  545. }
  546. return
  547. }