edb_info_calculate.go 25 KB

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