com_trade_data.go 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. package models
  2. import (
  3. "github.com/beego/beego/v2/client/orm"
  4. "github.com/shopspring/decimal"
  5. "time"
  6. )
  7. // ComTradeIndex 联合国商品贸易数据指标表
  8. type ComTradeIndex struct {
  9. ComTradeId int `orm:"column(com_trade_id);pk"`
  10. IndexCode string `description:"联合国商品贸易编码"`
  11. IndexName string `description:"联合国商品名称"`
  12. IndexNameCn string `description:"联合国商品名称(中文)"`
  13. CreateTime time.Time `description:"创建时间"`
  14. }
  15. // GetComTradeIndexList 获取指标数据列表
  16. func GetComTradeIndexList(condition string, pars []interface{}) (list []*ComTradeIndex, err error) {
  17. o := orm.NewOrmUsingDB("data")
  18. sql := `SELECT * FROM com_trade_index WHERE 1=1 `
  19. if condition != "" {
  20. sql += condition
  21. }
  22. sql += `ORDER BY com_trade_id DESC `
  23. _, err = o.Raw(sql, pars).QueryRows(&list)
  24. return
  25. }
  26. // GetAllComTradeIndexList 获取指标数据列表
  27. func GetAllComTradeIndexList() (list []*ComTradeIndex, err error) {
  28. o := orm.NewOrmUsingDB("data")
  29. sql := `SELECT * FROM com_trade_index WHERE 1=1 ORDER BY com_trade_id DESC `
  30. _, err = o.Raw(sql).QueryRows(&list)
  31. return
  32. }
  33. // ComTradeCodeMapping 联合国商品贸易数据指标编码与分类编码的关系表
  34. type ComTradeCodeMapping struct {
  35. Code string `orm:"column(code);pk"`
  36. IndexCode string `description:"联合国商品贸易编码"`
  37. Type int `description:"1:价格;2:重量"`
  38. Name string `description:"指标名称"`
  39. CreateTime time.Time `description:"创建时间"`
  40. }
  41. // GetAllComTradeCodeMappingList 获取联合国商品贸易数据指标编码与分类编码的关系列表
  42. func GetAllComTradeCodeMappingList() (list []*ComTradeCodeMapping, err error) {
  43. o := orm.NewOrmUsingDB("data")
  44. sql := `SELECT * FROM com_trade_code_mapping WHERE 1=1 ORDER BY create_time ASC `
  45. _, err = o.Raw(sql).QueryRows(&list)
  46. return
  47. }
  48. func AddComTradeCodeMapping(item *ComTradeCodeMapping) (err error) {
  49. o := orm.NewOrmUsingDB("data")
  50. _, err = o.Insert(item)
  51. if err != nil {
  52. return
  53. }
  54. return
  55. }
  56. func UpdateComTradeCodeMapping(item *ComTradeCodeMapping,cols []string) (err error) {
  57. o := orm.NewOrmUsingDB("data")
  58. _, err = o.Update(item, cols...)
  59. if err != nil {
  60. return
  61. }
  62. return
  63. }
  64. func AddMultiComTradeCodeMapping(items []*ComTradeCodeMapping) (err error) {
  65. o := orm.NewOrmUsingDB("data")
  66. _, err = o.InsertMulti(len(items), items)
  67. if err != nil {
  68. return
  69. }
  70. return
  71. }
  72. // ComTradeData 联合国商品贸易数据表
  73. type ComTradeData struct {
  74. ComTradeDataId int `orm:"column(com_trade_data_id);pk"`
  75. ComTradeId int `description:"指标id"`
  76. IndexCode string `description:"联合国商品贸易编码"`
  77. Flow string `description:"贸易流向:X(Export:出口);M(Import:进口)"`
  78. ReporterCode int `description:"出口国id"`
  79. ReporterName string `description:"出口国名称"`
  80. PartnerCode int `description:"进口国id"`
  81. PartnerName string `description:"进口国名称"`
  82. Partner2Code int `description:"第二进口国id"`
  83. Partner2Name string `description:"第二进口国名称"`
  84. DateType int `description:"日期类型,1:年度,2:月度"`
  85. DataTime string `description:"数据日期"`
  86. IndexTradeCode string `description:"离岸价编码"`
  87. TradeValue float64 `description:"离岸价(美元)"`
  88. IndexNetWeightCode string `description:"净重编码"`
  89. NetWeightValue float64 `description:"净重/公斤"`
  90. ModifyTime time.Time `description:"最新更新时间"`
  91. CreateTime time.Time `description:"创建时间"`
  92. }
  93. // Update 更新基础信息
  94. func (item *ComTradeData) Update(cols []string) (err error) {
  95. o := orm.NewOrmUsingDB("data")
  96. _, err = o.Update(item, cols...)
  97. return
  98. }
  99. //HandleComTradeData 数据处理
  100. func HandleComTradeData(dataList []*ComTradeData, indexCode, flowCode, dataTime, partnerCode string) (err error) {
  101. o := orm.NewOrmUsingDB("data")
  102. to, err := o.Begin()
  103. if err != nil {
  104. return
  105. }
  106. defer func() {
  107. if err != nil {
  108. to.Rollback()
  109. } else {
  110. to.Commit()
  111. }
  112. }()
  113. var existList []*ComTradeData
  114. sql := `SELECT * FROM com_trade_data WHERE index_code = ? and flow = ? and partner_code = ? and data_time = ? order by data_time asc`
  115. _, err = to.Raw(sql, indexCode, flowCode, partnerCode, dataTime).QueryRows(&existList)
  116. if err != nil {
  117. return err
  118. }
  119. existMap := make(map[string]*ComTradeData)
  120. for _, v := range existList {
  121. existMap[v.IndexTradeCode] = v
  122. }
  123. // 待添加的数据列表
  124. addDataList := make([]*ComTradeData, 0)
  125. for _, v := range dataList {
  126. existData, ok := existMap[v.IndexTradeCode]
  127. if !ok {
  128. v.IndexTradeCode = v.IndexTradeCode
  129. v.IndexNetWeightCode = v.IndexNetWeightCode
  130. addDataList = append(addDataList, v)
  131. } else {
  132. existTradeValue := decimal.NewFromFloat(existData.TradeValue)
  133. existNetWeightValue := decimal.NewFromFloat(existData.NetWeightValue)
  134. nowTradeValue := decimal.NewFromFloat(v.TradeValue)
  135. nowNetWeightValue := decimal.NewFromFloat(v.NetWeightValue)
  136. updateColStr := make([]string, 0)
  137. //fmt.Println(date, "=========", nowVal.Equal(valDecimal))
  138. if !existTradeValue.Equal(nowTradeValue) {
  139. existData.TradeValue = v.TradeValue
  140. }
  141. if !existNetWeightValue.Equal(nowNetWeightValue) {
  142. existData.NetWeightValue = v.NetWeightValue
  143. }
  144. if len(updateColStr) > 0 {
  145. _, err = to.Update(existData, updateColStr...)
  146. if err != nil {
  147. return
  148. }
  149. }
  150. }
  151. delete(existMap, v.DataTime)
  152. }
  153. lenAddDataList := len(addDataList)
  154. if lenAddDataList > 0 {
  155. _, err = to.InsertMulti(lenAddDataList, addDataList)
  156. }
  157. //existMapNum := len(existMap)
  158. //if existMapNum > 0 {
  159. // idList := make([]int, 0)
  160. // for _, v := range existMap {
  161. // idList = append(idList, v.BaseFromEiaSteoDataId)
  162. // }
  163. //
  164. // sql = `DELETE FROM base_from_eia_steo_data WHERE base_from_eia_steo_data_id in(` + utils.GetOrmInReplace(existMapNum) + `) `
  165. // _, err = to.Raw(sql, idList).Exec()
  166. // if err != nil {
  167. // return err
  168. // }
  169. //}
  170. // 更新指标信息
  171. sql = `UPDATE com_trade_index SET update_time = now() WHERE index_code = ? `
  172. _, err = to.Raw(sql, indexCode).Exec()
  173. if err != nil {
  174. return err
  175. }
  176. //{
  177. // isUpdateIndexDate := false
  178. // if endDate.After(indexInfo.EndDate) {
  179. // isUpdateIndexDate = true
  180. // indexInfo.EndDate = endDate
  181. // }
  182. // if startDate.Before(indexInfo.StartDate) {
  183. // isUpdateIndexDate = true
  184. // indexInfo.StartDate = startDate
  185. // }
  186. // if isUpdateIndexDate {
  187. // _, err = to.Update(indexInfo, "StartDate", "EndDate")
  188. // if err != nil {
  189. // return
  190. // }
  191. // }
  192. //}
  193. return
  194. }
  195. // GetAllComTradeDataList 获取联合国商品贸易数据指标编码与分类编码的关系列表
  196. func GetAllComTradeDataList() (list []*ComTradeData, err error) {
  197. o := orm.NewOrmUsingDB("data")
  198. //sql := `SELECT * FROM com_trade_data WHERE 1=1 ORDER BY create_time ASC `
  199. sql := `SELECT * FROM com_trade_data WHERE 1=1 GROUP BY index_trade_code`
  200. _, err = o.Raw(sql).QueryRows(&list)
  201. return
  202. }
  203. // UpdateComTradeData 更新
  204. func UpdateComTradeData(indexTradeCode, indexNetWeightCode, oldIndexTradeCode string) (err error) {
  205. o := orm.NewOrmUsingDB("data")
  206. sql := `UPDATE com_trade_data SET index_trade_code = ?,index_net_weight_code=? WHERE index_trade_code = ? ORDER BY create_time ASC `
  207. _, err = o.Raw(sql, indexTradeCode, indexNetWeightCode, oldIndexTradeCode).Exec()
  208. return
  209. }