base_from_calculate.go 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  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. if dk == 0 {
  126. dKey := edbInfoIdBytes[dk]
  127. if _, ok := formulaMap[dKey]; ok { //公式中存在
  128. if val, valOk := valArr[dv.EdbInfoId]; valOk { //值存在
  129. dvStr := fmt.Sprintf("%v", val)
  130. formulaStr = strings.Replace(formulaStr, dKey, dvStr, -1)
  131. replaceCount++
  132. } else {
  133. fmt.Println("valArr not found:", valArr, valOk)
  134. }
  135. } else {
  136. fmt.Println("formulaMap not found:", dKey, dk)
  137. }
  138. }
  139. if dk == 1 {
  140. dKey := edbInfoIdBytes[dk]
  141. if _, ok := formulaMap[dKey]; ok { //公式中存在
  142. if val, valOk := valArr[dv.EdbInfoId]; valOk { //值存在
  143. dvStr := fmt.Sprintf("%v", val)
  144. formulaStr = strings.Replace(formulaStr, dKey, dvStr, -1)
  145. replaceCount++
  146. } else {
  147. fmt.Println("valArr not found:", valArr, valOk)
  148. }
  149. } else {
  150. fmt.Println("formulaMap not found:", dKey, dk)
  151. }
  152. }
  153. if dk == 2 {
  154. dKey := edbInfoIdBytes[dk]
  155. if _, ok := formulaMap[dKey]; ok { //公式中存在
  156. if val, valOk := valArr[dv.EdbInfoId]; valOk { //值存在
  157. dvStr := fmt.Sprintf("%v", val)
  158. formulaStr = strings.Replace(formulaStr, dKey, dvStr, -1)
  159. replaceCount++
  160. }
  161. }
  162. }
  163. if dk == 3 {
  164. dKey := edbInfoIdBytes[dk]
  165. if _, ok := formulaMap[dKey]; ok { //公式中存在
  166. if val, valOk := valArr[dv.EdbInfoId]; valOk { //值存在
  167. dvStr := fmt.Sprintf("%v", val)
  168. formulaStr = strings.Replace(formulaStr, dKey, dvStr, -1)
  169. replaceCount++
  170. }
  171. }
  172. }
  173. if dk == 4 {
  174. dKey := edbInfoIdBytes[dk]
  175. if _, ok := formulaMap[dKey]; ok { //公式中存在
  176. if val, valOk := valArr[dv.EdbInfoId]; valOk { //值存在
  177. dvStr := fmt.Sprintf("%v", val)
  178. formulaStr = strings.Replace(formulaStr, dKey, dvStr, -1)
  179. replaceCount++
  180. }
  181. }
  182. }
  183. if dk == 5 {
  184. dKey := edbInfoIdBytes[dk]
  185. if _, ok := formulaMap[dKey]; ok { //公式中存在
  186. if val, valOk := valArr[dv.EdbInfoId]; valOk { //值存在
  187. dvStr := fmt.Sprintf("%v", val)
  188. formulaStr = strings.Replace(formulaStr, dKey, dvStr, -1)
  189. replaceCount++
  190. }
  191. }
  192. }
  193. if dk == 6 {
  194. dKey := edbInfoIdBytes[dk]
  195. if _, ok := formulaMap[dKey]; ok { //公式中存在
  196. if val, valOk := valArr[dv.EdbInfoId]; valOk { //值存在
  197. dvStr := fmt.Sprintf("%v", val)
  198. formulaStr = strings.Replace(formulaStr, dKey, dvStr, -1)
  199. replaceCount++
  200. }
  201. }
  202. }
  203. if dk == 7 {
  204. dKey := edbInfoIdBytes[dk]
  205. if _, ok := formulaMap[dKey]; ok { //公式中存在
  206. if val, valOk := valArr[dv.EdbInfoId]; valOk { //值存在
  207. dvStr := fmt.Sprintf("%v", val)
  208. formulaStr = strings.Replace(formulaStr, dKey, dvStr, -1)
  209. replaceCount++
  210. }
  211. }
  212. }
  213. if dk == 8 {
  214. dKey := edbInfoIdBytes[dk]
  215. if _, ok := formulaMap[dKey]; ok { //公式中存在
  216. if val, valOk := valArr[dv.EdbInfoId]; valOk { //值存在
  217. dvStr := fmt.Sprintf("%v", val)
  218. formulaStr = strings.Replace(formulaStr, dKey, dvStr, -1)
  219. replaceCount++
  220. }
  221. }
  222. }
  223. }
  224. for k, v := range funMap {
  225. formulaStr = strings.Replace(formulaStr, v, k, -1)
  226. }
  227. if replaceCount == len(formulaMap) {
  228. return formulaStr
  229. } else {
  230. return ""
  231. }
  232. }
  233. // GetFormulaMap 获取计算公式的map
  234. func GetFormulaMap() map[string]string {
  235. funMap := make(map[string]string)
  236. funMap["MAX"] = "[@@]"
  237. funMap["MIN"] = "[@!]"
  238. funMap["ABS"] = "[@#]"
  239. funMap["CEIL"] = "[@$]"
  240. funMap["COS"] = "[@%]"
  241. funMap["FLOOR"] = "[@^]"
  242. funMap["MOD"] = "[@&]"
  243. funMap["POW"] = "[@*]"
  244. funMap["ROUND"] = "[@(]"
  245. return funMap
  246. }
  247. // EdbInfoCalculateBatchSaveReq 计算指标的请求参数
  248. type EdbInfoCalculateBatchSaveReq struct {
  249. AdminId int `description:"添加人id"`
  250. AdminName string `description:"添加人名称"`
  251. EdbInfoId int `description:"指标id"`
  252. EdbName string `description:"指标名称"`
  253. Frequency string `description:"频度"`
  254. Unit string `description:"单位"`
  255. ClassifyId int `description:"分类id"`
  256. Formula string `description:"N值/移动天数"`
  257. FromEdbInfoId int `description:"计算来源指标id"`
  258. Source int `description:"来源:1:同花顺,2:wind,3:彭博,4:指标运算,5:累计值转月,6:同比值,7:同差值,8:N数值移动平均计算,12:环比值,13:环差值,14:变频"`
  259. CalculateFormula string `description:"计算公式"`
  260. EdbInfoIdArr []struct {
  261. EdbInfoId int `description:"指标id"`
  262. FromTag string `description:"指标对应标签"`
  263. }
  264. MoveType int `description:"移动方式:1:领先(默认),2:滞后"`
  265. MoveFrequency string `description:"移动频度:天/周/月/季/年"`
  266. }