edb_info_calculate.go 25 KB

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