base_from_calculate.go 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. package models
  2. import (
  3. "errors"
  4. "fmt"
  5. "github.com/beego/beego/v2/client/orm"
  6. "github.com/yidane/formula"
  7. "hongze/hongze_edb_lib/services"
  8. "hongze/hongze_edb_lib/utils"
  9. "strconv"
  10. "strings"
  11. "time"
  12. )
  13. // EdbInfoCalculateSaveReq 计算(运算)指标请求参数
  14. type EdbInfoCalculateSaveReq struct {
  15. AdminId int `description:"添加人id"`
  16. AdminName string `description:"添加人名称"`
  17. EdbName string `description:"指标名称"`
  18. Frequency string `description:"频率"`
  19. Unit string `description:"单位"`
  20. ClassifyId int `description:"分类id"`
  21. CalculateFormula string `description:"计算公式"`
  22. EdbInfoIdArr []struct {
  23. EdbInfoId int `description:"指标id"`
  24. FromTag string `description:"指标对应标签"`
  25. }
  26. }
  27. // CalculateItems 计算(运算)指标信息
  28. type CalculateItems struct {
  29. EdbInfoId int
  30. DataMap map[string]float64
  31. }
  32. // AddCalculate 新增计算(运算)指标
  33. func AddCalculate(edbInfoIdArr []*EdbInfo, edbInfoId int, edbCode, formulaStr string, edbInfoIdBytes []string) (err error) {
  34. o := orm.NewOrm()
  35. defer func() {
  36. if err != nil {
  37. utils.FileLog.Info("Calculate Err:%s" + err.Error())
  38. }
  39. }()
  40. saveDataMap := make(map[string]map[int]float64)
  41. for _, v := range edbInfoIdArr {
  42. var condition string
  43. var pars []interface{}
  44. condition += " AND edb_info_id=? "
  45. pars = append(pars, v.EdbInfoId)
  46. dataList, err := GetEdbDataListAll(condition, pars, v.Source, 1)
  47. if err != nil {
  48. return err
  49. }
  50. dataMap := make(map[string]float64)
  51. for _, dv := range dataList {
  52. if val, ok := saveDataMap[dv.DataTime]; ok {
  53. if _, ok := val[v.EdbInfoId]; !ok {
  54. val[v.EdbInfoId] = dv.Value
  55. }
  56. } else {
  57. temp := make(map[int]float64)
  58. temp[v.EdbInfoId] = dv.Value
  59. saveDataMap[dv.DataTime] = temp
  60. }
  61. }
  62. item := new(CalculateItems)
  63. item.EdbInfoId = v.EdbInfoId
  64. item.DataMap = dataMap
  65. }
  66. formulaMap := services.CheckFormula(formulaStr)
  67. addSql := ` INSERT INTO edb_data_calculate(edb_info_id,edb_code,data_time,value,create_time,modify_time,status,data_timestamp) values `
  68. nowStr := time.Now().Format(utils.FormatDateTime)
  69. var isAdd bool
  70. for sk, sv := range saveDataMap {
  71. formulaStr = strings.ToUpper(formulaStr)
  72. formulaFormStr := ReplaceFormula(edbInfoIdArr, sv, formulaMap, formulaStr, edbInfoIdBytes)
  73. if formulaStr == "" {
  74. return
  75. }
  76. if formulaFormStr != "" {
  77. expression := formula.NewExpression(formulaFormStr)
  78. calResult, err := expression.Evaluate()
  79. if err != nil {
  80. err = errors.New("计算失败:Err:" + err.Error() + ";formulaStr:" + formulaFormStr)
  81. fmt.Println(err)
  82. return err
  83. }
  84. calVal, err := calResult.Float64()
  85. if err != nil {
  86. err = errors.New("计算失败:获取计算值失败 Err:" + err.Error() + ";formulaStr:" + formulaFormStr)
  87. fmt.Println(err)
  88. return err
  89. }
  90. //需要存入的数据
  91. {
  92. dataTime, _ := time.Parse(utils.FormatDate, sk)
  93. timestamp := dataTime.UnixNano() / 1e6
  94. timeStr := fmt.Sprintf("%d", timestamp)
  95. addSql += "("
  96. addSql += strconv.Itoa(edbInfoId) + "," + "'" + edbCode + "'" + "," + "'" + sk + "'" + "," + utils.SubFloatToString(calVal, 4) + "," + "'" + nowStr + "'" +
  97. "," + "'" + nowStr + "'" + "," + "1"
  98. addSql += "," + "'" + timeStr + "'"
  99. addSql += "),"
  100. isAdd = true
  101. }
  102. } else {
  103. fmt.Println("formulaFormStr is empty")
  104. }
  105. }
  106. if isAdd {
  107. addSql = strings.TrimRight(addSql, ",")
  108. _, err = o.Raw(addSql).Exec()
  109. if err != nil {
  110. fmt.Println("AddEdbDataCalculate Err:" + err.Error())
  111. //errMsg = " tx.Exec Err :" + err.Error()
  112. return
  113. }
  114. }
  115. return
  116. }
  117. // ReplaceFormula 替换计算方式
  118. func ReplaceFormula(edbInfoIdArr []*EdbInfo, valArr map[int]float64, formulaMap map[string]string, formulaStr string, edbInfoIdBytes []string) string {
  119. funMap := GetFormulaMap()
  120. for k, v := range funMap {
  121. formulaStr = strings.Replace(formulaStr, k, v, -1)
  122. }
  123. replaceCount := 0
  124. for dk, dv := range edbInfoIdArr {
  125. var isReplace bool
  126. formulaStr, isReplace = GetFormulaReplace(dk, dv.EdbInfoId, formulaStr, edbInfoIdBytes, formulaMap, valArr)
  127. if isReplace {
  128. replaceCount++
  129. }
  130. }
  131. for k, v := range funMap {
  132. formulaStr = strings.Replace(formulaStr, v, k, -1)
  133. }
  134. if replaceCount == len(formulaMap) {
  135. return formulaStr
  136. } else {
  137. return ""
  138. }
  139. }
  140. func GetFormulaReplace(index, edbInfoId int, formulaStr string, edbInfoIdBytes []string, formulaMap map[string]string, valArr map[int]float64) (formulaResult string, isReplace bool) {
  141. dKey := edbInfoIdBytes[index]
  142. if _, ok := formulaMap[dKey]; ok { //公式中存在
  143. if val, valOk := valArr[edbInfoId]; valOk { //值存在
  144. dvStr := fmt.Sprintf("%v", val)
  145. formulaResult = strings.Replace(formulaStr, dKey, dvStr, -1)
  146. isReplace = true
  147. }
  148. }
  149. return
  150. }
  151. // GetFormulaMap 获取计算公式的map
  152. func GetFormulaMap() map[string]string {
  153. funMap := make(map[string]string)
  154. funMap["MAX"] = "[@@]"
  155. funMap["MIN"] = "[@!]"
  156. funMap["ABS"] = "[@#]"
  157. funMap["CEIL"] = "[@$]"
  158. funMap["COS"] = "[@%]"
  159. funMap["FLOOR"] = "[@^]"
  160. funMap["MOD"] = "[@&]"
  161. funMap["POW"] = "[@*]"
  162. funMap["ROUND"] = "[@(]"
  163. return funMap
  164. }
  165. // EdbInfoCalculateBatchSaveReq 添加计算指标的请求参数
  166. type EdbInfoCalculateBatchSaveReq struct {
  167. AdminId int `description:"添加人id"`
  168. AdminName string `description:"添加人名称"`
  169. EdbInfoId int `description:"指标id"`
  170. EdbName string `description:"指标名称"`
  171. Frequency string `description:"频度"`
  172. Unit string `description:"单位"`
  173. ClassifyId int `description:"分类id"`
  174. Formula string `description:"N值/移动天数"`
  175. FromEdbInfoId int `description:"计算来源指标id"`
  176. Source int `description:"来源:1:同花顺,2:wind,3:彭博,4:指标运算,5:累计值转月,6:同比值,7:同差值,8:N数值移动平均计算,12:环比值,13:环差值,14:变频"`
  177. CalculateFormula string `description:"计算公式"`
  178. EdbInfoIdArr []struct {
  179. EdbInfoId int `description:"指标id"`
  180. FromTag string `description:"指标对应标签"`
  181. }
  182. MoveType int `description:"移动方式:1:领先(默认),2:滞后"`
  183. MoveFrequency string `description:"移动频度:天/周/月/季/年"`
  184. }
  185. // EdbInfoCalculateBatchEditReq 编辑计算指标的请求参数
  186. type EdbInfoCalculateBatchEditReq struct {
  187. EdbName string `description:"指标名称"`
  188. Frequency string `description:"频度"`
  189. Unit string `description:"单位"`
  190. ClassifyId int `description:"分类id"`
  191. Formula string `description:"N值"`
  192. EdbInfoId int `description:"编辑指标id"`
  193. FromEdbInfoId int `description:"计算来源指标id"`
  194. Source int `description:"来源:1:同花顺,2:wind,3:彭博,4:指标运算,5:累计值转月,6:同比值,7:同差值,8:N数值移动平均计算,12:环比值,13:环差值,14:变频"`
  195. MoveType int `description:"移动方式:1:领先(默认),2:滞后"`
  196. MoveFrequency string `description:"移动频度:天/周/月/季/年"`
  197. EdbInfoIdArr []struct {
  198. EdbInfoId int `description:"指标id"`
  199. FromTag string `description:"指标对应标签"`
  200. }
  201. }