future_good_edb_data.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  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. // RefreshFutureChartInfoReq 刷新商品指标请求
  123. type RefreshFutureChartInfoReq struct {
  124. ChartInfoId int `description:"图表ID"`
  125. }
  126. // RefreshFutureGoodEdbDataFromThs 刷新wind期货指标数据
  127. func RefreshFutureGoodEdbDataFromThs(futureGoodEdbInfoId int, edbCode, startDate string, item FutureGoodDataFromThs) (err error) {
  128. o := orm.NewOrm()
  129. to, err := o.Begin()
  130. if err != nil {
  131. return
  132. }
  133. defer func() {
  134. if err != nil {
  135. fmt.Println("RefreshAllCalculateTbz,Err:" + err.Error())
  136. _ = to.Rollback()
  137. } else {
  138. _ = to.Commit()
  139. }
  140. }()
  141. var existList []*FutureGoodEdbData
  142. // 获取指标中所有存在的数据值
  143. {
  144. var condition string
  145. var pars []interface{}
  146. condition += " AND future_good_edb_info_id=? "
  147. pars = append(pars, futureGoodEdbInfoId)
  148. //if startDate != "" {
  149. // condition += " AND data_time >= ? "
  150. // pars = append(pars, startDate)
  151. //}
  152. sql := `SELECT * FROM future_good_edb_data WHERE 1=1 `
  153. if condition != "" {
  154. sql += condition
  155. }
  156. sql += `ORDER BY data_time asc `
  157. _, err = to.Raw(sql, pars).QueryRows(&existList)
  158. }
  159. existMap := make(map[string]*FutureGoodEdbData)
  160. for _, v := range existList {
  161. existMap[v.DataTime.Format(utils.FormatDate)] = v
  162. }
  163. 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 `
  164. var isAdd bool
  165. addMap := make(map[string]string)
  166. table := item.Tables
  167. dataLen := len(table.Time)
  168. for k := 0; k < dataLen; k++ {
  169. eDate := table.Time[k]
  170. dataTime, tmpErr := time.ParseInLocation(utils.FormatDate, eDate, time.Local)
  171. if tmpErr != nil {
  172. err = tmpErr
  173. return
  174. }
  175. timestamp := dataTime.UnixNano() / 1e6
  176. timeStr := fmt.Sprintf("%d", timestamp)
  177. tradeCode := ``
  178. open := utils.SubFloatToString(table.Open[k], 20)
  179. high := utils.SubFloatToString(table.High[k], 20)
  180. low := utils.SubFloatToString(table.Low[k], 20)
  181. closeVal := utils.SubFloatToString(table.Close[k], 20)
  182. volume := utils.SubFloatToString(table.Volume[k], 20)
  183. amt := utils.SubFloatToString(table.Amount[k], 20)
  184. oi := utils.SubFloatToString(table.Ccl[k], 20)
  185. settle := utils.SubFloatToString(table.Settlement[k], 20)
  186. if findItem, ok := existMap[eDate]; !ok {
  187. if _, existOk := addMap[eDate]; !existOk {
  188. addSql += GetAddSql(strconv.Itoa(futureGoodEdbInfoId), edbCode, eDate, tradeCode, open, high, low, closeVal, volume, amt, oi, settle, timeStr)
  189. addMap[eDate] = "1"
  190. }
  191. isAdd = true
  192. } else {
  193. if findItem != nil {
  194. updateCol := make([]string, 0)
  195. if findItem.TradeCode != tradeCode {
  196. findItem.TradeCode = tradeCode
  197. updateCol = append(updateCol, "TradeCode")
  198. }
  199. if utils.SubFloatToString(findItem.Open, 30) != open {
  200. findItem.Open = table.Open[k]
  201. updateCol = append(updateCol, "Open")
  202. }
  203. if utils.SubFloatToString(findItem.High, 30) != high {
  204. findItem.High = table.High[k]
  205. updateCol = append(updateCol, "High")
  206. }
  207. if utils.SubFloatToString(findItem.Low, 30) != low {
  208. findItem.Low = table.Low[k]
  209. updateCol = append(updateCol, "Low")
  210. }
  211. if utils.SubFloatToString(findItem.Close, 30) != closeVal {
  212. findItem.Close = table.Close[k]
  213. updateCol = append(updateCol, "Close")
  214. }
  215. if utils.SubFloatToString(findItem.Volume, 30) != volume {
  216. findItem.Volume = table.Volume[k]
  217. updateCol = append(updateCol, "Volume")
  218. }
  219. if utils.SubFloatToString(findItem.Amt, 30) != amt {
  220. findItem.Amt = table.Amount[k]
  221. updateCol = append(updateCol, "Amt")
  222. }
  223. if utils.SubFloatToString(findItem.Oi, 30) != oi {
  224. findItem.Oi = table.Ccl[k]
  225. updateCol = append(updateCol, "Oi")
  226. }
  227. if utils.SubFloatToString(findItem.Settle, 30) != settle {
  228. findItem.Settle = table.Settlement[k]
  229. updateCol = append(updateCol, "Settle")
  230. }
  231. if len(updateCol) > 0 {
  232. _, err = to.Update(findItem, updateCol...)
  233. if err != nil {
  234. return
  235. }
  236. }
  237. }
  238. }
  239. }
  240. if isAdd {
  241. addSql = strings.TrimRight(addSql, ",")
  242. _, err = to.Raw(addSql).Exec()
  243. if err != nil {
  244. fmt.Println("RefreshEdbDataFromWind add Err", err.Error())
  245. return
  246. }
  247. }
  248. return
  249. }
  250. func GetAddSql(futureGoodEdbInfoId, futureGoodEdbCode, dataTime, tradeCode, open, high, low, close, volume, amt, oi, settle, timestampStr string) (addSql string) {
  251. //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
  252. nowStr := time.Now().Format(utils.FormatDateTime)
  253. //addSql += "("
  254. //addSql += futureGoodEdbInfoId + "," + "'" + futureGoodEdbCode + "'" + "," + "'" + dataTime + "'" + "," + value + "," + "'" + nowStr + "'" +
  255. // "," + "'" + nowStr + "'"
  256. //addSql += "," + "'" + timestampStr + "'"
  257. //addSql += "),"
  258. 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)
  259. return
  260. }
  261. func GetFutureGoodEdbDataListByDate(futureGoodEdbInfoId int, startDate, endDate string) (list []*FutureGoodEdbData, err error) {
  262. var pars []interface{}
  263. sql := `SELECT * FROM future_good_edb_data WHERE 1=1 AND future_good_edb_info_id = ? `
  264. if startDate != "" {
  265. sql += ` AND data_time>=? `
  266. pars = append(pars, startDate)
  267. }
  268. if endDate != "" {
  269. sql += ` AND data_time<=? `
  270. pars = append(pars, endDate)
  271. }
  272. sql += ` ORDER BY data_time ASC `
  273. o := orm.NewOrm()
  274. _, err = o.Raw(sql, futureGoodEdbInfoId, pars).QueryRows(&list)
  275. return
  276. }