base_from_shfe.go 11 KB

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