future_good_edb_data.go 11 KB

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