future_good_edb_data.go 11 KB

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