base_from_com_trade.go 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. package models
  2. import (
  3. "eta_gn/eta_index_lib/utils"
  4. "fmt"
  5. "strconv"
  6. "strings"
  7. "time"
  8. "github.com/beego/beego/v2/client/orm"
  9. )
  10. // UN 联合国商品贸易数据指标
  11. // ComTradeCodeMapping 联合国商品贸易数据指标编码与分类编码的关系表
  12. type ComTradeCodeMapping struct {
  13. Code int `orm:"column(code);pk"`
  14. IndexCode string `description:"联合国商品贸易编码"`
  15. Type int `description:"1:价格;2:重量"`
  16. Name string `description:"指标名称"`
  17. CreateTime time.Time `description:"创建时间"`
  18. }
  19. // GetComTradeIndex 根据指标code获取指标信息
  20. func GetComTradeIndex(indexCode string) (item *ComTradeCodeMapping, err error) {
  21. o := orm.NewOrm()
  22. sql := `SELECT * FROM com_trade_code_mapping WHERE code=? `
  23. err = o.Raw(sql, indexCode).QueryRow(&item)
  24. return
  25. }
  26. type ComTradeData struct {
  27. Value float64 `orm:"column(value)" description:"值"`
  28. DataTime string `orm:"column(data_time)" description:"日期"`
  29. }
  30. func GetComTradeDataByTradeCode(tradeCode, startDate string) (items []*ComTradeData, err error) {
  31. o := orm.NewOrm()
  32. pars := []interface{}{tradeCode}
  33. sql1 := ` SELECT * FROM com_trade_data WHERE 1=1 AND index_trade_code = ? `
  34. if startDate != `` {
  35. sql1 += `AND data_time>=?`
  36. pars = append(pars, startDate)
  37. }
  38. sql := `select trade_value AS value,data_time from (` + sql1 + ` having 1 order by modify_time DESC ) tmp GROUP BY data_time ORDER BY data_time DESC `
  39. _, err = o.Raw(sql, pars).QueryRows(&items)
  40. return
  41. }
  42. func GetComTradeDataByWeightCode(weightCode, startDate string) (items []*ComTradeData, err error) {
  43. o := orm.NewOrm()
  44. pars := []interface{}{weightCode}
  45. sql1 := ` SELECT * FROM com_trade_data WHERE 1=1 AND index_net_weight_code = ? `
  46. if startDate != `` {
  47. sql1 += `AND data_time>=?`
  48. pars = append(pars, startDate)
  49. }
  50. sql := `select net_weight_value AS value,data_time from (` + sql1 + ` having 1 order by modify_time DESC ) tmp GROUP BY data_time ORDER BY data_time DESC `
  51. _, err = o.Raw(sql, pars).QueryRows(&items)
  52. return
  53. }
  54. func GetComTradeDataByCondition(condition string, pars []interface{}) (item []*ComTradeData, err error) {
  55. sql1 := ` SELECT * FROM com_trade_data WHERE 1=1 `
  56. o := orm.NewOrm()
  57. if condition != "" {
  58. sql1 += condition
  59. }
  60. sql := `select trade_value AS value,data_time from (` + sql1 + ` having 1 order by modify_time DESC ) tmp GROUP BY data_time ORDER BY data_time DESC `
  61. _, err = o.Raw(sql, pars).QueryRows(&item)
  62. return
  63. }
  64. // AddEdbDataFromComTrade 新增钢联指标数据
  65. func AddEdbDataFromComTrade(edbCode string) (err error) {
  66. o := orm.NewOrm()
  67. // 获取指标关系
  68. codeMapping, err := GetComTradeIndex(edbCode)
  69. if err != nil {
  70. return
  71. }
  72. var ComTradeDataList []*ComTradeData
  73. if codeMapping.Type == 1 || codeMapping.Type == 3 {
  74. ComTradeDataList, err = GetComTradeDataByTradeCode(edbCode, "")
  75. } else {
  76. ComTradeDataList, err = GetComTradeDataByWeightCode(edbCode, "")
  77. }
  78. dataLen := len(ComTradeDataList)
  79. existMap := make(map[string]string)
  80. if dataLen > 0 {
  81. var isAdd bool
  82. addSql := ` INSERT INTO edb_data_com_trade (edb_info_id,edb_code,data_time,value,create_time,modify_time,data_timestamp) values `
  83. for i := 0; i < dataLen; i++ {
  84. item := ComTradeDataList[i]
  85. eDate := item.DataTime
  86. sValue := utils.SubFloatToString(item.Value, 30)
  87. if sValue != "" {
  88. if _, ok := existMap[eDate]; !ok {
  89. dataTime, err := time.ParseInLocation(utils.FormatDate, eDate, time.Local)
  90. if err != nil {
  91. return err
  92. }
  93. timestamp := dataTime.UnixNano() / 1e6
  94. timeStr := fmt.Sprintf("%d", timestamp)
  95. addSql += GetAddSql("0", edbCode, eDate, timeStr, sValue)
  96. isAdd = true
  97. }
  98. }
  99. existMap[eDate] = eDate
  100. }
  101. if isAdd {
  102. addSql = strings.TrimRight(addSql, ",")
  103. utils.FileLog.Info("addSql:" + addSql)
  104. _, err = o.Raw(addSql).Exec()
  105. if err != nil {
  106. return err
  107. }
  108. }
  109. }
  110. return
  111. }
  112. // RefreshEdbDataFromComTrade 刷新UN世贸组织指标数据
  113. func RefreshEdbDataFromComTrade(edbInfoId int, edbCode, startDate string) (err error) {
  114. source := utils.DATA_SOURCE_COM_TRADE
  115. subSource := utils.DATA_SUB_SOURCE_EDB
  116. o := orm.NewOrm()
  117. if err != nil {
  118. return
  119. }
  120. edbInfoIdStr := strconv.Itoa(edbInfoId)
  121. // 获取指标关系
  122. codeMapping, err := GetComTradeIndex(edbCode)
  123. if err != nil {
  124. return
  125. }
  126. var ComTradeDataList []*ComTradeData
  127. if codeMapping.Type == 1 || codeMapping.Type == 3 {
  128. ComTradeDataList, err = GetComTradeDataByTradeCode(edbCode, startDate)
  129. } else {
  130. ComTradeDataList, err = GetComTradeDataByWeightCode(edbCode, startDate)
  131. }
  132. if err != nil {
  133. return
  134. }
  135. // 真实数据的最大日期 , 插入规则配置的日期
  136. var realDataMaxDate, edbDataInsertConfigDate time.Time
  137. var edbDataInsertConfig *EdbDataInsertConfig
  138. var isFindConfigDateRealData bool //是否找到配置日期的实际数据的值
  139. {
  140. edbDataInsertConfig, err = GetEdbDataInsertConfigByEdbId(edbInfoId)
  141. if err != nil && err.Error() != utils.ErrNoRow() {
  142. return
  143. }
  144. if edbDataInsertConfig != nil {
  145. edbDataInsertConfigDate = edbDataInsertConfig.Date
  146. }
  147. }
  148. var existCondition string
  149. var existPars []interface{}
  150. existCondition += " AND edb_info_id=? "
  151. existPars = append(existPars, edbInfoId)
  152. if startDate != "" {
  153. existCondition += " AND data_time>=? "
  154. existPars = append(existPars, startDate)
  155. }
  156. //获取指标所有数据
  157. existList, err := GetEdbDataByCondition(source, subSource, existCondition, existPars)
  158. if err != nil {
  159. return err
  160. }
  161. existMap := make(map[string]*EdbInfoSearchData)
  162. for _, v := range existList {
  163. existMap[v.DataTime] = v
  164. }
  165. addSql := ` INSERT INTO edb_data_com_trade(edb_info_id,edb_code,data_time,value,create_time,modify_time,data_timestamp) values `
  166. var isAdd bool
  167. addMap := make(map[string]string)
  168. for _, v := range ComTradeDataList {
  169. item := v
  170. eDate := item.DataTime
  171. sValue := utils.SubFloatToString(item.Value, 30)
  172. dataTime, err := time.ParseInLocation(utils.FormatDate, eDate, time.Local)
  173. if err != nil {
  174. return err
  175. }
  176. if findItem, ok := existMap[v.DataTime]; !ok {
  177. if sValue != "" {
  178. timestamp := dataTime.UnixNano() / 1e6
  179. timeStr := fmt.Sprintf("%d", timestamp)
  180. saveValue := sValue
  181. if _, addOk := addMap[eDate]; !addOk {
  182. addSql += GetAddSql(edbInfoIdStr, edbCode, eDate, timeStr, saveValue)
  183. isAdd = true
  184. }
  185. }
  186. } else {
  187. if findItem != nil && utils.SubFloatToString(findItem.Value, 30) != sValue {
  188. err = ModifyEdbDataById(source, subSource, findItem.EdbDataId, sValue)
  189. if err != nil {
  190. return err
  191. }
  192. }
  193. }
  194. addMap[v.DataTime] = v.DataTime
  195. // 下面代码主要目的是处理掉手动插入的数据判断
  196. {
  197. if realDataMaxDate.IsZero() || dataTime.After(realDataMaxDate) {
  198. realDataMaxDate = dataTime
  199. }
  200. if edbDataInsertConfigDate.IsZero() || dataTime.Equal(edbDataInsertConfigDate) {
  201. isFindConfigDateRealData = true
  202. }
  203. }
  204. }
  205. // 处理手工数据补充的配置
  206. HandleConfigInsertEdbData(realDataMaxDate, edbDataInsertConfig, edbInfoId, source, subSource, existMap, isFindConfigDateRealData)
  207. if isAdd {
  208. addSql = strings.TrimRight(addSql, ",")
  209. _, err = o.Raw(addSql).Exec()
  210. if err != nil {
  211. return err
  212. }
  213. }
  214. return
  215. }