base_from_shfe.go 11 KB

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