base_from_sh.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  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 BaseFromTradeShIndex struct {
  12. BaseFromTradeShIndexId int `orm:"column(base_from_trade_shanghai_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. type BaseFromShDataSimple struct {
  37. Id int `orm:"column(base_from_trade_shanghai_index_id);pk"`
  38. DealCode string
  39. BuyCode string
  40. SoldCode string
  41. DataTime string
  42. DealValue string
  43. BuyValue string
  44. SoldValue string
  45. }
  46. func GetBaseFromShDataAllByIndexCode(indexCode, suffix string) (list []*BaseFromTradeShIndex, err error) {
  47. o := orm.NewOrm()
  48. sql := `SELECT * FROM base_from_trade_shanghai_index WHERE %s_code=? `
  49. sql = fmt.Sprintf(sql, suffix)
  50. _, err = o.Raw(sql, indexCode).QueryRows(&list)
  51. return
  52. }
  53. func GetShDataByTradeCode(condition string, pars []interface{}) (item []*BaseFromShDataSimple, err error) {
  54. sql := ` SELECT * FROM base_from_trade_shanghai_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 AddEdbDataFromSh(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. shBaseDataAll, err := GetBaseFromShDataAllByIndexCode(edbCode, suffix)
  75. if err != nil && err.Error() != utils.ErrNoRow() {
  76. return
  77. }
  78. var isAdd bool
  79. addSql := ` INSERT INTO edb_data_sh(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 shBaseDataAll {
  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 RefreshEdbDataFromSh(edbInfoId int, edbCode, startDate string) (err error) {
  120. source := utils.DATA_SOURCE_SH
  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, 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, 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, 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 int `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.NewOrmUsingDB("data")
  303. lastId, err = o.Insert(item)
  304. return
  305. }
  306. func GetBaseFromTradeShangHaiIndexAll(dateStr string) (list []*BaseFromTradeShanghaiIndex, err error) {
  307. o := orm.NewOrmUsingDB("data")
  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.NewOrmUsingDB("data")
  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. }
  318. func ShIndexCodeGenerator(shortName, indexName, contractCode, suffix string) string {
  319. var indexCodeMap = make(map[string]string)
  320. if shortName == "" {
  321. return ""
  322. }
  323. strResult := ""
  324. if shortName != "top20" {
  325. //取公司全拼
  326. a := pinyin.NewArgs()
  327. rows := pinyin.LazyPinyin(shortName, a)
  328. for i := 0; i < len(rows); i++ {
  329. strResult += rows[i]
  330. }
  331. } else {
  332. strResult = "top20"
  333. }
  334. indexCode, _ := indexCodeMap[indexName]
  335. if indexCode == "" {
  336. indexCode = strResult + contractCode + suffix
  337. indexCode = strings.Replace(indexCode, " ", "", -1)
  338. indexCodeMap[indexName] = indexCode
  339. err := AddBaseFromTradeMapping(indexName, indexCode, "SH")
  340. if err != nil {
  341. fmt.Println("add Code err:", err)
  342. }
  343. }
  344. return strings.Replace(indexCode, " ", "", -1)
  345. }