base_from_stock_plant.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. package models
  2. import (
  3. "fmt"
  4. "github.com/beego/beego/v2/client/orm"
  5. "hongze/hongze_edb_lib/models/supply_analysis"
  6. "hongze/hongze_edb_lib/utils"
  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. o := orm.NewOrm()
  32. if err != nil {
  33. return
  34. }
  35. edbInfoIdStr := strconv.Itoa(edbInfoId)
  36. //计算数据
  37. var condition string
  38. var pars []interface{}
  39. // 获取供应分析的指标信息
  40. varietyEdbInfo, err := supply_analysis.GetVarietyEdbInfoByEdbInfoId(edbInfoId)
  41. if err != nil {
  42. return
  43. }
  44. // 获取基础库的指标数据
  45. condition += " AND variety_edb_id=? "
  46. pars = append(pars, varietyEdbInfo.VarietyEdbId)
  47. if startDate != "" {
  48. condition += " AND data_time >=? "
  49. pars = append(pars, startDate)
  50. } else {
  51. condition += " AND data_time != ? "
  52. pars = append(pars, `0000-00-00`)
  53. }
  54. baseDataList, err := GetStockPlantEdbDataByCondition(condition, pars)
  55. var existCondition string
  56. var existPars []interface{}
  57. existCondition += " AND edb_info_id=? "
  58. existPars = append(existPars, edbInfoId)
  59. if startDate != "" {
  60. existCondition += " AND data_time>=? "
  61. existPars = append(existPars, startDate)
  62. }
  63. existList, err := GetEdbDataByCondition(source, existCondition, existPars)
  64. if err != nil {
  65. return err
  66. }
  67. existMap := make(map[string]*EdbInfoSearchData)
  68. for _, v := range existList {
  69. existMap[v.DataTime] = v
  70. }
  71. addSql := ` INSERT INTO edb_data_stock_plant (edb_info_id,edb_code,data_time,value,create_time,modify_time,data_timestamp) values `
  72. var isAdd bool
  73. manualMap := make(map[string]*StockPlantEdbdata)
  74. for _, v := range baseDataList {
  75. item := v
  76. //fmt.Println("Item:", item.Dt, item.Close, item.TradeCode, item.ModifyTime)
  77. if findItem, ok := existMap[v.DataTime]; !ok {
  78. eDate := item.DataTime
  79. sValue := item.Value
  80. dataTime, err := time.Parse(utils.FormatDate, eDate)
  81. if err != nil {
  82. return err
  83. }
  84. timestamp := dataTime.UnixNano() / 1e6
  85. timeStr := fmt.Sprintf("%d", timestamp)
  86. addSql += GetAddSql(edbInfoIdStr, edbCode, eDate, timeStr, sValue)
  87. isAdd = true
  88. } else {
  89. if findItem != nil && utils.SubFloatToString(findItem.Value, 4) != item.Value {
  90. err = ModifyEdbDataById(source, findItem.EdbDataId, item.Value)
  91. if err != nil {
  92. return err
  93. }
  94. }
  95. }
  96. manualMap[v.DataTime] = v
  97. }
  98. // 删除不存在的日期数据
  99. for _, v := range existList {
  100. if _, ok := manualMap[v.DataTime]; !ok {
  101. go DeleteEdbDataById(source, v.EdbDataId)
  102. }
  103. }
  104. // 将数据入库
  105. if isAdd {
  106. addSql = strings.TrimRight(addSql, ",")
  107. _, err = o.Raw(addSql).Exec()
  108. if err != nil {
  109. fmt.Println("RefreshAllEdbDataByStockPlant add Err", err.Error())
  110. return
  111. }
  112. }
  113. return
  114. }