base_from_stock_plant.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. package models
  2. import (
  3. "eta_gn/eta_index_lib/global"
  4. "eta_gn/eta_index_lib/models/supply_analysis"
  5. "eta_gn/eta_index_lib/utils"
  6. "fmt"
  7. "strconv"
  8. "strings"
  9. "time"
  10. )
  11. type StockPlantEdbdata struct {
  12. VarietyEdbId string `gorm:"column:variety_edb_id;primaryKey" description:"指标编码"`
  13. DataTime string `gorm:"column:data_time" description:"日期"`
  14. Value string `gorm:"column:value" description:"值"`
  15. ModifyTime time.Time `gorm:"column:modify_time" description:"修改时间"`
  16. }
  17. func (m *StockPlantEdbdata) TableName() string {
  18. return "base_from_stock_plant_data"
  19. }
  20. func GetStockPlantEdbDataByCondition(condition string, pars []interface{}) (item []*StockPlantEdbdata, err error) {
  21. sql := ` SELECT * FROM base_from_stock_plant_data WHERE 1=1 `
  22. if condition != "" {
  23. sql += condition
  24. }
  25. sql += ` ORDER BY data_time DESC `
  26. err = global.DEFAULT_DmSQL.Raw(sql, pars...).Find(&item).Error
  27. return
  28. }
  29. // RefreshEdbDataFromStockPlant 刷新存量装置数据
  30. func RefreshEdbDataFromStockPlant(edbInfoId int, edbCode, startDate string) (err error) {
  31. source := utils.DATA_SOURCE_STOCK_PLANT
  32. subSource := utils.DATA_SUB_SOURCE_EDB
  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. 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, subSource, 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, subSource, v.EdbDataId)
  102. }
  103. }
  104. // 将数据入库
  105. if isAdd {
  106. addSql = strings.TrimRight(addSql, ",")
  107. err = global.DEFAULT_DmSQL.Exec(addSql).Error
  108. if err != nil {
  109. fmt.Println("RefreshAllEdbDataByStockPlant add Err", err.Error())
  110. return
  111. }
  112. }
  113. return
  114. }