base_predict_from_calculate.go 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592
  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. tmpEndDate, tmpErr := time.ParseInLocation(utils.FormatDate, v.EndDate, time.Local)
  288. if tmpErr != nil {
  289. err = errors.New(`最近的日期格式化失败;日期:` + v.EndDate + `;err:` + tmpErr.Error())
  290. return
  291. }
  292. if minEndDate.IsZero() || tmpEndDate.Before(minEndDate) {
  293. minEndDate = tmpEndDate
  294. }
  295. }*/
  296. // 获取关联指标数据
  297. dataList, tmpErr := GetPredictEdbDataListAllByStartDate(v, 1, startDate)
  298. if tmpErr != nil {
  299. err = tmpErr
  300. return
  301. }
  302. for _, dv := range dataList { // 现有实际数据
  303. if val, ok := realSaveDataMap[dv.DataTime]; ok {
  304. if _, ok := val[v.EdbInfoId]; !ok {
  305. val[v.EdbInfoId] = dv.Value
  306. }
  307. } else {
  308. temp := make(map[int]float64)
  309. temp[v.EdbInfoId] = dv.Value
  310. realSaveDataMap[dv.DataTime] = temp
  311. }
  312. // 待处理的数据
  313. if val, ok := saveDataMap[dv.DataTime]; ok {
  314. if _, ok := val[v.EdbInfoId]; !ok {
  315. val[v.EdbInfoId] = dv.Value
  316. }
  317. } else {
  318. temp := make(map[int]float64)
  319. temp[v.EdbInfoId] = dv.Value
  320. saveDataMap[dv.DataTime] = temp
  321. }
  322. if dateTagConfig == "all" {
  323. if _, ok := dateMap[dv.DataTime]; !ok {
  324. dateList = append(dateList, dv.DataTime)
  325. dateMap[dv.DataTime] = struct{}{}
  326. }
  327. } else if dateTagConfig == "" { // 默认取第一个指标的时间序列
  328. if edbInfoIndex == 0 {
  329. if _, ok := dateMap[dv.DataTime]; !ok {
  330. dateList = append(dateList, dv.DataTime)
  331. dateMap[dv.DataTime] = struct{}{}
  332. }
  333. }
  334. } else {
  335. if eId, ok := edbInfoTag[dateTagConfig]; ok {
  336. if v.EdbInfoId == eId {
  337. if _, ok1 := dateMap[dv.DataTime]; !ok1 {
  338. dateList = append(dateList, dv.DataTime)
  339. dateMap[dv.DataTime] = struct{}{}
  340. }
  341. }
  342. }
  343. }
  344. /*if edbInfoIndex == 0 {
  345. dateList = append(dateList, dv.DataTime)
  346. tmpDate, _ := time.ParseInLocation(utils.FormatDate, dv.DataTime, time.Local)
  347. if minLatestDate.IsZero() || tmpDate.After(minLatestDate) {
  348. minLatestDate = tmpDate
  349. }
  350. if maxStartDate.IsZero() || tmpDate.Before(maxStartDate) {
  351. maxStartDate = tmpDate
  352. }
  353. }*/
  354. }
  355. }
  356. // 处理最大日期和最小日期
  357. for _, v := range dateList {
  358. tmpDate, _ := time.ParseInLocation(utils.FormatDate, v, time.Local)
  359. if minLatestDate.IsZero() || tmpDate.After(minLatestDate) {
  360. minLatestDate = tmpDate
  361. }
  362. if maxStartDate.IsZero() || tmpDate.Before(maxStartDate) {
  363. maxStartDate = tmpDate
  364. }
  365. }
  366. //数据处理,将日期内不全的数据做填补
  367. HandleDateSaveDataMap(dateList, maxStartDate, minLatestDate, realSaveDataMap, saveDataMap, edbInfoIdList, emptyType)
  368. formulaDateSlice, formulaDateMap, err := utils.HandleFormulaJson(formulaStr, minLatestDate)
  369. if err != nil {
  370. return
  371. }
  372. addSql := ` INSERT INTO edb_data_predict_calculate (edb_info_id,edb_code,data_time,value,create_time,modify_time,data_timestamp) values `
  373. var isAdd bool
  374. //获取指标所有数据
  375. dataList := make([]*EdbData, 0)
  376. dataTableName := GetEdbDataTableName(source, subSource)
  377. sql := `SELECT * FROM %s WHERE edb_info_id=? `
  378. sql = fmt.Sprintf(sql, dataTableName)
  379. _, err = to.Raw(sql, edbInfoId).QueryRows(&dataList)
  380. if err != nil {
  381. return
  382. }
  383. dataMap := make(map[string]string)
  384. for _, v := range dataList {
  385. dataMap[v.DataTime] = v.Value
  386. }
  387. edbInfoIdStr := strconv.Itoa(edbInfoId)
  388. existDataMap := make(map[string]string)
  389. removeDateList := make([]string, 0) //需要移除的日期
  390. // 判断是否特殊处理max和min函数
  391. maxDealFlag := false
  392. if emptyType == 4 && maxEmptyType == 2 {
  393. maxDealFlag = true
  394. }
  395. for sk, sv := range saveDataMap {
  396. if _, ok := dateMap[sk]; !ok { //不在最终的日期序列里面不计算
  397. continue
  398. }
  399. // 当空值处理类型选择了不计算时,只要有一个指标在某个日期没有值(即空值),则计算指标在该日期没有值
  400. if emptyType == 1 {
  401. if len(sv) != len(edbInfoIdList) {
  402. continue
  403. }
  404. }
  405. //fmt.Println(sk, sv)
  406. // 根据时间范围,选择对应的公式
  407. formulaMap := make(map[string]string)
  408. formulaStr = ""
  409. for _, fv := range formulaDateSlice {
  410. if sk < fv {
  411. if f, ok := formulaDateMap[fv]; ok {
  412. formulaStr = f
  413. formulaMap, err = utils.CheckFormula(formulaStr)
  414. if err != nil {
  415. err = fmt.Errorf("公式错误,请重新填写")
  416. return
  417. }
  418. }
  419. break
  420. }
  421. }
  422. if formulaStr == "" {
  423. continue
  424. }
  425. svMax := make(map[int]float64)
  426. if maxDealFlag {
  427. // 特殊处理max和min函数,如果原本的值为空,则选择空值参与运算
  428. if svMaxData, ok := realSaveDataMap[sk]; ok {
  429. svMax = svMaxData
  430. }
  431. }
  432. formulaStr = strings.ToUpper(formulaStr)
  433. formulaFormStr := ReplaceFormula(edbInfoIdList, sv, svMax, formulaMap, formulaStr, edbInfoIdBytes, maxDealFlag)
  434. if formulaFormStr != "" {
  435. //utils.FileLog.Info(fmt.Sprintf("formulaFormStr:%s", formulaFormStr))
  436. fmt.Println(fmt.Sprintf("date %s:formulaFormStr:%s", sk, formulaFormStr))
  437. //expression := formula.NewExpression(formulaFormStr)
  438. //calResult, tmpErr := expression.Evaluate()
  439. calVal, err := engine.ParseAndExec(formulaFormStr)
  440. //calVal, err := calResult.Float64()
  441. if err != nil {
  442. // 分母为0的报错,忽略该循环
  443. if utils.IsDivideZero(err) {
  444. removeDateList = append(removeDateList, sk)
  445. continue
  446. }
  447. err = errors.New("计算失败:获取计算值失败 Err:" + err.Error() + ";formulaStr:" + formulaFormStr)
  448. return "", 0, err
  449. }
  450. //nanCheck := fmt.Sprintf("%0.f", calVal)
  451. //if tmpErr != nil {
  452. // // 分母为0的报错
  453. // if strings.Contains(err.Error(), "divide by zero") {
  454. // removeDateList = append(removeDateList, sk)
  455. // continue
  456. // }
  457. // err = errors.New("计算失败:Err:" + tmpErr.Error() + ";formulaStr:" + formulaFormStr)
  458. // fmt.Println(err)
  459. // return
  460. //}
  461. //calVal, tmpErr := calResult.Float64()
  462. //if tmpErr != nil {
  463. // err = errors.New("计算失败:获取计算值失败 Err:" + tmpErr.Error() + ";formulaStr:" + formulaFormStr)
  464. // fmt.Println(err)
  465. // return
  466. //}
  467. nanCheck := fmt.Sprintf("%0.f", calVal)
  468. // 分母为0.0的报错
  469. if nanCheck == "NaN" || nanCheck == "+Inf" || nanCheck == "-Inf" {
  470. removeDateList = append(removeDateList, sk)
  471. continue
  472. }
  473. saveValue := decimal.NewFromFloat(calVal).Round(4).String() //utils.SubFloatToString(calVal, 4)
  474. if existVal, ok := dataMap[sk]; !ok {
  475. dataTime, _ := time.ParseInLocation(utils.FormatDate, sk, time.Local)
  476. timestamp := dataTime.UnixNano() / 1e6
  477. timeStr := fmt.Sprintf("%d", timestamp)
  478. if _, existOk := existDataMap[sk]; !existOk {
  479. addSql += GetAddSql(edbInfoIdStr, edbCode, sk, timeStr, saveValue)
  480. isAdd = true
  481. }
  482. existDataMap[sk] = sk
  483. } else {
  484. existValDecimal, tmpErr := decimal.NewFromString(existVal)
  485. if tmpErr != nil {
  486. err = tmpErr
  487. return "", 0, err
  488. }
  489. existStr := existValDecimal.String()
  490. if existStr != saveValue {
  491. sql := ` UPDATE %s SET value=?,modify_time=NOW() WHERE edb_info_id=? AND data_time=? `
  492. sql = fmt.Sprintf(sql, dataTableName)
  493. _, err = to.Raw(sql, saveValue, edbInfoId, sk).Exec()
  494. if err != nil {
  495. return "", 0, err
  496. }
  497. }
  498. }
  499. } else {
  500. //计算公式异常,那么就移除该指标
  501. removeDateList = append(removeDateList, sk)
  502. continue
  503. }
  504. }
  505. if isAdd {
  506. addSql = strings.TrimRight(addSql, ",")
  507. _, err = to.Raw(addSql).Exec()
  508. if err != nil {
  509. fmt.Println("RefreshAllCalculate add Err", err.Error())
  510. return
  511. }
  512. }
  513. if len(removeDateList) > 0 {
  514. removeDateStr := strings.Join(removeDateList, `","`)
  515. removeDateStr = `"` + removeDateStr + `"`
  516. //如果拼接指标变更了,那么需要删除所有的指标数据
  517. tableName := GetEdbDataTableName(source, subSource)
  518. sql := fmt.Sprintf(` DELETE FROM %s WHERE edb_info_id = ? and data_time in (%s) `, tableName, removeDateStr)
  519. _, err = to.Raw(sql, edbInfoId).Exec()
  520. if err != nil {
  521. err = fmt.Errorf("删除计算失败的计算指标数据失败,Err:" + err.Error())
  522. return
  523. }
  524. }
  525. //查询预测日期
  526. if !newLatestDate.IsZero() {
  527. latestDateStr = newLatestDate.Format(utils.FormatDate)
  528. sql = fmt.Sprintf(` SELECT * FROM %s WHERE edb_info_id = ? and data_time <= ? ORDER BY data_time desc limit 1`, dataTableName)
  529. var latestDate EdbData
  530. e := to.Raw(sql, edbInfoId, latestDateStr).QueryRow(&latestDate)
  531. if e != nil {
  532. if e.Error() == utils.ErrNoRow() {
  533. //首个日期的值
  534. sql = fmt.Sprintf(` SELECT * FROM %s WHERE edb_info_id = ? ORDER BY data_time ASC limit 1`, dataTableName)
  535. e = to.Raw(sql, edbInfoId).QueryRow(&latestDate)
  536. if e != nil {
  537. err = fmt.Errorf("查询最新日期失败,Err:" + e.Error())
  538. return
  539. }
  540. floatValue, _ := strconv.ParseFloat(latestDate.Value, 64)
  541. latestDateStr = latestDate.DataTime
  542. latestValue = floatValue
  543. } else {
  544. err = fmt.Errorf("查询最新日期失败,Err:" + e.Error())
  545. return
  546. }
  547. } else {
  548. floatValue, _ := strconv.ParseFloat(latestDate.Value, 64)
  549. latestDateStr = latestDate.DataTime
  550. latestValue = floatValue
  551. }
  552. }
  553. return
  554. }