base_from_sh.go 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  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 BaseFromTradeShIndex struct {
  11. BaseFromTradeShIndexId int `orm:"column(base_from_trade_shanghai_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. type BaseFromShDataSimple struct {
  36. Id int `orm:"column(base_from_trade_shanghai_index_id);pk"`
  37. DealCode string
  38. BuyCode string
  39. SoldCode string
  40. DataTime string
  41. DealValue string
  42. BuyValue string
  43. SoldValue string
  44. }
  45. func GetBaseFromShDataAllByIndexCode(indexCode, suffix string) (list []*BaseFromTradeShIndex, err error) {
  46. o := orm.NewOrm()
  47. sql := `SELECT * FROM base_from_trade_shanghai_index WHERE %s_code=? `
  48. sql = fmt.Sprintf(sql, suffix)
  49. _, err = o.Raw(sql, indexCode).QueryRows(&list)
  50. return
  51. }
  52. func GetShDataByTradeCode(condition string, pars []interface{}) (item []*BaseFromShDataSimple, err error) {
  53. sql := ` SELECT * FROM base_from_trade_shanghai_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 AddEdbDataFromSh(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. shBaseDataAll, err := GetBaseFromShDataAllByIndexCode(edbCode, suffix)
  74. if err != nil && err.Error() != utils.ErrNoRow() {
  75. return
  76. }
  77. var isAdd bool
  78. addSql := ` INSERT INTO edb_data_sh(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 shBaseDataAll {
  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 RefreshEdbDataFromSh(edbInfoId int, edbCode, startDate string) (err error) {
  119. source := utils.DATA_SOURCE_SH
  120. subSource := utils.DATA_SUB_SOURCE_EDB
  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 := GetShDataByTradeCode(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, subSource, 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_sh(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) != sValue {
  214. err = ModifyEdbDataById(source, subSource, findItem.EdbDataId, sValue)
  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, subSource, 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 RefreshSHExchangeReq struct {
  244. Url string `description:"交易所链接"`
  245. Exchange string `description:"交易所"`
  246. Date string `description:"日期"`
  247. Data SHMessage
  248. }
  249. type Position []struct {
  250. ContractCode string `json:"INSTRUMENTID"`
  251. ProductSortNo int `json:"PRODUCTSORTNO"`
  252. Rank int `json:"RANK"`
  253. ParticipantID1 string `json:"PARTICIPANTID1"`
  254. ParticipantName1 string `json:"PARTICIPANTABBR1"`
  255. Deal interface{} `json:"CJ1"`
  256. Change1 interface{} `json:"CJ1_CHG"`
  257. ParticipantID2 string `json:"PARTICIPANTID2"`
  258. ParticipantName2 string `json:"PARTICIPANTABBR2"`
  259. BuyIn interface{} `json:"CJ2"`
  260. Change2 interface{} `json:"CJ2_CHG"`
  261. ParticipantID3 string `json:"PARTICIPANTID3"`
  262. ParticipantName3 string `json:"PARTICIPANTABBR3"`
  263. SoldOut interface{} `json:"CJ3"`
  264. Change3 interface{} `json:"CJ3_CHG"`
  265. ProductName string `json:"PRODUCTNAME"`
  266. }
  267. type SHMessage struct {
  268. Position Position `json:"o_cursor"`
  269. Length string `json:"showlength"`
  270. Code string `json:"o_code"`
  271. Msg string `json:"o_msg"`
  272. ReportDate string `json:"report_date"`
  273. UpdateDate string `json:"update_date"`
  274. PrintDate string `json:"print_date"`
  275. }
  276. type BaseFromTradeShanghaiIndex struct {
  277. BaseFromTradeShangHaiIndexId int `orm:"column(base_from_trade_shanghai_index_id);pk"`
  278. Rank int
  279. DealShortName string
  280. DealName string
  281. DealCode string
  282. DealValue int
  283. DealChange int
  284. BuyShortName string
  285. BuyName string
  286. BuyCode string
  287. BuyValue int
  288. BuyChange int
  289. SoldShortName string
  290. SoldName string
  291. SoldCode string
  292. SoldValue int
  293. SoldChange int
  294. Frequency string
  295. ClassifyName string
  296. ClassifyType string
  297. CreateTime time.Time
  298. ModifyTime time.Time
  299. DataTime string
  300. }
  301. func AddBaseFromTradeShangHaiIndex(item *BaseFromTradeShanghaiIndex) (lastId int64, err error) {
  302. o := orm.NewOrm()
  303. lastId, err = o.Insert(item)
  304. return
  305. }
  306. func GetBaseFromTradeShangHaiIndexAll(dateStr string) (list []*BaseFromTradeShanghaiIndex, err error) {
  307. o := orm.NewOrm()
  308. sql := `SELECT * FROM base_from_trade_shanghai_index WHERE data_time=?`
  309. _, err = o.Raw(sql, dateStr).QueryRows(&list)
  310. return
  311. }
  312. func ModifyBaseFromTradeShangHaiIndex(dealValue, buyValue, soldValue int, dataId int) (err error) {
  313. o := orm.NewOrm()
  314. sql := `UPDATE base_from_trade_shanghai_index SET deal_value=?,buy_value=?,sold_value=?,modify_time=NOW() WHERE base_from_trade_shanghai_index_id=? `
  315. _, err = o.Raw(sql, dealValue, buyValue, soldValue, dataId).Exec()
  316. return
  317. }