edb_info_calculate.go 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682
  1. package data_manage
  2. import (
  3. "errors"
  4. "fmt"
  5. "github.com/beego/beego/v2/client/orm"
  6. "github.com/yidane/formula"
  7. "eta/eta_api/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. Calendar string `description:"公历/农历"`
  19. EdbInfoIdArr []EdbInfoFromTag `description:"指标信息"`
  20. }
  21. // EdbInfoFromTag 计算指标的关联指标
  22. type EdbInfoFromTag struct {
  23. EdbInfoId int `description:"指标id"`
  24. FromTag string `description:"指标对应标签"`
  25. MoveValue int `description:"移动的值"`
  26. }
  27. type EdbInfoCalculate struct {
  28. EdbInfoCalculateId int `orm:"column(edb_info_calculate_id);pk"`
  29. EdbInfoId int `description:"指标id"`
  30. EdbCode string `description:"指标编码"`
  31. FromEdbInfoId int `description:"计算指标id"`
  32. FromEdbCode string `description:"计算指标编码"`
  33. FromEdbName string `description:"计算指标名称"`
  34. FromSource int `description:"计算指标来源"`
  35. FromSourceName string `description:"计算指标来源名称"`
  36. FromTag string `description:"来源指标标签"`
  37. Sort int `description:"计算指标名称排序"`
  38. CreateTime time.Time `description:"创建时间"`
  39. ModifyTime time.Time `description:"修改时间"`
  40. }
  41. func AddEdbInfoCalculateMulti(items []*EdbInfoCalculate) (err error) {
  42. o := orm.NewOrmUsingDB("data")
  43. _, err = o.InsertMulti(1, items)
  44. return
  45. }
  46. type FromEdbInfoData struct {
  47. EdbInfoId int `description:"指标id"`
  48. EdbName string `description:"指标名称"`
  49. DataList []*EdbInfoSearchData
  50. }
  51. type EdbInfoCalculateDetail struct {
  52. EdbInfoCalculateId int `orm:"column(edb_info_calculate_id);pk"`
  53. EdbInfoId int `description:"指标id"`
  54. EdbCode string `description:"指标编码"`
  55. FromEdbInfoId int `description:"计算指标id"`
  56. FromEdbCode string `description:"计算指标编码"`
  57. FromEdbName string `description:"计算指标名称"`
  58. FromSource int `description:"计算指标来源"`
  59. FromSourceName string `description:"计算指标来源名称"`
  60. FromTag string `description:"来源指标标签"`
  61. MoveValue int `description:"移动的值,小于0是提前,0是不变,大于0是滞后"`
  62. Sort int `description:"计算指标名称排序"`
  63. CreateTime time.Time `description:"创建时间"`
  64. ModifyTime time.Time `description:"修改时间"`
  65. StartDate string `description:"开始日期"`
  66. EndDate string `description:"结束日期"`
  67. EdbType int `description:"指标类型:1:基础指标,2:计算指标"`
  68. }
  69. func GetEdbInfoCalculateDetail(edbInfoId, source int) (list []*EdbInfoCalculateDetail, err error) {
  70. o := orm.NewOrmUsingDB("data")
  71. //calculateTableName := GetEdbInfoCalculateTableName(source)
  72. //sql := ` SELECT a.*,b.start_date,b.end_date,b.edb_type FROM %s 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. //
  76. //sql = fmt.Sprintf(sql, calculateTableName)
  77. sql := ` SELECT a.edb_info_calculate_mapping_id,a.edb_info_id,a.source,a.source_name,a.edb_code,a.from_edb_info_id,a.from_edb_code,a.from_source,a.from_source_name,a.sort,a.create_time,a.modify_time,a.from_tag,a.move_value,b.edb_name_source as from_edb_name,b.start_date,b.end_date,b.edb_type FROM edb_info_calculate_mapping AS a
  78. INNER JOIN edb_info AS b ON a.from_edb_info_id=b.edb_info_id
  79. WHERE a.edb_info_id=? ORDER BY sort ASC `
  80. _, err = o.Raw(sql, edbInfoId).QueryRows(&list)
  81. return
  82. }
  83. type CalculateDetailResp struct {
  84. EdbInfoDetail *EdbInfo
  85. CalculateList []*EdbInfoCalculateDetail
  86. }
  87. type EdbInfoCalculateEditReq struct {
  88. EdbInfoId int `description:"指标id"`
  89. EdbName string `description:"指标名称"`
  90. Frequency string `description:"频率"`
  91. Unit string `description:"单位"`
  92. ClassifyId int `description:"分类id"`
  93. CalculateFormula string `description:"计算公式"`
  94. EdbInfoIdArr []EdbInfoFromTag `description:"指标信息"`
  95. }
  96. func GetCalculateEdbInfo(edbInfoId int) (from_edb_info_id string, err error) {
  97. o := orm.NewOrmUsingDB("data")
  98. sql := ` SELECT GROUP_CONCAT(from_edb_info_id ORDER BY sort ASC SEPARATOR ',') AS from_edb_info_id
  99. FROM edb_info_calculate_mapping
  100. WHERE edb_info_id=?
  101. GROUP BY edb_info_id
  102. `
  103. err = o.Raw(sql, edbInfoId).QueryRow(&from_edb_info_id)
  104. return
  105. }
  106. func DeleteCalculateEdbInfo(edbInfoId int) (err error) {
  107. o := orm.NewOrmUsingDB("data")
  108. to, err := o.Begin()
  109. if err != nil {
  110. return
  111. }
  112. defer func() {
  113. if err != nil {
  114. _ = to.Rollback()
  115. } else {
  116. _ = to.Commit()
  117. }
  118. }()
  119. sql := `DELETE FROM edb_data_calculate WHERE edb_info_id=?`
  120. _, err = to.Raw(sql, edbInfoId).Exec()
  121. if err != nil {
  122. return
  123. }
  124. sql = `DELETE FROM edb_info_calculate_mapping WHERE edb_info_id=?`
  125. _, err = to.Raw(sql, edbInfoId).Exec()
  126. return
  127. }
  128. func DeleteCalculateData(edbInfoId int) (err error) {
  129. o := orm.NewOrmUsingDB("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 []EdbInfoFromTag `description:"指标信息"`
  145. MoveType int `description:"移动方式:1:领先(默认),2:滞后"`
  146. MoveFrequency string `description:"移动频度:天/周/月/季/年"`
  147. Calendar string `description:"公历/农历"`
  148. }
  149. type EdbInfoCalculateBatchEditReq struct {
  150. EdbName string `description:"指标名称"`
  151. Frequency string `description:"频度"`
  152. Unit string `description:"单位"`
  153. ClassifyId int `description:"分类id"`
  154. Formula string `description:"N值"`
  155. EdbInfoId int `description:"编辑指标id"`
  156. FromEdbInfoId int `description:"计算来源指标id"`
  157. Source int `description:"来源:1:同花顺,2:wind,3:彭博,4:指标运算,5:累计值转月,6:同比值,7:同差值,8:N数值移动平均计算,12:环比值,13:环差值,14:变频"`
  158. MoveType int `description:"移动方式:1:领先(默认),2:滞后"`
  159. MoveFrequency string `description:"移动频度:天/周/月/季/年"`
  160. Calendar string `description:"公历/农历"`
  161. EdbInfoIdArr []EdbInfoFromTag
  162. }
  163. // EdbInfoCalculateBatchSaveReqByEdbLib edb指标库的请求逻辑
  164. type EdbInfoCalculateBatchSaveReqByEdbLib struct {
  165. AdminId int `description:"添加人id"`
  166. AdminName string `description:"添加人名称"`
  167. EdbInfoId int `description:"指标id"`
  168. EdbName string `description:"指标名称"`
  169. Frequency string `description:"频度"`
  170. Unit string `description:"单位"`
  171. ClassifyId int `description:"分类id"`
  172. Formula string `description:"N值/移动天数"`
  173. FromEdbInfoId int `description:"计算来源指标id"`
  174. Source int `description:"来源:1:同花顺,2:wind,3:彭博,4:指标运算,5:累计值转月,6:同比值,7:同差值,8:N数值移动平均计算,12:环比值,13:环差值,14:变频"`
  175. CalculateFormula string `description:"计算公式"`
  176. EdbInfoIdArr []EdbInfoFromTag `description:"指标信息"`
  177. MoveType int `description:"移动方式:1:领先(默认),2:滞后"`
  178. MoveFrequency string `description:"移动频度:天/周/月/季/年"`
  179. Calendar string `description:"公历/农历"`
  180. }
  181. // EdbInfoCalculateBatchEditReq 编辑计算指标的请求参数
  182. type EdbInfoCalculateBatchEditReqByEdbLib struct {
  183. EdbName string `description:"指标名称"`
  184. Frequency string `description:"频度"`
  185. Unit string `description:"单位"`
  186. ClassifyId int `description:"分类id"`
  187. Formula string `description:"N值"`
  188. EdbInfoId int `description:"编辑指标id"`
  189. FromEdbInfoId int `description:"计算来源指标id"`
  190. Source int `description:"来源:1:同花顺,2:wind,3:彭博,4:指标运算,5:累计值转月,6:同比值,7:同差值,8:N数值移动平均计算,12:环比值,13:环差值,14:变频"`
  191. MoveType int `description:"移动方式:1:领先(默认),2:滞后"`
  192. MoveFrequency string `description:"移动频度:天/周/月/季/年"`
  193. Calendar string `description:"公历/农历"`
  194. EdbInfoIdArr []EdbInfoFromTag
  195. }
  196. func GetEdbInfoCalculateMap(edbInfoId, source int) (list []*EdbInfo, err error) {
  197. o := orm.NewOrmUsingDB("data")
  198. //calculateTableName := GetEdbInfoCalculateTableName(source)
  199. //sql := ` SELECT b.* FROM %s AS a
  200. // INNER JOIN edb_info AS b ON a.from_edb_info_id=b.edb_info_id
  201. // WHERE a.edb_info_id=? ORDER BY sort ASC `
  202. //sql = fmt.Sprintf(sql, calculateTableName)
  203. sql := ` SELECT b.* FROM edb_info_calculate_mapping AS a
  204. INNER JOIN edb_info AS b ON a.from_edb_info_id=b.edb_info_id
  205. WHERE a.edb_info_id=? ORDER BY sort ASC `
  206. _, err = o.Raw(sql, edbInfoId).QueryRows(&list)
  207. return
  208. }
  209. // 指标运算
  210. func AddCalculate(req *EdbInfoCalculateBatchSaveReq, fromEdbInfo []*EdbInfoCalculateDetail, edbCode, uniqueCode, formulaStr string, sysUserId int, sysUserRealName string) (edbInfoId int, err error) {
  211. o := orm.NewOrmUsingDB("data")
  212. to, err := o.Begin()
  213. if err != nil {
  214. return
  215. }
  216. defer func() {
  217. if err != nil {
  218. _ = to.Rollback()
  219. } else {
  220. _ = to.Commit()
  221. }
  222. }()
  223. if req.EdbInfoId <= 0 {
  224. edbInfo := new(EdbInfo)
  225. edbInfo.Source = utils.DATA_SOURCE_CALCULATE
  226. edbInfo.SourceName = "指标运算"
  227. edbInfo.EdbCode = edbCode
  228. edbInfo.EdbName = req.EdbName
  229. edbInfo.EdbNameSource = req.EdbName
  230. edbInfo.Frequency = req.Frequency
  231. edbInfo.Unit = req.Unit
  232. edbInfo.ClassifyId = req.ClassifyId
  233. edbInfo.SysUserId = sysUserId
  234. edbInfo.SysUserRealName = sysUserRealName
  235. edbInfo.CreateTime = time.Now()
  236. edbInfo.ModifyTime = time.Now()
  237. edbInfo.UniqueCode = uniqueCode
  238. edbInfo.CalculateFormula = req.CalculateFormula
  239. edbInfo.EdbType = 2
  240. newEdbInfoId, err := to.Insert(edbInfo)
  241. if err != nil {
  242. return int(newEdbInfoId), err
  243. }
  244. edbInfoId = int(newEdbInfoId)
  245. //处理同名指标
  246. {
  247. edbNameList, err := GetEdbInfoByName(req.EdbName)
  248. if err != nil {
  249. return edbInfoId, err
  250. }
  251. if len(edbNameList) >= 2 {
  252. for _, v := range edbNameList {
  253. edbName := v.EdbName + "(" + v.SourceName + ")"
  254. err = ModifyEdbInfoNameSource(edbName, v.EdbInfoId)
  255. if err != nil {
  256. return edbInfoId, err
  257. }
  258. }
  259. }
  260. }
  261. //calculateList := make([]*EdbInfoCalculate, 0)
  262. edbInfoList := make([]*EdbInfo, 0)
  263. for _, v := range req.EdbInfoIdArr {
  264. edbInfo, err := GetEdbInfoById(v.EdbInfoId)
  265. if err != nil {
  266. return edbInfoId, err
  267. }
  268. //calculateItem := new(EdbInfoCalculate)
  269. //calculateItem.CreateTime = time.Now()
  270. //calculateItem.ModifyTime = time.Now()
  271. //calculateItem.Sort = k + 1
  272. //calculateItem.EdbCode = edbCode
  273. //calculateItem.EdbInfoId = int(edbInfoId)
  274. //calculateItem.FromEdbInfoId = edbInfo.EdbInfoId
  275. //calculateItem.FromEdbCode = edbInfo.EdbCode
  276. //calculateItem.FromEdbName = edbInfo.EdbName
  277. //calculateItem.FromSource = edbInfo.Source
  278. //calculateItem.FromSourceName = edbInfo.SourceName
  279. //calculateItem.FromTag = v.FromTag
  280. //calculateList = append(calculateList, calculateItem)
  281. edbInfoList = append(edbInfoList, edbInfo)
  282. }
  283. //if len(calculateList) > 0 {
  284. // _, err = to.InsertMulti(1, calculateList)
  285. // if err != nil {
  286. // return edbInfoId, err
  287. // }
  288. //}
  289. } else {
  290. edbInfoId = req.EdbInfoId
  291. dataTableName := GetEdbDataTableName(utils.DATA_SOURCE_CALCULATE)
  292. deleteSql := ` DELETE FROM %s WHERE edb_info_id=? `
  293. deleteSql = fmt.Sprintf(deleteSql, dataTableName)
  294. _, err = to.Raw(deleteSql, req.EdbInfoId).Exec()
  295. }
  296. //计算数据
  297. saveDataMap := make(map[string]map[int]float64)
  298. for _, v := range fromEdbInfo {
  299. var condition string
  300. var pars []interface{}
  301. condition += " AND edb_info_id=? "
  302. pars = append(pars, v.FromEdbInfoId)
  303. dataList, err := GetEdbDataListAll(condition, pars, v.FromSource, 1)
  304. if err != nil {
  305. return edbInfoId, err
  306. }
  307. dataMap := make(map[string]float64)
  308. for _, dv := range dataList {
  309. if val, ok := saveDataMap[dv.DataTime]; ok {
  310. if _, ok := val[v.EdbInfoId]; !ok {
  311. val[v.EdbInfoId] = dv.Value
  312. }
  313. } else {
  314. temp := make(map[int]float64)
  315. temp[v.EdbInfoId] = dv.Value
  316. saveDataMap[dv.DataTime] = temp
  317. }
  318. }
  319. item := new(CalculateItems)
  320. item.EdbInfoId = v.EdbInfoId
  321. item.DataMap = dataMap
  322. }
  323. //formulaMap := CheckFormula(formulaStr)
  324. //addSql := ` INSERT INTO edb_data_calculate(edb_info_id,edb_code,data_time,value,create_time,modify_time,status,data_timestamp) values `
  325. //nowStr := time.Now().Format(utils.FormatDateTime)
  326. //var isAdd bool
  327. //
  328. //for sk, sv := range saveDataMap {
  329. // formulaStr = strings.ToUpper(formulaStr)
  330. // formulaFormStr := ReplaceFormula(edbInfoIdArr, sv, formulaMap, formulaStr, edbInfoIdBytes)
  331. // if formulaFormStr != "" {
  332. // utils.FileLog.Info("formulaFormStr:%s", formulaFormStr)
  333. // expression := formula.NewExpression(formulaFormStr)
  334. // calResult, err := expression.Evaluate()
  335. // if err != nil {
  336. // err = errors.New("计算失败:Err:" + err.Error() + ";formulaStr:" + formulaFormStr)
  337. // fmt.Println(err)
  338. // return err
  339. // }
  340. // calVal, err := calResult.Float64()
  341. // if err != nil {
  342. // err = errors.New("计算失败:获取计算值失败 Err:" + err.Error() + ";formulaStr:" + formulaFormStr)
  343. // fmt.Println(err)
  344. // return err
  345. // }
  346. //
  347. // //需要存入的数据
  348. // {
  349. // dataTime, _ := time.Parse(utils.FormatDate, sk)
  350. // timestamp := dataTime.UnixNano() / 1e6
  351. // timeStr := fmt.Sprintf("%d", timestamp)
  352. // addSql += "("
  353. // addSql += strconv.Itoa(edbInfoId) + "," + "'" + edbCode + "'" + "," + "'" + sk + "'" + "," + utils.SubFloatToString(calVal, 4) + "," + "'" + nowStr + "'" +
  354. // "," + "'" + nowStr + "'" + "," + "1"
  355. // addSql += "," + "'" + timeStr + "'"
  356. // addSql += "),"
  357. // isAdd = true
  358. // }
  359. // }
  360. //}
  361. //if isAdd {
  362. // addSql = strings.TrimRight(addSql, ",")
  363. // AddEdbDataCalculateBySql(addSql)
  364. // if err != nil {
  365. // fmt.Println("AddEdbDataCalculate Err:" + err.Error())
  366. // return
  367. // }
  368. //}
  369. return
  370. }
  371. type CalculateItems struct {
  372. EdbInfoId int
  373. DataMap map[string]float64
  374. }
  375. func CheckFormula(formula string) map[string]string {
  376. mathFormula := []string{"MAX", "MIN", "ABS", "ACOS", "ASIN", "CEIL", "MOD", "POW", "ROUND", "SIGN", "SIN", "TAN", "LOG10", "LOG2", "LOG", "LN"}
  377. str := strings.ToUpper(formula)
  378. for _, v := range mathFormula {
  379. str = strings.Replace(str, v, "", -1)
  380. }
  381. str = strings.Replace(str, "(", "", -1)
  382. str = strings.Replace(str, ")", "", -1)
  383. byteMap := make(map[string]string)
  384. for i := 0; i < len(str); i++ {
  385. byteInt := str[i]
  386. if byteInt >= 65 && byteInt <= 90 {
  387. byteStr := string(byteInt)
  388. if _, ok := byteMap[byteStr]; !ok {
  389. byteMap[byteStr] = byteStr
  390. }
  391. }
  392. }
  393. return byteMap
  394. }
  395. func ReplaceFormula(edbInfoIdArr []*EdbInfo, valArr map[int]float64, formulaMap map[string]string, formulaStr string, edbInfoIdBytes []string) string {
  396. funMap := GetFormulaMap()
  397. for k, v := range funMap {
  398. formulaStr = strings.Replace(formulaStr, k, v, -1)
  399. }
  400. replaceCount := 0
  401. for dk, dv := range edbInfoIdArr {
  402. if dk == 0 {
  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 == 1 {
  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 == 2 {
  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 == 3 {
  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 == 4 {
  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 == 5 {
  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. if dk == 6 {
  463. dKey := edbInfoIdBytes[dk]
  464. if _, ok := formulaMap[dKey]; ok { //公式中存在
  465. if val, valOk := valArr[dv.EdbInfoId]; valOk { //值存在
  466. dvStr := fmt.Sprintf("%v", val)
  467. formulaStr = strings.Replace(formulaStr, dKey, dvStr, -1)
  468. replaceCount++
  469. }
  470. }
  471. }
  472. if dk == 7 {
  473. dKey := edbInfoIdBytes[dk]
  474. if _, ok := formulaMap[dKey]; ok { //公式中存在
  475. if val, valOk := valArr[dv.EdbInfoId]; valOk { //值存在
  476. dvStr := fmt.Sprintf("%v", val)
  477. formulaStr = strings.Replace(formulaStr, dKey, dvStr, -1)
  478. replaceCount++
  479. }
  480. }
  481. }
  482. if dk == 8 {
  483. dKey := edbInfoIdBytes[dk]
  484. if _, ok := formulaMap[dKey]; ok { //公式中存在
  485. if val, valOk := valArr[dv.EdbInfoId]; valOk { //值存在
  486. dvStr := fmt.Sprintf("%v", val)
  487. formulaStr = strings.Replace(formulaStr, dKey, dvStr, -1)
  488. replaceCount++
  489. }
  490. }
  491. }
  492. }
  493. for k, v := range funMap {
  494. formulaStr = strings.Replace(formulaStr, v, k, -1)
  495. }
  496. if replaceCount == len(formulaMap) {
  497. return formulaStr
  498. } else {
  499. return ""
  500. }
  501. }
  502. func GetFormulaMap() map[string]string {
  503. funMap := make(map[string]string)
  504. funMap["MAX"] = "[@@]"
  505. funMap["MIN"] = "[@!]"
  506. funMap["ABS"] = "[@#]"
  507. funMap["CEIL"] = "[@$]"
  508. funMap["COS"] = "[@%]"
  509. funMap["FLOOR"] = "[@^]"
  510. funMap["MOD"] = "[@&]"
  511. funMap["POW"] = "[@*]"
  512. funMap["ROUND"] = "[@(]"
  513. return funMap
  514. }
  515. func Calculate(edbInfoIdArr []*EdbInfo, edbInfoId int, edbCode, formulaStr string, edbInfoIdBytes []string) (err error) {
  516. defer func() {
  517. if err != nil {
  518. utils.FileLog.Info("Calculate Err:%s" + err.Error())
  519. }
  520. }()
  521. saveDataMap := make(map[string]map[int]float64)
  522. for _, v := range edbInfoIdArr {
  523. var condition string
  524. var pars []interface{}
  525. condition += " AND edb_info_id=? "
  526. pars = append(pars, v.EdbInfoId)
  527. dataList, err := GetEdbDataListAll(condition, pars, v.Source, 1)
  528. if err != nil {
  529. return err
  530. }
  531. dataMap := make(map[string]float64)
  532. for _, dv := range dataList {
  533. if val, ok := saveDataMap[dv.DataTime]; ok {
  534. if _, ok := val[v.EdbInfoId]; !ok {
  535. val[v.EdbInfoId] = dv.Value
  536. }
  537. } else {
  538. temp := make(map[int]float64)
  539. temp[v.EdbInfoId] = dv.Value
  540. saveDataMap[dv.DataTime] = temp
  541. }
  542. }
  543. item := new(CalculateItems)
  544. item.EdbInfoId = v.EdbInfoId
  545. item.DataMap = dataMap
  546. }
  547. formulaMap := CheckFormula(formulaStr)
  548. addSql := ` INSERT INTO edb_data_calculate(edb_info_id,edb_code,data_time,value,create_time,modify_time,status,data_timestamp) values `
  549. nowStr := time.Now().Format(utils.FormatDateTime)
  550. var isAdd bool
  551. for sk, sv := range saveDataMap {
  552. formulaStr = strings.ToUpper(formulaStr)
  553. formulaFormStr := ReplaceFormula(edbInfoIdArr, sv, formulaMap, formulaStr, edbInfoIdBytes)
  554. if formulaStr == "" {
  555. return
  556. }
  557. if formulaFormStr != "" {
  558. utils.FileLog.Info("formulaFormStr:%s", formulaFormStr)
  559. expression := formula.NewExpression(formulaFormStr)
  560. calResult, err := expression.Evaluate()
  561. if err != nil {
  562. err = errors.New("计算失败:Err:" + err.Error() + ";formulaStr:" + formulaFormStr)
  563. fmt.Println(err)
  564. return err
  565. }
  566. calVal, err := calResult.Float64()
  567. if err != nil {
  568. err = errors.New("计算失败:获取计算值失败 Err:" + err.Error() + ";formulaStr:" + formulaFormStr)
  569. fmt.Println(err)
  570. return err
  571. }
  572. //需要存入的数据
  573. {
  574. dataTime, _ := time.Parse(utils.FormatDate, sk)
  575. timestamp := dataTime.UnixNano() / 1e6
  576. timeStr := fmt.Sprintf("%d", timestamp)
  577. addSql += "("
  578. addSql += strconv.Itoa(edbInfoId) + "," + "'" + edbCode + "'" + "," + "'" + sk + "'" + "," + utils.SubFloatToString(calVal, 4) + "," + "'" + nowStr + "'" +
  579. "," + "'" + nowStr + "'" + "," + "1"
  580. addSql += "," + "'" + timeStr + "'"
  581. addSql += "),"
  582. isAdd = true
  583. }
  584. } else {
  585. fmt.Println("formulaFormStr is empty")
  586. }
  587. }
  588. if isAdd {
  589. addSql = strings.TrimRight(addSql, ",")
  590. AddEdbDataCalculateBySql(addSql)
  591. if err != nil {
  592. fmt.Println("AddEdbDataCalculate Err:" + err.Error())
  593. return err
  594. }
  595. }
  596. return
  597. }
  598. // SaveAdjustEdbInfoReq 保存数据调整请求参数(请求指标服务)
  599. type SaveAdjustEdbInfoReq struct {
  600. AdminId int `description:"添加人id"`
  601. AdminName string `description:"添加人名称"`
  602. EdbInfoId int `description:"指标id"`
  603. FromEdbInfoId int `description:"来源指标id"`
  604. EdbName string `description:"指标名称"`
  605. Frequency string `description:"频度"`
  606. Unit string `description:"单位"`
  607. ClassifyId int `description:"分类id"`
  608. DataList []SaveAdjustEdbDataReq `description:"指标对应的数据值"`
  609. }
  610. // SaveAdjustEdbDataReq 保存数据调整请求的数据的参数
  611. type SaveAdjustEdbDataReq struct {
  612. Date string `description:"数据日期"`
  613. Value float64 `description:"数据值"`
  614. }
  615. // BatchEdbInfoCalculateBatchSaveReq 批量添加 计算指标
  616. type BatchEdbInfoCalculateBatchSaveReq struct {
  617. CalculateId string `description:"当前请求时,单个计算的唯一标识"`
  618. CalculateInfo EdbInfoCalculateBatchSaveReq
  619. }
  620. // BatchEdbInfoCalculateBatchSaveResp 批量添加 计算指标 返回数据
  621. type BatchEdbInfoCalculateBatchSaveResp struct {
  622. Fail []BatchEdbInfoCalculateBatchSaveFailResp `description:"添加失败的指标"`
  623. Success []BatchEdbInfoCalculateBatchSaveSuccessResp `description:"添加成功的指标"`
  624. }
  625. // BatchEdbInfoCalculateBatchSaveFailResp 添加失败的指标信息
  626. type BatchEdbInfoCalculateBatchSaveFailResp struct {
  627. CalculateId string `description:"当前请求时,单个计算的唯一标识"`
  628. Msg string `description:"用户提示信息"`
  629. ErrMsg string `description:"错误信息,内部查看"`
  630. }
  631. // BatchEdbInfoCalculateBatchSaveSuccessResp 添加成功的指标信息
  632. type BatchEdbInfoCalculateBatchSaveSuccessResp struct {
  633. ClassifyId int `description:"分类id"`
  634. CalculateId string `description:"当前请求时,单个计算的唯一标识"`
  635. EdbInfoId int `description:"指标ID"`
  636. UniqueCode string `description:"指标唯一编码"`
  637. }