base_from_shfe.go 11 KB

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