edb_info_calculate.go 19 KB

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