predict_edb_data_calculate_ljz.go 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633
  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. "reflect"
  9. "strconv"
  10. "strings"
  11. "time"
  12. )
  13. // PredictLjz 预测累计值
  14. type PredictLjz struct {
  15. }
  16. // Add 添加
  17. func (obj PredictLjz) Add(params BatchSaveCalculateBatchParams) (edbInfo *EdbInfo, latestDateStr string, latestValue float64, err error, errMsg string) {
  18. req := params.Req
  19. fromEdbInfo := params.FromEdbInfo
  20. edbCode := params.EdbCode
  21. frequencyInt := utils.CheckFrequency(fromEdbInfo.Frequency, req.Frequency)
  22. if frequencyInt < 0 {
  23. errMsg = "频度异常,不允许低频转为高频"
  24. err = errors.New(errMsg)
  25. return
  26. }
  27. if frequencyInt == 0 {
  28. errMsg = "频度异常,不允许同频计算预测累计值"
  29. err = errors.New(errMsg)
  30. return
  31. }
  32. o := orm.NewOrm()
  33. to, err := o.Begin()
  34. if err != nil {
  35. return
  36. }
  37. defer func() {
  38. if err != nil {
  39. _ = to.Rollback()
  40. fmt.Println(reflect.TypeOf(obj).Name(), ";Add,Err:"+err.Error())
  41. } else {
  42. _ = to.Commit()
  43. }
  44. }()
  45. edbInfo = &EdbInfo{
  46. //EdbInfoId: 0,
  47. SourceName: obj.GetSourceName(),
  48. Source: obj.GetSource(),
  49. EdbCode: edbCode,
  50. EdbName: req.EdbName,
  51. EdbNameSource: req.EdbName,
  52. Frequency: req.Frequency,
  53. Unit: req.Unit,
  54. StartDate: "",
  55. EndDate: "",
  56. ClassifyId: req.ClassifyId,
  57. SysUserId: params.SysUserId,
  58. SysUserRealName: params.SysUserRealName,
  59. UniqueCode: params.UniqueCode,
  60. CreateTime: time.Now(),
  61. ModifyTime: time.Now(),
  62. MinValue: 0,
  63. MaxValue: 0,
  64. CalculateFormula: req.Formula,
  65. EdbType: 2,
  66. Sort: GetAddEdbMaxSortByClassifyId(req.ClassifyId, utils.PREDICT_EDB_INFO_TYPE),
  67. MoveType: 0,
  68. MoveFrequency: "",
  69. NoUpdate: 0,
  70. ServerUrl: "",
  71. EdbInfoType: 1,
  72. EdbNameEn: req.EdbName,
  73. UnitEn: req.Unit,
  74. LatestDate: "",
  75. LatestValue: 0,
  76. ChartImage: "",
  77. Calendar: "",
  78. }
  79. newEdbInfoId, tmpErr := to.Insert(edbInfo)
  80. if tmpErr != nil {
  81. err = tmpErr
  82. return
  83. }
  84. edbInfo.EdbInfoId = int(newEdbInfoId)
  85. //关联关系
  86. {
  87. calculateMappingItem := new(EdbInfoCalculateMapping)
  88. calculateMappingItem.CreateTime = time.Now()
  89. calculateMappingItem.ModifyTime = time.Now()
  90. calculateMappingItem.Sort = 1
  91. calculateMappingItem.EdbCode = edbCode
  92. calculateMappingItem.EdbInfoId = edbInfo.EdbInfoId
  93. calculateMappingItem.FromEdbInfoId = fromEdbInfo.EdbInfoId
  94. calculateMappingItem.FromEdbCode = fromEdbInfo.EdbCode
  95. calculateMappingItem.FromEdbName = fromEdbInfo.EdbName
  96. calculateMappingItem.FromSource = fromEdbInfo.Source
  97. calculateMappingItem.FromSourceName = fromEdbInfo.SourceName
  98. calculateMappingItem.FromTag = ""
  99. calculateMappingItem.Source = edbInfo.Source
  100. calculateMappingItem.SourceName = edbInfo.SourceName
  101. _, err = to.Insert(calculateMappingItem)
  102. if err != nil {
  103. return
  104. }
  105. }
  106. //计算数据
  107. latestDateStr, latestValue, err = obj.refresh(to, edbInfo.EdbInfoId, edbInfo.Source, edbInfo.SubSource, edbInfo, fromEdbInfo, edbInfo.EdbCode, "")
  108. return
  109. }
  110. // Edit 编辑
  111. func (obj PredictLjz) Edit(params BatchSaveCalculateBatchParams) (latestDateStr string, latestValue float64, err error, errMsg string) {
  112. req := params.Req
  113. edbInfo := params.EdbInfo
  114. fromEdbInfo := params.FromEdbInfo
  115. latestDateStr = edbInfo.LatestDate
  116. latestValue = edbInfo.LatestValue
  117. frequencyInt := utils.CheckFrequency(fromEdbInfo.Frequency, req.Frequency)
  118. if frequencyInt < 0 {
  119. errMsg = "频度异常,不允许低频转为高频"
  120. err = errors.New(errMsg)
  121. return
  122. }
  123. if frequencyInt == 0 {
  124. errMsg = "频度异常,不允许同频计算预测累计值"
  125. err = errors.New(errMsg)
  126. return
  127. }
  128. latestDateStr = edbInfo.LatestDate
  129. latestValue = edbInfo.LatestValue
  130. o := orm.NewOrm()
  131. to, err := o.Begin()
  132. if err != nil {
  133. return
  134. }
  135. defer func() {
  136. if err != nil {
  137. _ = to.Rollback()
  138. fmt.Println(reflect.TypeOf(obj).Name(), ";Edit,Err:"+err.Error())
  139. } else {
  140. _ = to.Commit()
  141. }
  142. }()
  143. tableName := GetEdbDataTableName(edbInfo.Source, edbInfo.SubSource)
  144. var isRecalculate bool
  145. if edbInfo.Frequency != req.Frequency {
  146. isRecalculate = true
  147. }
  148. //修改指标信息
  149. switch params.Lang {
  150. case utils.EnLangVersion:
  151. edbInfo.EdbNameEn = req.EdbName
  152. edbInfo.UnitEn = req.Unit
  153. default:
  154. edbInfo.EdbName = req.EdbName
  155. edbInfo.Unit = req.Unit
  156. edbInfo.EdbNameSource = req.EdbName
  157. }
  158. edbInfo.Frequency = req.Frequency
  159. edbInfo.ClassifyId = req.ClassifyId
  160. edbInfo.ModifyTime = time.Now()
  161. _, err = to.Update(edbInfo, "EdbName", "EdbNameSource", "Frequency", "Unit", "ClassifyId", "ModifyTime", "EdbNameEn", "UnitEn")
  162. if err != nil {
  163. return
  164. }
  165. var existCondition string
  166. var existPars []interface{}
  167. existCondition += " AND edb_info_id=? AND from_edb_info_id=? "
  168. existPars = append(existPars, edbInfo.EdbInfoId, req.FromEdbInfoId)
  169. //判断计算指标是否被更换
  170. count, err := GetEdbInfoCalculateCountByCondition(existCondition, existPars)
  171. if err != nil {
  172. err = errors.New("判断指标是否改变失败,Err:" + err.Error())
  173. return
  174. }
  175. if count > 0 { // 指标未被替换,无需删除关联数据
  176. // 频度被换了,需要重新计算
  177. if isRecalculate {
  178. latestDateStr, latestValue, err = obj.refresh(to, edbInfo.EdbInfoId, edbInfo.Source, edbInfo.SubSource, edbInfo, fromEdbInfo, edbInfo.EdbCode, "")
  179. }
  180. return
  181. }
  182. //删除,计算指标关联的,基础指标的关联关系
  183. sql := ` DELETE FROM edb_info_calculate_mapping WHERE edb_info_id = ? `
  184. _, err = to.Raw(sql, edbInfo.EdbInfoId).Exec()
  185. if err != nil {
  186. return
  187. }
  188. //清空原有数据
  189. sql = ` DELETE FROM ` + tableName + ` WHERE edb_info_id = ? `
  190. _, err = to.Raw(sql, edbInfo.EdbInfoId).Exec()
  191. if err != nil {
  192. return
  193. }
  194. //关联关系
  195. {
  196. calculateMappingItem := &EdbInfoCalculateMapping{
  197. EdbInfoCalculateMappingId: 0,
  198. EdbInfoId: edbInfo.EdbInfoId,
  199. Source: obj.GetSource(),
  200. SourceName: obj.GetSourceName(),
  201. EdbCode: edbInfo.EdbCode,
  202. FromEdbInfoId: fromEdbInfo.EdbInfoId,
  203. FromEdbCode: fromEdbInfo.EdbCode,
  204. FromEdbName: fromEdbInfo.EdbName,
  205. FromSource: fromEdbInfo.Source,
  206. FromSourceName: fromEdbInfo.SourceName,
  207. FromTag: "",
  208. Sort: 1,
  209. CreateTime: time.Now(),
  210. ModifyTime: time.Now(),
  211. }
  212. _, err = to.Insert(calculateMappingItem)
  213. if err != nil {
  214. return
  215. }
  216. }
  217. //计算数据
  218. latestDateStr, latestValue, err = obj.refresh(to, edbInfo.EdbInfoId, edbInfo.Source, edbInfo.SubSource, edbInfo, fromEdbInfo, edbInfo.EdbCode, "")
  219. return
  220. }
  221. // Refresh 刷新
  222. func (obj PredictLjz) Refresh(params RefreshParams) (latestDateStr string, latestValue float64, err error, errMsg string) {
  223. calculateMapping, err := GetEdbInfoCalculateMappingDetail(params.EdbInfo.EdbInfoId)
  224. if err != nil {
  225. errMsg = "GetEdbInfoCalculatePredictLjzDetail Err:" + err.Error()
  226. return
  227. }
  228. fromEdbInfo, err := GetEdbInfoById(calculateMapping.FromEdbInfoId)
  229. if err != nil {
  230. errMsg = "GetEdbInfoById Err:" + err.Error()
  231. return
  232. }
  233. o := orm.NewOrm()
  234. to, err := o.Begin()
  235. if err != nil {
  236. return
  237. }
  238. defer func() {
  239. if err != nil {
  240. _ = to.Rollback()
  241. fmt.Println(reflect.TypeOf(obj).Name(), ";Refresh,Err:"+err.Error())
  242. } else {
  243. _ = to.Commit()
  244. }
  245. }()
  246. // 计算数据
  247. latestDateStr, latestValue, err = obj.refresh(to, params.EdbInfo.EdbInfoId, params.EdbInfo.Source, params.EdbInfo.SubSource, params.EdbInfo, fromEdbInfo, params.EdbInfo.EdbCode, params.StartDate)
  248. return
  249. }
  250. // GetSource 获取来源编码id
  251. func (obj PredictLjz) GetSource() int {
  252. return utils.DATA_SOURCE_PREDICT_CALCULATE_LJZ
  253. }
  254. // GetSourceName 获取来源名称
  255. func (obj PredictLjz) GetSourceName() string {
  256. return utils.DATA_SOURCE_NAME_PREDICT_CALCULATE_LJZ
  257. }
  258. func (obj PredictLjz) refresh(to orm.TxOrmer, edbInfoId, source, subSource int, edbInfo, fromEdbInfo *EdbInfo, edbCode, startDate string) (latestDateStr string, latestValue float64, err error) {
  259. dataTableName := GetEdbDataTableName(source, subSource)
  260. edbInfoIdStr := strconv.Itoa(edbInfoId)
  261. var isWeekData bool // 是否周度数据,如果是周度数据的话,是需要变频的,最后结果还需要除以7
  262. // 周度数据需要先变成日度的
  263. if fromEdbInfo.Frequency == `周度` {
  264. isWeekData = true
  265. }
  266. // 获取来源指标的数据
  267. dataList, err := GetPredictEdbDataListAll(fromEdbInfo, 1)
  268. if err != nil {
  269. return
  270. }
  271. fromEdbDataMap := make(map[string]float64)
  272. if isWeekData {
  273. dataList, err = HandleDataByLinearRegression(dataList, fromEdbDataMap)
  274. if err != nil {
  275. return
  276. }
  277. }
  278. //日度转周度:日期选周五,计算上周六到本周五的日度值的加总,最新日期为最新值对应的周五。
  279. //日度转月度:日期选每个月最后一天,计算当月所有日度值的加总,最新日期为最新值对应当月最后一天。
  280. //日度转季度、年度:方法类似转月度。
  281. //周度转月度/季度/年度:将周度值转成日度,空值用插值法插值,计算当月/当季/当年所有值的加总,然后除以7。
  282. //月度转季度/年度: 当季/当年月度值相加。
  283. dateList := make([]time.Time, 0)
  284. valueMap := make(map[time.Time]float64)
  285. switch edbInfo.Frequency {
  286. case "年度":
  287. yearMap := make(map[int]float64)
  288. yearList := make([]int, 0)
  289. for _, item := range dataList {
  290. itemDate, tmpErr := time.ParseInLocation(utils.FormatDate, item.DataTime, time.Local)
  291. if tmpErr != nil {
  292. err = tmpErr
  293. return
  294. }
  295. year := itemDate.Year()
  296. yearVal, ok := yearMap[year]
  297. if ok {
  298. yearMap[year] = item.Value + yearVal
  299. } else {
  300. yearList = append(yearList, year)
  301. yearMap[year] = item.Value
  302. }
  303. }
  304. for _, v := range yearList {
  305. currTime := time.Date(v, 12, 31, 0, 0, 0, 0, time.Local)
  306. dateList = append(dateList, currTime)
  307. valueMap[currTime] = yearMap[v]
  308. }
  309. case "半年度":
  310. yearMonthMap := make(map[string]float64)
  311. yearMonthList := make([]string, 0)
  312. for _, item := range dataList {
  313. itemDate, tmpErr := time.ParseInLocation(utils.FormatDate, item.DataTime, time.Local)
  314. if tmpErr != nil {
  315. err = tmpErr
  316. return
  317. }
  318. year := itemDate.Year()
  319. var tmpK string
  320. if itemDate.Month() <= 6 {
  321. tmpK = fmt.Sprint(year, "06")
  322. } else {
  323. tmpK = fmt.Sprint(year, "12")
  324. }
  325. yearVal, ok := yearMonthMap[tmpK]
  326. if ok {
  327. yearMonthMap[tmpK] = item.Value + yearVal
  328. } else {
  329. yearMonthList = append(yearMonthList, tmpK)
  330. yearMonthMap[tmpK] = item.Value
  331. }
  332. }
  333. for _, v := range yearMonthList {
  334. currTime, tmpErr := time.ParseInLocation(utils.FormatYearMonthUnSpace, v, time.Local)
  335. if tmpErr != nil {
  336. err = tmpErr
  337. return
  338. }
  339. currTime = currTime.AddDate(0, 1, -1)
  340. dateList = append(dateList, currTime)
  341. valueMap[currTime] = yearMonthMap[v]
  342. }
  343. case "季度":
  344. yearMonthMap := make(map[string]float64)
  345. yearMonthList := make([]string, 0)
  346. for _, item := range dataList {
  347. itemDate, tmpErr := time.ParseInLocation(utils.FormatDate, item.DataTime, time.Local)
  348. if tmpErr != nil {
  349. err = tmpErr
  350. return
  351. }
  352. year := itemDate.Year()
  353. var tmpK string
  354. if itemDate.Month() <= 3 {
  355. tmpK = fmt.Sprint(year, "03")
  356. } else if itemDate.Month() <= 6 {
  357. tmpK = fmt.Sprint(year, "06")
  358. } else if itemDate.Month() <= 9 {
  359. tmpK = fmt.Sprint(year, "09")
  360. } else {
  361. tmpK = fmt.Sprint(year, "12")
  362. }
  363. yearVal, ok := yearMonthMap[tmpK]
  364. if ok {
  365. yearMonthMap[tmpK] = item.Value + yearVal
  366. } else {
  367. yearMonthList = append(yearMonthList, tmpK)
  368. yearMonthMap[tmpK] = item.Value
  369. }
  370. }
  371. for _, v := range yearMonthList {
  372. currTime, tmpErr := time.ParseInLocation(utils.FormatYearMonthUnSpace, v, time.Local)
  373. if tmpErr != nil {
  374. err = tmpErr
  375. return
  376. }
  377. currTime = currTime.AddDate(0, 1, -1)
  378. dateList = append(dateList, currTime)
  379. valueMap[currTime] = yearMonthMap[v]
  380. }
  381. case "月度":
  382. yearMonthMap := make(map[string]float64)
  383. yearMonthList := make([]string, 0)
  384. for _, item := range dataList {
  385. itemDate, tmpErr := time.ParseInLocation(utils.FormatDate, item.DataTime, time.Local)
  386. if tmpErr != nil {
  387. err = tmpErr
  388. return
  389. }
  390. year := itemDate.Year()
  391. var tmpK string
  392. tmpK = fmt.Sprint(year*100 + int(itemDate.Month()))
  393. yearVal, ok := yearMonthMap[tmpK]
  394. if ok {
  395. yearMonthMap[tmpK] = item.Value + yearVal
  396. } else {
  397. yearMonthList = append(yearMonthList, tmpK)
  398. yearMonthMap[tmpK] = item.Value
  399. }
  400. }
  401. for _, v := range yearMonthList {
  402. currTime, tmpErr := time.ParseInLocation(utils.FormatYearMonthUnSpace, v, time.Local)
  403. if tmpErr != nil {
  404. err = tmpErr
  405. return
  406. }
  407. currTime = currTime.AddDate(0, 1, -1)
  408. dateList = append(dateList, currTime)
  409. valueMap[currTime] = yearMonthMap[v]
  410. }
  411. case "旬度":
  412. tmpDateDataMap := make(map[time.Time]float64)
  413. tmpDateList := make([]time.Time, 0)
  414. for _, item := range dataList {
  415. itemDate, tmpErr := time.ParseInLocation(utils.FormatDate, item.DataTime, time.Local)
  416. if tmpErr != nil {
  417. err = tmpErr
  418. return
  419. }
  420. dayInt := itemDate.Year()*100 + int(itemDate.Month())
  421. var currTime time.Time
  422. if itemDate.Month() <= 10 {
  423. tmpK := fmt.Sprint(dayInt*100, "10")
  424. currTime, err = time.ParseInLocation(utils.FormatDateUnSpace, tmpK, time.Local)
  425. if err != nil {
  426. return
  427. }
  428. } else if itemDate.Month() <= 20 {
  429. tmpK := fmt.Sprint(dayInt*100, "20")
  430. currTime, err = time.ParseInLocation(utils.FormatDateUnSpace, tmpK, time.Local)
  431. if err != nil {
  432. return
  433. }
  434. } else {
  435. currTime, err = time.ParseInLocation(utils.FormatYearMonthUnSpace, fmt.Sprint(dayInt), time.Local)
  436. if err != nil {
  437. return
  438. }
  439. currTime = currTime.AddDate(0, 1, -1)
  440. }
  441. yearVal, ok := tmpDateDataMap[currTime]
  442. if ok {
  443. tmpDateDataMap[currTime] = item.Value + yearVal
  444. } else {
  445. tmpDateList = append(tmpDateList, currTime)
  446. tmpDateDataMap[currTime] = item.Value
  447. }
  448. }
  449. for _, currTime := range tmpDateList {
  450. dateList = append(dateList, currTime)
  451. valueMap[currTime] = tmpDateDataMap[currTime]
  452. }
  453. case "周度":
  454. tmpDateDataMap := make(map[time.Time]float64)
  455. tmpDateList := make([]time.Time, 0)
  456. for _, item := range dataList {
  457. itemDate, tmpErr := time.ParseInLocation(utils.FormatDate, item.DataTime, time.Local)
  458. if tmpErr != nil {
  459. err = tmpErr
  460. return
  461. }
  462. var currTime time.Time
  463. // 周六周日,这是下一个周五的数据
  464. if itemDate.Weekday() == 0 {
  465. currTime = itemDate.AddDate(0, 0, 5)
  466. } else if itemDate.Weekday() == 6 {
  467. currTime = itemDate.AddDate(0, 0, 6)
  468. } else {
  469. currTime = itemDate.AddDate(0, 0, 5-int(itemDate.Weekday()))
  470. }
  471. yearVal, ok := tmpDateDataMap[currTime]
  472. if ok {
  473. tmpDateDataMap[currTime] = item.Value + yearVal
  474. } else {
  475. tmpDateList = append(tmpDateList, currTime)
  476. tmpDateDataMap[currTime] = item.Value
  477. }
  478. }
  479. for _, currTime := range tmpDateList {
  480. dateList = append(dateList, currTime)
  481. valueMap[currTime] = tmpDateDataMap[currTime]
  482. }
  483. }
  484. //获取指标所有数据
  485. existDataList := make([]*EdbData, 0)
  486. sql := `SELECT * FROM %s WHERE edb_info_id=? `
  487. sql = fmt.Sprintf(sql, dataTableName)
  488. _, err = to.Raw(sql, edbInfoId).QueryRows(&existDataList)
  489. if err != nil {
  490. return
  491. }
  492. existDataMap := make(map[string]string)
  493. removeDataTimeMap := make(map[string]int) //需要移除的日期数据
  494. for _, v := range existDataList {
  495. existDataMap[v.DataTime] = v.Value
  496. removeDataTimeMap[v.DataTime] = 1
  497. }
  498. needAddDateMap := make(map[time.Time]int)
  499. addSql := ` INSERT INTO ` + dataTableName + `(edb_info_id,edb_code,data_time,value,create_time,modify_time,data_timestamp) values `
  500. var isAdd bool
  501. for _, currTime := range dateList {
  502. currDateStr := currTime.Format(utils.FormatDate)
  503. existVal, ok := existDataMap[currDateStr]
  504. tmpVal, ok2 := valueMap[currTime]
  505. if !ok2 {
  506. err = errors.New("数据异常,date:" + currDateStr)
  507. return
  508. }
  509. var saveValue string
  510. if isWeekData { //周度指标转的话,最后结果要除以7
  511. saveValue = decimal.NewFromFloat(tmpVal).Div(decimal.NewFromInt(7)).Round(4).String()
  512. } else {
  513. saveValue = decimal.NewFromFloat(tmpVal).Round(4).String()
  514. }
  515. // 如果库中已经存在该数据的话,那么就进行值的变更操作
  516. if ok {
  517. //校验待删除日期数据里面是否存在该元素,如果存在的话,那么移除该日期
  518. delete(removeDataTimeMap, currDateStr)
  519. if existVal != saveValue {
  520. sql := ` UPDATE %s SET value=?,modify_time=NOW() WHERE edb_info_id=? AND data_time=? `
  521. sql = fmt.Sprintf(sql, dataTableName)
  522. _, err = to.Raw(sql, saveValue, edbInfoId, currDateStr).Exec()
  523. if err != nil {
  524. return
  525. }
  526. }
  527. continue
  528. }
  529. // 库中不存在该日期的数据
  530. timestamp := currTime.UnixNano() / 1e6
  531. timeStr := fmt.Sprintf("%d", timestamp)
  532. if _, existOk := needAddDateMap[currTime]; !existOk {
  533. addSql += GetAddSql(edbInfoIdStr, edbCode, currDateStr, timeStr, saveValue)
  534. isAdd = true
  535. }
  536. needAddDateMap[currTime] = 1
  537. }
  538. //删除已经不存在的指标数据(由于该指标当日的数据删除了)
  539. {
  540. removeDateList := make([]string, 0)
  541. for dateTime := range removeDataTimeMap {
  542. removeDateList = append(removeDateList, dateTime)
  543. }
  544. removeNum := len(removeDateList)
  545. if removeNum > 0 {
  546. sql := fmt.Sprintf(` DELETE FROM %s WHERE edb_info_id = ? and data_time in (`+utils.GetOrmInReplace(removeNum)+`) `, dataTableName)
  547. _, err = to.Raw(sql, edbInfo.EdbInfoId, removeDateList).Exec()
  548. if err != nil {
  549. fmt.Println(reflect.TypeOf(obj).Name(), " add data ;delete Err", err.Error())
  550. err = fmt.Errorf("删除不存在的指标数据失败,Err:" + err.Error())
  551. return
  552. }
  553. }
  554. }
  555. if isAdd {
  556. addSql = strings.TrimRight(addSql, ",")
  557. _, err = to.Raw(addSql).Exec()
  558. if err != nil {
  559. fmt.Println(reflect.TypeOf(obj).Name(), " add data Err", err.Error())
  560. return
  561. }
  562. }
  563. // 确定实际数据的最终值
  564. {
  565. var finalLast EdbInfoSearchData
  566. sql = fmt.Sprintf(` SELECT data_time , value FROM %s WHERE edb_info_id=? and data_time<=? ORDER BY data_time DESC `, dataTableName)
  567. err = to.Raw(sql, edbInfoId, fromEdbInfo.LatestDate).QueryRow(&finalLast)
  568. if err != nil && err.Error() != utils.ErrNoRow() {
  569. return
  570. } else {
  571. latestDateStr = finalLast.DataTime
  572. latestValue = finalLast.Value
  573. }
  574. if err != nil {
  575. err = nil
  576. } else {
  577. latestDateStr = finalLast.DataTime
  578. latestValue = finalLast.Value
  579. }
  580. }
  581. return
  582. }