edb_info_calculate.go 20 KB

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