edb_info_calculate.go 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617
  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. EdbNameEn string `description:"英文指标名称"`
  152. Frequency string `description:"频度"`
  153. Unit string `description:"单位"`
  154. UnitEn string `description:"英文单位"`
  155. ClassifyId int `description:"分类id"`
  156. HaveOperaAuth bool `description:"是否有数据权限,默认:false"`
  157. }
  158. type CalculateEdbInfoItem struct {
  159. EdbInfoId int `description:"指标id"`
  160. EdbName string `description:"指标名称"`
  161. Frequency string `description:"频度"`
  162. Unit string `description:"单位"`
  163. ClassifyId int `description:"分类id"`
  164. CalculateId string `description:"当前请求时,单个计算的唯一标识"`
  165. FromEdbInfoId int `description:"计算来源指标id"`
  166. }
  167. type EdbInfoCalculateBatchSaveMultiReq struct {
  168. List []CalculateEdbInfoItem
  169. Formula string `description:"N值/移动天数"`
  170. FromEdbInfoId int `description:"计算来源指标id"`
  171. Source int `description:"来源:1:同花顺,2:wind,3:彭博,4:指标运算,5:累计值转月,6:同比值,7:同差值,8:N数值移动平均计算,12:环比值,13:环差值,14:变频"`
  172. CalculateFormula string `description:"计算公式"`
  173. EdbInfoIdArr []EdbInfoFromTag `description:"指标信息"`
  174. MoveType int `description:"移动方式:1:领先(默认),2:滞后"`
  175. MoveFrequency string `description:"移动频度:天/周/月/季/年"`
  176. Extra string `description:"指标的额外配置"`
  177. Calendar string `description:"公历/农历"`
  178. }
  179. type EdbInfoCalculateBatchSaveReq struct {
  180. EdbInfoId int `description:"指标id"`
  181. EdbName string `description:"指标名称"`
  182. Frequency string `description:"频度"`
  183. Unit string `description:"单位"`
  184. ClassifyId int `description:"分类id"`
  185. Formula string `description:"N值/移动天数"`
  186. FromEdbInfoId int `description:"计算来源指标id"`
  187. Source int `description:"来源:1:同花顺,2:wind,3:彭博,4:指标运算,5:累计值转月,6:同比值,7:同差值,8:N数值移动平均计算,12:环比值,13:环差值,14:变频"`
  188. CalculateFormula string `description:"计算公式"`
  189. EdbInfoIdArr []EdbInfoFromTag `description:"指标信息"`
  190. MoveType int `description:"移动方式:1:领先(默认),2:滞后"`
  191. MoveFrequency string `description:"移动频度:天/周/月/季/年"`
  192. Extra string `description:"指标的额外配置"`
  193. Calendar string `description:"公历/农历"`
  194. }
  195. type EdbInfoCalculateBatchEditReq struct {
  196. EdbName string `description:"指标名称"`
  197. Frequency string `description:"频度"`
  198. Unit string `description:"单位"`
  199. ClassifyId int `description:"分类id"`
  200. Formula string `description:"N值"`
  201. EdbInfoId int `description:"编辑指标id"`
  202. FromEdbInfoId int `description:"计算来源指标id"`
  203. Source int `description:"来源:1:同花顺,2:wind,3:彭博,4:指标运算,5:累计值转月,6:同比值,7:同差值,8:N数值移动平均计算,12:环比值,13:环差值,14:变频"`
  204. MoveType int `description:"移动方式:1:领先(默认),2:滞后"`
  205. MoveFrequency string `description:"移动频度:天/周/月/季/年"`
  206. Calendar string `description:"公历/农历"`
  207. Extra string `description:"指标的额外配置"`
  208. EdbInfoIdArr []EdbInfoFromTag
  209. }
  210. // EdbInfoCalculateBatchSaveReqByEdbLib edb指标库的请求逻辑
  211. type EdbInfoCalculateBatchSaveReqByEdbLib struct {
  212. AdminId int `description:"添加人id"`
  213. AdminName string `description:"添加人名称"`
  214. EdbInfoId int `description:"指标id"`
  215. EdbName string `description:"指标名称"`
  216. Frequency string `description:"频度"`
  217. Unit string `description:"单位"`
  218. ClassifyId int `description:"分类id"`
  219. Formula string `description:"N值/移动天数"`
  220. FromEdbInfoId int `description:"计算来源指标id"`
  221. Source int `description:"来源:1:同花顺,2:wind,3:彭博,4:指标运算,5:累计值转月,6:同比值,7:同差值,8:N数值移动平均计算,12:环比值,13:环差值,14:变频"`
  222. CalculateFormula string `description:"计算公式"`
  223. EdbInfoIdArr []EdbInfoFromTag `description:"指标信息"`
  224. MoveType int `description:"移动方式:1:领先(默认),2:滞后"`
  225. MoveFrequency string `description:"移动频度:天/周/月/季/年"`
  226. Calendar string `description:"公历/农历"`
  227. Data interface{} `description:"数据列"`
  228. EmptyType int `description:"空值处理类型(0查找前后35天,1不计算,2前值填充,3后值填充,4等于0)"`
  229. MaxEmptyType int `description:"MAX、MIN公式空值处理类型(1、等于0;2、跳过空值)"`
  230. Extra string `description:"指标的额外配置"`
  231. }
  232. // EdbInfoCalculateBatchEditReqByEdbLib 编辑计算指标的请求参数
  233. type EdbInfoCalculateBatchEditReqByEdbLib struct {
  234. EdbName string `description:"指标名称"`
  235. Frequency string `description:"频度"`
  236. Unit string `description:"单位"`
  237. ClassifyId int `description:"分类id"`
  238. Formula string `description:"N值"`
  239. EdbInfoId int `description:"编辑指标id"`
  240. FromEdbInfoId int `description:"计算来源指标id"`
  241. Source int `description:"来源:1:同花顺,2:wind,3:彭博,4:指标运算,5:累计值转月,6:同比值,7:同差值,8:N数值移动平均计算,12:环比值,13:环差值,14:变频"`
  242. MoveType int `description:"移动方式:1:领先(默认),2:滞后"`
  243. MoveFrequency string `description:"移动频度:天/周/月/季/年"`
  244. Calendar string `description:"公历/农历"`
  245. EdbInfoIdArr []EdbInfoFromTag
  246. Data interface{} `description:"数据列"`
  247. Extra string `description:"指标的额外配置"`
  248. }
  249. func GetEdbInfoCalculateMap(edbInfoId, source int) (list []*EdbInfo, err error) {
  250. o := orm.NewOrmUsingDB("data")
  251. //calculateTableName := GetEdbInfoCalculateTableName(source)
  252. //sql := ` SELECT b.* FROM %s AS a
  253. // INNER JOIN edb_info AS b ON a.from_edb_info_id=b.edb_info_id
  254. // WHERE a.edb_info_id=? ORDER BY sort ASC `
  255. //sql = fmt.Sprintf(sql, calculateTableName)
  256. sql := ` SELECT b.* FROM edb_info_calculate_mapping AS a
  257. INNER JOIN edb_info AS b ON a.from_edb_info_id=b.edb_info_id
  258. WHERE a.edb_info_id=? ORDER BY sort ASC `
  259. _, err = o.Raw(sql, edbInfoId).QueryRows(&list)
  260. return
  261. }
  262. type CalculateItems struct {
  263. EdbInfoId int
  264. DataMap map[string]float64
  265. }
  266. func CheckFormula(formula string) map[string]string {
  267. mathFormula := []string{"MAX", "MIN", "ABS", "ACOS", "ASIN", "CEIL", "MOD", "POW", "ROUND", "SIGN", "SIN", "TAN", "LOG10", "LOG2", "LOG", "LN"}
  268. str := strings.ToUpper(formula)
  269. for _, v := range mathFormula {
  270. str = strings.Replace(str, v, "", -1)
  271. }
  272. str = strings.Replace(str, "(", "", -1)
  273. str = strings.Replace(str, ")", "", -1)
  274. byteMap := make(map[string]string)
  275. for i := 0; i < len(str); i++ {
  276. byteInt := str[i]
  277. if byteInt >= 65 && byteInt <= 90 {
  278. byteStr := string(byteInt)
  279. if _, ok := byteMap[byteStr]; !ok {
  280. byteMap[byteStr] = byteStr
  281. }
  282. }
  283. }
  284. return byteMap
  285. }
  286. func ReplaceFormula(edbInfoIdArr []*EdbInfo, valArr map[int]float64, formulaMap map[string]string, formulaStr string, edbInfoIdBytes []string) string {
  287. funMap := GetFormulaMap()
  288. for k, v := range funMap {
  289. formulaStr = strings.Replace(formulaStr, k, v, -1)
  290. }
  291. replaceCount := 0
  292. for dk, dv := range edbInfoIdArr {
  293. if dk == 0 {
  294. dKey := edbInfoIdBytes[dk]
  295. if _, ok := formulaMap[dKey]; ok { //公式中存在
  296. if val, valOk := valArr[dv.EdbInfoId]; valOk { //值存在
  297. dvStr := fmt.Sprintf("%v", val)
  298. formulaStr = strings.Replace(formulaStr, dKey, dvStr, -1)
  299. replaceCount++
  300. }
  301. }
  302. }
  303. if dk == 1 {
  304. dKey := edbInfoIdBytes[dk]
  305. if _, ok := formulaMap[dKey]; ok { //公式中存在
  306. if val, valOk := valArr[dv.EdbInfoId]; valOk { //值存在
  307. dvStr := fmt.Sprintf("%v", val)
  308. formulaStr = strings.Replace(formulaStr, dKey, dvStr, -1)
  309. replaceCount++
  310. }
  311. }
  312. }
  313. if dk == 2 {
  314. dKey := edbInfoIdBytes[dk]
  315. if _, ok := formulaMap[dKey]; ok { //公式中存在
  316. if val, valOk := valArr[dv.EdbInfoId]; valOk { //值存在
  317. dvStr := fmt.Sprintf("%v", val)
  318. formulaStr = strings.Replace(formulaStr, dKey, dvStr, -1)
  319. replaceCount++
  320. }
  321. }
  322. }
  323. if dk == 3 {
  324. dKey := edbInfoIdBytes[dk]
  325. if _, ok := formulaMap[dKey]; ok { //公式中存在
  326. if val, valOk := valArr[dv.EdbInfoId]; valOk { //值存在
  327. dvStr := fmt.Sprintf("%v", val)
  328. formulaStr = strings.Replace(formulaStr, dKey, dvStr, -1)
  329. replaceCount++
  330. }
  331. }
  332. }
  333. if dk == 4 {
  334. dKey := edbInfoIdBytes[dk]
  335. if _, ok := formulaMap[dKey]; ok { //公式中存在
  336. if val, valOk := valArr[dv.EdbInfoId]; valOk { //值存在
  337. dvStr := fmt.Sprintf("%v", val)
  338. formulaStr = strings.Replace(formulaStr, dKey, dvStr, -1)
  339. replaceCount++
  340. }
  341. }
  342. }
  343. if dk == 5 {
  344. dKey := edbInfoIdBytes[dk]
  345. if _, ok := formulaMap[dKey]; ok { //公式中存在
  346. if val, valOk := valArr[dv.EdbInfoId]; valOk { //值存在
  347. dvStr := fmt.Sprintf("%v", val)
  348. formulaStr = strings.Replace(formulaStr, dKey, dvStr, -1)
  349. replaceCount++
  350. }
  351. }
  352. }
  353. if dk == 6 {
  354. dKey := edbInfoIdBytes[dk]
  355. if _, ok := formulaMap[dKey]; ok { //公式中存在
  356. if val, valOk := valArr[dv.EdbInfoId]; valOk { //值存在
  357. dvStr := fmt.Sprintf("%v", val)
  358. formulaStr = strings.Replace(formulaStr, dKey, dvStr, -1)
  359. replaceCount++
  360. }
  361. }
  362. }
  363. if dk == 7 {
  364. dKey := edbInfoIdBytes[dk]
  365. if _, ok := formulaMap[dKey]; ok { //公式中存在
  366. if val, valOk := valArr[dv.EdbInfoId]; valOk { //值存在
  367. dvStr := fmt.Sprintf("%v", val)
  368. formulaStr = strings.Replace(formulaStr, dKey, dvStr, -1)
  369. replaceCount++
  370. }
  371. }
  372. }
  373. if dk == 8 {
  374. dKey := edbInfoIdBytes[dk]
  375. if _, ok := formulaMap[dKey]; ok { //公式中存在
  376. if val, valOk := valArr[dv.EdbInfoId]; valOk { //值存在
  377. dvStr := fmt.Sprintf("%v", val)
  378. formulaStr = strings.Replace(formulaStr, dKey, dvStr, -1)
  379. replaceCount++
  380. }
  381. }
  382. }
  383. }
  384. for k, v := range funMap {
  385. formulaStr = strings.Replace(formulaStr, v, k, -1)
  386. }
  387. if replaceCount == len(formulaMap) {
  388. return formulaStr
  389. } else {
  390. return ""
  391. }
  392. }
  393. func GetFormulaMap() map[string]string {
  394. funMap := make(map[string]string)
  395. funMap["MAX"] = "[@@]"
  396. funMap["MIN"] = "[@!]"
  397. funMap["ABS"] = "[@#]"
  398. funMap["CEIL"] = "[@$]"
  399. funMap["COS"] = "[@%]"
  400. funMap["FLOOR"] = "[@^]"
  401. funMap["MOD"] = "[@&]"
  402. funMap["POW"] = "[@*]"
  403. funMap["ROUND"] = "[@(]"
  404. return funMap
  405. }
  406. func Calculate(edbInfoIdArr []*EdbInfo, edbInfoId int, edbCode, formulaStr string, edbInfoIdBytes []string) (err error) {
  407. defer func() {
  408. if err != nil {
  409. utils.FileLog.Info("Calculate Err:%s" + err.Error())
  410. }
  411. }()
  412. saveDataMap := make(map[string]map[int]float64)
  413. for _, v := range edbInfoIdArr {
  414. var condition string
  415. var pars []interface{}
  416. condition += " AND edb_info_id=? "
  417. pars = append(pars, v.EdbInfoId)
  418. dataList, err := GetEdbDataListAll(condition, pars, v.Source, v.SubSource, 1)
  419. if err != nil {
  420. return err
  421. }
  422. dataMap := make(map[string]float64)
  423. for _, dv := range dataList {
  424. if val, ok := saveDataMap[dv.DataTime]; ok {
  425. if _, ok := val[v.EdbInfoId]; !ok {
  426. val[v.EdbInfoId] = dv.Value
  427. }
  428. } else {
  429. temp := make(map[int]float64)
  430. temp[v.EdbInfoId] = dv.Value
  431. saveDataMap[dv.DataTime] = temp
  432. }
  433. }
  434. item := new(CalculateItems)
  435. item.EdbInfoId = v.EdbInfoId
  436. item.DataMap = dataMap
  437. }
  438. formulaMap := CheckFormula(formulaStr)
  439. addSql := ` INSERT INTO edb_data_calculate(edb_info_id,edb_code,data_time,value,create_time,modify_time,status,data_timestamp) values `
  440. nowStr := time.Now().Format(utils.FormatDateTime)
  441. var isAdd bool
  442. for sk, sv := range saveDataMap {
  443. formulaStr = strings.ToUpper(formulaStr)
  444. formulaFormStr := ReplaceFormula(edbInfoIdArr, sv, formulaMap, formulaStr, edbInfoIdBytes)
  445. if formulaStr == "" {
  446. return
  447. }
  448. if formulaFormStr != "" {
  449. utils.FileLog.Info("formulaFormStr:%s", formulaFormStr)
  450. expression := formula.NewExpression(formulaFormStr)
  451. calResult, err := expression.Evaluate()
  452. if err != nil {
  453. err = errors.New("计算失败:Err:" + err.Error() + ";formulaStr:" + formulaFormStr)
  454. fmt.Println(err)
  455. return err
  456. }
  457. calVal, err := calResult.Float64()
  458. if err != nil {
  459. err = errors.New("计算失败:获取计算值失败 Err:" + err.Error() + ";formulaStr:" + formulaFormStr)
  460. fmt.Println(err)
  461. return err
  462. }
  463. //需要存入的数据
  464. {
  465. dataTime, _ := time.Parse(utils.FormatDate, sk)
  466. timestamp := dataTime.UnixNano() / 1e6
  467. timeStr := fmt.Sprintf("%d", timestamp)
  468. addSql += "("
  469. addSql += strconv.Itoa(edbInfoId) + "," + "'" + edbCode + "'" + "," + "'" + sk + "'" + "," + utils.SubFloatToString(calVal, 4) + "," + "'" + nowStr + "'" +
  470. "," + "'" + nowStr + "'" + "," + "1"
  471. addSql += "," + "'" + timeStr + "'"
  472. addSql += "),"
  473. isAdd = true
  474. }
  475. } else {
  476. fmt.Println("formulaFormStr is empty")
  477. }
  478. }
  479. if isAdd {
  480. addSql = strings.TrimRight(addSql, ",")
  481. AddEdbDataCalculateBySql(addSql)
  482. if err != nil {
  483. fmt.Println("AddEdbDataCalculate Err:" + err.Error())
  484. return err
  485. }
  486. }
  487. return
  488. }
  489. // SaveAdjustEdbInfoReq 保存数据调整请求参数(请求指标服务)
  490. type SaveAdjustEdbInfoReq struct {
  491. AdminId int `description:"添加人id"`
  492. AdminName string `description:"添加人名称"`
  493. EdbInfoId int `description:"指标id"`
  494. FromEdbInfoId int `description:"来源指标id"`
  495. EdbName string `description:"指标名称"`
  496. Frequency string `description:"频度"`
  497. Unit string `description:"单位"`
  498. ClassifyId int `description:"分类id"`
  499. DataList []SaveAdjustEdbDataReq `description:"指标对应的数据值"`
  500. }
  501. // SaveAdjustEdbDataReq 保存数据调整请求的数据的参数
  502. type SaveAdjustEdbDataReq struct {
  503. Date string `description:"数据日期"`
  504. Value float64 `description:"数据值"`
  505. }
  506. // BatchEdbInfoCalculateBatchSaveReq 批量添加 计算指标
  507. type BatchEdbInfoCalculateBatchSaveReq struct {
  508. AdminId int `description:"添加人id"`
  509. AdminName string `description:"添加人名称"`
  510. EdbList []*CalculateEdbInfoItem //需要批量计算的指标列表
  511. Formula string `description:"N值/移动天数"`
  512. Source int `description:"来源:1:同花顺,2:wind,3:彭博,4:指标运算,5:累计值转月,6:同比值,7:同差值,8:N数值移动平均计算,12:环比值,13:环差值,14:升频"`
  513. CalculateFormula string `description:"计算公式"`
  514. EdbInfoIdArr []EdbInfoFromTag `description:"关联指标列表"`
  515. MoveType int `description:"移动方式:1:领先(默认),2:滞后"`
  516. MoveFrequency string `description:"移动频度:天/周/月/季/年"`
  517. Calendar string `description:"公历/农历"`
  518. Data interface{} `description:"数据"`
  519. EmptyType int `description:"空值处理类型(0查找前后35天,1不计算,2前值填充,3后值填充,4等于0)"`
  520. MaxEmptyType int `description:"MAX、MIN公式空值处理类型(1、等于0;2、跳过空值)"`
  521. Extra string `description:"指标的额外配置"`
  522. }
  523. // BatchEdbInfoCalculateBatchSaveResp 批量添加 计算指标 返回数据
  524. type BatchEdbInfoCalculateBatchSaveResp struct {
  525. Fail []BatchEdbInfoCalculateBatchSaveFailResp `description:"添加失败的指标"`
  526. Success []BatchEdbInfoCalculateBatchSaveSuccessResp `description:"添加成功的指标"`
  527. }
  528. // BatchEdbInfoCalculateBatchSaveFailResp 添加失败的指标信息
  529. type BatchEdbInfoCalculateBatchSaveFailResp struct {
  530. CalculateId string `description:"当前请求时,单个计算的唯一标识"`
  531. Msg string `description:"用户提示信息"`
  532. ErrMsg string `description:"错误信息,内部查看"`
  533. }
  534. // BatchEdbInfoCalculateBatchSaveSuccessResp 添加成功的指标信息
  535. type BatchEdbInfoCalculateBatchSaveSuccessResp struct {
  536. ClassifyId int `description:"分类id"`
  537. CalculateId string `description:"当前请求时,单个计算的唯一标识"`
  538. EdbInfoId int `description:"指标ID"`
  539. UniqueCode string `description:"指标唯一编码"`
  540. }
  541. // CalculateMultiSearchReq 批量计算模块下的指标搜索
  542. type CalculateMultiChoiceReq struct {
  543. ClassifyIds string `description:"分类ID, 用英文逗号拼接"`
  544. Frequency string `description:"频度"`
  545. SysUserIds string `description:"创建人ID,用英文逗号拼接"`
  546. Keyword string `description:"关键字"`
  547. SelectAll bool `description:"是否全选"`
  548. EdbInfoIds string `description:"指标Id, 用英文逗号拼接"`
  549. }
  550. type CalculateMultiChoiceResp struct {
  551. SearchItem []EdbInfoBase `description:"查询结果"`
  552. }
  553. type CalculateMultiEdbSearchResp struct {
  554. SearchItem []CalculateMultiEdbSearchItem `description:"查询结果"`
  555. Paging *paging.PagingItem
  556. }
  557. type CalculateMultiEdbSearchItem struct {
  558. EdbInfoId int `description:"指标id"`
  559. EdbName string `description:"指标名称"`
  560. EdbNameEn string `description:"英文指标名称"`
  561. Frequency string `description:"频度"`
  562. Unit string `description:"单位"`
  563. UnitEn string `description:"英文单位"`
  564. ClassifyId int `description:"分类id"`
  565. SysUserId int
  566. SysUserRealName string
  567. EndValue float64 `description:"数据的最新值(预测日期的最新值)"`
  568. EndDate string `description:"终止日期"`
  569. HaveOperaAuth bool `description:"是否有数据权限,默认:false"`
  570. }