edb_info_calculate.go 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708
  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. "strconv"
  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"}
  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. // CheckFormula2 校验公式是否正常(比如说除法的分母不能为0之类的,实际上就是用预设的字段数据做一次计算)
  36. func CheckFormula2(edbInfoArr []*data_manage.EdbInfo, formulaMap map[string]string, formulaStr string, edbInfoIdBytes []string) (ok bool, err error) {
  37. valArr := make(map[int]float64)
  38. for _, v := range edbInfoArr {
  39. valArr[v.EdbInfoId] = 100
  40. }
  41. formulaStr = strings.ToUpper(formulaStr)
  42. formulaFormStr := ReplaceFormula(edbInfoArr, valArr, formulaMap, formulaStr, edbInfoIdBytes)
  43. if formulaFormStr == "" {
  44. return
  45. }
  46. expression := formula.NewExpression(formulaFormStr)
  47. _, err = expression.Evaluate()
  48. if err != nil {
  49. } else {
  50. ok = true
  51. }
  52. return
  53. }
  54. type CalculateItems struct {
  55. EdbInfoId int
  56. DataMap map[string]float64
  57. }
  58. func Calculate(edbInfoIdArr []*data_manage.EdbInfo, edbInfoId int, edbCode, formulaStr string, edbInfoIdBytes []string) (err error) {
  59. defer func() {
  60. if err != nil {
  61. utils.FileLog.Info("Calculate Err:%s" + err.Error())
  62. }
  63. }()
  64. saveDataMap := make(map[string]map[int]float64)
  65. for _, v := range edbInfoIdArr {
  66. var condition string
  67. var pars []interface{}
  68. condition += " AND edb_info_id=? "
  69. pars = append(pars, v.EdbInfoId)
  70. dataList, err := data_manage.GetEdbDataListAll(condition, pars, v.Source, 1)
  71. if err != nil {
  72. return err
  73. }
  74. dataMap := make(map[string]float64)
  75. for _, dv := range dataList {
  76. if val, ok := saveDataMap[dv.DataTime]; ok {
  77. if _, ok := val[v.EdbInfoId]; !ok {
  78. val[v.EdbInfoId] = dv.Value
  79. }
  80. } else {
  81. temp := make(map[int]float64)
  82. temp[v.EdbInfoId] = dv.Value
  83. saveDataMap[dv.DataTime] = temp
  84. }
  85. }
  86. item := new(CalculateItems)
  87. item.EdbInfoId = v.EdbInfoId
  88. item.DataMap = dataMap
  89. }
  90. formulaMap := CheckFormula(formulaStr)
  91. addSql := ` INSERT INTO edb_data_calculate(edb_info_id,edb_code,data_time,value,create_time,modify_time,status,data_timestamp) values `
  92. nowStr := time.Now().Format(utils.FormatDateTime)
  93. var isAdd bool
  94. for sk, sv := range saveDataMap {
  95. formulaStr = strings.ToUpper(formulaStr)
  96. formulaFormStr := ReplaceFormula(edbInfoIdArr, sv, formulaMap, formulaStr, edbInfoIdBytes)
  97. if formulaStr == "" {
  98. return
  99. }
  100. if formulaFormStr != "" {
  101. expression := formula.NewExpression(formulaFormStr)
  102. calResult, err := expression.Evaluate()
  103. if err != nil {
  104. err = errors.New("计算失败:Err:" + err.Error() + ";formulaStr:" + formulaFormStr)
  105. fmt.Println(err)
  106. return err
  107. }
  108. calVal, err := calResult.Float64()
  109. if err != nil {
  110. err = errors.New("计算失败:获取计算值失败 Err:" + err.Error() + ";formulaStr:" + formulaFormStr)
  111. fmt.Println(err)
  112. return err
  113. }
  114. //需要存入的数据
  115. {
  116. dataTime, _ := time.Parse(utils.FormatDate, sk)
  117. timestamp := dataTime.UnixNano() / 1e6
  118. timeStr := fmt.Sprintf("%d", timestamp)
  119. addSql += "("
  120. addSql += strconv.Itoa(edbInfoId) + "," + "'" + edbCode + "'" + "," + "'" + sk + "'" + "," + utils.SubFloatToString(calVal, 4) + "," + "'" + nowStr + "'" +
  121. "," + "'" + nowStr + "'" + "," + "1"
  122. addSql += "," + "'" + timeStr + "'"
  123. addSql += "),"
  124. isAdd = true
  125. }
  126. } else {
  127. fmt.Println("formulaFormStr is empty")
  128. }
  129. }
  130. if isAdd {
  131. addSql = strings.TrimRight(addSql, ",")
  132. data_manage.AddEdbDataCalculateBySql(addSql)
  133. if err != nil {
  134. fmt.Println("AddEdbDataCalculate Err:" + err.Error())
  135. return err
  136. }
  137. }
  138. return
  139. }
  140. func ReplaceFormula(edbInfoIdArr []*data_manage.EdbInfo, valArr map[int]float64, formulaMap map[string]string, formulaStr string, edbInfoIdBytes []string) string {
  141. funMap := GetFormulaMap()
  142. for k, v := range funMap {
  143. formulaStr = strings.Replace(formulaStr, k, v, -1)
  144. }
  145. replaceCount := 0
  146. for dk, dv := range edbInfoIdArr {
  147. if dk == 0 {
  148. dKey := edbInfoIdBytes[dk]
  149. if _, ok := formulaMap[dKey]; ok { //公式中存在
  150. if val, valOk := valArr[dv.EdbInfoId]; valOk { //值存在
  151. dvStr := fmt.Sprintf("%v", val)
  152. formulaStr = strings.Replace(formulaStr, dKey, dvStr, -1)
  153. replaceCount++
  154. } else {
  155. fmt.Println("valArr not found:", valArr, valOk)
  156. }
  157. } else {
  158. fmt.Println("formulaMap not found:", dKey, dk)
  159. }
  160. }
  161. if dk == 1 {
  162. dKey := edbInfoIdBytes[dk]
  163. if _, ok := formulaMap[dKey]; ok { //公式中存在
  164. if val, valOk := valArr[dv.EdbInfoId]; valOk { //值存在
  165. dvStr := fmt.Sprintf("%v", val)
  166. formulaStr = strings.Replace(formulaStr, dKey, dvStr, -1)
  167. replaceCount++
  168. } else {
  169. fmt.Println("valArr not found:", valArr, valOk)
  170. }
  171. } else {
  172. fmt.Println("formulaMap not found:", dKey, dk)
  173. }
  174. }
  175. if dk == 2 {
  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 == 3 {
  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 == 4 {
  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 == 5 {
  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 == 6 {
  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 == 7 {
  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 == 8 {
  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 == 9 {
  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 == 10 {
  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 == 11 {
  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 == 12 {
  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 == 13 {
  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 == 14 {
  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 == 15 {
  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 == 16 {
  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 == 17 {
  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. if dk == 18 {
  336. dKey := edbInfoIdBytes[dk]
  337. if _, ok := formulaMap[dKey]; ok { //公式中存在
  338. if val, valOk := valArr[dv.EdbInfoId]; valOk { //值存在
  339. dvStr := fmt.Sprintf("%v", val)
  340. formulaStr = strings.Replace(formulaStr, dKey, dvStr, -1)
  341. replaceCount++
  342. }
  343. }
  344. }
  345. if dk == 19 {
  346. dKey := edbInfoIdBytes[dk]
  347. if _, ok := formulaMap[dKey]; ok { //公式中存在
  348. if val, valOk := valArr[dv.EdbInfoId]; valOk { //值存在
  349. dvStr := fmt.Sprintf("%v", val)
  350. formulaStr = strings.Replace(formulaStr, dKey, dvStr, -1)
  351. replaceCount++
  352. }
  353. }
  354. }
  355. if dk == 20 {
  356. dKey := edbInfoIdBytes[dk]
  357. if _, ok := formulaMap[dKey]; ok { //公式中存在
  358. if val, valOk := valArr[dv.EdbInfoId]; valOk { //值存在
  359. dvStr := fmt.Sprintf("%v", val)
  360. formulaStr = strings.Replace(formulaStr, dKey, dvStr, -1)
  361. replaceCount++
  362. }
  363. }
  364. }
  365. if dk == 21 {
  366. dKey := edbInfoIdBytes[dk]
  367. if _, ok := formulaMap[dKey]; ok { //公式中存在
  368. if val, valOk := valArr[dv.EdbInfoId]; valOk { //值存在
  369. dvStr := fmt.Sprintf("%v", val)
  370. formulaStr = strings.Replace(formulaStr, dKey, dvStr, -1)
  371. replaceCount++
  372. }
  373. }
  374. }
  375. if dk == 22 {
  376. dKey := edbInfoIdBytes[dk]
  377. if _, ok := formulaMap[dKey]; ok { //公式中存在
  378. if val, valOk := valArr[dv.EdbInfoId]; valOk { //值存在
  379. dvStr := fmt.Sprintf("%v", val)
  380. formulaStr = strings.Replace(formulaStr, dKey, dvStr, -1)
  381. replaceCount++
  382. }
  383. }
  384. }
  385. if dk == 23 {
  386. dKey := edbInfoIdBytes[dk]
  387. if _, ok := formulaMap[dKey]; ok { //公式中存在
  388. if val, valOk := valArr[dv.EdbInfoId]; valOk { //值存在
  389. dvStr := fmt.Sprintf("%v", val)
  390. formulaStr = strings.Replace(formulaStr, dKey, dvStr, -1)
  391. replaceCount++
  392. }
  393. }
  394. }
  395. if dk == 24 {
  396. dKey := edbInfoIdBytes[dk]
  397. if _, ok := formulaMap[dKey]; ok { //公式中存在
  398. if val, valOk := valArr[dv.EdbInfoId]; valOk { //值存在
  399. dvStr := fmt.Sprintf("%v", val)
  400. formulaStr = strings.Replace(formulaStr, dKey, dvStr, -1)
  401. replaceCount++
  402. }
  403. }
  404. }
  405. if dk == 25 {
  406. dKey := edbInfoIdBytes[dk]
  407. if _, ok := formulaMap[dKey]; ok { //公式中存在
  408. if val, valOk := valArr[dv.EdbInfoId]; valOk { //值存在
  409. dvStr := fmt.Sprintf("%v", val)
  410. formulaStr = strings.Replace(formulaStr, dKey, dvStr, -1)
  411. replaceCount++
  412. }
  413. }
  414. }
  415. }
  416. for k, v := range funMap {
  417. formulaStr = strings.Replace(formulaStr, v, k, -1)
  418. }
  419. if replaceCount == len(formulaMap) {
  420. return formulaStr
  421. } else {
  422. return ""
  423. }
  424. }
  425. func GetFormulaMap() map[string]string {
  426. funMap := make(map[string]string)
  427. funMap["MAX"] = "[@@]"
  428. funMap["MIN"] = "[@!]"
  429. funMap["ABS"] = "[@#]"
  430. funMap["CEIL"] = "[@$]"
  431. funMap["COS"] = "[@%]"
  432. funMap["FLOOR"] = "[@^]"
  433. funMap["MOD"] = "[@&]"
  434. funMap["POW"] = "[@*]"
  435. funMap["ROUND"] = "[@(]"
  436. return funMap
  437. }
  438. // 刷新数据
  439. func RefreshCalculate(edbInfoIdArr []*data_manage.EdbInfo, edbInfoId int, edbCode, formulaStr, startDate, endDate string, edbInfoIdBytes []string) (err error) {
  440. defer func() {
  441. if err != nil {
  442. fmt.Println("RefreshCalculate:Err " + err.Error())
  443. utils.FileLog.Info("Calculate Err:%s" + err.Error())
  444. }
  445. }()
  446. saveDataMap := make(map[string]map[int]float64)
  447. for _, v := range edbInfoIdArr {
  448. var condition string
  449. var pars []interface{}
  450. condition += " AND edb_info_id=? "
  451. pars = append(pars, v.EdbInfoId)
  452. if startDate != "" {
  453. condition += " AND data_time>=? "
  454. pars = append(pars, startDate)
  455. }
  456. if endDate != "" {
  457. condition += " AND data_time<=? "
  458. pars = append(pars, endDate)
  459. }
  460. dataList, err := data_manage.GetEdbDataListAll(condition, pars, v.Source, 1)
  461. if err != nil {
  462. return err
  463. }
  464. dataMap := make(map[string]float64)
  465. for _, dv := range dataList {
  466. if val, ok := saveDataMap[dv.DataTime]; ok {
  467. if _, ok := val[v.EdbInfoId]; !ok {
  468. val[v.EdbInfoId] = dv.Value
  469. }
  470. } else {
  471. temp := make(map[int]float64)
  472. temp[v.EdbInfoId] = dv.Value
  473. saveDataMap[dv.DataTime] = temp
  474. }
  475. }
  476. item := new(CalculateItems)
  477. item.EdbInfoId = v.EdbInfoId
  478. item.DataMap = dataMap
  479. }
  480. formulaMap := CheckFormula(formulaStr)
  481. addSql := ` INSERT INTO edb_data_calculate(edb_info_id,edb_code,data_time,value,create_time,modify_time,status,data_timestamp) values `
  482. nowStr := time.Now().Format(utils.FormatDateTime)
  483. var isAdd bool
  484. for sk, sv := range saveDataMap {
  485. formulaStr = strings.ToUpper(formulaStr)
  486. formulaFormStr := ReplaceFormula(edbInfoIdArr, sv, formulaMap, formulaStr, edbInfoIdBytes)
  487. if formulaFormStr != "" {
  488. utils.FileLog.Info("formulaFormStr:%s", formulaFormStr)
  489. expression := formula.NewExpression(formulaFormStr)
  490. calResult, err := expression.Evaluate()
  491. if err != nil {
  492. err = errors.New("计算失败:Err:" + err.Error() + ";formulaStr:" + formulaFormStr)
  493. fmt.Println(err)
  494. return err
  495. }
  496. calVal, err := calResult.Float64()
  497. if err != nil {
  498. err = errors.New("计算失败:获取计算值失败 Err:" + err.Error() + ";formulaStr:" + formulaFormStr)
  499. fmt.Println(err)
  500. return err
  501. }
  502. count, err := data_manage.GetEdbDataCalculateByCodeAndDate(edbCode, sk)
  503. if err != nil && err.Error() != utils.ErrNoRow() {
  504. return err
  505. }
  506. if count <= 0 { //需要存入的数据
  507. dataTime, _ := time.Parse(utils.FormatDate, sk)
  508. timestamp := dataTime.UnixNano() / 1e6
  509. timeStr := fmt.Sprintf("%d", timestamp)
  510. addSql += "("
  511. addSql += strconv.Itoa(edbInfoId) + "," + "'" + edbCode + "'" + "," + "'" + sk + "'" + "," + utils.SubFloatToString(calVal, 4) + "," + "'" + nowStr + "'" +
  512. "," + "'" + nowStr + "'" + "," + "1"
  513. addSql += "," + "'" + timeStr + "'"
  514. addSql += "),"
  515. isAdd = true
  516. } else {
  517. calVal = utils.FixFloat(calVal, 4)
  518. err = data_manage.ModifyEdbDataCalculate(int64(edbInfoId), sk, calVal)
  519. if err != nil {
  520. return err
  521. }
  522. }
  523. }
  524. }
  525. if isAdd {
  526. addSql = strings.TrimRight(addSql, ",")
  527. data_manage.AddEdbDataCalculateBySql(addSql)
  528. if err != nil {
  529. fmt.Println("AddEdbDataCalculate Err:" + err.Error())
  530. return err
  531. }
  532. }
  533. return
  534. }
  535. // 处理整个数据
  536. func handleDateSaveDataMap(dateList []string, realSaveDataMap, saveDataMap map[string]map[int]float64, edbInfoIdArr []*data_manage.EdbInfo) {
  537. for _, date := range dateList {
  538. tmpDataMap := realSaveDataMap[date]
  539. for _, edbInfo := range edbInfoIdArr {
  540. tmpEdbInfoId := edbInfo.EdbInfoId // 当前指标id
  541. // 如果该日期不存在该指标数据,那么需要找寻前后日期的数据,进行填补
  542. if _, ok := tmpDataMap[tmpEdbInfoId]; !ok {
  543. //day := 0
  544. //switch edbInfo.Frequency {
  545. //case "周度":
  546. // day = 7
  547. //case "旬度":
  548. // day = 15
  549. //case "月度":
  550. // day = 30
  551. //case "季度":
  552. // day = 90
  553. //case "年度":
  554. // day = 365
  555. //}
  556. // 需求池 255 指标运算文案修改,补数据遍历区间修改(2023-3-7 09:37:23修改)
  557. day := 35
  558. handleDateDataMap(realSaveDataMap, saveDataMap, date, tmpEdbInfoId, day)
  559. }
  560. }
  561. }
  562. }
  563. // handleDataMap 处理单个日期的数据
  564. func handleDateDataMap(realSaveDataMap, saveDataMap map[string]map[int]float64, date string, edbInfoId, day int) {
  565. currDate, _ := time.ParseInLocation(utils.FormatDate, date, time.Local)
  566. // 后一天
  567. nextDateDayStr := currDate.AddDate(0, 0, 1).Format(utils.FormatDate)
  568. // 前一天
  569. preDateDayStr := currDate.AddDate(0, 0, -1).Format(utils.FormatDate)
  570. for i := 1; i <= day; i++ {
  571. // 下个日期的数据
  572. {
  573. if i >= 1 {
  574. nextDateDayStr = currDate.AddDate(0, 0, i).Format(utils.FormatDate)
  575. }
  576. if findDataMap, hasFindDataMap := realSaveDataMap[nextDateDayStr]; hasFindDataMap { // 下一个日期有数据
  577. if val, hasFindItem := findDataMap[edbInfoId]; hasFindItem {
  578. saveDataMap[date][edbInfoId] = val
  579. return
  580. }
  581. }
  582. }
  583. // 上个日期的数据
  584. {
  585. if i >= 1 {
  586. preDateDayStr = currDate.AddDate(0, 0, -i).Format(utils.FormatDate)
  587. }
  588. if findDataMap, hasFindDataMap := realSaveDataMap[preDateDayStr]; hasFindDataMap { // 下一个日期有数据
  589. if val, hasFindItem := findDataMap[edbInfoId]; hasFindItem {
  590. saveDataMap[date][edbInfoId] = val
  591. return
  592. }
  593. }
  594. }
  595. }
  596. }
  597. // handleDataByLinearRegression 插值法补充数据(线性方程式)
  598. func handleDataByLinearRegression(edbInfoDataList []*data_manage.EdbDataList, handleDataMap map[string]float64) (err error) {
  599. if len(edbInfoDataList) < 2 {
  600. return
  601. }
  602. var startEdbInfoData *data_manage.EdbDataList
  603. for _, v := range edbInfoDataList {
  604. handleDataMap[v.DataTime] = v.Value
  605. // 第一个数据就给过滤了,给后面的试用
  606. if startEdbInfoData == nil {
  607. startEdbInfoData = v
  608. continue
  609. }
  610. // 获取两条数据之间相差的天数
  611. startDataTime, _ := time.ParseInLocation(utils.FormatDate, startEdbInfoData.DataTime, time.Local)
  612. currDataTime, _ := time.ParseInLocation(utils.FormatDate, v.DataTime, time.Local)
  613. betweenHour := int(currDataTime.Sub(startDataTime).Hours())
  614. betweenDay := betweenHour / 24
  615. // 如果相差一天,那么过滤
  616. if betweenDay <= 1 {
  617. startEdbInfoData = v
  618. continue
  619. }
  620. // 生成线性方程式
  621. var a, b float64
  622. {
  623. coordinateData := make([]utils.Coordinate, 0)
  624. tmpCoordinate1 := utils.Coordinate{
  625. X: 1,
  626. Y: startEdbInfoData.Value,
  627. }
  628. coordinateData = append(coordinateData, tmpCoordinate1)
  629. tmpCoordinate2 := utils.Coordinate{
  630. X: float64(betweenDay) + 1,
  631. Y: v.Value,
  632. }
  633. coordinateData = append(coordinateData, tmpCoordinate2)
  634. a, b = utils.GetLinearResult(coordinateData)
  635. if math.IsNaN(a) || math.IsNaN(b) {
  636. err = errors.New("线性方程公式生成失败")
  637. return
  638. }
  639. }
  640. // 生成对应的值
  641. {
  642. for i := 1; i < betweenDay; i++ {
  643. tmpDataTime := startDataTime.AddDate(0, 0, i)
  644. aDecimal := decimal.NewFromFloat(a)
  645. xDecimal := decimal.NewFromInt(int64(i) + 1)
  646. bDecimal := decimal.NewFromFloat(b)
  647. val, _ := aDecimal.Mul(xDecimal).Add(bDecimal).Round(4).Float64()
  648. handleDataMap[tmpDataTime.Format(utils.FormatDate)] = val
  649. }
  650. }
  651. startEdbInfoData = v
  652. }
  653. return
  654. }
  655. // CallCalculateComputeCorrelation 调用计算拟合残差的相关系数
  656. func CallCalculateComputeCorrelation(data *data_manage.EdbInfoCalculateBatchSaveReqByEdbLib) (val string, err error, errMsg string) {
  657. errMsg = "计算失败"
  658. // 调用指标库去更新
  659. reqJson, err := json.Marshal(data)
  660. if err != nil {
  661. errMsg = "计算相关系数参数解析异常!"
  662. err = errors.New("参数解析失败,Err:" + err.Error())
  663. return
  664. }
  665. respItem, err := CalculateComputeCorrelation(string(reqJson))
  666. if err != nil {
  667. return
  668. }
  669. if respItem.Ret == 200 {
  670. val = respItem.Data
  671. }
  672. return
  673. }