edb_data_calculate_ljztbpj.go 20 KB

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