predict_edb_data_static.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. package models
  2. import (
  3. "fmt"
  4. "github.com/beego/beego/v2/client/orm"
  5. "github.com/shopspring/decimal"
  6. "strconv"
  7. "strings"
  8. )
  9. // AddStaticPredictEdbInfoReq 添加预测指标静态指标请求
  10. type AddStaticPredictEdbInfoReq struct {
  11. ClassifyId int `description:"分类id"`
  12. SourceEdbInfoId int `description:"来源指标id"`
  13. EdbName string `description:"指标名称"`
  14. Frequency string `description:"频率"`
  15. Unit string `description:"单位"`
  16. AdminId int `description:"添加人id"`
  17. AdminName string `description:"添加人名称"`
  18. }
  19. // AddPredictStaticEdb 添加预测指标
  20. // edbInfo, calculateMappingList, predictEdbConfList,calculateRule9List,trendsMappingList
  21. func AddPredictStaticEdb(edbInfo, sourceEdbInfoItem *EdbInfo, calculateMappingList []*EdbInfoCalculateMapping, predictEdbConfList []*PredictEdbConf) (err error, errMsg string) {
  22. o := orm.NewOrm()
  23. tx, err := o.Begin()
  24. if err != nil {
  25. return
  26. }
  27. defer func() {
  28. if err != nil {
  29. tx.Rollback()
  30. } else {
  31. err = tx.Commit()
  32. }
  33. }()
  34. // 新增预测指标
  35. edbInfoId, err := tx.Insert(edbInfo)
  36. if err != nil {
  37. return
  38. }
  39. edbInfo.EdbInfoId = int(edbInfoId)
  40. // 新增预测指标的关联关系
  41. lenCalculateMapping := len(calculateMappingList)
  42. if lenCalculateMapping > 0 {
  43. for _, calculateMappingItem := range calculateMappingList {
  44. calculateMappingItem.EdbInfoId = edbInfo.EdbInfoId
  45. calculateMappingItem.EdbCode = edbInfo.EdbCode
  46. }
  47. _, err = tx.InsertMulti(lenCalculateMapping, calculateMappingList)
  48. if err != nil {
  49. return
  50. }
  51. }
  52. if len(predictEdbConfList) > 0 {
  53. // 新增预测指标配置
  54. for _, v := range predictEdbConfList {
  55. v.PredictEdbInfoId = edbInfo.EdbInfoId
  56. _, tmpErr := tx.Insert(v)
  57. if tmpErr != nil {
  58. err = fmt.Errorf(`新增预测指标配置失败:%v`, tmpErr)
  59. return
  60. }
  61. }
  62. }
  63. // 新增预测指标数据
  64. dataTableName := GetEdbDataTableName(edbInfo.Source, edbInfo.SubSource)
  65. edbInfoIdStr := strconv.Itoa(int(edbInfoId))
  66. //获取指标数据(实际已生成)
  67. fromDataList, err := GetEdbDataListAll(sourceEdbInfoItem.Source, sourceEdbInfoItem.SubSource, FindEdbDataListAllCond{
  68. EdbInfoId: sourceEdbInfoItem.EdbInfoId,
  69. StartDataTime: "",
  70. StartDataTimeCond: ">=",
  71. }, 1)
  72. if err != nil {
  73. err = fmt.Errorf(`获取原指标数据失败:%v`, err)
  74. return
  75. }
  76. addSql := ` INSERT INTO ` + dataTableName + `(edb_info_id,edb_code,data_time,value,create_time,modify_time,data_timestamp) values `
  77. count := 0
  78. for _, tmpData := range fromDataList {
  79. currDateStr := tmpData.DataTime
  80. timeStr := fmt.Sprintf("%d", tmpData.DataTimestamp)
  81. // 当前的实际值
  82. saveValue := decimal.NewFromFloat(tmpData.Value).Round(4).String()
  83. addSql += GetAddSql(edbInfoIdStr, edbInfo.EdbCode, currDateStr, timeStr, saveValue)
  84. count += 1
  85. if count >= 500 {
  86. addSql = strings.TrimRight(addSql, ",")
  87. _, err = tx.Raw(addSql).Exec()
  88. if err != nil {
  89. err = fmt.Errorf(`新增预测指标数据失败:%v`, err)
  90. return
  91. }
  92. addSql = ` INSERT INTO ` + dataTableName + `(edb_info_id,edb_code,data_time,value,create_time,modify_time,data_timestamp) values `
  93. count = 0
  94. }
  95. }
  96. if count > 0 {
  97. addSql = strings.TrimRight(addSql, ",")
  98. _, err = tx.Raw(addSql).Exec()
  99. if err != nil {
  100. err = fmt.Errorf(`新增预测指标数据失败:%v`, err)
  101. return
  102. }
  103. }
  104. return
  105. }