edb_info_calculate.go 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594
  1. package data
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "eta/eta_api/models/data_manage"
  6. "eta/eta_api/utils"
  7. "fmt"
  8. "github.com/shopspring/decimal"
  9. "math"
  10. "regexp"
  11. "sort"
  12. "strings"
  13. "time"
  14. )
  15. func CheckFormula(formula string) map[string]string {
  16. mathFormula := []string{"MAX", "MIN", "ABS", "ACOS", "ASIN", "CEIL", "MOD", "POW", "ROUND", "SIGN", "SIN", "TAN", "LOG10", "LOG2", "LOG", "LN", "EXP"}
  17. str := strings.ToUpper(formula)
  18. for _, v := range mathFormula {
  19. str = strings.Replace(str, v, "", -1)
  20. }
  21. str = strings.Replace(str, "(", "", -1)
  22. str = strings.Replace(str, ")", "", -1)
  23. byteMap := make(map[string]string)
  24. for i := 0; i < len(str); i++ {
  25. byteInt := str[i]
  26. if byteInt >= 65 && byteInt <= 90 {
  27. byteStr := string(byteInt)
  28. if _, ok := byteMap[byteStr]; !ok {
  29. byteMap[byteStr] = byteStr
  30. }
  31. }
  32. }
  33. return byteMap
  34. }
  35. type FormulaListItem struct {
  36. Formula string `json:"f"`
  37. Date string `json:"d"`
  38. }
  39. // CheckFormulaJson 检测计算公式json串是否异常
  40. func CheckFormulaJson(formula string) (formulaSlice []string, err error) {
  41. list := make([]FormulaListItem, 0)
  42. err = json.Unmarshal([]byte(formula), &list)
  43. if err != nil {
  44. err = fmt.Errorf("公式串解析失败: json.Unmarshal Err: %v", err)
  45. return
  46. }
  47. formulaSlice = make([]string, 0)
  48. // 日期排序
  49. for _, v := range list {
  50. formulaSlice = append(formulaSlice, v.Formula)
  51. }
  52. return
  53. }
  54. type CalculateItems struct {
  55. EdbInfoId int
  56. DataMap map[string]float64
  57. }
  58. func GetFormulaMap() map[string]string {
  59. funMap := make(map[string]string)
  60. funMap["MAX"] = "[@@]"
  61. funMap["MIN"] = "[@!]"
  62. funMap["ABS"] = "[@#]"
  63. funMap["CEIL"] = "[@$]"
  64. funMap["COS"] = "[@%]"
  65. funMap["FLOOR"] = "[@^]"
  66. funMap["MOD"] = "[@&]"
  67. funMap["POW"] = "[@*]"
  68. funMap["ROUND"] = "[@(]"
  69. return funMap
  70. }
  71. // 处理整个数据
  72. func handleDateSaveDataMap(dateList []string, realSaveDataMap, saveDataMap map[string]map[int]float64, edbInfoIdArr []*data_manage.EdbInfo, emptyType int) {
  73. var startDate, endDate string
  74. var startDateT, endDateT time.Time
  75. if emptyType == 2 || emptyType == 3 {
  76. for k, _ := range realSaveDataMap {
  77. if k > endDate {
  78. endDate = k
  79. }
  80. if k < startDate || startDate == "" {
  81. startDate = k
  82. }
  83. }
  84. startDateT, _ = time.ParseInLocation(utils.FormatDate, startDate, time.Local)
  85. endDateT, _ = time.ParseInLocation(utils.FormatDate, endDate, time.Local)
  86. }
  87. for _, date := range dateList {
  88. tmpDataMap := realSaveDataMap[date]
  89. for _, edbInfo := range edbInfoIdArr {
  90. tmpEdbInfoId := edbInfo.EdbInfoId // 当前指标id
  91. // 如果该日期不存在该指标数据,那么需要找寻前后日期的数据,进行填补
  92. if _, ok := tmpDataMap[tmpEdbInfoId]; !ok {
  93. //day := 0
  94. //switch edbInfo.Frequency {
  95. //case "周度":
  96. // day = 7
  97. //case "旬度":
  98. // day = 15
  99. //case "月度":
  100. // day = 30
  101. //case "季度":
  102. // day = 90
  103. //case "年度":
  104. // day = 365
  105. //}
  106. // 需求池 255 指标运算文案修改,补数据遍历区间修改(2023-3-7 09:37:23修改)
  107. switch emptyType {
  108. case 0:
  109. handleDateDataMap(realSaveDataMap, saveDataMap, date, tmpEdbInfoId, 35)
  110. case 2:
  111. handleDateDataMapBefore(realSaveDataMap, saveDataMap, date, tmpEdbInfoId, startDateT, endDateT)
  112. case 3:
  113. handleDateDataMapAfter(realSaveDataMap, saveDataMap, date, tmpEdbInfoId, startDateT, endDateT)
  114. case 4:
  115. handleDateDataMapZero(saveDataMap, date, tmpEdbInfoId)
  116. }
  117. }
  118. }
  119. }
  120. }
  121. // handleDataMap 处理单个日期的数据
  122. func handleDateDataMap(realSaveDataMap, saveDataMap map[string]map[int]float64, date string, edbInfoId, day int) {
  123. currDate, _ := time.ParseInLocation(utils.FormatDate, date, time.Local)
  124. // 后一天
  125. nextDateDayStr := currDate.AddDate(0, 0, 1).Format(utils.FormatDate)
  126. // 前一天
  127. preDateDayStr := currDate.AddDate(0, 0, -1).Format(utils.FormatDate)
  128. for i := 1; i <= day; i++ {
  129. // 下个日期的数据
  130. {
  131. if i >= 1 {
  132. nextDateDayStr = currDate.AddDate(0, 0, i).Format(utils.FormatDate)
  133. }
  134. if findDataMap, hasFindDataMap := realSaveDataMap[nextDateDayStr]; hasFindDataMap { // 下一个日期有数据
  135. if val, hasFindItem := findDataMap[edbInfoId]; hasFindItem {
  136. saveDataMap[date][edbInfoId] = val
  137. return
  138. }
  139. }
  140. }
  141. // 上个日期的数据
  142. {
  143. if i >= 1 {
  144. preDateDayStr = currDate.AddDate(0, 0, -i).Format(utils.FormatDate)
  145. }
  146. if findDataMap, hasFindDataMap := realSaveDataMap[preDateDayStr]; hasFindDataMap { // 下一个日期有数据
  147. if val, hasFindItem := findDataMap[edbInfoId]; hasFindItem {
  148. saveDataMap[date][edbInfoId] = val
  149. return
  150. }
  151. }
  152. }
  153. }
  154. }
  155. // handleDataByLinearRegression 插值法补充数据(线性方程式)
  156. func handleDataByLinearRegression(edbInfoDataList []*data_manage.EdbDataList, handleDataMap map[string]float64) (err error) {
  157. if len(edbInfoDataList) < 2 {
  158. return
  159. }
  160. var startEdbInfoData *data_manage.EdbDataList
  161. for _, v := range edbInfoDataList {
  162. handleDataMap[v.DataTime] = v.Value
  163. // 第一个数据就给过滤了,给后面的试用
  164. if startEdbInfoData == nil {
  165. startEdbInfoData = v
  166. continue
  167. }
  168. // 获取两条数据之间相差的天数
  169. startDataTime, _ := time.ParseInLocation(utils.FormatDate, startEdbInfoData.DataTime, time.Local)
  170. currDataTime, _ := time.ParseInLocation(utils.FormatDate, v.DataTime, time.Local)
  171. betweenHour := int(currDataTime.Sub(startDataTime).Hours())
  172. betweenDay := betweenHour / 24
  173. // 如果相差一天,那么过滤
  174. if betweenDay <= 1 {
  175. startEdbInfoData = v
  176. continue
  177. }
  178. // 生成线性方程式
  179. var a, b float64
  180. {
  181. coordinateData := make([]utils.Coordinate, 0)
  182. tmpCoordinate1 := utils.Coordinate{
  183. X: 1,
  184. Y: startEdbInfoData.Value,
  185. }
  186. coordinateData = append(coordinateData, tmpCoordinate1)
  187. tmpCoordinate2 := utils.Coordinate{
  188. X: float64(betweenDay) + 1,
  189. Y: v.Value,
  190. }
  191. coordinateData = append(coordinateData, tmpCoordinate2)
  192. a, b = utils.GetLinearResult(coordinateData)
  193. if math.IsNaN(a) || math.IsNaN(b) {
  194. err = errors.New("线性方程公式生成失败")
  195. return
  196. }
  197. }
  198. // 生成对应的值
  199. {
  200. for i := 1; i < betweenDay; i++ {
  201. tmpDataTime := startDataTime.AddDate(0, 0, i)
  202. aDecimal := decimal.NewFromFloat(a)
  203. xDecimal := decimal.NewFromInt(int64(i) + 1)
  204. bDecimal := decimal.NewFromFloat(b)
  205. val, _ := aDecimal.Mul(xDecimal).Add(bDecimal).Round(4).Float64()
  206. handleDataMap[tmpDataTime.Format(utils.FormatDate)] = val
  207. }
  208. }
  209. startEdbInfoData = v
  210. }
  211. return
  212. }
  213. // HandleDataByLinearRegression 插值法补充数据(线性方程式)
  214. func HandleDataByLinearRegression(edbInfoDataList []*data_manage.EdbDataList, handleDataMap map[string]float64) (err error) {
  215. return handleDataByLinearRegression(edbInfoDataList, handleDataMap)
  216. }
  217. // CallCalculateComputeCorrelation 调用计算拟合残差的相关系数
  218. func CallCalculateComputeCorrelation(data *data_manage.EdbInfoCalculateBatchSaveReqByEdbLib, lang string) (val string, err error, errMsg string) {
  219. errMsg = "计算失败"
  220. // 调用指标库去更新
  221. reqJson, err := json.Marshal(data)
  222. if err != nil {
  223. errMsg = "计算相关系数参数解析异常!"
  224. err = errors.New("参数解析失败,Err:" + err.Error())
  225. return
  226. }
  227. respItem, err := CalculateComputeCorrelation(string(reqJson), lang)
  228. if err != nil {
  229. return
  230. }
  231. if respItem.Ret == 200 {
  232. val = respItem.Data
  233. }
  234. return
  235. }
  236. // handleDateDataMapBefore 前值填充:空值优先以最近的前值填充,没有前值时,用后值填充
  237. func handleDateDataMapBefore(realSaveDataMap, saveDataMap map[string]map[int]float64, date string, edbInfoId int, startDateT, endDateT time.Time) {
  238. currDate, _ := time.ParseInLocation(utils.FormatDate, date, time.Local)
  239. // 后一天
  240. nextDateDay := currDate
  241. // 前一天
  242. preDateDay := currDate
  243. for i := 1; preDateDay.After(startDateT) || preDateDay == startDateT; i++ {
  244. // 上个日期的数据
  245. {
  246. preDateDay = currDate.AddDate(0, 0, -i)
  247. preDateDayStr := preDateDay.Format(utils.FormatDate)
  248. if findDataMap, hasFindDataMap := realSaveDataMap[preDateDayStr]; hasFindDataMap { // 下一个日期有数据
  249. if val, hasFindItem := findDataMap[edbInfoId]; hasFindItem {
  250. fmt.Println(fmt.Sprintf("date:%s, 无值,取%s的值%.4f", date, preDateDayStr, val))
  251. saveDataMap[date][edbInfoId] = val
  252. return
  253. }
  254. }
  255. }
  256. }
  257. for i := 1; nextDateDay.Before(endDateT) || nextDateDay == endDateT; i++ {
  258. // 下个日期的数据
  259. {
  260. nextDateDay = currDate.AddDate(0, 0, i)
  261. nextDateDayStr := nextDateDay.Format(utils.FormatDate)
  262. if findDataMap, hasFindDataMap := realSaveDataMap[nextDateDayStr]; hasFindDataMap { // 下一个日期有数据
  263. if val, hasFindItem := findDataMap[edbInfoId]; hasFindItem {
  264. fmt.Println(fmt.Sprintf("date:%s, 无值,取%s的值%.4f", date, nextDateDayStr, val))
  265. saveDataMap[date][edbInfoId] = val
  266. return
  267. }
  268. }
  269. }
  270. }
  271. return
  272. }
  273. // handleDateDataMapAfter 后值填充:空值优先以最近的后值填充,没有后值时,用前值填充
  274. func handleDateDataMapAfter(realSaveDataMap, saveDataMap map[string]map[int]float64, date string, edbInfoId int, startDateT, endDateT time.Time) {
  275. currDate, _ := time.ParseInLocation(utils.FormatDate, date, time.Local)
  276. // 后一天
  277. nextDateDay := currDate
  278. // 前一天
  279. preDateDay := currDate
  280. for i := 1; nextDateDay.Before(endDateT) || nextDateDay == endDateT; i++ {
  281. // 下个日期的数据
  282. {
  283. nextDateDay = currDate.AddDate(0, 0, i)
  284. nextDateDayStr := nextDateDay.Format(utils.FormatDate)
  285. if findDataMap, hasFindDataMap := realSaveDataMap[nextDateDayStr]; hasFindDataMap { // 下一个日期有数据
  286. if val, hasFindItem := findDataMap[edbInfoId]; hasFindItem {
  287. fmt.Println(fmt.Sprintf("date:%s, 无值,取%s的值%.4f", date, nextDateDayStr, val))
  288. saveDataMap[date][edbInfoId] = val
  289. return
  290. }
  291. }
  292. }
  293. }
  294. for i := 1; preDateDay.After(startDateT) || preDateDay == startDateT; i++ {
  295. // 上个日期的数据
  296. {
  297. preDateDay = currDate.AddDate(0, 0, -i)
  298. preDateDayStr := preDateDay.Format(utils.FormatDate)
  299. if findDataMap, hasFindDataMap := realSaveDataMap[preDateDayStr]; hasFindDataMap { // 下一个日期有数据
  300. if val, hasFindItem := findDataMap[edbInfoId]; hasFindItem {
  301. fmt.Println(fmt.Sprintf("date:%s, 无值,取%s的值%.4f", date, preDateDayStr, val))
  302. saveDataMap[date][edbInfoId] = val
  303. return
  304. }
  305. }
  306. }
  307. }
  308. return
  309. }
  310. // handleDateDataMapZero 等于0
  311. func handleDateDataMapZero(saveDataMap map[string]map[int]float64, date string, edbInfoId int) {
  312. saveDataMap[date][edbInfoId] = 0
  313. return
  314. }
  315. func GetMaxMinEdbInfo(formula string) string {
  316. //formula := "A+min(A,B,max(A,C))"
  317. // todo 无法处理max里嵌套max或者min的情况
  318. // 使用正则表达式匹配MAX和MIN函数及其参数
  319. regex := regexp.MustCompile(`(?i)(MAX|MIN)\((.*?)\)`)
  320. matches := regex.FindAllStringSubmatch(formula, -1)
  321. // 遍历匹配结果,输出MAX和MIN函数及其参数
  322. for _, match := range matches {
  323. if len(match) == 3 {
  324. parameter := strings.ToLower(match[0]) // 参数
  325. formula = strings.ReplaceAll(formula, match[0], parameter)
  326. fmt.Printf("formula: %s\n", formula)
  327. }
  328. }
  329. formula = strings.ReplaceAll(formula, "max", "MAX")
  330. formula = strings.ReplaceAll(formula, "min", "MIN")
  331. return formula
  332. }
  333. // HandleDataByLinearRegressionToList 插值法补充数据(线性方程式)
  334. func HandleDataByLinearRegressionToList(edbInfoDataList []*data_manage.EdbDataList, handleDataMap map[string]float64) (dataTimeList []string, valueList []float64, err error) {
  335. if len(edbInfoDataList) < 2 {
  336. return
  337. }
  338. var startEdbInfoData *data_manage.EdbDataList
  339. for _, v := range edbInfoDataList {
  340. handleDataMap[v.DataTime] = v.Value
  341. dataTimeList = append(dataTimeList, v.DataTime)
  342. // 第一个数据就给过滤了,给后面的试用
  343. if startEdbInfoData == nil {
  344. startEdbInfoData = v
  345. //startEdbInfoData.DataTime = startEdbInfoData.DataTime[:5]+ "01-01"
  346. continue
  347. }
  348. // 获取两条数据之间相差的天数
  349. startDataTime, _ := time.ParseInLocation(utils.FormatDate, startEdbInfoData.DataTime, time.Local)
  350. currDataTime, _ := time.ParseInLocation(utils.FormatDate, v.DataTime, time.Local)
  351. betweenHour := int(currDataTime.Sub(startDataTime).Hours())
  352. betweenDay := betweenHour / 24
  353. // 如果相差一天,那么过滤
  354. if betweenDay <= 1 {
  355. startEdbInfoData = v
  356. continue
  357. }
  358. // 生成线性方程式
  359. var a, b float64
  360. {
  361. coordinateData := make([]utils.Coordinate, 0)
  362. tmpCoordinate1 := utils.Coordinate{
  363. X: 1,
  364. Y: startEdbInfoData.Value,
  365. }
  366. coordinateData = append(coordinateData, tmpCoordinate1)
  367. tmpCoordinate2 := utils.Coordinate{
  368. X: float64(betweenDay) + 1,
  369. Y: v.Value,
  370. }
  371. coordinateData = append(coordinateData, tmpCoordinate2)
  372. a, b = utils.GetLinearResult(coordinateData)
  373. if math.IsNaN(a) || math.IsNaN(b) {
  374. err = errors.New("线性方程公式生成失败")
  375. return
  376. }
  377. }
  378. // 生成对应的值
  379. {
  380. for i := 1; i < betweenDay; i++ {
  381. tmpDataTime := startDataTime.AddDate(0, 0, i)
  382. aDecimal := decimal.NewFromFloat(a)
  383. xDecimal := decimal.NewFromInt(int64(i) + 1)
  384. bDecimal := decimal.NewFromFloat(b)
  385. val, _ := aDecimal.Mul(xDecimal).Add(bDecimal).Round(4).Float64()
  386. handleDataMap[tmpDataTime.Format(utils.FormatDate)] = val
  387. dataTimeList = append(dataTimeList, tmpDataTime.Format(utils.FormatDate))
  388. valueList = append(valueList, val)
  389. }
  390. }
  391. startEdbInfoData = v
  392. }
  393. return
  394. }
  395. // HandleDataByLinearRegressionToList 保证生成365个数据点的线性插值法
  396. func HandleDataByLinearRegressionToListV2(edbInfoDataList []*data_manage.EdbDataList, handleDataMap map[string]float64) (dataTimeList []string, valueList []float64, err error) {
  397. if len(edbInfoDataList) < 2 {
  398. return
  399. }
  400. // 确保至少有两天数据来生成线性方程
  401. if len(edbInfoDataList) < 2 {
  402. err = errors.New("至少需要两天的数据来执行线性插值")
  403. return
  404. }
  405. // 对数据按日期排序,确保顺序正确
  406. sort.Slice(edbInfoDataList, func(i, j int) bool {
  407. t1, _ := time.ParseInLocation(utils.FormatDate, edbInfoDataList[i].DataTime, time.Local)
  408. t2, _ := time.ParseInLocation(utils.FormatDate, edbInfoDataList[j].DataTime, time.Local)
  409. return t1.Before(t2)
  410. })
  411. startEdbInfoData := edbInfoDataList[0]
  412. endEdbInfoData := edbInfoDataList[len(edbInfoDataList)-1]
  413. // 计算起始和结束日期间实际的天数
  414. startDate, _ := time.ParseInLocation(utils.FormatDate, startEdbInfoData.DataTime, time.Local)
  415. endDate, _ := time.ParseInLocation(utils.FormatDate, endEdbInfoData.DataTime, time.Local)
  416. actualDays := endDate.Sub(startDate).Hours() / 24
  417. // 生成365个数据点,首先处理已有数据
  418. for _, v := range edbInfoDataList {
  419. handleDataMap[v.DataTime] = v.Value
  420. dataTimeList = append(dataTimeList, v.DataTime)
  421. valueList = append(valueList, v.Value)
  422. }
  423. // 如果已有数据跨越天数不足365天,则对缺失的日期进行线性插值
  424. if actualDays < 365 {
  425. // 使用已有数据点生成线性方程(这里简化处理,实际可能需更细致处理边界情况)
  426. var a, b float64
  427. coordinateData := []utils.Coordinate{
  428. {X: 1, Y: startEdbInfoData.Value},
  429. {X: float64(len(edbInfoDataList)), Y: endEdbInfoData.Value},
  430. }
  431. a, b = utils.GetLinearResult(coordinateData)
  432. if math.IsNaN(a) || math.IsNaN(b) {
  433. err = errors.New("线性方程公式生成失败")
  434. return
  435. }
  436. // 对剩余日期进行插值
  437. for i := 1; i < 365; i++ {
  438. day := startDate.AddDate(0, 0, i)
  439. if _, exists := handleDataMap[day.Format(utils.FormatDate)]; !exists {
  440. aDecimal := decimal.NewFromFloat(a)
  441. xDecimal := decimal.NewFromInt(int64(i) + 1)
  442. bDecimal := decimal.NewFromFloat(b)
  443. val, _ := aDecimal.Mul(xDecimal).Add(bDecimal).Round(4).Float64()
  444. handleDataMap[day.Format(utils.FormatDate)] = val
  445. dataTimeList = append(dataTimeList, day.Format(utils.FormatDate))
  446. valueList = append(valueList, val)
  447. }
  448. }
  449. }
  450. return
  451. }
  452. // HandleDataByLinearRegressionToListV3 插值法补充数据(线性方程式)-直接补充指标起始日期间的所有数据
  453. func HandleDataByLinearRegressionToListV3(edbInfoDataList []*data_manage.EdbDataList, handleDataMap map[string]float64) (newEdbInfoDataList []*data_manage.EdbDataList, dataTimeList []string, valueList []float64, err error) {
  454. if len(edbInfoDataList) < 2 {
  455. return
  456. }
  457. var startEdbInfoData *data_manage.EdbDataList
  458. for _, v := range edbInfoDataList {
  459. handleDataMap[v.DataTime] = v.Value
  460. newEdbInfoDataList = append(newEdbInfoDataList, v)
  461. dataTimeList = append(dataTimeList, v.DataTime)
  462. // 第一个数据就给过滤了,给后面的试用
  463. if startEdbInfoData == nil {
  464. startEdbInfoData = v
  465. //startEdbInfoData.DataTime = startEdbInfoData.DataTime[:5]+ "01-01"
  466. continue
  467. }
  468. // 获取两条数据之间相差的天数
  469. startDataTime, _ := time.ParseInLocation(utils.FormatDate, startEdbInfoData.DataTime, time.Local)
  470. currDataTime, _ := time.ParseInLocation(utils.FormatDate, v.DataTime, time.Local)
  471. betweenHour := int(currDataTime.Sub(startDataTime).Hours())
  472. betweenDay := betweenHour / 24
  473. // 如果相差一天,那么过滤
  474. if betweenDay <= 1 {
  475. startEdbInfoData = v
  476. continue
  477. }
  478. // 生成线性方程式
  479. var a, b float64
  480. {
  481. coordinateData := make([]utils.Coordinate, 0)
  482. tmpCoordinate1 := utils.Coordinate{
  483. X: 1,
  484. Y: startEdbInfoData.Value,
  485. }
  486. coordinateData = append(coordinateData, tmpCoordinate1)
  487. tmpCoordinate2 := utils.Coordinate{
  488. X: float64(betweenDay) + 1,
  489. Y: v.Value,
  490. }
  491. coordinateData = append(coordinateData, tmpCoordinate2)
  492. a, b = utils.GetLinearResult(coordinateData)
  493. if math.IsNaN(a) || math.IsNaN(b) {
  494. err = errors.New("线性方程公式生成失败")
  495. return
  496. }
  497. }
  498. // 生成对应的值
  499. {
  500. for i := 1; i < betweenDay; i++ {
  501. tmpDataTime := startDataTime.AddDate(0, 0, i)
  502. aDecimal := decimal.NewFromFloat(a)
  503. xDecimal := decimal.NewFromInt(int64(i) + 1)
  504. bDecimal := decimal.NewFromFloat(b)
  505. val, _ := aDecimal.Mul(xDecimal).Add(bDecimal).Round(4).Float64()
  506. handleDataMap[tmpDataTime.Format(utils.FormatDate)] = val
  507. dataTimeList = append(dataTimeList, tmpDataTime.Format(utils.FormatDate))
  508. valueList = append(valueList, val)
  509. newEdbInfoDataList = append(newEdbInfoDataList, &data_manage.EdbDataList{
  510. EdbDataId: v.EdbDataId,
  511. EdbInfoId: v.EdbInfoId,
  512. DataTime: tmpDataTime.Format(utils.FormatDate),
  513. DataTimestamp: tmpDataTime.UnixNano() / 1e6,
  514. Value: val,
  515. })
  516. }
  517. }
  518. startEdbInfoData = v
  519. }
  520. return
  521. }