future_good_edb_data.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. package future_good
  2. import (
  3. "eta/eta_index_lib/utils"
  4. "fmt"
  5. "github.com/beego/beego/v2/client/orm"
  6. "strconv"
  7. "strings"
  8. "time"
  9. )
  10. // FutureGoodEdbData 期货指标数据的表
  11. type FutureGoodEdbData struct {
  12. FutureGoodEdbDataId int `orm:"column(future_good_edb_data_id);pk"`
  13. FutureGoodEdbInfoId int `description:"期货指标id"`
  14. FutureGoodEdbCode string `description:"期货指标code"`
  15. DataTime time.Time `description:"数据日期"`
  16. TradeCode string `description:"证券代码"`
  17. Open float64 `description:"开盘价"`
  18. High float64 `description:"最高价"`
  19. Low float64 `description:"最低价"`
  20. Close float64 `description:"收盘价"`
  21. Volume float64 `description:"成交量"`
  22. Amt float64 `description:"成交额"`
  23. Oi float64 `description:"持仓量"`
  24. Settle float64 `description:"结算价"`
  25. DataTimestamp int64 `description:"数据日期时间戳"`
  26. ModifyTime time.Time
  27. CreateTime time.Time
  28. }
  29. // FutureGoodEdbDataItem 期货指标数据的表
  30. type FutureGoodEdbDataItem struct {
  31. FutureGoodEdbDataId int `orm:"column(future_good_edb_data_id);pk"`
  32. FutureGoodEdbInfoId int `description:"期货指标id"`
  33. FutureGoodEdbCode string `description:"期货指标code"`
  34. DataTime string `description:"数据日期"`
  35. TradeCode string `description:"证券代码"`
  36. Open float64 `description:"开盘价"`
  37. High float64 `description:"最高价"`
  38. Low float64 `description:"最低价"`
  39. Close float64 `description:"收盘价"`
  40. Volume float64 `description:"成交量"`
  41. Amt float64 `description:"成交额"`
  42. Oi float64 `description:"持仓量"`
  43. Settle float64 `description:"结算价"`
  44. DataTimestamp int64 `description:"数据日期时间戳"`
  45. ModifyTime time.Time
  46. CreateTime time.Time
  47. }
  48. // FutureGoodDataFromThs 同花顺期货数据
  49. type FutureGoodDataFromThs struct {
  50. DataVol int64 `json:"dataVol"`
  51. Errmsg string `json:"errmsg"`
  52. Errorcode int64 `json:"errorcode"`
  53. Perf interface{} `json:"perf"`
  54. Tables FutureGoodDataTables `json:"tables"`
  55. }
  56. // FutureGoodDataTables 同花顺表格数据
  57. type FutureGoodDataTables struct {
  58. //TradeCode []string `json:"id"`
  59. Time []string `json:"time"`
  60. Open []float64 `json:"open"`
  61. High []float64 `json:"high"`
  62. Low []float64 `json:"low"`
  63. Close []float64 `json:"close"`
  64. Volume []float64 `json:"volume"`
  65. Amount []float64 `json:"amount"`
  66. Ccl []float64 `json:"ccl"`
  67. Settlement []float64 `json:"settlement"`
  68. }
  69. // GetFutureGoodEdbDataList 获取期货指标数据列表
  70. func GetFutureGoodEdbDataList(condition string, pars []interface{}) (list []*FutureGoodEdbDataItem, err error) {
  71. o := orm.NewOrm()
  72. sql := `SELECT * FROM future_good_edb_data WHERE 1=1 `
  73. if condition != "" {
  74. sql += condition
  75. }
  76. sql += `ORDER BY data_time asc `
  77. _, err = o.Raw(sql, pars).QueryRows(&list)
  78. return
  79. }
  80. // AddEdbDataFromThs 添加Ths商品指标数据
  81. func AddEdbDataFromThs(futureGoodEdbInfoId int, edbCode string, item FutureGoodDataFromThs) (err error) {
  82. o := orm.NewOrm()
  83. var isAdd bool
  84. addSql := ` INSERT INTO future_good_edb_data(future_good_edb_info_id,future_good_edb_code,data_time,trade_code,open,high,low,close,volume,amt,oi,settle,create_time,modify_time,data_timestamp) values `
  85. table := item.Tables
  86. dataLen := len(table.Time)
  87. for k := 0; k < dataLen; k++ {
  88. eDate := table.Time[k]
  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. tradeCode := ``
  96. open := utils.SubFloatToString(table.Open[k], 20)
  97. high := utils.SubFloatToString(table.High[k], 20)
  98. low := utils.SubFloatToString(table.Low[k], 20)
  99. closeVal := utils.SubFloatToString(table.Close[k], 20)
  100. volume := utils.SubFloatToString(table.Volume[k], 20)
  101. amt := utils.SubFloatToString(table.Amount[k], 20)
  102. oi := utils.SubFloatToString(table.Ccl[k], 20)
  103. settle := utils.SubFloatToString(table.Settlement[k], 20)
  104. addSql += GetAddSql(strconv.Itoa(futureGoodEdbInfoId), edbCode, eDate, tradeCode, open, high, low, closeVal, volume, amt, oi, settle, timeStr)
  105. isAdd = true
  106. }
  107. if isAdd {
  108. addSql = strings.TrimRight(addSql, ",")
  109. _, err = o.Raw(addSql).Exec()
  110. if err != nil {
  111. return
  112. }
  113. }
  114. return
  115. }
  116. // RefreshFutureEdbEdbInfoReq 刷新商品指标请求
  117. type RefreshFutureEdbEdbInfoReq struct {
  118. FutureGoodEdbInfoId int `description:"指标ID"`
  119. FutureGoodEdbCode string `description:"指标编码"`
  120. StartDate string `description:"开始日期"`
  121. }
  122. // RefreshFutureGoodEdbDataFromThs 刷新wind期货指标数据
  123. func RefreshFutureGoodEdbDataFromThs(futureGoodEdbInfoId int, edbCode, startDate string, item FutureGoodDataFromThs) (err error) {
  124. o := orm.NewOrm()
  125. to, err := o.Begin()
  126. if err != nil {
  127. return
  128. }
  129. defer func() {
  130. if err != nil {
  131. fmt.Println("RefreshAllCalculateTbz,Err:" + err.Error())
  132. _ = to.Rollback()
  133. } else {
  134. _ = to.Commit()
  135. }
  136. }()
  137. var existList []*FutureGoodEdbData
  138. // 获取指标中所有存在的数据值
  139. {
  140. var condition string
  141. var pars []interface{}
  142. condition += " AND future_good_edb_info_id=? "
  143. pars = append(pars, futureGoodEdbInfoId)
  144. //if startDate != "" {
  145. // condition += " AND data_time >= ? "
  146. // pars = append(pars, startDate)
  147. //}
  148. sql := `SELECT * FROM future_good_edb_data WHERE 1=1 `
  149. if condition != "" {
  150. sql += condition
  151. }
  152. sql += `ORDER BY data_time asc `
  153. _, err = to.Raw(sql, pars).QueryRows(&existList)
  154. }
  155. existMap := make(map[string]*FutureGoodEdbData)
  156. for _, v := range existList {
  157. existMap[v.DataTime.Format(utils.FormatDate)] = v
  158. }
  159. addSql := ` INSERT INTO future_good_edb_data(future_good_edb_info_id,future_good_edb_code,data_time,trade_code,open,high,low,close,volume,amt,oi,settle,create_time,modify_time,data_timestamp) values `
  160. var isAdd bool
  161. addMap := make(map[string]string)
  162. table := item.Tables
  163. dataLen := len(table.Time)
  164. for k := 0; k < dataLen; k++ {
  165. eDate := table.Time[k]
  166. dataTime, tmpErr := time.ParseInLocation(utils.FormatDate, eDate, time.Local)
  167. if tmpErr != nil {
  168. err = tmpErr
  169. return
  170. }
  171. timestamp := dataTime.UnixNano() / 1e6
  172. timeStr := fmt.Sprintf("%d", timestamp)
  173. tradeCode := ``
  174. open := utils.SubFloatToString(table.Open[k], 20)
  175. high := utils.SubFloatToString(table.High[k], 20)
  176. low := utils.SubFloatToString(table.Low[k], 20)
  177. closeVal := utils.SubFloatToString(table.Close[k], 20)
  178. volume := utils.SubFloatToString(table.Volume[k], 20)
  179. amt := utils.SubFloatToString(table.Amount[k], 20)
  180. oi := utils.SubFloatToString(table.Ccl[k], 20)
  181. settle := utils.SubFloatToString(table.Settlement[k], 20)
  182. if findItem, ok := existMap[eDate]; !ok {
  183. if _, existOk := addMap[eDate]; !existOk {
  184. addSql += GetAddSql(strconv.Itoa(futureGoodEdbInfoId), edbCode, eDate, tradeCode, open, high, low, closeVal, volume, amt, oi, settle, timeStr)
  185. addMap[eDate] = "1"
  186. }
  187. isAdd = true
  188. } else {
  189. if findItem != nil {
  190. updateCol := make([]string, 0)
  191. if findItem.TradeCode != tradeCode {
  192. findItem.TradeCode = tradeCode
  193. updateCol = append(updateCol, "TradeCode")
  194. }
  195. if utils.SubFloatToString(findItem.Open, 30) != open {
  196. findItem.Open = table.Open[k]
  197. updateCol = append(updateCol, "Open")
  198. }
  199. if utils.SubFloatToString(findItem.High, 30) != high {
  200. findItem.High = table.High[k]
  201. updateCol = append(updateCol, "High")
  202. }
  203. if utils.SubFloatToString(findItem.Low, 30) != low {
  204. findItem.Low = table.Low[k]
  205. updateCol = append(updateCol, "Low")
  206. }
  207. if utils.SubFloatToString(findItem.Close, 30) != closeVal {
  208. findItem.Close = table.Close[k]
  209. updateCol = append(updateCol, "Close")
  210. }
  211. if utils.SubFloatToString(findItem.Volume, 30) != volume {
  212. findItem.Volume = table.Volume[k]
  213. updateCol = append(updateCol, "Volume")
  214. }
  215. if utils.SubFloatToString(findItem.Amt, 30) != amt {
  216. findItem.Amt = table.Amount[k]
  217. updateCol = append(updateCol, "Amt")
  218. }
  219. if utils.SubFloatToString(findItem.Oi, 30) != oi {
  220. findItem.Oi = table.Ccl[k]
  221. updateCol = append(updateCol, "Oi")
  222. }
  223. if utils.SubFloatToString(findItem.Settle, 30) != settle {
  224. findItem.Settle = table.Settlement[k]
  225. updateCol = append(updateCol, "Settle")
  226. }
  227. if len(updateCol) > 0 {
  228. _, err = to.Update(findItem, updateCol...)
  229. if err != nil {
  230. return
  231. }
  232. }
  233. }
  234. }
  235. }
  236. if isAdd {
  237. addSql = strings.TrimRight(addSql, ",")
  238. _, err = to.Raw(addSql).Exec()
  239. if err != nil {
  240. fmt.Println("RefreshEdbDataFromWind add Err", err.Error())
  241. return
  242. }
  243. }
  244. return
  245. }
  246. func GetAddSql(futureGoodEdbInfoId, futureGoodEdbCode, dataTime, tradeCode, open, high, low, close, volume, amt, oi, settle, timestampStr string) (addSql string) {
  247. //future_good_edb_info_id,future_good_edb_code,data_time,trade_code,open,high,low,close,volume,amt,oi,settle,create_time,modify_time,data_timestamp
  248. nowStr := time.Now().Format(utils.FormatDateTime)
  249. //addSql += "("
  250. //addSql += futureGoodEdbInfoId + "," + "'" + futureGoodEdbCode + "'" + "," + "'" + dataTime + "'" + "," + value + "," + "'" + nowStr + "'" +
  251. // "," + "'" + nowStr + "'"
  252. //addSql += "," + "'" + timestampStr + "'"
  253. //addSql += "),"
  254. addSql = fmt.Sprintf("(%s,'%s','%s','%s',%s,%s,%s,%s,%s,%s,%s,%s,'%s','%s',%s),", futureGoodEdbInfoId, futureGoodEdbCode, dataTime, tradeCode, open, high, low, close, volume, amt, oi, settle, nowStr, nowStr, timestampStr)
  255. return
  256. }
  257. func GetFutureGoodEdbDataListByDate(futureGoodEdbInfoId int, startDate, endDate string) (list []*FutureGoodEdbData, err error) {
  258. var pars []interface{}
  259. sql := `SELECT * FROM future_good_edb_data WHERE 1=1 AND future_good_edb_info_id = ? `
  260. if startDate != "" {
  261. sql += ` AND data_time>=? `
  262. pars = append(pars, startDate)
  263. }
  264. if endDate != "" {
  265. sql += ` AND data_time<=? `
  266. pars = append(pars, endDate)
  267. }
  268. sql += ` ORDER BY data_time ASC `
  269. o := orm.NewOrm()
  270. _, err = o.Raw(sql, futureGoodEdbInfoId, pars).QueryRows(&list)
  271. return
  272. }