edb_data_shfe.go 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. package data_manage
  2. import (
  3. "eta_gn/eta_task/global"
  4. "eta_gn/eta_task/utils"
  5. "fmt"
  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. err = global.DEFAULT_DmSQL.Raw(sql, edbCode).First(&minDate, &maxDate).Error
  59. return
  60. }
  61. // RefreshEdbDataByShfe 刷新上期能源指标数据
  62. func RefreshEdbDataByShfe(edbInfoId int, edbCode, startDate, endDate string) (err error) {
  63. //o := orm.NewOrm()
  64. //tx, err := o.Begin()
  65. tx := global.DEFAULT_DmSQL.Begin()
  66. if err != nil {
  67. return err
  68. }
  69. defer func() {
  70. if err != nil {
  71. tx.Rollback()
  72. } else {
  73. tx.Commit()
  74. }
  75. }()
  76. if err != nil {
  77. return
  78. }
  79. var suffix string
  80. if strings.Contains(edbCode, "deal") {
  81. suffix = "deal"
  82. } else if strings.Contains(edbCode, "buy") {
  83. suffix = "buy"
  84. } else if strings.Contains(edbCode, "sold") {
  85. suffix = "sold"
  86. }
  87. edbInfoIdStr := strconv.Itoa(edbInfoId)
  88. //计算数据
  89. var condition string
  90. var pars []interface{}
  91. if edbCode != "" {
  92. if suffix == "deal" {
  93. condition += " AND deal_code=? "
  94. } else if suffix == "buy" {
  95. condition += " AND buy_code=? "
  96. } else {
  97. condition += " AND sold_code=? "
  98. }
  99. pars = append(pars, edbCode)
  100. }
  101. if startDate != "" {
  102. condition += " AND data_time>=? "
  103. pars = append(pars, startDate)
  104. }
  105. if endDate != "" {
  106. condition += " AND data_time<=? "
  107. pars = append(pars, endDate)
  108. }
  109. glDataList, err := GetShfeDataByTradeCode(condition, pars)
  110. addSql := ` INSERT INTO edb_data_ine(edb_info_id,edb_code,data_time,value,create_time,modify_time,status,data_timestamp) values `
  111. var isAdd bool
  112. existMap := make(map[string]string)
  113. for _, v := range glDataList {
  114. var value string
  115. if suffix == "deal" {
  116. value = v.DealValue
  117. } else if suffix == "buy" {
  118. value = v.BuyValue
  119. } else {
  120. value = v.SoldValue
  121. }
  122. item := v
  123. itemValue := value
  124. if _, ok := existMap[v.DataTime]; !ok {
  125. count, err := GetEdbDataShfeByCodeAndDate(edbCode, v.DataTime)
  126. if err != nil && err.Error() != utils.ErrNoRow() {
  127. return err
  128. }
  129. if count <= 0 {
  130. eDate := item.DataTime
  131. sValue := itemValue
  132. if sValue != "" {
  133. dataTime, err := time.Parse(utils.FormatDate, eDate)
  134. if err != nil {
  135. return err
  136. }
  137. timestamp := dataTime.UnixNano() / 1e6
  138. timeStr := fmt.Sprintf("%d", timestamp)
  139. addSql += GetAddSql(edbInfoIdStr, edbCode, eDate, timeStr, sValue)
  140. isAdd = true
  141. }
  142. } else {
  143. err = ModifyEdbDataShfe(int64(edbInfoId), v.DataTime, value)
  144. if err != nil {
  145. return err
  146. }
  147. }
  148. }
  149. existMap[v.DataTime] = value
  150. }
  151. if isAdd {
  152. addSql = strings.TrimRight(addSql, ",")
  153. //_, err = tx.Raw(addSql).Exec()
  154. err = tx.Exec(addSql).Error
  155. if err != nil {
  156. return err
  157. }
  158. }
  159. return
  160. }
  161. // GetBaseInfoFromShfeByIndexCode 获取指标信息
  162. func GetBaseInfoFromShfeByIndexCode(indexCode, suffix string) (list []*BaseInfoFromShfe, err error) {
  163. //o := orm.NewOrm()
  164. sql := `SELECT * FROM base_from_trade_ine_index WHERE %s_code=? `
  165. sql = fmt.Sprintf(sql, suffix)
  166. //_, err = o.Raw(sql, indexCode).QueryRows(&list)
  167. err = global.DEFAULT_DmSQL.Raw(sql, indexCode).Find(&list).Error
  168. return
  169. }
  170. func GetShfeDataByTradeCode(condition string, pars []interface{}) (item []*BaseFromShfeDataSimple, err error) {
  171. sql := ` SELECT * FROM base_from_trade_ine_index WHERE 1=1 `
  172. //o := orm.NewOrm()
  173. if condition != "" {
  174. sql += condition
  175. }
  176. sql += ` ORDER BY data_time DESC `
  177. //_, err = o.Raw(sql, pars).QueryRows(&item)
  178. err = global.DEFAULT_DmSQL.Raw(sql, pars).Find(&item).Error
  179. return
  180. }
  181. func AddEdbDataShfeBySql(sqlStr string) (err error) {
  182. //o := orm.NewOrm()
  183. //_, err = o.Raw(sqlStr).Exec()
  184. err = global.DEFAULT_DmSQL.Exec(sqlStr).Error
  185. return
  186. }
  187. func GetBaseFromShfeDataAllByIndexCode(indexCode, suffix string) (list []*BaseFromTradeShfeIndex, err error) {
  188. //o := orm.NewOrm()
  189. sql := `SELECT * FROM base_from_trade_ine_index WHERE %s_code=? `
  190. sql = fmt.Sprintf(sql, suffix)
  191. //_, err = o.Raw(sql, indexCode).QueryRows(&list)
  192. err = global.DEFAULT_DmSQL.Raw(sql, indexCode).Find(&list).Error
  193. return
  194. }
  195. func GetEdbDataShfeByCodeAndDate(edbCode string, startDate string) (count int, err error) {
  196. //o := orm.NewOrm()
  197. sql := ` SELECT COUNT(1) AS count FROM edb_data_ine WHERE edb_code=? AND data_time=? `
  198. //err = o.Raw(sql, edbCode, startDate).QueryRow(&count)
  199. err = global.DEFAULT_DmSQL.Raw(sql, edbCode, startDate).Scan(&count).Error
  200. return
  201. }
  202. func ModifyEdbDataShfe(edbInfoId int64, dataTime, value string) (err error) {
  203. //o := orm.NewOrm()
  204. sql := ` UPDATE edb_data_ine SET value=?,modify_time=NOW() WHERE edb_info_id=? AND data_time=? `
  205. //_, err = o.Raw(sql, value, edbInfoId, dataTime).Exec()
  206. err = global.DEFAULT_DmSQL.Exec(sql, value, edbInfoId, dataTime).Error
  207. return
  208. }