future_good_edb_data.go 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. package future_good
  2. import (
  3. "fmt"
  4. "github.com/beego/beego/v2/client/orm"
  5. "hongze/hongze_edb_lib/utils"
  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. // GetFutureGoodEdbDataList 获取期货指标数据列表
  49. func GetFutureGoodEdbDataList(condition string, pars []interface{}) (list []*FutureGoodEdbDataItem, err error) {
  50. o := orm.NewOrm()
  51. sql := `SELECT * FROM future_good_edb_data WHERE 1=1 `
  52. if condition != "" {
  53. sql += condition
  54. }
  55. sql += `ORDER BY data_time asc `
  56. _, err = o.Raw(sql, pars).QueryRows(&list)
  57. return
  58. }
  59. // AddEdbDataFromThs 添加同花顺商品指标数据
  60. func AddEdbDataFromThs(futureGoodEdbInfoId int, edbCode string, item FutureGoodDataFromThs) (err error) {
  61. var errMsg string
  62. o := orm.NewOrm()
  63. defer func() {
  64. if err != nil {
  65. //go utils.SendEmail(utils.APP_NAME_CN+"【"+utils.RunMode+"】"+"失败提醒", " 同花顺数据获取失败:err:"+errMsg, utils.EmailSendToUsers)
  66. go alarm_msg.SendAlarmMsg("wind商品数据获取失败:err:"+errMsg, 3)
  67. }
  68. }()
  69. var isAdd bool
  70. 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 `
  71. table := item.Tables
  72. dataLen := len(table.Time)
  73. for k := 0; k < dataLen; k++ {
  74. eDate := table.Time[k]
  75. dataTime, err := time.ParseInLocation(utils.FormatDate, eDate, time.Local)
  76. if err != nil {
  77. errMsg = " time.Parse :" + err.Error()
  78. return err
  79. }
  80. timestamp := dataTime.UnixNano() / 1e6
  81. timeStr := fmt.Sprintf("%d", timestamp)
  82. tradeCode := ``
  83. open := utils.SubFloatToString(table.Open[k], 20)
  84. high := utils.SubFloatToString(table.High[k], 20)
  85. low := utils.SubFloatToString(table.Low[k], 20)
  86. closeVal := utils.SubFloatToString(table.Close[k], 20)
  87. volume := utils.SubFloatToString(table.Volume[k], 20)
  88. amt := utils.SubFloatToString(table.Amount[k], 20)
  89. oi := utils.SubFloatToString(table.Ccl[k], 20)
  90. settle := utils.SubFloatToString(table.Settlement[k], 20)
  91. addSql += GetAddSql(strconv.Itoa(futureGoodEdbInfoId), edbCode, eDate, tradeCode, open, high, low, closeVal, volume, amt, oi, settle, timeStr)
  92. isAdd = true
  93. }
  94. if isAdd {
  95. addSql = strings.TrimRight(addSql, ",")
  96. _, err = o.Raw(addSql).Exec()
  97. if err != nil {
  98. errMsg = " tx.Exec Err :" + err.Error()
  99. return
  100. }
  101. }
  102. return
  103. }
  104. // RefreshFutureEdbEdbInfoReq 刷新商品指标请求
  105. type RefreshFutureEdbEdbInfoReq struct {
  106. FutureGoodEdbInfoId int `description:"指标ID"`
  107. FutureGoodEdbCode string `description:"指标编码"`
  108. StartDate string `description:"开始日期"`
  109. }
  110. // RefreshFutureGoodEdbDataFromThs 刷新wind期货指标数据
  111. func RefreshFutureGoodEdbDataFromThs(futureGoodEdbInfoId int, edbCode, startDate string, item FutureGoodDataFromThs) (err error) {
  112. o := orm.NewOrm()
  113. to, err := o.Begin()
  114. if err != nil {
  115. return
  116. }
  117. defer func() {
  118. if err != nil {
  119. fmt.Println("RefreshAllCalculateTbz,Err:" + err.Error())
  120. _ = to.Rollback()
  121. } else {
  122. _ = to.Commit()
  123. }
  124. }()
  125. var existList []*FutureGoodEdbData
  126. // 获取指标中所有存在的数据值
  127. {
  128. var condition string
  129. var pars []interface{}
  130. condition += " AND future_good_edb_info_id=? "
  131. pars = append(pars, futureGoodEdbInfoId)
  132. //if startDate != "" {
  133. // condition += " AND data_time >= ? "
  134. // pars = append(pars, startDate)
  135. //}
  136. sql := `SELECT * FROM future_good_edb_data WHERE 1=1 `
  137. if condition != "" {
  138. sql += condition
  139. }
  140. sql += `ORDER BY data_time asc `
  141. _, err = to.Raw(sql, pars).QueryRows(&existList)
  142. }
  143. existMap := make(map[string]*FutureGoodEdbData)
  144. for _, v := range existList {
  145. existMap[v.DataTime.Format(utils.FormatDate)] = v
  146. }
  147. 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 `
  148. var isAdd bool
  149. addMap := make(map[string]string)
  150. table := item.Tables
  151. dataLen := len(table.Time)
  152. for k := 0; k < dataLen; k++ {
  153. eDate := table.Time[k]
  154. dataTime, tmpErr := time.ParseInLocation(utils.FormatDate, eDate, time.Local)
  155. if tmpErr != nil {
  156. err = tmpErr
  157. return
  158. }
  159. timestamp := dataTime.UnixNano() / 1e6
  160. timeStr := fmt.Sprintf("%d", timestamp)
  161. tradeCode := ``
  162. open := utils.SubFloatToString(table.Open[k], 20)
  163. high := utils.SubFloatToString(table.High[k], 20)
  164. low := utils.SubFloatToString(table.Low[k], 20)
  165. closeVal := utils.SubFloatToString(table.Close[k], 20)
  166. volume := utils.SubFloatToString(table.Volume[k], 20)
  167. amt := utils.SubFloatToString(table.Amount[k], 20)
  168. oi := utils.SubFloatToString(table.Ccl[k], 20)
  169. settle := utils.SubFloatToString(table.Settlement[k], 20)
  170. if findItem, ok := existMap[eDate]; !ok {
  171. if _, existOk := addMap[eDate]; !existOk {
  172. addSql += GetAddSql(strconv.Itoa(futureGoodEdbInfoId), edbCode, eDate, tradeCode, open, high, low, closeVal, volume, amt, oi, settle, timeStr)
  173. addMap[eDate] = "1"
  174. }
  175. isAdd = true
  176. } else {
  177. if findItem != nil {
  178. updateCol := make([]string, 0)
  179. if findItem.TradeCode != tradeCode {
  180. findItem.TradeCode = tradeCode
  181. updateCol = append(updateCol, "TradeCode")
  182. }
  183. if utils.SubFloatToString(findItem.Open, 30) != open {
  184. findItem.Open = table.Open[k]
  185. updateCol = append(updateCol, "Open")
  186. }
  187. if utils.SubFloatToString(findItem.High, 30) != high {
  188. findItem.High = table.High[k]
  189. updateCol = append(updateCol, "High")
  190. }
  191. if utils.SubFloatToString(findItem.Low, 30) != low {
  192. findItem.Low = table.Low[k]
  193. updateCol = append(updateCol, "Low")
  194. }
  195. if utils.SubFloatToString(findItem.Close, 30) != closeVal {
  196. findItem.Close = table.Close[k]
  197. updateCol = append(updateCol, "Close")
  198. }
  199. if utils.SubFloatToString(findItem.Volume, 30) != volume {
  200. findItem.Volume = table.Volume[k]
  201. updateCol = append(updateCol, "Volume")
  202. }
  203. if utils.SubFloatToString(findItem.Amt, 30) != amt {
  204. findItem.Amt = table.Amount[k]
  205. updateCol = append(updateCol, "Amt")
  206. }
  207. if utils.SubFloatToString(findItem.Oi, 30) != oi {
  208. findItem.Oi = table.Ccl[k]
  209. updateCol = append(updateCol, "Oi")
  210. }
  211. if utils.SubFloatToString(findItem.Settle, 30) != settle {
  212. findItem.Settle = table.Settlement[k]
  213. updateCol = append(updateCol, "Settle")
  214. }
  215. if len(updateCol) > 0 {
  216. _, err = to.Update(findItem, updateCol...)
  217. if err != nil {
  218. return
  219. }
  220. }
  221. }
  222. }
  223. }
  224. if isAdd {
  225. addSql = strings.TrimRight(addSql, ",")
  226. _, err = to.Raw(addSql).Exec()
  227. if err != nil {
  228. fmt.Println("RefreshEdbDataFromWind add Err", err.Error())
  229. return
  230. }
  231. }
  232. return
  233. }
  234. func GetAddSql(futureGoodEdbInfoId, futureGoodEdbCode, dataTime, tradeCode, open, high, low, close, volume, amt, oi, settle, timestampStr string) (addSql string) {
  235. //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
  236. nowStr := time.Now().Format(utils.FormatDateTime)
  237. //addSql += "("
  238. //addSql += futureGoodEdbInfoId + "," + "'" + futureGoodEdbCode + "'" + "," + "'" + dataTime + "'" + "," + value + "," + "'" + nowStr + "'" +
  239. // "," + "'" + nowStr + "'"
  240. //addSql += "," + "'" + timestampStr + "'"
  241. //addSql += "),"
  242. 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)
  243. return
  244. }
  245. func GetFutureGoodEdbDataListByDate(futureGoodEdbInfoId int, startDate, endDate string) (list []*FutureGoodEdbData, err error) {
  246. var pars []interface{}
  247. sql := `SELECT * FROM future_good_edb_data WHERE 1=1 AND future_good_edb_info_id = ? `
  248. if startDate != "" {
  249. sql += ` AND data_time>=? `
  250. pars = append(pars, startDate)
  251. }
  252. if endDate != "" {
  253. sql += ` AND data_time<=? `
  254. pars = append(pars, endDate)
  255. }
  256. sql += ` ORDER BY data_time ASC `
  257. o := orm.NewOrm()
  258. _, err = o.Raw(sql, futureGoodEdbInfoId, pars).QueryRows(&list)
  259. return
  260. }