edb_data_shfe.go 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. package data_manage
  2. import (
  3. "eta/eta_task/utils"
  4. "fmt"
  5. "github.com/beego/beego/v2/client/orm"
  6. "strconv"
  7. "strings"
  8. "time"
  9. )
  10. type ShfeData struct {
  11. InputValue string `orm:"column(DATA_VALUE)" description:"日期"`
  12. DataTime string `orm:"column(DATA_DATE)" description:"值"`
  13. }
  14. type BaseFromShfeDataSimple struct {
  15. Id int `orm:"column(base_from_trade_ine_index_id);pk"`
  16. DealCode string
  17. BuyCode string
  18. SoldCode string
  19. DataTime string
  20. DealValue string
  21. BuyValue string
  22. SoldValue string
  23. }
  24. type BaseInfoFromShfe struct {
  25. DealName string
  26. BuyName string
  27. SoldName string
  28. }
  29. type BaseFromTradeShfeIndex struct {
  30. BaseFromTradeShfeIndexId int `orm:"column(base_from_trade_ine_index_id);pk"`
  31. Rank int
  32. DealShortName string
  33. DealName string
  34. DealCode string
  35. DealValue string
  36. DealChange int
  37. BuyShortName string
  38. BuyName string
  39. BuyCode string
  40. BuyValue string
  41. BuyChange int
  42. SoldShortName string
  43. SoldName string
  44. SoldCode string
  45. SoldValue string
  46. SoldChange int
  47. Frequency string
  48. ClassifyName string
  49. ClassifyType string
  50. CreateTime time.Time
  51. ModifyTime time.Time
  52. DataTime string
  53. }
  54. func GetEdbDataShfeMaxOrMinDate(edbCode string) (minDate, maxDate string, err error) {
  55. o := orm.NewOrm()
  56. sql := ` SELECT MIN(data_time) AS minDate,MAX(data_time) AS maxDate FROM edb_data_ine WHERE edb_code=? `
  57. err = o.Raw(sql, edbCode).QueryRow(&minDate, &maxDate)
  58. return
  59. }
  60. // RefreshEdbDataByShfe 刷新上期能源指标数据
  61. func RefreshEdbDataByShfe(edbInfoId int, edbCode, startDate, endDate string) (err error) {
  62. o := orm.NewOrm()
  63. tx, err := o.Begin()
  64. if err != nil {
  65. return err
  66. }
  67. defer func() {
  68. if err != nil {
  69. tx.Rollback()
  70. } else {
  71. tx.Commit()
  72. }
  73. }()
  74. if err != nil {
  75. return
  76. }
  77. var suffix string
  78. if strings.Contains(edbCode, "deal") {
  79. suffix = "deal"
  80. } else if strings.Contains(edbCode, "buy") {
  81. suffix = "buy"
  82. } else if strings.Contains(edbCode, "sold") {
  83. suffix = "sold"
  84. }
  85. edbInfoIdStr := strconv.Itoa(edbInfoId)
  86. //计算数据
  87. var condition string
  88. var pars []interface{}
  89. if edbCode != "" {
  90. if suffix == "deal" {
  91. condition += " AND deal_code=? "
  92. } else if suffix == "buy" {
  93. condition += " AND buy_code=? "
  94. } else {
  95. condition += " AND sold_code=? "
  96. }
  97. pars = append(pars, edbCode)
  98. }
  99. if startDate != "" {
  100. condition += " AND data_time>=? "
  101. pars = append(pars, startDate)
  102. }
  103. if endDate != "" {
  104. condition += " AND data_time<=? "
  105. pars = append(pars, endDate)
  106. }
  107. glDataList, err := GetShfeDataByTradeCode(condition, pars)
  108. addSql := ` INSERT INTO edb_data_ine(edb_info_id,edb_code,data_time,value,create_time,modify_time,status,data_timestamp) values `
  109. var isAdd bool
  110. existMap := make(map[string]string)
  111. for _, v := range glDataList {
  112. var value string
  113. if suffix == "deal" {
  114. value = v.DealValue
  115. } else if suffix == "buy" {
  116. value = v.BuyValue
  117. } else {
  118. value = v.SoldValue
  119. }
  120. item := v
  121. itemValue := value
  122. if _, ok := existMap[v.DataTime]; !ok {
  123. count, err := GetEdbDataShfeByCodeAndDate(edbCode, v.DataTime)
  124. if err != nil && err.Error() != utils.ErrNoRow() {
  125. return err
  126. }
  127. if count <= 0 {
  128. eDate := item.DataTime
  129. sValue := itemValue
  130. if sValue != "" {
  131. dataTime, err := time.Parse(utils.FormatDate, eDate)
  132. if err != nil {
  133. return err
  134. }
  135. timestamp := dataTime.UnixNano() / 1e6
  136. timeStr := fmt.Sprintf("%d", timestamp)
  137. addSql += GetAddSql(edbInfoIdStr, edbCode, eDate, timeStr, sValue)
  138. isAdd = true
  139. }
  140. } else {
  141. err = ModifyEdbDataShfe(int64(edbInfoId), v.DataTime, value)
  142. if err != nil {
  143. return err
  144. }
  145. }
  146. }
  147. existMap[v.DataTime] = value
  148. }
  149. if isAdd {
  150. addSql = strings.TrimRight(addSql, ",")
  151. _, err = tx.Raw(addSql).Exec()
  152. if err != nil {
  153. return err
  154. }
  155. }
  156. return
  157. }
  158. // GetBaseInfoFromShfeByIndexCode 获取指标信息
  159. func GetBaseInfoFromShfeByIndexCode(indexCode, suffix string) (list []*BaseInfoFromShfe, err error) {
  160. o := orm.NewOrm()
  161. sql := `SELECT * FROM base_from_trade_ine_index WHERE %s_code=? `
  162. sql = fmt.Sprintf(sql, suffix)
  163. _, err = o.Raw(sql, indexCode).QueryRows(&list)
  164. return
  165. }
  166. func GetShfeDataByTradeCode(condition string, pars []interface{}) (item []*BaseFromShfeDataSimple, err error) {
  167. sql := ` SELECT * FROM base_from_trade_ine_index WHERE 1=1 `
  168. o := orm.NewOrm()
  169. if condition != "" {
  170. sql += condition
  171. }
  172. sql += ` ORDER BY data_time DESC `
  173. _, err = o.Raw(sql, pars).QueryRows(&item)
  174. return
  175. }
  176. func AddEdbDataShfeBySql(sqlStr string) (err error) {
  177. o := orm.NewOrm()
  178. _, err = o.Raw(sqlStr).Exec()
  179. return
  180. }
  181. func GetBaseFromShfeDataAllByIndexCode(indexCode, suffix string) (list []*BaseFromTradeShfeIndex, err error) {
  182. o := orm.NewOrm()
  183. sql := `SELECT * FROM base_from_trade_ine_index WHERE %s_code=? `
  184. sql = fmt.Sprintf(sql, suffix)
  185. _, err = o.Raw(sql, indexCode).QueryRows(&list)
  186. return
  187. }
  188. func GetEdbDataShfeByCodeAndDate(edbCode string, startDate string) (count int, err error) {
  189. o := orm.NewOrm()
  190. sql := ` SELECT COUNT(1) AS count FROM edb_data_ine WHERE edb_code=? AND data_time=? `
  191. err = o.Raw(sql, edbCode, startDate).QueryRow(&count)
  192. return
  193. }
  194. func ModifyEdbDataShfe(edbInfoId int64, dataTime, value string) (err error) {
  195. o := orm.NewOrm()
  196. sql := ` UPDATE edb_data_ine SET value=?,modify_time=NOW() WHERE edb_info_id=? AND data_time=? `
  197. _, err = o.Raw(sql, value, edbInfoId, dataTime).Exec()
  198. return
  199. }