edb_data_calculate_ljztbpj.go 21 KB

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