predict_edb_data_calculate_ljztbpj.go 20 KB

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