edb_data_calculate_ljztbpj.go 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662
  1. package models
  2. import (
  3. "errors"
  4. "eta/eta_index_lib/utils"
  5. "fmt"
  6. "github.com/beego/beego/v2/client/orm"
  7. "github.com/shopspring/decimal"
  8. "sort"
  9. "strings"
  10. "time"
  11. )
  12. // EdbDataCalculateLjztbpj 累计值同比拼接数据结构体
  13. type EdbDataCalculateLjztbpj struct {
  14. EdbDataId int `orm:"column(edb_data_id);pk"`
  15. EdbInfoId int
  16. EdbCode string
  17. DataTime string
  18. Value float64
  19. Status int
  20. CreateTime time.Time
  21. ModifyTime time.Time
  22. DataTimestamp int64
  23. }
  24. // AddCalculateLjztbpj 新增累计值同比拼接数据
  25. func AddCalculateLjztbpj(req *EdbInfoCalculateBatchSaveReq, firstEdbInfo, secondEdbInfo *EdbInfo, edbCode, uniqueCode string, sysUserId int, sysUserRealName string) (edbInfo *EdbInfo, err error) {
  26. o := orm.NewOrm()
  27. to, err := o.Begin()
  28. if err != nil {
  29. return
  30. }
  31. defer func() {
  32. if err != nil {
  33. fmt.Println("AddCalculateLjztbpj,Err:" + err.Error())
  34. _ = to.Rollback()
  35. } else {
  36. _ = to.Commit()
  37. }
  38. }()
  39. //待拼接指标map
  40. pjDataMap := make(map[string]float64)
  41. //拼接指标的日期切片数据
  42. pjEdbDataTimeList := make([]string, 0)
  43. //最近开始的时间
  44. var lastDateTime time.Time
  45. //获取待拼接指标
  46. {
  47. var condition string
  48. var pars []interface{}
  49. //获取待拼接指标最近的个12月31日有值的年份
  50. condition += " AND data_time like ? AND edb_info_id=? "
  51. pars = append(pars, "%12-31", firstEdbInfo.EdbInfoId)
  52. lastEdbData, tmpErr := GetLastEdbData(condition, pars, firstEdbInfo.Source, firstEdbInfo.SubSource)
  53. if tmpErr != nil {
  54. err = tmpErr
  55. return
  56. }
  57. lastDateTime, _ = time.ParseInLocation(utils.FormatDate, lastEdbData.DataTime, time.Local)
  58. //获取待拼接指标的数据列表
  59. firstDataList, tmpErr := GetEdbDataListAllByTo(to, firstEdbInfo.Source, firstEdbInfo.SubSource, FindEdbDataListAllCond{
  60. EdbInfoId: firstEdbInfo.EdbInfoId,
  61. EndDataTime: lastEdbData.DataTime,
  62. EndDataTimeCond: "<=",
  63. }, 0)
  64. if tmpErr != nil {
  65. err = tmpErr
  66. return
  67. }
  68. for _, v := range firstDataList {
  69. pjDataMap[v.DataTime] = v.Value
  70. pjEdbDataTimeList = append(pjEdbDataTimeList, v.DataTime)
  71. }
  72. }
  73. if req.EdbInfoId <= 0 {
  74. edbInfo = &EdbInfo{
  75. SourceName: "累计值同比拼接",
  76. Source: utils.DATA_SOURCE_CALCULATE_LJZTBPJ,
  77. EdbCode: edbCode,
  78. EdbName: req.EdbName,
  79. EdbNameSource: req.EdbName,
  80. Frequency: req.Frequency,
  81. Unit: req.Unit,
  82. StartDate: firstEdbInfo.StartDate,
  83. EndDate: firstEdbInfo.EndDate,
  84. ClassifyId: req.ClassifyId,
  85. SysUserId: sysUserId,
  86. SysUserRealName: sysUserRealName,
  87. UniqueCode: uniqueCode,
  88. CreateTime: time.Now(),
  89. ModifyTime: time.Now(),
  90. CalculateFormula: lastDateTime.Format(utils.FormatDate),
  91. EdbNameEn: req.EdbName,
  92. UnitEn: req.Unit,
  93. EdbType: 2,
  94. Sort: GetAddEdbMaxSortByClassifyId(req.ClassifyId, utils.EDB_INFO_TYPE),
  95. }
  96. newEdbInfoId, tmpErr := to.Insert(edbInfo)
  97. if tmpErr != nil {
  98. err = tmpErr
  99. return
  100. }
  101. edbInfo.EdbInfoId = int(newEdbInfoId)
  102. } else {
  103. edbInfo, err = GetEdbInfoById(req.EdbInfoId)
  104. if err != nil {
  105. return
  106. }
  107. //查询
  108. tmpEdbInfo, tmpErr := GetEdbInfoById(edbInfo.EdbInfoId)
  109. if tmpErr != nil {
  110. err = tmpErr
  111. return
  112. }
  113. tmpEdbInfo.EdbName = req.EdbName
  114. tmpEdbInfo.ClassifyId = req.ClassifyId
  115. tmpEdbInfo.Frequency = req.Frequency
  116. tmpEdbInfo.Unit = req.Unit
  117. tmpEdbInfo.CalculateFormula = req.Formula
  118. edbInfo = tmpEdbInfo
  119. //删除指标数据
  120. dataTableName := GetEdbDataTableName(utils.DATA_SOURCE_CALCULATE_LJZTBPJ, utils.DATA_SUB_SOURCE_EDB)
  121. fmt.Println("dataTableName:" + dataTableName)
  122. deleteSql := ` DELETE FROM %s WHERE edb_info_id=? `
  123. deleteSql = fmt.Sprintf(deleteSql, dataTableName)
  124. _, err = to.Raw(deleteSql, req.EdbInfoId).Exec()
  125. if err != nil {
  126. return
  127. }
  128. //删除指标关系
  129. sql := ` DELETE FROM edb_info_calculate_mapping WHERE edb_info_id=? `
  130. _, err = to.Raw(sql, edbInfo.EdbInfoId).Exec()
  131. }
  132. //关联关系
  133. var existItemA, existItemB *EdbInfoCalculateMapping
  134. //拼接指标
  135. {
  136. existItemA = &EdbInfoCalculateMapping{
  137. EdbInfoCalculateMappingId: 0,
  138. EdbInfoId: edbInfo.EdbInfoId,
  139. Source: edbInfo.Source,
  140. SourceName: edbInfo.SourceName,
  141. EdbCode: edbInfo.EdbCode,
  142. FromEdbInfoId: firstEdbInfo.EdbInfoId,
  143. FromEdbCode: firstEdbInfo.EdbCode,
  144. FromEdbName: firstEdbInfo.EdbName,
  145. FromSource: firstEdbInfo.Source,
  146. FromSourceName: firstEdbInfo.SourceName,
  147. FromTag: "A",
  148. Sort: 1,
  149. CreateTime: time.Now(),
  150. ModifyTime: time.Now(),
  151. FromSubSource: firstEdbInfo.SubSource,
  152. }
  153. insertId, tmpErr := to.Insert(existItemA)
  154. if tmpErr != nil {
  155. err = tmpErr
  156. return
  157. }
  158. existItemA.EdbInfoCalculateMappingId = int(insertId)
  159. }
  160. //同比值指标
  161. {
  162. existItemB = &EdbInfoCalculateMapping{
  163. EdbInfoCalculateMappingId: 0,
  164. EdbInfoId: edbInfo.EdbInfoId,
  165. Source: edbInfo.Source,
  166. SourceName: edbInfo.SourceName,
  167. EdbCode: edbInfo.EdbCode,
  168. FromEdbInfoId: secondEdbInfo.EdbInfoId,
  169. FromEdbCode: secondEdbInfo.EdbCode,
  170. FromEdbName: secondEdbInfo.EdbName,
  171. FromSource: secondEdbInfo.Source,
  172. FromSourceName: secondEdbInfo.SourceName,
  173. FromTag: "B",
  174. Sort: 1,
  175. CreateTime: time.Now(),
  176. ModifyTime: time.Now(),
  177. FromSubSource: secondEdbInfo.SubSource,
  178. }
  179. insertId, tmpErr := to.Insert(existItemB)
  180. if tmpErr != nil {
  181. err = tmpErr
  182. return
  183. }
  184. existItemB.EdbInfoCalculateMappingId = int(insertId)
  185. }
  186. // 添加数据
  187. err = refreshAllCalculateLjztbpj(to, edbInfo, existItemA, existItemB)
  188. return
  189. }
  190. // EditCalculateLjztbpj 编辑累计值同比拼接数据
  191. func EditCalculateLjztbpj(req *EdbInfoCalculateBatchEditReq, edbInfo, firstEdbInfo, secondEdbInfo *EdbInfo) (err error) {
  192. o := orm.NewOrm()
  193. to, err := o.Begin()
  194. if err != nil {
  195. return
  196. }
  197. defer func() {
  198. if err != nil {
  199. fmt.Println("EditCalculateLjztbpj,Err:" + err.Error())
  200. _ = to.Rollback()
  201. } else {
  202. _ = to.Commit()
  203. }
  204. }()
  205. nowEdbInfo := *edbInfo // 现在的指标信息
  206. sql := ``
  207. //修改指标信息
  208. edbInfo.EdbNameSource = req.EdbName
  209. edbInfo.Frequency = req.Frequency
  210. edbInfo.Unit = req.Unit
  211. edbInfo.ClassifyId = req.ClassifyId
  212. //edbInfo.CalculateFormula = req.Formula
  213. edbInfo.EdbNameEn = req.EdbNameEn
  214. edbInfo.UnitEn = req.UnitEn
  215. edbInfo.ModifyTime = time.Now()
  216. _, err = to.Update(edbInfo, "EdbName", "EdbNameSource", "Frequency", "Unit", "ClassifyId", "ModifyTime", "EdbNameEn", "UnitEn")
  217. if err != nil {
  218. return
  219. }
  220. //查询出所有的关联指标
  221. var existCondition string
  222. var existPars []interface{}
  223. existCondition += " AND edb_info_id=? "
  224. existPars = append(existPars, edbInfo.EdbInfoId)
  225. existList, err := GetEdbInfoCalculateListByCondition(existCondition, existPars)
  226. if err != nil {
  227. err = errors.New("判断指标是否改变失败,Err:" + err.Error())
  228. return
  229. }
  230. var existItemA, existItemB *EdbInfoCalculateMapping
  231. for _, existItem := range existList {
  232. if existItem.FromTag == "A" {
  233. existItemA = existItem
  234. } else if existItem.FromTag == "B" {
  235. existItemB = existItem
  236. }
  237. }
  238. // 是否需要删除数据重新计算
  239. isNeedCalculateData := false
  240. // 如果截止日期变更,那么需要重新计算
  241. if req.Formula != nowEdbInfo.CalculateFormula {
  242. isNeedCalculateData = true
  243. }
  244. //待拼接指标数据
  245. //如果拼接指标变更了,那么需要删除所有的指标进行重新拼接
  246. if existItemA.FromEdbInfoId != firstEdbInfo.EdbInfoId {
  247. //删除之前的A指标关联关系
  248. sql := ` DELETE FROM edb_info_calculate_mapping WHERE edb_info_id = ? and from_edb_info_id = ?`
  249. _, err = to.Raw(sql, edbInfo.EdbInfoId, existItemA.FromEdbInfoId).Exec()
  250. if err != nil {
  251. err = errors.New("删除拼接日期之前的指标关联关系失败,Err:" + err.Error())
  252. return
  253. }
  254. //添加新的指标关系
  255. {
  256. existItemA = &EdbInfoCalculateMapping{
  257. EdbInfoCalculateMappingId: 0,
  258. EdbInfoId: edbInfo.EdbInfoId,
  259. Source: edbInfo.Source,
  260. SourceName: edbInfo.SourceName,
  261. EdbCode: edbInfo.EdbCode,
  262. FromEdbInfoId: firstEdbInfo.EdbInfoId,
  263. FromEdbCode: firstEdbInfo.EdbCode,
  264. FromEdbName: firstEdbInfo.EdbName,
  265. FromSource: firstEdbInfo.Source,
  266. FromSourceName: firstEdbInfo.SourceName,
  267. FromTag: "A",
  268. Sort: 1,
  269. CreateTime: time.Now(),
  270. ModifyTime: time.Now(),
  271. FromSubSource: firstEdbInfo.SubSource,
  272. }
  273. insertId, tmpErr := to.Insert(existItemA)
  274. if tmpErr != nil {
  275. err = tmpErr
  276. return
  277. }
  278. existItemA.EdbInfoCalculateMappingId = int(insertId)
  279. isNeedCalculateData = true
  280. }
  281. }
  282. //同比值指标
  283. if existItemB.FromEdbInfoId != secondEdbInfo.EdbInfoId {
  284. //删除之前的B指标关联关系
  285. sql = ` DELETE FROM edb_info_calculate_mapping WHERE edb_info_id = ? and from_edb_info_id = ?`
  286. _, err = to.Raw(sql, edbInfo.EdbInfoId, existItemB.FromEdbInfoId).Exec()
  287. if err != nil {
  288. err = errors.New("删除拼接日期之后的指标关联关系失败,Err:" + err.Error())
  289. return
  290. }
  291. //添加新的指标关系
  292. {
  293. existItemB = &EdbInfoCalculateMapping{
  294. EdbInfoCalculateMappingId: 0,
  295. EdbInfoId: edbInfo.EdbInfoId,
  296. Source: edbInfo.Source,
  297. SourceName: edbInfo.SourceName,
  298. EdbCode: edbInfo.EdbCode,
  299. FromEdbInfoId: secondEdbInfo.EdbInfoId,
  300. FromEdbCode: secondEdbInfo.EdbCode,
  301. FromEdbName: secondEdbInfo.EdbName,
  302. FromSource: secondEdbInfo.Source,
  303. FromSourceName: secondEdbInfo.SourceName,
  304. FromTag: "B",
  305. Sort: 2,
  306. CreateTime: time.Now(),
  307. ModifyTime: time.Now(),
  308. FromSubSource: secondEdbInfo.SubSource,
  309. }
  310. insertId, tmpErr := to.Insert(existItemB)
  311. if tmpErr != nil {
  312. err = tmpErr
  313. return
  314. }
  315. existItemB.EdbInfoCalculateMappingId = int(insertId)
  316. isNeedCalculateData = true
  317. }
  318. }
  319. // 如果需要重新计算,那么先删除所有的指标数据,然后再重新计算
  320. if isNeedCalculateData {
  321. // 删除之前所有的指标数据
  322. tableName := GetEdbDataTableName(edbInfo.Source, edbInfo.SubSource)
  323. sql = fmt.Sprintf(` DELETE FROM %s WHERE edb_info_id = ? `, tableName)
  324. _, err = to.Raw(sql, edbInfo.EdbInfoId).Exec()
  325. if err != nil {
  326. err = errors.New("删除所有的累计值同比拼接指标数据失败,Err:" + err.Error())
  327. return
  328. }
  329. //获取待拼接指标最近一年的12月份日期
  330. {
  331. var condition string
  332. var pars []interface{}
  333. //获取待拼接指标最近的个12月31日有值的年份
  334. condition += " AND data_time like ? AND edb_info_id=? "
  335. pars = append(pars, "%12-31", firstEdbInfo.EdbInfoId)
  336. lastEdbData, tmpErr := GetLastEdbData(condition, pars, firstEdbInfo.Source, firstEdbInfo.SubSource)
  337. if tmpErr != nil {
  338. err = tmpErr
  339. return
  340. }
  341. lastDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, lastEdbData.DataTime, time.Local)
  342. if tmpErr != nil {
  343. err = tmpErr
  344. return
  345. }
  346. edbInfo.CalculateFormula = lastDateTime.Format(utils.FormatDate)
  347. _, err = to.Update(edbInfo, "CalculateFormula")
  348. if err != nil {
  349. return
  350. }
  351. }
  352. // 重新计算
  353. err = refreshAllCalculateLjztbpj(to, edbInfo, existItemA, existItemB)
  354. }
  355. return
  356. }
  357. // GetAllEdbDataCalculateLjztbpjByEdbInfoId 根据指标id获取全部的数据
  358. func GetAllEdbDataCalculateLjztbpjByEdbInfoId(edbInfoId int) (items []*EdbDataCalculateLjztbpj, err error) {
  359. o := orm.NewOrm()
  360. sql := ` SELECT * FROM edb_data_calculate_ljztbpj WHERE edb_info_id=? ORDER BY data_time DESC `
  361. _, err = o.Raw(sql, edbInfoId).QueryRows(&items)
  362. return
  363. }
  364. // RefreshAllCalculateLjztbpj 刷新所有 累计值同比拼接 数据
  365. func RefreshAllCalculateLjztbpj(edbInfo *EdbInfo) (err error) {
  366. o := orm.NewOrm()
  367. to, err := o.Begin()
  368. if err != nil {
  369. return
  370. }
  371. defer func() {
  372. if err != nil {
  373. fmt.Println("RefreshAllCalculateLjztbpj,Err:" + err.Error())
  374. _ = to.Rollback()
  375. } else {
  376. _ = to.Commit()
  377. }
  378. }()
  379. //查询关联指标信息
  380. var existCondition string
  381. var existPars []interface{}
  382. existCondition += " AND edb_info_id=? "
  383. existPars = append(existPars, edbInfo.EdbInfoId)
  384. existList, err := GetEdbInfoCalculateListByCondition(existCondition, existPars)
  385. if err != nil {
  386. err = errors.New("判断指标是否改变失败,Err:" + err.Error())
  387. return
  388. }
  389. var existItemA, existItemB *EdbInfoCalculateMapping
  390. for _, existItem := range existList {
  391. if existItem.FromTag == "A" {
  392. existItemA = existItem
  393. } else if existItem.FromTag == "B" {
  394. existItemB = existItem
  395. }
  396. }
  397. // 刷新数据
  398. err = refreshAllCalculateLjztbpj(to, edbInfo, existItemA, existItemB)
  399. return
  400. }
  401. // refreshAllCalculateLjztbpj 刷新所有 累计值同比拼接 数据
  402. func refreshAllCalculateLjztbpj(to orm.TxOrmer, edbInfo *EdbInfo, existItemA, existItemB *EdbInfoCalculateMapping) (err error) {
  403. //查询当前指标现有的数据
  404. var condition string
  405. var pars []interface{}
  406. condition += " AND edb_info_id=? "
  407. pars = append(pars, edbInfo.EdbInfoId)
  408. //根据指标id获取全部的数据
  409. var dataList []*EdbDataCalculateLjztbpj
  410. sql := ` SELECT * FROM edb_data_calculate_ljztbpj WHERE edb_info_id=? ORDER BY data_time DESC `
  411. _, err = to.Raw(sql, edbInfo.EdbInfoId).QueryRows(&dataList)
  412. if err != nil {
  413. return err
  414. }
  415. //待拼接指标map
  416. pjDataMap := make(map[string]float64) //需要入库的数据
  417. nowEdbDataMap := make(map[string]float64) //当前指标的数据(已经在库里了,不需要重新)
  418. //拼接指标的日期切片数据
  419. pjEdbDataTimeList := make([]string, 0)
  420. dataMap := make(map[string]*EdbDataCalculateLjztbpj)
  421. for _, v := range dataList {
  422. pjEdbDataTimeList = append(pjEdbDataTimeList, v.DataTime)
  423. dataMap[v.DataTime] = v
  424. nowEdbDataMap[v.DataTime] = v.Value
  425. }
  426. // 原数据开始计算日期
  427. startCalculationDate, _ := time.ParseInLocation(utils.FormatDate, edbInfo.CalculateFormula, time.Local)
  428. //待拼接指标
  429. {
  430. //第一个指标的数据列表
  431. firstDataList, tmpErr := GetEdbDataListAllByTo(to, existItemA.FromSource, existItemA.FromSubSource, FindEdbDataListAllCond{
  432. EdbInfoId: existItemA.FromEdbInfoId,
  433. EndDataTime: startCalculationDate.Format(utils.FormatDate),
  434. EndDataTimeCond: "<=",
  435. }, 0)
  436. if tmpErr != nil {
  437. return tmpErr
  438. }
  439. for _, v := range firstDataList {
  440. //时间戳
  441. if edbData, ok := dataMap[v.DataTime]; ok {
  442. if edbData.Value != v.Value {
  443. //更新指标数据
  444. edbData.Value = v.Value
  445. _, _ = to.Update(edbData, "Value")
  446. }
  447. } else {
  448. pjDataMap[v.DataTime] = v.Value
  449. pjEdbDataTimeList = append(pjEdbDataTimeList, v.DataTime)
  450. }
  451. //将新的数据存入已入库指标map里面,以便后续计算
  452. nowEdbDataMap[v.DataTime] = v.Value
  453. }
  454. }
  455. //同比值指标map
  456. tbzEdbDataMap := make(map[string]float64)
  457. //同比值日期切片列表
  458. tbzEdbDataTimeList := make([]string, 0)
  459. //同比值指标
  460. {
  461. //第二个指标的数据列表
  462. secondDataList, tmpErr := GetEdbDataListAllByTo(to, existItemB.FromSource, existItemB.FromSubSource, FindEdbDataListAllCond{
  463. EdbInfoId: existItemB.FromEdbInfoId,
  464. StartDataTime: startCalculationDate.Format(utils.FormatDate),
  465. StartDataTimeCond: ">",
  466. }, 0)
  467. if tmpErr != nil {
  468. return tmpErr
  469. }
  470. for _, v := range secondDataList {
  471. tbzEdbDataMap[v.DataTime] = v.Value
  472. tbzEdbDataTimeList = append(tbzEdbDataTimeList, v.DataTime)
  473. }
  474. }
  475. sort.Strings(tbzEdbDataTimeList)
  476. // 遍历现有的数据,判断拼接指标中是否存在该日期数据,如果拼接指标无此数据,那么需要删除该日期数据(日期的判断:需要在开始计算日期之后)
  477. removeDateList := make([]string, 0)
  478. for nowEdbDate := range nowEdbDataMap {
  479. nowEdbDateTime, _ := time.ParseInLocation(utils.FormatDate, nowEdbDate, time.Local)
  480. //校验日期 需要 大于 拼接前日期
  481. if startCalculationDate.Before(nowEdbDateTime) {
  482. if _, ok := tbzEdbDataMap[nowEdbDate]; !ok {
  483. // 同比指标中,不存在该日期数据,那么需要移除 现有数据 中该日期的数据
  484. removeDateList = append(removeDateList, nowEdbDate)
  485. }
  486. }
  487. }
  488. //待修改的指标数据map(index:日期,value:值)
  489. updateEdbDataMap := make(map[string]float64)
  490. for _, v := range tbzEdbDataTimeList {
  491. tbzDataTime, _ := time.ParseInLocation(utils.FormatDate, v, time.Local)
  492. //获取拼接指标上一年同一天的数据
  493. var pjDataTime time.Time
  494. if tbzDataTime.Month() == 2 {
  495. pjDataTime = tbzDataTime.AddDate(0, -11, 0)
  496. pjDataTime = time.Date(pjDataTime.Year(), pjDataTime.Month(), 1, 0, 0, 0, 0, time.Now().Location()).AddDate(0, 0, -1)
  497. } else {
  498. pjDataTime = tbzDataTime.AddDate(-1, 0, 0)
  499. }
  500. //校验现有数据中,是否存在该日期的数据,如果存在的话,那么就要去校验 最新计算数据 与 现有数据 是否一致
  501. if nowEdbDataValue, isHas := nowEdbDataMap[v]; isHas {
  502. //获取去年今日的数据,获取到后,然后是去修改该日期的数据
  503. if lastYearEdbDataValue, ok := nowEdbDataMap[pjDataTime.Format(utils.FormatDate)]; ok {
  504. if v == "2021-08-31" {
  505. fmt.Println("进来了")
  506. }
  507. tbzDataValue := tbzEdbDataMap[v] //同比值
  508. currValue := lastYearEdbDataValue * (1 + tbzDataValue/100)
  509. currValue, _ = decimal.NewFromFloat(currValue).Truncate(4).Float64() //保留4位小数
  510. //如果计算出来的值与库里面的值不匹配,那么就去修改该值
  511. if nowEdbDataValue != currValue {
  512. //将计算后的数据存入待拼接指标map里面,以便后续计算
  513. updateEdbDataMap[v] = currValue
  514. }
  515. }
  516. } else {
  517. //因为 现有数据中 不存在该日期数据,那么需要做新增数据处理
  518. //如果去年今日存在该数据,那么就去计算当前的数据
  519. if pjDataValue, ok := nowEdbDataMap[pjDataTime.Format(utils.FormatDate)]; ok {
  520. tbzDataValue := tbzEdbDataMap[v] //同比值
  521. currValue := pjDataValue * (1 + tbzDataValue/100)
  522. currValue, _ = decimal.NewFromFloat(currValue).Truncate(4).Float64()
  523. //将计算后的数据存入已入库指标map里面,以便后续计算
  524. nowEdbDataMap[v] = currValue
  525. //将计算后的数据存入待拼接指标map里面,以便后续入库
  526. pjDataMap[v] = currValue
  527. pjEdbDataTimeList = append(pjEdbDataTimeList, v)
  528. }
  529. }
  530. }
  531. //新增的数据入库
  532. {
  533. addDataList := make([]*EdbDataCalculateLjztbpj, 0)
  534. //日期排序下
  535. sort.Strings(pjEdbDataTimeList)
  536. //这么做的目的是为了让数据插入的时候,可以正序插入(业务上没啥卵用,就是为了让我看数据的时候舒服点,手动狗头-_-|)
  537. for _, dataTime := range pjEdbDataTimeList {
  538. if dataValue, ok := pjDataMap[dataTime]; ok {
  539. //时间戳
  540. currentDate, _ := time.ParseInLocation(utils.FormatDate, dataTime, time.Local)
  541. timestamp := currentDate.UnixNano() / 1e6
  542. edbDataLjztbpj := &EdbDataCalculateLjztbpj{
  543. EdbInfoId: edbInfo.EdbInfoId,
  544. EdbCode: edbInfo.EdbCode,
  545. DataTime: dataTime,
  546. Value: dataValue,
  547. Status: 1,
  548. CreateTime: time.Now(),
  549. ModifyTime: time.Now(),
  550. DataTimestamp: timestamp,
  551. }
  552. addDataList = append(addDataList, edbDataLjztbpj)
  553. }
  554. }
  555. //数据入库
  556. if len(addDataList) > 0 {
  557. _, tmpErr := to.InsertMulti(len(addDataList), addDataList)
  558. if tmpErr != nil {
  559. err = tmpErr
  560. return
  561. }
  562. }
  563. }
  564. //删除已经不存在的累计同比拼接指标数据(由于同比值当日的数据删除了)
  565. {
  566. if len(removeDateList) > 0 {
  567. removeDateStr := strings.Join(removeDateList, `","`)
  568. removeDateStr = `"` + removeDateStr + `"`
  569. //如果拼接指标变更了,那么需要删除所有的指标数据
  570. tableName := GetEdbDataTableName(edbInfo.Source, edbInfo.SubSource)
  571. sql := fmt.Sprintf(` DELETE FROM %s WHERE edb_info_id = ? and data_time in (%s) `, tableName, removeDateStr)
  572. _, err = to.Raw(sql, edbInfo.EdbInfoId).Exec()
  573. if err != nil {
  574. err = errors.New("删除不存在的累计值同比拼接指标数据失败,Err:" + err.Error())
  575. return
  576. }
  577. }
  578. }
  579. //修改现有的数据中对应的值
  580. {
  581. tableName := GetEdbDataTableName(edbInfo.Source, edbInfo.SubSource)
  582. for edbDate, edbDataValue := range updateEdbDataMap {
  583. sql := fmt.Sprintf(` UPDATE %s set value = ?,modify_time=now() WHERE edb_info_id = ? and data_time = ? `, tableName)
  584. _, err = to.Raw(sql, edbDataValue, edbInfo.EdbInfoId, edbDate).Exec()
  585. if err != nil {
  586. err = errors.New("更新现有的累计值同比拼接指标数据失败,Err:" + err.Error())
  587. return
  588. }
  589. }
  590. }
  591. return
  592. }