predict_edb_data_calculate_ljz.go 18 KB

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