edb_info_calculate.go 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764
  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. "github.com/yidane/formula"
  10. "math"
  11. "regexp"
  12. "strconv"
  13. "strings"
  14. "time"
  15. )
  16. func CheckFormula(formula string) map[string]string {
  17. mathFormula := []string{"MAX", "MIN", "ABS", "ACOS", "ASIN", "CEIL", "MOD", "POW", "ROUND", "SIGN", "SIN", "TAN", "LOG10", "LOG2", "LOG", "LN", "EXP"}
  18. str := strings.ToUpper(formula)
  19. for _, v := range mathFormula {
  20. str = strings.Replace(str, v, "", -1)
  21. }
  22. str = strings.Replace(str, "(", "", -1)
  23. str = strings.Replace(str, ")", "", -1)
  24. byteMap := make(map[string]string)
  25. for i := 0; i < len(str); i++ {
  26. byteInt := str[i]
  27. if byteInt >= 65 && byteInt <= 90 {
  28. byteStr := string(byteInt)
  29. if _, ok := byteMap[byteStr]; !ok {
  30. byteMap[byteStr] = byteStr
  31. }
  32. }
  33. }
  34. return byteMap
  35. }
  36. type FormulaListItem struct {
  37. Formula string `json:"f"`
  38. Date string `json:"d"`
  39. }
  40. // CheckFormulaJson 检测计算公式json串是否异常
  41. func CheckFormulaJson(formula string) (formulaSlice []string, err error) {
  42. list := make([]FormulaListItem, 0)
  43. err = json.Unmarshal([]byte(formula), &list)
  44. if err != nil {
  45. err = fmt.Errorf("公式串解析失败: json.Unmarshal Err: %v", err)
  46. return
  47. }
  48. formulaSlice = make([]string, 0)
  49. // 日期排序
  50. for _, v := range list {
  51. formulaSlice = append(formulaSlice, v.Formula)
  52. }
  53. return
  54. }
  55. type CalculateItems struct {
  56. EdbInfoId int
  57. DataMap map[string]float64
  58. }
  59. func ReplaceFormula(edbInfoIdArr []*data_manage.EdbInfo, valArr map[int]float64, formulaMap map[string]string, formulaStr string, edbInfoIdBytes []string) string {
  60. // todo 处理min和max
  61. funMap := GetFormulaMap()
  62. for k, v := range funMap {
  63. formulaStr = strings.Replace(formulaStr, k, v, -1)
  64. }
  65. replaceCount := 0
  66. for dk, dv := range edbInfoIdArr {
  67. if dk == 0 {
  68. dKey := edbInfoIdBytes[dk]
  69. if _, ok := formulaMap[dKey]; ok { //公式中存在
  70. if val, valOk := valArr[dv.EdbInfoId]; valOk { //值存在
  71. dvStr := fmt.Sprintf("%v", val)
  72. formulaStr = strings.Replace(formulaStr, dKey, dvStr, -1)
  73. replaceCount++
  74. } else {
  75. fmt.Println("valArr not found:", valArr, valOk)
  76. }
  77. } else {
  78. fmt.Println("formulaMap not found:", dKey, dk)
  79. }
  80. }
  81. if dk == 1 {
  82. dKey := edbInfoIdBytes[dk]
  83. if _, ok := formulaMap[dKey]; ok { //公式中存在
  84. if val, valOk := valArr[dv.EdbInfoId]; valOk { //值存在
  85. dvStr := fmt.Sprintf("%v", val)
  86. formulaStr = strings.Replace(formulaStr, dKey, dvStr, -1)
  87. replaceCount++
  88. } else {
  89. fmt.Println("valArr not found:", valArr, valOk)
  90. }
  91. } else {
  92. fmt.Println("formulaMap not found:", dKey, dk)
  93. }
  94. }
  95. if dk == 2 {
  96. dKey := edbInfoIdBytes[dk]
  97. if _, ok := formulaMap[dKey]; ok { //公式中存在
  98. if val, valOk := valArr[dv.EdbInfoId]; valOk { //值存在
  99. dvStr := fmt.Sprintf("%v", val)
  100. formulaStr = strings.Replace(formulaStr, dKey, dvStr, -1)
  101. replaceCount++
  102. }
  103. }
  104. }
  105. if dk == 3 {
  106. dKey := edbInfoIdBytes[dk]
  107. if _, ok := formulaMap[dKey]; ok { //公式中存在
  108. if val, valOk := valArr[dv.EdbInfoId]; valOk { //值存在
  109. dvStr := fmt.Sprintf("%v", val)
  110. formulaStr = strings.Replace(formulaStr, dKey, dvStr, -1)
  111. replaceCount++
  112. }
  113. }
  114. }
  115. if dk == 4 {
  116. dKey := edbInfoIdBytes[dk]
  117. if _, ok := formulaMap[dKey]; ok { //公式中存在
  118. if val, valOk := valArr[dv.EdbInfoId]; valOk { //值存在
  119. dvStr := fmt.Sprintf("%v", val)
  120. formulaStr = strings.Replace(formulaStr, dKey, dvStr, -1)
  121. replaceCount++
  122. }
  123. }
  124. }
  125. if dk == 5 {
  126. dKey := edbInfoIdBytes[dk]
  127. if _, ok := formulaMap[dKey]; ok { //公式中存在
  128. if val, valOk := valArr[dv.EdbInfoId]; valOk { //值存在
  129. dvStr := fmt.Sprintf("%v", val)
  130. formulaStr = strings.Replace(formulaStr, dKey, dvStr, -1)
  131. replaceCount++
  132. }
  133. }
  134. }
  135. if dk == 6 {
  136. dKey := edbInfoIdBytes[dk]
  137. if _, ok := formulaMap[dKey]; ok { //公式中存在
  138. if val, valOk := valArr[dv.EdbInfoId]; valOk { //值存在
  139. dvStr := fmt.Sprintf("%v", val)
  140. formulaStr = strings.Replace(formulaStr, dKey, dvStr, -1)
  141. replaceCount++
  142. }
  143. }
  144. }
  145. if dk == 7 {
  146. dKey := edbInfoIdBytes[dk]
  147. if _, ok := formulaMap[dKey]; ok { //公式中存在
  148. if val, valOk := valArr[dv.EdbInfoId]; valOk { //值存在
  149. dvStr := fmt.Sprintf("%v", val)
  150. formulaStr = strings.Replace(formulaStr, dKey, dvStr, -1)
  151. replaceCount++
  152. }
  153. }
  154. }
  155. if dk == 8 {
  156. dKey := edbInfoIdBytes[dk]
  157. if _, ok := formulaMap[dKey]; ok { //公式中存在
  158. if val, valOk := valArr[dv.EdbInfoId]; valOk { //值存在
  159. dvStr := fmt.Sprintf("%v", val)
  160. formulaStr = strings.Replace(formulaStr, dKey, dvStr, -1)
  161. replaceCount++
  162. }
  163. }
  164. }
  165. if dk == 9 {
  166. dKey := edbInfoIdBytes[dk]
  167. if _, ok := formulaMap[dKey]; ok { //公式中存在
  168. if val, valOk := valArr[dv.EdbInfoId]; valOk { //值存在
  169. dvStr := fmt.Sprintf("%v", val)
  170. formulaStr = strings.Replace(formulaStr, dKey, dvStr, -1)
  171. replaceCount++
  172. }
  173. }
  174. }
  175. if dk == 10 {
  176. dKey := edbInfoIdBytes[dk]
  177. if _, ok := formulaMap[dKey]; ok { //公式中存在
  178. if val, valOk := valArr[dv.EdbInfoId]; valOk { //值存在
  179. dvStr := fmt.Sprintf("%v", val)
  180. formulaStr = strings.Replace(formulaStr, dKey, dvStr, -1)
  181. replaceCount++
  182. }
  183. }
  184. }
  185. if dk == 11 {
  186. dKey := edbInfoIdBytes[dk]
  187. if _, ok := formulaMap[dKey]; ok { //公式中存在
  188. if val, valOk := valArr[dv.EdbInfoId]; valOk { //值存在
  189. dvStr := fmt.Sprintf("%v", val)
  190. formulaStr = strings.Replace(formulaStr, dKey, dvStr, -1)
  191. replaceCount++
  192. }
  193. }
  194. }
  195. if dk == 12 {
  196. dKey := edbInfoIdBytes[dk]
  197. if _, ok := formulaMap[dKey]; ok { //公式中存在
  198. if val, valOk := valArr[dv.EdbInfoId]; valOk { //值存在
  199. dvStr := fmt.Sprintf("%v", val)
  200. formulaStr = strings.Replace(formulaStr, dKey, dvStr, -1)
  201. replaceCount++
  202. }
  203. }
  204. }
  205. if dk == 13 {
  206. dKey := edbInfoIdBytes[dk]
  207. if _, ok := formulaMap[dKey]; ok { //公式中存在
  208. if val, valOk := valArr[dv.EdbInfoId]; valOk { //值存在
  209. dvStr := fmt.Sprintf("%v", val)
  210. formulaStr = strings.Replace(formulaStr, dKey, dvStr, -1)
  211. replaceCount++
  212. }
  213. }
  214. }
  215. if dk == 14 {
  216. dKey := edbInfoIdBytes[dk]
  217. if _, ok := formulaMap[dKey]; ok { //公式中存在
  218. if val, valOk := valArr[dv.EdbInfoId]; valOk { //值存在
  219. dvStr := fmt.Sprintf("%v", val)
  220. formulaStr = strings.Replace(formulaStr, dKey, dvStr, -1)
  221. replaceCount++
  222. }
  223. }
  224. }
  225. if dk == 15 {
  226. dKey := edbInfoIdBytes[dk]
  227. if _, ok := formulaMap[dKey]; ok { //公式中存在
  228. if val, valOk := valArr[dv.EdbInfoId]; valOk { //值存在
  229. dvStr := fmt.Sprintf("%v", val)
  230. formulaStr = strings.Replace(formulaStr, dKey, dvStr, -1)
  231. replaceCount++
  232. }
  233. }
  234. }
  235. if dk == 16 {
  236. dKey := edbInfoIdBytes[dk]
  237. if _, ok := formulaMap[dKey]; ok { //公式中存在
  238. if val, valOk := valArr[dv.EdbInfoId]; valOk { //值存在
  239. dvStr := fmt.Sprintf("%v", val)
  240. formulaStr = strings.Replace(formulaStr, dKey, dvStr, -1)
  241. replaceCount++
  242. }
  243. }
  244. }
  245. if dk == 17 {
  246. dKey := edbInfoIdBytes[dk]
  247. if _, ok := formulaMap[dKey]; ok { //公式中存在
  248. if val, valOk := valArr[dv.EdbInfoId]; valOk { //值存在
  249. dvStr := fmt.Sprintf("%v", val)
  250. formulaStr = strings.Replace(formulaStr, dKey, dvStr, -1)
  251. replaceCount++
  252. }
  253. }
  254. }
  255. if dk == 18 {
  256. dKey := edbInfoIdBytes[dk]
  257. if _, ok := formulaMap[dKey]; ok { //公式中存在
  258. if val, valOk := valArr[dv.EdbInfoId]; valOk { //值存在
  259. dvStr := fmt.Sprintf("%v", val)
  260. formulaStr = strings.Replace(formulaStr, dKey, dvStr, -1)
  261. replaceCount++
  262. }
  263. }
  264. }
  265. if dk == 19 {
  266. dKey := edbInfoIdBytes[dk]
  267. if _, ok := formulaMap[dKey]; ok { //公式中存在
  268. if val, valOk := valArr[dv.EdbInfoId]; valOk { //值存在
  269. dvStr := fmt.Sprintf("%v", val)
  270. formulaStr = strings.Replace(formulaStr, dKey, dvStr, -1)
  271. replaceCount++
  272. }
  273. }
  274. }
  275. if dk == 20 {
  276. dKey := edbInfoIdBytes[dk]
  277. if _, ok := formulaMap[dKey]; ok { //公式中存在
  278. if val, valOk := valArr[dv.EdbInfoId]; valOk { //值存在
  279. dvStr := fmt.Sprintf("%v", val)
  280. formulaStr = strings.Replace(formulaStr, dKey, dvStr, -1)
  281. replaceCount++
  282. }
  283. }
  284. }
  285. if dk == 21 {
  286. dKey := edbInfoIdBytes[dk]
  287. if _, ok := formulaMap[dKey]; ok { //公式中存在
  288. if val, valOk := valArr[dv.EdbInfoId]; valOk { //值存在
  289. dvStr := fmt.Sprintf("%v", val)
  290. formulaStr = strings.Replace(formulaStr, dKey, dvStr, -1)
  291. replaceCount++
  292. }
  293. }
  294. }
  295. if dk == 22 {
  296. dKey := edbInfoIdBytes[dk]
  297. if _, ok := formulaMap[dKey]; ok { //公式中存在
  298. if val, valOk := valArr[dv.EdbInfoId]; valOk { //值存在
  299. dvStr := fmt.Sprintf("%v", val)
  300. formulaStr = strings.Replace(formulaStr, dKey, dvStr, -1)
  301. replaceCount++
  302. }
  303. }
  304. }
  305. if dk == 23 {
  306. dKey := edbInfoIdBytes[dk]
  307. if _, ok := formulaMap[dKey]; ok { //公式中存在
  308. if val, valOk := valArr[dv.EdbInfoId]; valOk { //值存在
  309. dvStr := fmt.Sprintf("%v", val)
  310. formulaStr = strings.Replace(formulaStr, dKey, dvStr, -1)
  311. replaceCount++
  312. }
  313. }
  314. }
  315. if dk == 24 {
  316. dKey := edbInfoIdBytes[dk]
  317. if _, ok := formulaMap[dKey]; ok { //公式中存在
  318. if val, valOk := valArr[dv.EdbInfoId]; valOk { //值存在
  319. dvStr := fmt.Sprintf("%v", val)
  320. formulaStr = strings.Replace(formulaStr, dKey, dvStr, -1)
  321. replaceCount++
  322. }
  323. }
  324. }
  325. if dk == 25 {
  326. dKey := edbInfoIdBytes[dk]
  327. if _, ok := formulaMap[dKey]; ok { //公式中存在
  328. if val, valOk := valArr[dv.EdbInfoId]; valOk { //值存在
  329. dvStr := fmt.Sprintf("%v", val)
  330. formulaStr = strings.Replace(formulaStr, dKey, dvStr, -1)
  331. replaceCount++
  332. }
  333. }
  334. }
  335. }
  336. for k, v := range funMap {
  337. formulaStr = strings.Replace(formulaStr, v, k, -1)
  338. }
  339. if replaceCount == len(formulaMap) {
  340. return formulaStr
  341. } else {
  342. return ""
  343. }
  344. }
  345. func GetFormulaMap() map[string]string {
  346. funMap := make(map[string]string)
  347. funMap["MAX"] = "[@@]"
  348. funMap["MIN"] = "[@!]"
  349. funMap["ABS"] = "[@#]"
  350. funMap["CEIL"] = "[@$]"
  351. funMap["COS"] = "[@%]"
  352. funMap["FLOOR"] = "[@^]"
  353. funMap["MOD"] = "[@&]"
  354. funMap["POW"] = "[@*]"
  355. funMap["ROUND"] = "[@(]"
  356. return funMap
  357. }
  358. // 刷新数据
  359. func RefreshCalculate(edbInfoIdArr []*data_manage.EdbInfo, edbInfoId int, edbCode, formulaStr, startDate, endDate string, edbInfoIdBytes []string) (err error) {
  360. defer func() {
  361. if err != nil {
  362. fmt.Println("RefreshCalculate:Err " + err.Error())
  363. utils.FileLog.Info("Calculate Err:%s" + err.Error())
  364. }
  365. }()
  366. saveDataMap := make(map[string]map[int]float64)
  367. for _, v := range edbInfoIdArr {
  368. var condition string
  369. var pars []interface{}
  370. condition += " AND edb_info_id=? "
  371. pars = append(pars, v.EdbInfoId)
  372. if startDate != "" {
  373. condition += " AND data_time>=? "
  374. pars = append(pars, startDate)
  375. }
  376. if endDate != "" {
  377. condition += " AND data_time<=? "
  378. pars = append(pars, endDate)
  379. }
  380. dataList, err := data_manage.GetEdbDataListAll(condition, pars, v.Source, v.SubSource, 1)
  381. if err != nil {
  382. return err
  383. }
  384. dataMap := make(map[string]float64)
  385. for _, dv := range dataList {
  386. if val, ok := saveDataMap[dv.DataTime]; ok {
  387. if _, ok := val[v.EdbInfoId]; !ok {
  388. val[v.EdbInfoId] = dv.Value
  389. }
  390. } else {
  391. temp := make(map[int]float64)
  392. temp[v.EdbInfoId] = dv.Value
  393. saveDataMap[dv.DataTime] = temp
  394. }
  395. }
  396. item := new(CalculateItems)
  397. item.EdbInfoId = v.EdbInfoId
  398. item.DataMap = dataMap
  399. }
  400. formulaMap := CheckFormula(formulaStr)
  401. addSql := ` INSERT INTO edb_data_calculate(edb_info_id,edb_code,data_time,value,create_time,modify_time,status,data_timestamp) values `
  402. nowStr := time.Now().Format(utils.FormatDateTime)
  403. var isAdd bool
  404. for sk, sv := range saveDataMap {
  405. formulaStr = strings.ToUpper(formulaStr)
  406. formulaFormStr := ReplaceFormula(edbInfoIdArr, sv, formulaMap, formulaStr, edbInfoIdBytes)
  407. if formulaFormStr != "" {
  408. utils.FileLog.Info("formulaFormStr:%s", formulaFormStr)
  409. expression := formula.NewExpression(formulaFormStr)
  410. calResult, err := expression.Evaluate()
  411. if err != nil {
  412. err = errors.New("计算失败:Err:" + err.Error() + ";formulaStr:" + formulaFormStr)
  413. fmt.Println(err)
  414. return err
  415. }
  416. calVal, err := calResult.Float64()
  417. if err != nil {
  418. err = errors.New("计算失败:获取计算值失败 Err:" + err.Error() + ";formulaStr:" + formulaFormStr)
  419. fmt.Println(err)
  420. return err
  421. }
  422. count, err := data_manage.GetEdbDataCalculateByCodeAndDate(edbCode, sk)
  423. if err != nil && err.Error() != utils.ErrNoRow() {
  424. return err
  425. }
  426. if count <= 0 { //需要存入的数据
  427. dataTime, _ := time.Parse(utils.FormatDate, sk)
  428. timestamp := dataTime.UnixNano() / 1e6
  429. timeStr := fmt.Sprintf("%d", timestamp)
  430. addSql += "("
  431. addSql += strconv.Itoa(edbInfoId) + "," + "'" + edbCode + "'" + "," + "'" + sk + "'" + "," + utils.SubFloatToString(calVal, 4) + "," + "'" + nowStr + "'" +
  432. "," + "'" + nowStr + "'" + "," + "1"
  433. addSql += "," + "'" + timeStr + "'"
  434. addSql += "),"
  435. isAdd = true
  436. } else {
  437. calVal = utils.FixFloat(calVal, 4)
  438. err = data_manage.ModifyEdbDataCalculate(int64(edbInfoId), sk, calVal)
  439. if err != nil {
  440. return err
  441. }
  442. }
  443. }
  444. }
  445. if isAdd {
  446. addSql = strings.TrimRight(addSql, ",")
  447. data_manage.AddEdbDataCalculateBySql(addSql)
  448. if err != nil {
  449. fmt.Println("AddEdbDataCalculate Err:" + err.Error())
  450. return err
  451. }
  452. }
  453. return
  454. }
  455. // 处理整个数据
  456. func handleDateSaveDataMap(dateList []string, realSaveDataMap, saveDataMap map[string]map[int]float64, edbInfoIdArr []*data_manage.EdbInfo, emptyType int) {
  457. var startDate, endDate string
  458. var startDateT, endDateT time.Time
  459. if emptyType == 2 || emptyType == 3 {
  460. for k, _ := range realSaveDataMap {
  461. if k > endDate {
  462. endDate = k
  463. }
  464. if k < startDate || startDate == "" {
  465. startDate = k
  466. }
  467. }
  468. startDateT, _ = time.ParseInLocation(utils.FormatDate, startDate, time.Local)
  469. endDateT, _ = time.ParseInLocation(utils.FormatDate, endDate, time.Local)
  470. }
  471. for _, date := range dateList {
  472. tmpDataMap := realSaveDataMap[date]
  473. for _, edbInfo := range edbInfoIdArr {
  474. tmpEdbInfoId := edbInfo.EdbInfoId // 当前指标id
  475. // 如果该日期不存在该指标数据,那么需要找寻前后日期的数据,进行填补
  476. if _, ok := tmpDataMap[tmpEdbInfoId]; !ok {
  477. //day := 0
  478. //switch edbInfo.Frequency {
  479. //case "周度":
  480. // day = 7
  481. //case "旬度":
  482. // day = 15
  483. //case "月度":
  484. // day = 30
  485. //case "季度":
  486. // day = 90
  487. //case "年度":
  488. // day = 365
  489. //}
  490. // 需求池 255 指标运算文案修改,补数据遍历区间修改(2023-3-7 09:37:23修改)
  491. switch emptyType {
  492. case 0:
  493. handleDateDataMap(realSaveDataMap, saveDataMap, date, tmpEdbInfoId, 35)
  494. case 2:
  495. handleDateDataMapBefore(realSaveDataMap, saveDataMap, date, tmpEdbInfoId, startDateT, endDateT)
  496. case 3:
  497. handleDateDataMapAfter(realSaveDataMap, saveDataMap, date, tmpEdbInfoId, startDateT, endDateT)
  498. case 4:
  499. handleDateDataMapZero(saveDataMap, date, tmpEdbInfoId)
  500. }
  501. }
  502. }
  503. }
  504. }
  505. // handleDataMap 处理单个日期的数据
  506. func handleDateDataMap(realSaveDataMap, saveDataMap map[string]map[int]float64, date string, edbInfoId, day int) {
  507. currDate, _ := time.ParseInLocation(utils.FormatDate, date, time.Local)
  508. // 后一天
  509. nextDateDayStr := currDate.AddDate(0, 0, 1).Format(utils.FormatDate)
  510. // 前一天
  511. preDateDayStr := currDate.AddDate(0, 0, -1).Format(utils.FormatDate)
  512. for i := 1; i <= day; i++ {
  513. // 下个日期的数据
  514. {
  515. if i >= 1 {
  516. nextDateDayStr = currDate.AddDate(0, 0, i).Format(utils.FormatDate)
  517. }
  518. if findDataMap, hasFindDataMap := realSaveDataMap[nextDateDayStr]; hasFindDataMap { // 下一个日期有数据
  519. if val, hasFindItem := findDataMap[edbInfoId]; hasFindItem {
  520. saveDataMap[date][edbInfoId] = val
  521. return
  522. }
  523. }
  524. }
  525. // 上个日期的数据
  526. {
  527. if i >= 1 {
  528. preDateDayStr = currDate.AddDate(0, 0, -i).Format(utils.FormatDate)
  529. }
  530. if findDataMap, hasFindDataMap := realSaveDataMap[preDateDayStr]; hasFindDataMap { // 下一个日期有数据
  531. if val, hasFindItem := findDataMap[edbInfoId]; hasFindItem {
  532. saveDataMap[date][edbInfoId] = val
  533. return
  534. }
  535. }
  536. }
  537. }
  538. }
  539. // handleDataByLinearRegression 插值法补充数据(线性方程式)
  540. func handleDataByLinearRegression(edbInfoDataList []*data_manage.EdbDataList, handleDataMap map[string]float64) (err error) {
  541. if len(edbInfoDataList) < 2 {
  542. return
  543. }
  544. var startEdbInfoData *data_manage.EdbDataList
  545. for _, v := range edbInfoDataList {
  546. handleDataMap[v.DataTime] = v.Value
  547. // 第一个数据就给过滤了,给后面的试用
  548. if startEdbInfoData == nil {
  549. startEdbInfoData = v
  550. continue
  551. }
  552. // 获取两条数据之间相差的天数
  553. startDataTime, _ := time.ParseInLocation(utils.FormatDate, startEdbInfoData.DataTime, time.Local)
  554. currDataTime, _ := time.ParseInLocation(utils.FormatDate, v.DataTime, time.Local)
  555. betweenHour := int(currDataTime.Sub(startDataTime).Hours())
  556. betweenDay := betweenHour / 24
  557. // 如果相差一天,那么过滤
  558. if betweenDay <= 1 {
  559. startEdbInfoData = v
  560. continue
  561. }
  562. // 生成线性方程式
  563. var a, b float64
  564. {
  565. coordinateData := make([]utils.Coordinate, 0)
  566. tmpCoordinate1 := utils.Coordinate{
  567. X: 1,
  568. Y: startEdbInfoData.Value,
  569. }
  570. coordinateData = append(coordinateData, tmpCoordinate1)
  571. tmpCoordinate2 := utils.Coordinate{
  572. X: float64(betweenDay) + 1,
  573. Y: v.Value,
  574. }
  575. coordinateData = append(coordinateData, tmpCoordinate2)
  576. a, b = utils.GetLinearResult(coordinateData)
  577. if math.IsNaN(a) || math.IsNaN(b) {
  578. err = errors.New("线性方程公式生成失败")
  579. return
  580. }
  581. }
  582. // 生成对应的值
  583. {
  584. for i := 1; i < betweenDay; i++ {
  585. tmpDataTime := startDataTime.AddDate(0, 0, i)
  586. aDecimal := decimal.NewFromFloat(a)
  587. xDecimal := decimal.NewFromInt(int64(i) + 1)
  588. bDecimal := decimal.NewFromFloat(b)
  589. val, _ := aDecimal.Mul(xDecimal).Add(bDecimal).Round(4).Float64()
  590. handleDataMap[tmpDataTime.Format(utils.FormatDate)] = val
  591. }
  592. }
  593. startEdbInfoData = v
  594. }
  595. return
  596. }
  597. // HandleDataByLinearRegression 插值法补充数据(线性方程式)
  598. func HandleDataByLinearRegression(edbInfoDataList []*data_manage.EdbDataList, handleDataMap map[string]float64) (err error) {
  599. return handleDataByLinearRegression(edbInfoDataList, handleDataMap)
  600. }
  601. // CallCalculateComputeCorrelation 调用计算拟合残差的相关系数
  602. func CallCalculateComputeCorrelation(data *data_manage.EdbInfoCalculateBatchSaveReqByEdbLib) (val string, err error, errMsg string) {
  603. errMsg = "计算失败"
  604. // 调用指标库去更新
  605. reqJson, err := json.Marshal(data)
  606. if err != nil {
  607. errMsg = "计算相关系数参数解析异常!"
  608. err = errors.New("参数解析失败,Err:" + err.Error())
  609. return
  610. }
  611. respItem, err := CalculateComputeCorrelation(string(reqJson))
  612. if err != nil {
  613. return
  614. }
  615. if respItem.Ret == 200 {
  616. val = respItem.Data
  617. }
  618. return
  619. }
  620. // handleDateDataMapBefore 前值填充:空值优先以最近的前值填充,没有前值时,用后值填充
  621. func handleDateDataMapBefore(realSaveDataMap, saveDataMap map[string]map[int]float64, date string, edbInfoId int, startDateT, endDateT time.Time) {
  622. currDate, _ := time.ParseInLocation(utils.FormatDate, date, time.Local)
  623. // 后一天
  624. nextDateDay := currDate
  625. // 前一天
  626. preDateDay := currDate
  627. for i := 1; preDateDay.After(startDateT) || preDateDay == startDateT; i++ {
  628. // 上个日期的数据
  629. {
  630. preDateDay = currDate.AddDate(0, 0, -i)
  631. preDateDayStr := preDateDay.Format(utils.FormatDate)
  632. if findDataMap, hasFindDataMap := realSaveDataMap[preDateDayStr]; hasFindDataMap { // 下一个日期有数据
  633. if val, hasFindItem := findDataMap[edbInfoId]; hasFindItem {
  634. fmt.Println(fmt.Sprintf("date:%s, 无值,取%s的值%.4f", date, preDateDayStr, val))
  635. saveDataMap[date][edbInfoId] = val
  636. return
  637. }
  638. }
  639. }
  640. }
  641. for i := 1; nextDateDay.Before(endDateT) || nextDateDay == endDateT; i++ {
  642. // 下个日期的数据
  643. {
  644. nextDateDay = currDate.AddDate(0, 0, i)
  645. nextDateDayStr := nextDateDay.Format(utils.FormatDate)
  646. if findDataMap, hasFindDataMap := realSaveDataMap[nextDateDayStr]; hasFindDataMap { // 下一个日期有数据
  647. if val, hasFindItem := findDataMap[edbInfoId]; hasFindItem {
  648. fmt.Println(fmt.Sprintf("date:%s, 无值,取%s的值%.4f", date, nextDateDayStr, val))
  649. saveDataMap[date][edbInfoId] = val
  650. return
  651. }
  652. }
  653. }
  654. }
  655. return
  656. }
  657. // handleDateDataMapAfter 后值填充:空值优先以最近的后值填充,没有后值时,用前值填充
  658. func handleDateDataMapAfter(realSaveDataMap, saveDataMap map[string]map[int]float64, date string, edbInfoId int, startDateT, endDateT time.Time) {
  659. currDate, _ := time.ParseInLocation(utils.FormatDate, date, time.Local)
  660. // 后一天
  661. nextDateDay := currDate
  662. // 前一天
  663. preDateDay := currDate
  664. for i := 1; nextDateDay.Before(endDateT) || nextDateDay == endDateT; i++ {
  665. // 下个日期的数据
  666. {
  667. nextDateDay = currDate.AddDate(0, 0, i)
  668. nextDateDayStr := nextDateDay.Format(utils.FormatDate)
  669. if findDataMap, hasFindDataMap := realSaveDataMap[nextDateDayStr]; hasFindDataMap { // 下一个日期有数据
  670. if val, hasFindItem := findDataMap[edbInfoId]; hasFindItem {
  671. fmt.Println(fmt.Sprintf("date:%s, 无值,取%s的值%.4f", date, nextDateDayStr, val))
  672. saveDataMap[date][edbInfoId] = val
  673. return
  674. }
  675. }
  676. }
  677. }
  678. for i := 1; preDateDay.After(startDateT) || preDateDay == startDateT; i++ {
  679. // 上个日期的数据
  680. {
  681. preDateDay = currDate.AddDate(0, 0, -i)
  682. preDateDayStr := preDateDay.Format(utils.FormatDate)
  683. if findDataMap, hasFindDataMap := realSaveDataMap[preDateDayStr]; hasFindDataMap { // 下一个日期有数据
  684. if val, hasFindItem := findDataMap[edbInfoId]; hasFindItem {
  685. fmt.Println(fmt.Sprintf("date:%s, 无值,取%s的值%.4f", date, preDateDayStr, val))
  686. saveDataMap[date][edbInfoId] = val
  687. return
  688. }
  689. }
  690. }
  691. }
  692. return
  693. }
  694. // handleDateDataMapZero 等于0
  695. func handleDateDataMapZero(saveDataMap map[string]map[int]float64, date string, edbInfoId int) {
  696. saveDataMap[date][edbInfoId] = 0
  697. return
  698. }
  699. func GetMaxMinEdbInfo(formula string) string {
  700. //formula := "A+min(A,B,max(A,C))"
  701. // todo 无法处理max里嵌套max或者min的情况
  702. // 使用正则表达式匹配MAX和MIN函数及其参数
  703. regex := regexp.MustCompile(`(?i)(MAX|MIN)\((.*?)\)`)
  704. matches := regex.FindAllStringSubmatch(formula, -1)
  705. // 遍历匹配结果,输出MAX和MIN函数及其参数
  706. for _, match := range matches {
  707. if len(match) == 3 {
  708. parameter := strings.ToLower(match[0]) // 参数
  709. formula = strings.ReplaceAll(formula, match[0], parameter)
  710. fmt.Printf("formula: %s\n", formula)
  711. }
  712. }
  713. formula = strings.ReplaceAll(formula, "max", "MAX")
  714. formula = strings.ReplaceAll(formula, "min", "MIN")
  715. return formula
  716. }