edb_info_calculate.go 23 KB

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