edb_info_calculate.go 22 KB

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