edb_info_calculate.go 24 KB

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