base_predict_from_calculate.go 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583
  1. package models
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "eta/eta_index_lib/utils"
  6. "fmt"
  7. "github.com/beego/beego/v2/client/orm"
  8. "github.com/dengsgo/math-engine/engine"
  9. "github.com/shopspring/decimal"
  10. "strconv"
  11. "strings"
  12. "time"
  13. )
  14. // PredictEdbInfoCalculateSaveReq 预测计算(运算)指标请求参数
  15. type PredictEdbInfoCalculateSaveReq struct {
  16. AdminId int `description:"添加人id"`
  17. AdminName string `description:"添加人名称"`
  18. EdbName string `description:"指标名称"`
  19. Frequency string `description:"频率"`
  20. Unit string `description:"单位"`
  21. ClassifyId int `description:"分类id"`
  22. CalculateFormula string `description:"计算公式"`
  23. EdbInfoIdArr []struct {
  24. EdbInfoId int `description:"指标id"`
  25. FromTag string `description:"指标对应标签"`
  26. }
  27. }
  28. // PredictCalculateItems 预测计算(运算)指标信息
  29. type PredictCalculateItems struct {
  30. EdbInfoId int
  31. DataMap map[string]float64
  32. }
  33. // EditPredictCalculate 新增预测计算(运算)指标
  34. func EditPredictCalculate(edbInfo *EdbInfo, calculateMappingList []*EdbInfoCalculateMapping) (err error) {
  35. o := orm.NewOrm()
  36. to, err := o.Begin()
  37. if err != nil {
  38. return
  39. }
  40. defer func() {
  41. if err != nil {
  42. tmpErr := to.Rollback()
  43. errMsg := fmt.Sprintf("AddPredictCalculate Err:%s" + err.Error())
  44. if tmpErr != nil {
  45. errMsg += `;rollback失败:` + tmpErr.Error()
  46. }
  47. utils.FileLog.Info(errMsg)
  48. return
  49. }
  50. tmpErr := to.Commit()
  51. if tmpErr != nil {
  52. errMsg := fmt.Sprintf("AddPredictCalculate commit Err:%s" + tmpErr.Error())
  53. utils.FileLog.Info(errMsg)
  54. }
  55. }()
  56. // 修改指标信息
  57. _, err = to.Update(edbInfo, "EdbName", "Frequency", "Unit", "ClassifyId", "CalculateFormula", "EmptyType", "MaxEmptyType", "Extra", "EdbNameEn", "UnitEn")
  58. if err != nil {
  59. return
  60. }
  61. //删除,计算指标关联的,基础指标的关联关系
  62. sql := ` DELETE FROM edb_info_calculate_mapping WHERE edb_info_id = ? `
  63. _, err = to.Raw(sql, edbInfo.EdbInfoId).Exec()
  64. if err != nil {
  65. err = errors.New("删除计算指标关联关系失败,Err:" + err.Error())
  66. return
  67. }
  68. // 插入新的关系表
  69. if len(calculateMappingList) > 0 {
  70. _, err = to.InsertMulti(len(calculateMappingList), calculateMappingList)
  71. }
  72. //数据表
  73. tableName := GetEdbDataTableName(edbInfo.Source, edbInfo.SubSource)
  74. //清空原有数据
  75. sql = ` DELETE FROM ` + tableName + ` WHERE edb_info_id = ? `
  76. _, err = o.Raw(sql, edbInfo.EdbInfoId).Exec()
  77. if err != nil {
  78. return
  79. }
  80. return
  81. }
  82. // AddPredictCalculateData 新增预测计算(运算)指标的数据
  83. //func AddPredictCalculateDataBak(edbInfoIdList []*EdbInfo, edbInfoId int, edbCode, formulaStr string, edbInfoIdBytes []string) (latestDateStr string, latestValue float64, err error) {
  84. // o := orm.NewOrm()
  85. // defer func() {
  86. // if err != nil {
  87. // utils.FileLog.Info(fmt.Sprintf("Calculate Err:%s" + err.Error()))
  88. // }
  89. // }()
  90. //
  91. // var minLatestDate time.Time
  92. // oldSaveDataMap := make(map[string]map[int]float64)
  93. // saveDataMap := make(map[string]map[int]float64)
  94. // dateList := make([]string, 0)
  95. //
  96. // for edbInfoIndex, v := range edbInfoIdList {
  97. // tmpLatestDate, tmpErr := time.ParseInLocation(utils.FormatDate, v.LatestDate, time.Local)
  98. // if tmpErr != nil {
  99. // err = errors.New(`最近的日期格式化失败;日期:` + v.LatestDate + `;err:` + tmpErr.Error())
  100. // return
  101. // }
  102. // // 如果不存在最小的真实数据日期,那么将当前指标的真实数据日期给赋值;
  103. // // 如果存在最小的真实数据日期,同时当前指标的真实数据日期 早于 最小的真实数据日期, 那么将当前指标的真实数据日期给赋值;
  104. // if minLatestDate.IsZero() || tmpLatestDate.Before(minLatestDate) {
  105. // minLatestDate = tmpLatestDate
  106. // }
  107. //
  108. // dataList, tmpErr := GetPredictEdbDataListAll(v, 1)
  109. // if tmpErr != nil {
  110. // err = tmpErr
  111. // return
  112. // }
  113. // dataMap := make(map[string]float64)
  114. // for _, dv := range dataList {
  115. // // 现有实际数据
  116. // if val, ok := oldSaveDataMap[dv.DataTime]; ok {
  117. // if _, ok := val[v.EdbInfoId]; !ok {
  118. // val[v.EdbInfoId] = dv.Value
  119. // }
  120. // } else {
  121. // temp := make(map[int]float64)
  122. // temp[v.EdbInfoId] = dv.Value
  123. // oldSaveDataMap[dv.DataTime] = temp
  124. // }
  125. //
  126. // // 待处理的数据
  127. // if val, ok := saveDataMap[dv.DataTime]; ok {
  128. // if _, ok := val[v.EdbInfoId]; !ok {
  129. // val[v.EdbInfoId] = dv.Value
  130. // }
  131. // } else {
  132. // temp := make(map[int]float64)
  133. // temp[v.EdbInfoId] = dv.Value
  134. // saveDataMap[dv.DataTime] = temp
  135. // }
  136. //
  137. // // 以第一个指标的日期作为基准日期
  138. // if edbInfoIndex == 0 {
  139. // dateList = append(dateList, dv.DataTime)
  140. // }
  141. // }
  142. // item := new(PredictCalculateItems)
  143. // item.EdbInfoId = v.EdbInfoId
  144. // item.DataMap = dataMap
  145. // }
  146. //
  147. // //数据处理,将日期内不全的数据做补全
  148. // handleDateSaveDataMap(dateList, oldSaveDataMap, saveDataMap, edbInfoIdList)
  149. //
  150. // formulaMap := utils.CheckFormula(formulaStr)
  151. // addSql := ` INSERT INTO edb_data_predict_calculate (edb_info_id,edb_code,data_time,value,create_time,modify_time,data_timestamp) values `
  152. // nowStr := time.Now().Format(utils.FormatDateTime)
  153. // var isAdd bool
  154. //
  155. // if !minLatestDate.IsZero() {
  156. // latestDateStr = minLatestDate.Format(utils.FormatDate)
  157. // }
  158. // for sk, sv := range saveDataMap {
  159. // formulaStr = strings.ToUpper(formulaStr)
  160. // formulaFormStr := ReplaceFormula(edbInfoIdList, sv, formulaMap, formulaStr, edbInfoIdBytes)
  161. // if formulaStr == "" {
  162. // return
  163. // }
  164. // if formulaFormStr != "" {
  165. // expression := formula.NewExpression(formulaFormStr)
  166. // calResult, tmpErr := expression.Evaluate()
  167. // if tmpErr != nil {
  168. // err = errors.New("计算失败:Err:" + tmpErr.Error() + ";formulaStr:" + formulaFormStr)
  169. // fmt.Println(err)
  170. // return
  171. // }
  172. // calVal, tmpErr := calResult.Float64()
  173. // if tmpErr != nil {
  174. // err = errors.New("计算失败:获取计算值失败 Err:" + tmpErr.Error() + ";formulaStr:" + formulaFormStr)
  175. // fmt.Println(err)
  176. // return
  177. // }
  178. //
  179. // //需要存入的数据
  180. // {
  181. // if latestDateStr == sk {
  182. // nanCheck := fmt.Sprintf("%0.f", calVal)
  183. // // 分母为0.0的报错
  184. // if nanCheck == "NaN" || nanCheck == "+Inf" || nanCheck == "-Inf" {
  185. // err = errors.New("计算失败 分母为0:Err: formulaStr:" + formulaFormStr)
  186. // fmt.Println(err)
  187. // return
  188. // }
  189. // latestValue, _ = decimal.NewFromFloat(calVal).Truncate(4).Float64() //保留4位小数
  190. // }
  191. // dataTime, _ := time.ParseInLocation(utils.FormatDate, sk, time.Local)
  192. // timestamp := dataTime.UnixNano() / 1e6
  193. // timeStr := fmt.Sprintf("%d", timestamp)
  194. // addSql += "("
  195. // addSql += strconv.Itoa(edbInfoId) + "," + "'" + edbCode + "'" + "," + "'" + sk + "'" + "," + utils.SubFloatToString(calVal, 4) + "," + "'" + nowStr + "'" +
  196. // "," + "'" + nowStr + "'" + "," + "'" + timeStr + "'"
  197. // addSql += "),"
  198. // isAdd = true
  199. // }
  200. // } else {
  201. // fmt.Println("formulaFormStr is empty")
  202. // }
  203. // }
  204. // if isAdd {
  205. // addSql = strings.TrimRight(addSql, ",")
  206. // _, err = o.Raw(addSql).Exec()
  207. // if err != nil {
  208. // fmt.Println("AddPredictCalculate Err:" + err.Error())
  209. // //errMsg = " tx.Exec Err :" + err.Error()
  210. // return
  211. // }
  212. // }
  213. // return
  214. //}
  215. func AddPredictCalculateData(edbInfoIdList []*EdbInfo, edbInfoTag map[string]int, edbInfo *EdbInfo, edbCode, formulaStr string, edbInfoIdBytes []string) (latestDateStr string, latestValue float64, err error) {
  216. o := orm.NewOrm()
  217. to, err := o.Begin()
  218. if err != nil {
  219. return
  220. }
  221. defer func() {
  222. if err != nil {
  223. fmt.Println("AddPredictCalculateData Calculate,Err:" + err.Error())
  224. _ = to.Rollback()
  225. } else {
  226. _ = to.Commit()
  227. }
  228. }()
  229. latestDateStr, latestValue, err = refreshAllPredictCalculate(to, edbInfoIdList, edbInfoTag, edbInfo.EdbInfoId, edbInfo.Source, edbInfo.SubSource, edbCode, formulaStr, "", edbInfoIdBytes, edbInfo.EmptyType, edbInfo.MaxEmptyType, edbInfo.Extra)
  230. return
  231. }
  232. // RefreshAllPredictCalculate 刷新预测计算指标的全部数据
  233. func RefreshAllPredictCalculate(edbInfoIdList []*EdbInfo, edbInfoTag map[string]int, edbInfoId, source, subSource int, edbCode, formulaStr, startDate string, edbInfoIdBytes []string, emptyType, maxEmptyType int, extra string) (latestDateStr string, latestValue float64, err error) {
  234. o := orm.NewOrm()
  235. to, err := o.Begin()
  236. if err != nil {
  237. return
  238. }
  239. defer func() {
  240. if err != nil {
  241. fmt.Println("RefreshAllCalculate,Err:" + err.Error())
  242. _ = to.Rollback()
  243. } else {
  244. _ = to.Commit()
  245. }
  246. }()
  247. latestDateStr, latestValue, err = refreshAllPredictCalculate(to, edbInfoIdList, edbInfoTag, edbInfoId, source, subSource, edbCode, formulaStr, startDate, edbInfoIdBytes, emptyType, maxEmptyType, extra)
  248. return
  249. }
  250. // refreshAllPredictCalculate 刷新预测计算指标的全部数据
  251. func refreshAllPredictCalculate(to orm.TxOrmer, edbInfoIdList []*EdbInfo, edbInfoTag map[string]int, edbInfoId, source, subSource int, edbCode, formulaStr, startDate string, edbInfoIdBytes []string, emptyType, maxEmptyType int, extra string) (latestDateStr string, latestValue float64, err error) {
  252. fmt.Println("startDate:", startDate)
  253. // 最小的结束日期 , 最晚的数据开始日期
  254. var newLatestDate, minLatestDate, maxStartDate time.Time
  255. dateList := make([]string, 0) // 第一个指标的日期数据
  256. realSaveDataMap := make(map[string]map[int]float64)
  257. saveDataMap := make(map[string]map[int]float64)
  258. dateMap := make(map[string]struct{}) // 最终的日期数据
  259. dateTagConfig := ""
  260. if extra != "" {
  261. var dateConfig CalculateEdbExtra
  262. err = json.Unmarshal([]byte(extra), &dateConfig)
  263. if err != nil {
  264. err = fmt.Errorf("refreshAllCalculate,extra解析失败,Err:%s", err.Error())
  265. return
  266. }
  267. dateTagConfig = dateConfig.DateTag
  268. }
  269. for edbInfoIndex, v := range edbInfoIdList {
  270. // 单独存储max、min函数里的指标的数据
  271. tmpNewLatestDate, tmpErr := time.ParseInLocation(utils.FormatDate, v.LatestDate, time.Local)
  272. if tmpErr != nil {
  273. err = errors.New(`最近的日期格式化失败;日期:` + v.LatestDate + `;err:` + tmpErr.Error())
  274. return
  275. }
  276. if newLatestDate.IsZero() || tmpNewLatestDate.Before(newLatestDate) {
  277. newLatestDate = tmpNewLatestDate
  278. }
  279. /*tmpStartDate, tmpErr := time.ParseInLocation(utils.FormatDate, v.StartDate, time.Local)
  280. if tmpErr != nil {
  281. err = errors.New(`最近的日期格式化失败;日期:` + v.StartDate + `;err:` + tmpErr.Error())
  282. return
  283. }
  284. if maxStartDate.IsZero() || maxStartDate.Before(tmpStartDate) {
  285. maxStartDate = tmpStartDate
  286. }*/
  287. // 获取关联指标数据
  288. dataList, tmpErr := GetPredictEdbDataListAllByStartDate(v, 1, startDate)
  289. if tmpErr != nil {
  290. err = tmpErr
  291. return
  292. }
  293. for _, dv := range dataList { // 现有实际数据
  294. if val, ok := realSaveDataMap[dv.DataTime]; ok {
  295. if _, ok := val[v.EdbInfoId]; !ok {
  296. val[v.EdbInfoId] = dv.Value
  297. }
  298. } else {
  299. temp := make(map[int]float64)
  300. temp[v.EdbInfoId] = dv.Value
  301. realSaveDataMap[dv.DataTime] = temp
  302. }
  303. // 待处理的数据
  304. if val, ok := saveDataMap[dv.DataTime]; ok {
  305. if _, ok := val[v.EdbInfoId]; !ok {
  306. val[v.EdbInfoId] = dv.Value
  307. }
  308. } else {
  309. temp := make(map[int]float64)
  310. temp[v.EdbInfoId] = dv.Value
  311. saveDataMap[dv.DataTime] = temp
  312. }
  313. if dateTagConfig == "all" {
  314. if _, ok := dateMap[dv.DataTime]; !ok {
  315. dateList = append(dateList, dv.DataTime)
  316. dateMap[dv.DataTime] = struct{}{}
  317. }
  318. } else if dateTagConfig == "" { // 默认取第一个指标的时间序列
  319. if edbInfoIndex == 0 {
  320. if _, ok := dateMap[dv.DataTime]; !ok {
  321. dateList = append(dateList, dv.DataTime)
  322. dateMap[dv.DataTime] = struct{}{}
  323. }
  324. }
  325. } else {
  326. if eId, ok := edbInfoTag[dateTagConfig]; ok {
  327. if v.EdbInfoId == eId {
  328. if _, ok1 := dateMap[dv.DataTime]; !ok1 {
  329. dateList = append(dateList, dv.DataTime)
  330. dateMap[dv.DataTime] = struct{}{}
  331. }
  332. }
  333. }
  334. }
  335. /*if edbInfoIndex == 0 {
  336. dateList = append(dateList, dv.DataTime)
  337. tmpDate, _ := time.ParseInLocation(utils.FormatDate, dv.DataTime, time.Local)
  338. if minLatestDate.IsZero() || tmpDate.After(minLatestDate) {
  339. minLatestDate = tmpDate
  340. }
  341. if maxStartDate.IsZero() || tmpDate.Before(maxStartDate) {
  342. maxStartDate = tmpDate
  343. }
  344. }*/
  345. }
  346. }
  347. // 处理最大日期和最小日期
  348. for _, v := range dateList {
  349. tmpDate, _ := time.ParseInLocation(utils.FormatDate, v, time.Local)
  350. if minLatestDate.IsZero() || tmpDate.After(minLatestDate) {
  351. minLatestDate = tmpDate
  352. }
  353. if maxStartDate.IsZero() || tmpDate.Before(maxStartDate) {
  354. maxStartDate = tmpDate
  355. }
  356. }
  357. //数据处理,将日期内不全的数据做填补
  358. HandleDateSaveDataMap(dateList, maxStartDate, minLatestDate, realSaveDataMap, saveDataMap, edbInfoIdList, emptyType)
  359. formulaDateSlice, formulaDateMap, err := utils.HandleFormulaJson(formulaStr, minLatestDate)
  360. if err != nil {
  361. return
  362. }
  363. addSql := ` INSERT INTO edb_data_predict_calculate (edb_info_id,edb_code,data_time,value,create_time,modify_time,data_timestamp) values `
  364. var isAdd bool
  365. //获取指标所有数据
  366. dataList := make([]*EdbData, 0)
  367. dataTableName := GetEdbDataTableName(source, subSource)
  368. sql := `SELECT * FROM %s WHERE edb_info_id=? `
  369. sql = fmt.Sprintf(sql, dataTableName)
  370. _, err = to.Raw(sql, edbInfoId).QueryRows(&dataList)
  371. if err != nil {
  372. return
  373. }
  374. dataMap := make(map[string]string)
  375. for _, v := range dataList {
  376. dataMap[v.DataTime] = v.Value
  377. }
  378. edbInfoIdStr := strconv.Itoa(edbInfoId)
  379. existDataMap := make(map[string]string)
  380. removeDateList := make([]string, 0) //需要移除的日期
  381. // 判断是否特殊处理max和min函数
  382. maxDealFlag := false
  383. if emptyType == 4 && maxEmptyType == 2 {
  384. maxDealFlag = true
  385. }
  386. for sk, sv := range saveDataMap {
  387. if _, ok := dateMap[sk]; !ok { //不在最终的日期序列里面不计算
  388. continue
  389. }
  390. // 当空值处理类型选择了不计算时,只要有一个指标在某个日期没有值(即空值),则计算指标在该日期没有值
  391. if emptyType == 1 {
  392. if len(sv) != len(edbInfoIdList) {
  393. continue
  394. }
  395. }
  396. //fmt.Println(sk, sv)
  397. // 根据时间范围,选择对应的公式
  398. formulaMap := make(map[string]string)
  399. formulaStr = ""
  400. for _, fv := range formulaDateSlice {
  401. if sk < fv {
  402. if f, ok := formulaDateMap[fv]; ok {
  403. formulaStr = f
  404. formulaMap, err = utils.CheckFormula(formulaStr)
  405. if err != nil {
  406. err = fmt.Errorf("公式错误,请重新填写")
  407. return
  408. }
  409. }
  410. break
  411. }
  412. }
  413. if formulaStr == "" {
  414. continue
  415. }
  416. svMax := make(map[int]float64)
  417. if maxDealFlag {
  418. // 特殊处理max和min函数,如果原本的值为空,则选择空值参与运算
  419. if svMaxData, ok := realSaveDataMap[sk]; ok {
  420. svMax = svMaxData
  421. }
  422. }
  423. formulaStr = strings.ToUpper(formulaStr)
  424. formulaFormStr := ReplaceFormula(edbInfoIdList, sv, svMax, formulaMap, formulaStr, edbInfoIdBytes, maxDealFlag)
  425. if formulaFormStr != "" {
  426. //utils.FileLog.Info(fmt.Sprintf("formulaFormStr:%s", formulaFormStr))
  427. fmt.Println(fmt.Sprintf("date %s:formulaFormStr:%s", sk, formulaFormStr))
  428. //expression := formula.NewExpression(formulaFormStr)
  429. //calResult, tmpErr := expression.Evaluate()
  430. calVal, err := engine.ParseAndExec(formulaFormStr)
  431. //calVal, err := calResult.Float64()
  432. if err != nil {
  433. // 分母为0的报错,忽略该循环
  434. if utils.IsDivideZero(err) {
  435. removeDateList = append(removeDateList, sk)
  436. continue
  437. }
  438. err = errors.New("计算失败:获取计算值失败 Err:" + err.Error() + ";formulaStr:" + formulaFormStr)
  439. return "", 0, err
  440. }
  441. //nanCheck := fmt.Sprintf("%0.f", calVal)
  442. //if tmpErr != nil {
  443. // // 分母为0的报错
  444. // if strings.Contains(err.Error(), "divide by zero") {
  445. // removeDateList = append(removeDateList, sk)
  446. // continue
  447. // }
  448. // err = errors.New("计算失败:Err:" + tmpErr.Error() + ";formulaStr:" + formulaFormStr)
  449. // fmt.Println(err)
  450. // return
  451. //}
  452. //calVal, tmpErr := calResult.Float64()
  453. //if tmpErr != nil {
  454. // err = errors.New("计算失败:获取计算值失败 Err:" + tmpErr.Error() + ";formulaStr:" + formulaFormStr)
  455. // fmt.Println(err)
  456. // return
  457. //}
  458. nanCheck := fmt.Sprintf("%0.f", calVal)
  459. // 分母为0.0的报错
  460. if nanCheck == "NaN" || nanCheck == "+Inf" || nanCheck == "-Inf" {
  461. removeDateList = append(removeDateList, sk)
  462. continue
  463. }
  464. saveValue := decimal.NewFromFloat(calVal).RoundCeil(4).String() //utils.SubFloatToString(calVal, 4)
  465. if existVal, ok := dataMap[sk]; !ok {
  466. dataTime, _ := time.ParseInLocation(utils.FormatDate, sk, time.Local)
  467. timestamp := dataTime.UnixNano() / 1e6
  468. timeStr := fmt.Sprintf("%d", timestamp)
  469. if _, existOk := existDataMap[sk]; !existOk {
  470. addSql += GetAddSql(edbInfoIdStr, edbCode, sk, timeStr, saveValue)
  471. isAdd = true
  472. }
  473. existDataMap[sk] = sk
  474. } else {
  475. existValDecimal, tmpErr := decimal.NewFromString(existVal)
  476. if tmpErr != nil {
  477. err = tmpErr
  478. return "", 0, err
  479. }
  480. existStr := existValDecimal.String()
  481. if existStr != saveValue {
  482. sql := ` UPDATE %s SET value=?,modify_time=NOW() WHERE edb_info_id=? AND data_time=? `
  483. sql = fmt.Sprintf(sql, dataTableName)
  484. _, err = to.Raw(sql, saveValue, edbInfoId, sk).Exec()
  485. if err != nil {
  486. return "", 0, err
  487. }
  488. }
  489. }
  490. } else {
  491. //计算公式异常,那么就移除该指标
  492. removeDateList = append(removeDateList, sk)
  493. continue
  494. }
  495. }
  496. if isAdd {
  497. addSql = strings.TrimRight(addSql, ",")
  498. _, err = to.Raw(addSql).Exec()
  499. if err != nil {
  500. fmt.Println("RefreshAllCalculate add Err", err.Error())
  501. return
  502. }
  503. }
  504. if len(removeDateList) > 0 {
  505. removeDateStr := strings.Join(removeDateList, `","`)
  506. removeDateStr = `"` + removeDateStr + `"`
  507. //如果拼接指标变更了,那么需要删除所有的指标数据
  508. tableName := GetEdbDataTableName(source, subSource)
  509. sql := fmt.Sprintf(` DELETE FROM %s WHERE edb_info_id = ? and data_time in (%s) `, tableName, removeDateStr)
  510. _, err = to.Raw(sql, edbInfoId).Exec()
  511. if err != nil {
  512. err = fmt.Errorf("删除计算失败的计算指标数据失败,Err:" + err.Error())
  513. return
  514. }
  515. }
  516. //查询预测日期
  517. if !newLatestDate.IsZero() {
  518. latestDateStr = newLatestDate.Format(utils.FormatDate)
  519. sql = fmt.Sprintf(` SELECT * FROM %s WHERE edb_info_id = ? and data_time <= ? ORDER BY data_time desc limit 1`, dataTableName)
  520. var latestDate EdbData
  521. e := to.Raw(sql, edbInfoId, latestDateStr).QueryRow(&latestDate)
  522. if e != nil {
  523. if e.Error() == utils.ErrNoRow() {
  524. //首个日期的值
  525. sql = fmt.Sprintf(` SELECT * FROM %s WHERE edb_info_id = ? ORDER BY data_time ASC limit 1`, dataTableName)
  526. e = to.Raw(sql, edbInfoId).QueryRow(&latestDate)
  527. if e != nil {
  528. err = fmt.Errorf("查询最新日期失败,Err:" + e.Error())
  529. return
  530. }
  531. floatValue, _ := strconv.ParseFloat(latestDate.Value, 64)
  532. latestDateStr = latestDate.DataTime
  533. latestValue = floatValue
  534. } else {
  535. err = fmt.Errorf("查询最新日期失败,Err:" + e.Error())
  536. return
  537. }
  538. } else {
  539. floatValue, _ := strconv.ParseFloat(latestDate.Value, 64)
  540. latestDateStr = latestDate.DataTime
  541. latestValue = floatValue
  542. }
  543. }
  544. return
  545. }