com_trade_data.go 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. package data_manage
  2. import (
  3. sql2 "database/sql"
  4. "eta/eta_task/global"
  5. "eta/eta_task/utils"
  6. "gorm.io/gorm"
  7. "time"
  8. )
  9. // ComTradeData 联合国商品贸易数据表
  10. type ComTradeData struct {
  11. ComTradeDataId int `gorm:"column:com_trade_data_id;primaryKey;autoIncrement"`
  12. ComTradeId int `description:"指标id"`
  13. IndexCode string `description:"联合国商品贸易编码"`
  14. Flow string `description:"贸易流向:X(Export:出口);M(Import:进口)"`
  15. ReporterCode int `description:"出口国id"`
  16. ReporterName string `description:"出口国名称"`
  17. PartnerCode int `description:"进口国id"`
  18. PartnerName string `description:"进口国名称"`
  19. Partner2Code int `description:"第二进口国id"`
  20. Partner2Name string `description:"第二进口国名称"`
  21. DateType int `description:"日期类型,1:年度,2:月度"`
  22. DataTime string `description:"数据日期"`
  23. IndexTradeCode string `description:"离岸价编码"`
  24. TradeValue float64 `description:"离岸价(美元)"`
  25. IndexNetWeightCode string `description:"净重编码"`
  26. NetWeightValue float64 `description:"净重/公斤"`
  27. ModifyTime time.Time `description:"最新更新时间"`
  28. CreateTime time.Time `description:"创建时间"`
  29. }
  30. func (m *ComTradeData) AfterFind(db *gorm.DB) (err error) {
  31. m.DataTime = utils.GormDateStrToDateStr(m.DataTime)
  32. return
  33. }
  34. // GetAllComTradeDataList 获取联合国商品贸易数据列表
  35. func GetAllComTradeDataList(startDate string) (list []*ComTradeData, err error) {
  36. o := global.DbMap[utils.DbNameIndex]
  37. sql := `SELECT * FROM com_trade_data WHERE create_time>=? ORDER BY com_trade_data_id ASC `
  38. err = o.Raw(sql, startDate).Find(&list).Error
  39. return
  40. }
  41. // AddBaseFromComTradeData 添加数据
  42. func AddBaseFromComTradeData(item *ComTradeData) (lastId int64, err error) {
  43. err = global.DbMap[utils.DbNameIndex].Create(item).Error
  44. if err != nil {
  45. return
  46. }
  47. lastId = int64(item.ComTradeDataId)
  48. return
  49. }
  50. // MultiAddBaseFromComTradeData 批量添加数据
  51. func MultiAddBaseFromComTradeData(items []*ComTradeData) (lastId int64, err error) {
  52. num := len(items)
  53. if num <= 0 {
  54. return
  55. }
  56. o := global.DbMap[utils.DbNameIndex]
  57. err = o.CreateInBatches(items, utils.MultiAddNum).Error
  58. return
  59. }
  60. // GetBaseFromComTradeMaxDate 获取un数据中的最大日期数据
  61. func GetBaseFromComTradeMaxDate() (maxDate time.Time, err error) {
  62. o := global.DbMap[utils.DbNameIndex]
  63. sql := ` SELECT max(a.create_time) as max_date FROM com_trade_data as a `
  64. var timeNull sql2.NullTime
  65. err = o.Raw(sql).Scan(&timeNull).Error
  66. if err != nil {
  67. return
  68. }
  69. if timeNull.Valid {
  70. maxDate = timeNull.Time
  71. }
  72. return
  73. }