base_from_stock_plant.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. package models
  2. import (
  3. "eta/eta_index_lib/models/supply_analysis"
  4. "eta/eta_index_lib/utils"
  5. "fmt"
  6. "github.com/beego/beego/v2/client/orm"
  7. "strconv"
  8. "strings"
  9. "time"
  10. )
  11. // StockPlantEdbdata 存量装置数据
  12. type StockPlantEdbdata struct {
  13. VarietyEdbId string `orm:"column(variety_edb_id);pk" description:"指标编码"`
  14. DataTime string `orm:"column(data_time)" description:"日期"`
  15. Value string `orm:"column(value)" description:"值"`
  16. ModifyTime time.Time `orm:"column(modify_time)" description:"修改时间"`
  17. }
  18. func GetStockPlantEdbDataByCondition(condition string, pars []interface{}) (item []*StockPlantEdbdata, err error) {
  19. sql := ` SELECT * FROM base_from_stock_plant_data WHERE 1=1 `
  20. if condition != "" {
  21. sql += condition
  22. }
  23. sql += ` ORDER BY data_time DESC `
  24. o := orm.NewOrm()
  25. _, err = o.Raw(sql, pars).QueryRows(&item)
  26. return
  27. }
  28. // RefreshEdbDataFromStockPlant 刷新存量装置数据
  29. func RefreshEdbDataFromStockPlant(edbInfoId int, edbCode, startDate string) (err error) {
  30. source := utils.DATA_SOURCE_STOCK_PLANT
  31. subSource := utils.DATA_SUB_SOURCE_EDB
  32. o := orm.NewOrm()
  33. if err != nil {
  34. return
  35. }
  36. edbInfoIdStr := strconv.Itoa(edbInfoId)
  37. //计算数据
  38. var condition string
  39. var pars []interface{}
  40. // 获取供应分析的指标信息
  41. varietyEdbInfo, err := supply_analysis.GetVarietyEdbInfoByEdbInfoId(edbInfoId)
  42. if err != nil {
  43. return
  44. }
  45. // 获取基础库的指标数据
  46. condition += " AND variety_edb_id=? "
  47. pars = append(pars, varietyEdbInfo.VarietyEdbId)
  48. if startDate != "" {
  49. condition += " AND data_time >=? "
  50. pars = append(pars, startDate)
  51. } else {
  52. condition += " AND data_time != ? "
  53. pars = append(pars, `0000-00-00`)
  54. }
  55. baseDataList, err := GetStockPlantEdbDataByCondition(condition, pars)
  56. var existCondition string
  57. var existPars []interface{}
  58. existCondition += " AND edb_info_id=? "
  59. existPars = append(existPars, edbInfoId)
  60. if startDate != "" {
  61. existCondition += " AND data_time>=? "
  62. existPars = append(existPars, startDate)
  63. }
  64. existList, err := GetEdbDataByCondition(source, subSource, existCondition, existPars)
  65. if err != nil {
  66. return err
  67. }
  68. existMap := make(map[string]*EdbInfoSearchData)
  69. for _, v := range existList {
  70. existMap[v.DataTime] = v
  71. }
  72. addSql := ` INSERT INTO edb_data_stock_plant (edb_info_id,edb_code,data_time,value,create_time,modify_time,data_timestamp) values `
  73. var isAdd bool
  74. manualMap := make(map[string]*StockPlantEdbdata)
  75. for _, v := range baseDataList {
  76. item := v
  77. //fmt.Println("Item:", item.Dt, item.Close, item.TradeCode, item.ModifyTime)
  78. if findItem, ok := existMap[v.DataTime]; !ok {
  79. eDate := item.DataTime
  80. sValue := item.Value
  81. dataTime, err := time.Parse(utils.FormatDate, eDate)
  82. if err != nil {
  83. return err
  84. }
  85. timestamp := dataTime.UnixNano() / 1e6
  86. timeStr := fmt.Sprintf("%d", timestamp)
  87. addSql += GetAddSql(edbInfoIdStr, edbCode, eDate, timeStr, sValue)
  88. isAdd = true
  89. } else {
  90. if findItem != nil && utils.SubFloatToString(findItem.Value, 4) != item.Value {
  91. err = ModifyEdbDataById(source, subSource, findItem.EdbDataId, item.Value)
  92. if err != nil {
  93. return err
  94. }
  95. }
  96. }
  97. manualMap[v.DataTime] = v
  98. }
  99. // 删除不存在的日期数据
  100. for _, v := range existList {
  101. if _, ok := manualMap[v.DataTime]; !ok {
  102. go DeleteEdbDataById(source, subSource, v.EdbDataId)
  103. }
  104. }
  105. // 将数据入库
  106. if isAdd {
  107. addSql = strings.TrimRight(addSql, ",")
  108. _, err = o.Raw(addSql).Exec()
  109. if err != nil {
  110. fmt.Println("RefreshAllEdbDataByStockPlant add Err", err.Error())
  111. return
  112. }
  113. }
  114. return
  115. }