edb_info_calculate.go 25 KB

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