edb_info_calculate.go 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596
  1. package data_manage
  2. import (
  3. "errors"
  4. "fmt"
  5. "github.com/beego/beego/v2/client/orm"
  6. "github.com/yidane/formula"
  7. "hongze/hongze_chart_lib/utils"
  8. "strconv"
  9. "strings"
  10. "time"
  11. )
  12. type EdbInfoCalculateSaveReq struct {
  13. EdbName string `description:"指标名称"`
  14. Frequency string `description:"频率"`
  15. Unit string `description:"单位"`
  16. ClassifyId int `description:"分类id"`
  17. CalculateFormula string `description:"计算公式"`
  18. EdbInfoIdArr []struct {
  19. EdbInfoId int `description:"指标id"`
  20. FromTag string `description:"指标对应标签"`
  21. }
  22. }
  23. type EdbInfoCalculate struct {
  24. EdbInfoCalculateId int `orm:"column(edb_info_calculate_id);pk"`
  25. EdbInfoId int `description:"指标id"`
  26. EdbCode string `description:"指标编码"`
  27. FromEdbInfoId int `description:"计算指标id"`
  28. FromEdbCode string `description:"计算指标编码"`
  29. FromEdbName string `description:"计算指标名称"`
  30. FromSource int `description:"计算指标来源"`
  31. FromSourceName string `description:"计算指标来源名称"`
  32. FromTag string `description:"来源指标标签"`
  33. Sort int `description:"计算指标名称排序"`
  34. CreateTime time.Time `description:"创建时间"`
  35. ModifyTime time.Time `description:"修改时间"`
  36. }
  37. func AddEdbInfoCalculateMulti(items []*EdbInfoCalculate) (err error) {
  38. o := orm.NewOrmUsingDB("data")
  39. _, err = o.InsertMulti(1, items)
  40. return
  41. }
  42. type FromEdbInfoData struct {
  43. EdbInfoId int `description:"指标id"`
  44. EdbName string `description:"指标名称"`
  45. DataList []*EdbInfoSearchData
  46. }
  47. type EdbInfoCalculateDetail struct {
  48. EdbInfoCalculateId int `orm:"column(edb_info_calculate_id);pk"`
  49. EdbInfoId int `description:"指标id"`
  50. EdbCode string `description:"指标编码"`
  51. FromEdbInfoId int `description:"计算指标id"`
  52. FromEdbCode string `description:"计算指标编码"`
  53. FromEdbName string `description:"计算指标名称"`
  54. FromSource int `description:"计算指标来源"`
  55. FromSourceName string `description:"计算指标来源名称"`
  56. FromTag string `description:"来源指标标签"`
  57. Sort int `description:"计算指标名称排序"`
  58. CreateTime time.Time `description:"创建时间"`
  59. ModifyTime time.Time `description:"修改时间"`
  60. StartDate string `description:"开始日期"`
  61. EndDate string `description:"结束日期"`
  62. EdbType int `description:"指标类型:1:基础指标,2:计算指标"`
  63. }
  64. func GetEdbInfoCalculateDetail(edbInfoId, source int) (list []*EdbInfoCalculateDetail, err error) {
  65. o := orm.NewOrmUsingDB("data")
  66. //calculateTableName := GetEdbInfoCalculateTableName(source)
  67. //sql := ` SELECT a.*,b.start_date,b.end_date,b.edb_type FROM %s AS a
  68. // INNER JOIN edb_info AS b ON a.from_edb_info_id=b.edb_info_id
  69. // WHERE a.edb_info_id=? ORDER BY sort ASC `
  70. //
  71. //sql = fmt.Sprintf(sql, calculateTableName)
  72. sql := ` SELECT a.*,b.start_date,b.end_date,b.edb_type FROM edb_info_calculate_mapping AS a
  73. INNER JOIN edb_info AS b ON a.from_edb_info_id=b.edb_info_id
  74. WHERE a.edb_info_id=? ORDER BY sort ASC `
  75. _, err = o.Raw(sql, edbInfoId).QueryRows(&list)
  76. return
  77. }
  78. type CalculateDetailResp struct {
  79. EdbInfoDetail *EdbInfo
  80. CalculateList []*EdbInfoCalculateDetail
  81. }
  82. type EdbInfoCalculateEditReq struct {
  83. EdbInfoId int `description:"指标id"`
  84. EdbName string `description:"指标名称"`
  85. Frequency string `description:"频率"`
  86. Unit string `description:"单位"`
  87. ClassifyId int `description:"分类id"`
  88. CalculateFormula string `description:"计算公式"`
  89. EdbInfoIdArr []struct {
  90. EdbInfoId int `description:"指标id"`
  91. FromTag string `description:"指标对应标签"`
  92. }
  93. }
  94. func GetCalculateEdbInfo(edbInfoId int) (from_edb_info_id string, err error) {
  95. o := orm.NewOrmUsingDB("data")
  96. sql := ` SELECT GROUP_CONCAT(from_edb_info_id ORDER BY sort ASC SEPARATOR ',') AS from_edb_info_id
  97. FROM edb_info_calculate_mapping
  98. WHERE edb_info_id=?
  99. GROUP BY edb_info_id
  100. `
  101. err = o.Raw(sql, edbInfoId).QueryRow(&from_edb_info_id)
  102. return
  103. }
  104. func DeleteCalculateEdbInfo(edbInfoId int) (err error) {
  105. o := orm.NewOrmUsingDB("data")
  106. defer func() {
  107. if err != nil {
  108. o.Rollback()
  109. } else {
  110. o.Commit()
  111. }
  112. }()
  113. o.Begin()
  114. sql := `DELETE FROM edb_data_calculate WHERE edb_info_id=?`
  115. _, err = o.Raw(sql, edbInfoId).Exec()
  116. if err != nil {
  117. return
  118. }
  119. sql = `DELETE FROM edb_info_calculate_mapping WHERE edb_info_id=?`
  120. o.Raw(sql, edbInfoId).Exec()
  121. return
  122. }
  123. func DeleteCalculateData(edbInfoId int) (err error) {
  124. o := orm.NewOrmUsingDB("data")
  125. sql := `DELETE FROM edb_data_calculate WHERE edb_info_id=?`
  126. _, err = o.Raw(sql, edbInfoId).Exec()
  127. return
  128. }
  129. type EdbInfoCalculateBatchSaveReq struct {
  130. EdbInfoId int `description:"指标id"`
  131. EdbName string `description:"指标名称"`
  132. Frequency string `description:"频度"`
  133. Unit string `description:"单位"`
  134. ClassifyId int `description:"分类id"`
  135. Formula string `description:"N值/移动天数"`
  136. FromEdbInfoId int `description:"计算来源指标id"`
  137. Source int `description:"来源:1:同花顺,2:wind,3:彭博,4:指标运算,5:累计值转月,6:同比值,7:同差值,8:N数值移动平均计算,12:环比值,13:环差值,14:变频"`
  138. CalculateFormula string `description:"计算公式"`
  139. EdbInfoIdArr []struct {
  140. EdbInfoId int `description:"指标id"`
  141. FromTag string `description:"指标对应标签"`
  142. }
  143. MoveType int `description:"移动方式:1:领先(默认),2:滞后"`
  144. MoveFrequency string `description:"移动频度:天/周/月/季/年"`
  145. }
  146. type EdbInfoCalculateBatchEditReq struct {
  147. EdbName string `description:"指标名称"`
  148. Frequency string `description:"频度"`
  149. Unit string `description:"单位"`
  150. ClassifyId int `description:"分类id"`
  151. Formula string `description:"N值"`
  152. EdbInfoId int `description:"编辑指标id"`
  153. FromEdbInfoId int `description:"计算来源指标id"`
  154. Source int `description:"来源:1:同花顺,2:wind,3:彭博,4:指标运算,5:累计值转月,6:同比值,7:同差值,8:N数值移动平均计算,12:环比值,13:环差值,14:变频"`
  155. MoveType int `description:"移动方式:1:领先(默认),2:滞后"`
  156. MoveFrequency string `description:"移动频度:天/周/月/季/年"`
  157. EdbInfoIdArr []struct {
  158. EdbInfoId int `description:"指标id"`
  159. FromTag string `description:"指标对应标签"`
  160. }
  161. }
  162. func GetEdbInfoCalculateMap(edbInfoId, source int) (list []*EdbInfo, err error) {
  163. o := orm.NewOrmUsingDB("data")
  164. //calculateTableName := GetEdbInfoCalculateTableName(source)
  165. //sql := ` SELECT b.* FROM %s AS a
  166. // INNER JOIN edb_info AS b ON a.from_edb_info_id=b.edb_info_id
  167. // WHERE a.edb_info_id=? ORDER BY sort ASC `
  168. //sql = fmt.Sprintf(sql, calculateTableName)
  169. sql := ` SELECT b.* FROM edb_info_calculate_mapping AS a
  170. INNER JOIN edb_info AS b ON a.from_edb_info_id=b.edb_info_id
  171. WHERE a.edb_info_id=? ORDER BY sort ASC `
  172. _, err = o.Raw(sql, edbInfoId).QueryRows(&list)
  173. return
  174. }
  175. //指标运算
  176. func AddCalculate(req *EdbInfoCalculateBatchSaveReq, fromEdbInfo []*EdbInfoCalculateDetail, edbCode, uniqueCode, formulaStr string, sysUserId int, sysUserRealName string) (edbInfoId int, err error) {
  177. o := orm.NewOrmUsingDB("data")
  178. o.Begin()
  179. defer func() {
  180. if err != nil {
  181. o.Rollback()
  182. } else {
  183. o.Commit()
  184. }
  185. }()
  186. if req.EdbInfoId <= 0 {
  187. edbInfo := new(EdbInfo)
  188. edbInfo.Source = utils.DATA_SOURCE_CALCULATE
  189. edbInfo.SourceName = "指标运算"
  190. edbInfo.EdbCode = edbCode
  191. edbInfo.EdbName = req.EdbName
  192. edbInfo.EdbNameSource = req.EdbName
  193. edbInfo.Frequency = req.Frequency
  194. edbInfo.Unit = req.Unit
  195. edbInfo.ClassifyId = req.ClassifyId
  196. edbInfo.SysUserId = sysUserId
  197. edbInfo.SysUserRealName = sysUserRealName
  198. edbInfo.CreateTime = time.Now()
  199. edbInfo.ModifyTime = time.Now()
  200. edbInfo.UniqueCode = uniqueCode
  201. edbInfo.CalculateFormula = req.CalculateFormula
  202. edbInfo.EdbType = 2
  203. newEdbInfoId, err := o.Insert(edbInfo)
  204. if err != nil {
  205. return int(newEdbInfoId), err
  206. }
  207. edbInfoId = int(newEdbInfoId)
  208. //处理同名指标
  209. {
  210. edbNameList, err := GetEdbInfoByName(req.EdbName)
  211. if err != nil {
  212. return edbInfoId, err
  213. }
  214. if len(edbNameList) >= 2 {
  215. for _, v := range edbNameList {
  216. edbName := v.EdbName + "(" + v.SourceName + ")"
  217. err = ModifyEdbInfoNameSource(edbName, v.EdbInfoId)
  218. if err != nil {
  219. return edbInfoId, err
  220. }
  221. }
  222. }
  223. }
  224. //calculateList := make([]*EdbInfoCalculate, 0)
  225. edbInfoList := make([]*EdbInfo, 0)
  226. for _, v := range req.EdbInfoIdArr {
  227. edbInfo, err := GetEdbInfoById(v.EdbInfoId)
  228. if err != nil {
  229. return edbInfoId, err
  230. }
  231. //calculateItem := new(EdbInfoCalculate)
  232. //calculateItem.CreateTime = time.Now()
  233. //calculateItem.ModifyTime = time.Now()
  234. //calculateItem.Sort = k + 1
  235. //calculateItem.EdbCode = edbCode
  236. //calculateItem.EdbInfoId = int(edbInfoId)
  237. //calculateItem.FromEdbInfoId = edbInfo.EdbInfoId
  238. //calculateItem.FromEdbCode = edbInfo.EdbCode
  239. //calculateItem.FromEdbName = edbInfo.EdbName
  240. //calculateItem.FromSource = edbInfo.Source
  241. //calculateItem.FromSourceName = edbInfo.SourceName
  242. //calculateItem.FromTag = v.FromTag
  243. //calculateList = append(calculateList, calculateItem)
  244. edbInfoList = append(edbInfoList, edbInfo)
  245. }
  246. //if len(calculateList) > 0 {
  247. // _, err = o.InsertMulti(1, calculateList)
  248. // if err != nil {
  249. // return edbInfoId, err
  250. // }
  251. //}
  252. } else {
  253. edbInfoId = req.EdbInfoId
  254. dataTableName := GetEdbDataTableName(utils.DATA_SOURCE_CALCULATE)
  255. deleteSql := ` DELETE FROM %s WHERE edb_info_id=? `
  256. deleteSql = fmt.Sprintf(deleteSql, dataTableName)
  257. _, err = o.Raw(deleteSql, req.EdbInfoId).Exec()
  258. }
  259. //计算数据
  260. saveDataMap := make(map[string]map[int]float64)
  261. for _, v := range fromEdbInfo {
  262. var condition string
  263. var pars []interface{}
  264. condition += " AND edb_info_id=? "
  265. pars = append(pars, v.FromEdbInfoId)
  266. dataList, err := GetEdbDataListAll(condition, pars, v.FromSource, 1)
  267. if err != nil {
  268. return edbInfoId, err
  269. }
  270. dataMap := make(map[string]float64)
  271. for _, dv := range dataList {
  272. if val, ok := saveDataMap[dv.DataTime]; ok {
  273. if _, ok := val[v.EdbInfoId]; !ok {
  274. val[v.EdbInfoId] = dv.Value
  275. }
  276. } else {
  277. temp := make(map[int]float64)
  278. temp[v.EdbInfoId] = dv.Value
  279. saveDataMap[dv.DataTime] = temp
  280. }
  281. }
  282. item := new(CalculateItems)
  283. item.EdbInfoId = v.EdbInfoId
  284. item.DataMap = dataMap
  285. }
  286. //formulaMap := CheckFormula(formulaStr)
  287. //addSql := ` INSERT INTO edb_data_calculate(edb_info_id,edb_code,data_time,value,create_time,modify_time,status,data_timestamp) values `
  288. //nowStr := time.Now().Format(utils.FormatDateTime)
  289. //var isAdd bool
  290. //
  291. //for sk, sv := range saveDataMap {
  292. // formulaStr = strings.ToUpper(formulaStr)
  293. // formulaFormStr := ReplaceFormula(edbInfoIdArr, sv, formulaMap, formulaStr, edbInfoIdBytes)
  294. // if formulaFormStr != "" {
  295. // utils.FileLog.Info("formulaFormStr:%s", formulaFormStr)
  296. // expression := formula.NewExpression(formulaFormStr)
  297. // calResult, err := expression.Evaluate()
  298. // if err != nil {
  299. // err = errors.New("计算失败:Err:" + err.Error() + ";formulaStr:" + formulaFormStr)
  300. // fmt.Println(err)
  301. // return err
  302. // }
  303. // calVal, err := calResult.Float64()
  304. // if err != nil {
  305. // err = errors.New("计算失败:获取计算值失败 Err:" + err.Error() + ";formulaStr:" + formulaFormStr)
  306. // fmt.Println(err)
  307. // return err
  308. // }
  309. //
  310. // //需要存入的数据
  311. // {
  312. // dataTime, _ := time.Parse(utils.FormatDate, sk)
  313. // timestamp := dataTime.UnixNano() / 1e6
  314. // timeStr := fmt.Sprintf("%d", timestamp)
  315. // addSql += "("
  316. // addSql += strconv.Itoa(edbInfoId) + "," + "'" + edbCode + "'" + "," + "'" + sk + "'" + "," + utils.SubFloatToString(calVal, 4) + "," + "'" + nowStr + "'" +
  317. // "," + "'" + nowStr + "'" + "," + "1"
  318. // addSql += "," + "'" + timeStr + "'"
  319. // addSql += "),"
  320. // isAdd = true
  321. // }
  322. // }
  323. //}
  324. //if isAdd {
  325. // addSql = strings.TrimRight(addSql, ",")
  326. // AddEdbDataCalculateBySql(addSql)
  327. // if err != nil {
  328. // fmt.Println("AddEdbDataCalculate Err:" + err.Error())
  329. // return
  330. // }
  331. //}
  332. return
  333. }
  334. type CalculateItems struct {
  335. EdbInfoId int
  336. DataMap map[string]float64
  337. }
  338. func CheckFormula(formula string) map[string]string {
  339. mathFormula := []string{"MAX", "MIN", "ABS", "ACOS", "ASIN", "CEIL", "MOD", "POW", "ROUND", "SIGN", "SIN", "TAN", "LOG10", "LOG2", "LOG"}
  340. str := strings.ToUpper(formula)
  341. for _, v := range mathFormula {
  342. str = strings.Replace(str, v, "", -1)
  343. }
  344. str = strings.Replace(str, "(", "", -1)
  345. str = strings.Replace(str, ")", "", -1)
  346. byteMap := make(map[string]string)
  347. for i := 0; i < len(str); i++ {
  348. byteInt := str[i]
  349. if byteInt >= 65 && byteInt <= 90 {
  350. byteStr := string(byteInt)
  351. if _, ok := byteMap[byteStr]; !ok {
  352. byteMap[byteStr] = byteStr
  353. }
  354. }
  355. }
  356. return byteMap
  357. }
  358. func ReplaceFormula(edbInfoIdArr []*EdbInfo, valArr map[int]float64, formulaMap map[string]string, formulaStr string, edbInfoIdBytes []string) string {
  359. funMap := GetFormulaMap()
  360. for k, v := range funMap {
  361. formulaStr = strings.Replace(formulaStr, k, v, -1)
  362. }
  363. replaceCount := 0
  364. for dk, dv := range edbInfoIdArr {
  365. if dk == 0 {
  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 == 1 {
  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 == 2 {
  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 == 3 {
  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 == 4 {
  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. if dk == 5 {
  416. dKey := edbInfoIdBytes[dk]
  417. if _, ok := formulaMap[dKey]; ok { //公式中存在
  418. if val, valOk := valArr[dv.EdbInfoId]; valOk { //值存在
  419. dvStr := fmt.Sprintf("%v", val)
  420. formulaStr = strings.Replace(formulaStr, dKey, dvStr, -1)
  421. replaceCount++
  422. }
  423. }
  424. }
  425. if dk == 6 {
  426. dKey := edbInfoIdBytes[dk]
  427. if _, ok := formulaMap[dKey]; ok { //公式中存在
  428. if val, valOk := valArr[dv.EdbInfoId]; valOk { //值存在
  429. dvStr := fmt.Sprintf("%v", val)
  430. formulaStr = strings.Replace(formulaStr, dKey, dvStr, -1)
  431. replaceCount++
  432. }
  433. }
  434. }
  435. if dk == 7 {
  436. dKey := edbInfoIdBytes[dk]
  437. if _, ok := formulaMap[dKey]; ok { //公式中存在
  438. if val, valOk := valArr[dv.EdbInfoId]; valOk { //值存在
  439. dvStr := fmt.Sprintf("%v", val)
  440. formulaStr = strings.Replace(formulaStr, dKey, dvStr, -1)
  441. replaceCount++
  442. }
  443. }
  444. }
  445. if dk == 8 {
  446. dKey := edbInfoIdBytes[dk]
  447. if _, ok := formulaMap[dKey]; ok { //公式中存在
  448. if val, valOk := valArr[dv.EdbInfoId]; valOk { //值存在
  449. dvStr := fmt.Sprintf("%v", val)
  450. formulaStr = strings.Replace(formulaStr, dKey, dvStr, -1)
  451. replaceCount++
  452. }
  453. }
  454. }
  455. }
  456. for k, v := range funMap {
  457. formulaStr = strings.Replace(formulaStr, v, k, -1)
  458. }
  459. if replaceCount == len(formulaMap) {
  460. return formulaStr
  461. } else {
  462. return ""
  463. }
  464. }
  465. func GetFormulaMap() map[string]string {
  466. funMap := make(map[string]string)
  467. funMap["MAX"] = "[@@]"
  468. funMap["MIN"] = "[@!]"
  469. funMap["ABS"] = "[@#]"
  470. funMap["CEIL"] = "[@$]"
  471. funMap["COS"] = "[@%]"
  472. funMap["FLOOR"] = "[@^]"
  473. funMap["MOD"] = "[@&]"
  474. funMap["POW"] = "[@*]"
  475. funMap["ROUND"] = "[@(]"
  476. return funMap
  477. }
  478. func Calculate(edbInfoIdArr []*EdbInfo, edbInfoId int, edbCode, formulaStr string, edbInfoIdBytes []string) (err error) {
  479. defer func() {
  480. if err != nil {
  481. utils.FileLog.Info("Calculate Err:%s" + err.Error())
  482. }
  483. }()
  484. saveDataMap := make(map[string]map[int]float64)
  485. for _, v := range edbInfoIdArr {
  486. var condition string
  487. var pars []interface{}
  488. condition += " AND edb_info_id=? "
  489. pars = append(pars, v.EdbInfoId)
  490. dataList, err := GetEdbDataListAll(condition, pars, v.Source, 1)
  491. if err != nil {
  492. return err
  493. }
  494. dataMap := make(map[string]float64)
  495. for _, dv := range dataList {
  496. if val, ok := saveDataMap[dv.DataTime]; ok {
  497. if _, ok := val[v.EdbInfoId]; !ok {
  498. val[v.EdbInfoId] = dv.Value
  499. }
  500. } else {
  501. temp := make(map[int]float64)
  502. temp[v.EdbInfoId] = dv.Value
  503. saveDataMap[dv.DataTime] = temp
  504. }
  505. }
  506. item := new(CalculateItems)
  507. item.EdbInfoId = v.EdbInfoId
  508. item.DataMap = dataMap
  509. }
  510. formulaMap := CheckFormula(formulaStr)
  511. addSql := ` INSERT INTO edb_data_calculate(edb_info_id,edb_code,data_time,value,create_time,modify_time,status,data_timestamp) values `
  512. nowStr := time.Now().Format(utils.FormatDateTime)
  513. var isAdd bool
  514. for sk, sv := range saveDataMap {
  515. formulaStr = strings.ToUpper(formulaStr)
  516. formulaFormStr := ReplaceFormula(edbInfoIdArr, sv, formulaMap, formulaStr, edbInfoIdBytes)
  517. if formulaStr == "" {
  518. return
  519. }
  520. if formulaFormStr != "" {
  521. utils.FileLog.Info("formulaFormStr:%s", formulaFormStr)
  522. expression := formula.NewExpression(formulaFormStr)
  523. calResult, err := expression.Evaluate()
  524. if err != nil {
  525. err = errors.New("计算失败:Err:" + err.Error() + ";formulaStr:" + formulaFormStr)
  526. fmt.Println(err)
  527. return err
  528. }
  529. calVal, err := calResult.Float64()
  530. if err != nil {
  531. err = errors.New("计算失败:获取计算值失败 Err:" + err.Error() + ";formulaStr:" + formulaFormStr)
  532. fmt.Println(err)
  533. return err
  534. }
  535. //需要存入的数据
  536. {
  537. dataTime, _ := time.Parse(utils.FormatDate, sk)
  538. timestamp := dataTime.UnixNano() / 1e6
  539. timeStr := fmt.Sprintf("%d", timestamp)
  540. addSql += "("
  541. addSql += strconv.Itoa(edbInfoId) + "," + "'" + edbCode + "'" + "," + "'" + sk + "'" + "," + utils.SubFloatToString(calVal, 4) + "," + "'" + nowStr + "'" +
  542. "," + "'" + nowStr + "'" + "," + "1"
  543. addSql += "," + "'" + timeStr + "'"
  544. addSql += "),"
  545. isAdd = true
  546. }
  547. } else {
  548. fmt.Println("formulaFormStr is empty")
  549. }
  550. }
  551. if isAdd {
  552. addSql = strings.TrimRight(addSql, ",")
  553. AddEdbDataCalculateBySql(addSql)
  554. if err != nil {
  555. fmt.Println("AddEdbDataCalculate Err:" + err.Error())
  556. return err
  557. }
  558. }
  559. return
  560. }