base_from_shfe.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. package models
  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. type BaseFromTradeShfeIndex struct {
  11. BaseFromTradeShfeIndexId int `orm:"column(base_from_trade_ine_index_id);pk"`
  12. Rank int
  13. DealShortName string
  14. DealName string
  15. DealCode string
  16. DealValue string
  17. DealChange int
  18. BuyShortName string
  19. BuyName string
  20. BuyCode string
  21. BuyValue string
  22. BuyChange int
  23. SoldShortName string
  24. SoldName string
  25. SoldCode string
  26. SoldValue string
  27. SoldChange int
  28. Frequency string
  29. ClassifyName string
  30. ClassifyType string
  31. CreateTime time.Time
  32. ModifyTime time.Time
  33. DataTime string
  34. }
  35. func GetBaseFromShfeDataAllByIndexCode(indexCode, suffix string) (list []*BaseFromTradeShfeIndex, err error) {
  36. o := orm.NewOrm()
  37. sql := `SELECT * FROM base_from_trade_ine_index WHERE %s_code=? `
  38. sql = fmt.Sprintf(sql, suffix)
  39. _, err = o.Raw(sql, indexCode).QueryRows(&list)
  40. return
  41. }
  42. type BaseFromShfeDataSimple struct {
  43. Id int `orm:"column(base_from_trade_ine_index_id);pk"`
  44. DealCode string
  45. BuyCode string
  46. SoldCode string
  47. DataTime string
  48. DealValue string
  49. BuyValue string
  50. SoldValue string
  51. }
  52. func GetShfeDataByTradeCode(condition string, pars []interface{}) (item []*BaseFromShfeDataSimple, err error) {
  53. sql := ` SELECT * FROM base_from_trade_ine_index WHERE 1=1 `
  54. o := orm.NewOrm()
  55. if condition != "" {
  56. sql += condition
  57. }
  58. sql += ` ORDER BY data_time DESC `
  59. _, err = o.Raw(sql, pars).QueryRows(&item)
  60. return
  61. }
  62. // 新增上期能源指标数据
  63. func AddEdbDataFromShfe(edbCode string) (err error) {
  64. var suffix string
  65. if strings.Contains(edbCode, "deal") {
  66. suffix = "deal"
  67. } else if strings.Contains(edbCode, "buy") {
  68. suffix = "buy"
  69. } else if strings.Contains(edbCode, "sold") {
  70. suffix = "sold"
  71. }
  72. o := orm.NewOrm()
  73. ineBaseDataAll, err := GetBaseFromShfeDataAllByIndexCode(edbCode, suffix)
  74. if err != nil && err.Error() != utils.ErrNoRow() {
  75. return
  76. }
  77. var isAdd bool
  78. addSql := ` INSERT INTO edb_data_ine(edb_info_id,edb_code,data_time,value,create_time,modify_time,data_timestamp) values `
  79. existMap := make(map[string]string)
  80. for _, sv := range ineBaseDataAll {
  81. eDate := sv.DataTime
  82. dataTime, err := time.ParseInLocation(utils.FormatDate, eDate, time.Local)
  83. if err != nil {
  84. fmt.Println("time.Parse Err:" + eDate)
  85. return err
  86. }
  87. timestamp := dataTime.UnixNano() / 1e6
  88. timeStr := fmt.Sprintf("%d", timestamp)
  89. if _, ok := existMap[eDate]; !ok {
  90. if suffix == "deal" {
  91. addSql += GetAddSql("0", edbCode, eDate, timeStr, sv.DealValue)
  92. } else if suffix == "buy" {
  93. addSql += GetAddSql("0", edbCode, eDate, timeStr, sv.BuyValue)
  94. } else {
  95. addSql += GetAddSql("0", edbCode, eDate, timeStr, sv.SoldValue)
  96. }
  97. isAdd = true
  98. }
  99. if suffix == "deal" {
  100. existMap[eDate] = sv.DealValue
  101. } else if suffix == "buy" {
  102. existMap[eDate] = sv.BuyValue
  103. } else {
  104. existMap[eDate] = sv.SoldValue
  105. }
  106. }
  107. if isAdd {
  108. addSql = strings.TrimRight(addSql, ",")
  109. utils.FileLog.Info("addSql:" + addSql)
  110. _, err = o.Raw(addSql).Exec()
  111. if err != nil {
  112. return err
  113. }
  114. }
  115. return
  116. }
  117. // 刷新上期能源指标数据
  118. func RefreshEdbDataFromShfe(edbInfoId int, edbCode, startDate string) (err error) {
  119. source := utils.DATA_SOURCE_SHFE
  120. o := orm.NewOrm()
  121. if err != nil {
  122. return
  123. }
  124. var suffix string
  125. if strings.Contains(edbCode, "deal") {
  126. suffix = "deal"
  127. } else if strings.Contains(edbCode, "buy") {
  128. suffix = "buy"
  129. } else if strings.Contains(edbCode, "sold") {
  130. suffix = "sold"
  131. }
  132. edbInfoIdStr := strconv.Itoa(edbInfoId)
  133. //计算数据
  134. var condition string
  135. var pars []interface{}
  136. if edbCode != "" {
  137. if suffix == "deal" {
  138. condition += " AND deal_code=? "
  139. } else if suffix == "buy" {
  140. condition += " AND buy_code=? "
  141. } else {
  142. condition += " AND sold_code=? "
  143. }
  144. pars = append(pars, edbCode)
  145. }
  146. if startDate != "" {
  147. condition += " AND data_time>=? "
  148. pars = append(pars, startDate)
  149. }
  150. glDataList, err := GetShfeDataByTradeCode(condition, pars)
  151. if err != nil {
  152. return
  153. }
  154. // 真实数据的最大日期 , 插入规则配置的日期
  155. var realDataMaxDate, edbDataInsertConfigDate time.Time
  156. var edbDataInsertConfig *EdbDataInsertConfig
  157. var isFindConfigDateRealData bool //是否找到配置日期的实际数据的值
  158. {
  159. edbDataInsertConfig, err = GetEdbDataInsertConfigByEdbId(edbInfoId)
  160. if err != nil && err.Error() != utils.ErrNoRow() {
  161. return
  162. }
  163. if edbDataInsertConfig != nil {
  164. edbDataInsertConfigDate = edbDataInsertConfig.Date
  165. }
  166. }
  167. //获取指标所有数据
  168. var existCondition string
  169. var existPars []interface{}
  170. existCondition += " AND edb_info_id=? "
  171. existPars = append(existPars, edbInfoId)
  172. if startDate != "" {
  173. existCondition += " AND data_time>=? "
  174. existPars = append(existPars, startDate)
  175. }
  176. existList, err := GetEdbDataByCondition(source, existCondition, existPars)
  177. if err != nil {
  178. return err
  179. }
  180. existMap := make(map[string]*EdbInfoSearchData)
  181. for _, v := range existList {
  182. existMap[v.DataTime] = v
  183. }
  184. addSql := ` INSERT INTO edb_data_ine(edb_info_id,edb_code,data_time,value,create_time,modify_time,data_timestamp) values `
  185. var isAdd bool
  186. for _, v := range glDataList {
  187. var value string
  188. if suffix == "deal" {
  189. value = v.DealValue
  190. } else if suffix == "buy" {
  191. value = v.BuyValue
  192. } else {
  193. value = v.SoldValue
  194. }
  195. item := v
  196. itemValue := value
  197. eDate := item.DataTime
  198. dataTime, err := time.ParseInLocation(utils.FormatDate, eDate, time.Local)
  199. if err != nil {
  200. return err
  201. }
  202. if _, ok := existMap[v.DataTime]; !ok {
  203. sValue := itemValue
  204. if sValue != "" {
  205. timestamp := dataTime.UnixNano() / 1e6
  206. timeStr := fmt.Sprintf("%d", timestamp)
  207. saveValue := sValue
  208. if findItem, ok := existMap[eDate]; !ok {
  209. addSql += GetAddSql(edbInfoIdStr, edbCode, eDate, timeStr, saveValue)
  210. isAdd = true
  211. } else {
  212. if findItem != nil && utils.SubFloatToString(findItem.Value, 30) != saveValue {
  213. err = ModifyEdbDataById(source, findItem.EdbDataId, saveValue)
  214. if err != nil {
  215. return err
  216. }
  217. }
  218. }
  219. }
  220. }
  221. // 下面代码主要目的是处理掉手动插入的数据判断
  222. {
  223. if realDataMaxDate.IsZero() || dataTime.After(realDataMaxDate) {
  224. realDataMaxDate = dataTime
  225. }
  226. if edbDataInsertConfigDate.IsZero() || dataTime.Equal(edbDataInsertConfigDate) {
  227. isFindConfigDateRealData = true
  228. }
  229. }
  230. }
  231. // 处理手工数据补充的配置
  232. HandleConfigInsertEdbData(realDataMaxDate, edbDataInsertConfig, edbInfoId, source, existMap, isFindConfigDateRealData)
  233. if isAdd {
  234. addSql = strings.TrimRight(addSql, ",")
  235. _, err = o.Raw(addSql).Exec()
  236. if err != nil {
  237. return err
  238. }
  239. }
  240. return
  241. }
  242. type BaseFromTradeIneIndex struct {
  243. BaseFromTradeIneIndexId int `orm:"column(base_from_trade_ine_index_id);pk"`
  244. Rank int
  245. DealShortName string
  246. DealName string
  247. DealCode string
  248. DealValue int
  249. DealChange int
  250. BuyShortName string
  251. BuyName string
  252. BuyCode string
  253. BuyValue int
  254. BuyChange int
  255. SoldShortName string
  256. SoldName string
  257. SoldCode string
  258. SoldValue int
  259. SoldChange int
  260. Frequency string
  261. ClassifyName string
  262. ClassifyType string
  263. CreateTime time.Time
  264. ModifyTime time.Time
  265. DataTime string
  266. }
  267. type BaseFromTradeMapping struct {
  268. BaseFromTradeMappingId int `orm:"column(base_from_trade_mapping_id);pk"`
  269. IndexName string
  270. IndexCode string
  271. Exchange string
  272. }
  273. func AddBaseFromTradeIneIndex(item *BaseFromTradeIneIndex) (lastId int64, err error) {
  274. o := orm.NewOrm()
  275. lastId, err = o.Insert(item)
  276. return
  277. }
  278. func GetBaseFromTradeIneIndexAll(dateStr string) (list []*BaseFromTradeIneIndex, err error) {
  279. o := orm.NewOrm()
  280. sql := `SELECT * FROM base_from_trade_ine_index where data_time=?`
  281. _, err = o.Raw(sql, dateStr).QueryRows(&list)
  282. return
  283. }
  284. func ModifyBaseFromTradeIneIndex(dealValue, buyValue, soldValue int, dataId int) (err error) {
  285. o := orm.NewOrm()
  286. sql := `UPDATE base_from_trade_ine_index SET deal_value=?,buy_value=?,sold_value=?,modify_time=NOW() WHERE base_from_trade_ine_index_id=? `
  287. _, err = o.Raw(sql, dealValue, buyValue, soldValue, dataId).Exec()
  288. return
  289. }
  290. func GetIndexCodeFromMapping(exchange string) (list []*BaseFromTradeMapping, err error) {
  291. o := orm.NewOrm()
  292. sql := `SELECT * FROM base_from_trade_mapping where exchange=?`
  293. _, err = o.Raw(sql, exchange).QueryRows(&list)
  294. return
  295. }
  296. func AddBaseFromTradeMapping(indexName, indexCode, exchange string) (err error) {
  297. o := orm.NewOrm()
  298. sql := "Insert Into base_from_trade_mapping(index_name,index_code,exchange) Values('" + indexName + "','" + indexCode + "','" + exchange + "')"
  299. _, err = o.Raw(sql).Exec()
  300. return
  301. }
  302. type RefreshINEExchangeReq struct {
  303. Date string `description:"日期"`
  304. Data IneJSONData
  305. }
  306. type IneJSONData struct {
  307. OCursor []OCursor `json:"o_cursor"`
  308. OCode interface{} `json:"o_code"`
  309. OMsg string `json:"o_msg"`
  310. ReportDate string `json:"report_date"`
  311. UpdateDate string `json:"update_date"`
  312. }
  313. type OCursor struct {
  314. Instrumentid string `json:"INSTRUMENTID"`
  315. Participantid3 string `json:"PARTICIPANTID3"`
  316. Participantid2 string `json:"PARTICIPANTID2"`
  317. Participantid1 string `json:"PARTICIPANTID1"`
  318. Participantabbr3 string `json:"PARTICIPANTABBR3"`
  319. Participantabbr2 string `json:"PARTICIPANTABBR2"`
  320. Rank int `json:"RANK"`
  321. Participantabbr1 string `json:"PARTICIPANTABBR1"`
  322. BuyIn interface{} `json:"CJ2"`
  323. Deal interface{} `json:"CJ1"`
  324. Change1 interface{} `json:"CJ1_CHG"`
  325. Change3 interface{} `json:"CJ3_CHG"`
  326. Productname string `json:"Productname"`
  327. Productsortno interface{} `json:"PRODUCTSORTNO"`
  328. SoldOut interface{} `json:"CJ3"`
  329. Change2 interface{} `json:"CJ2_CHG"`
  330. }