edb_info_calculate.go 25 KB

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