base_from_ths_hf.go 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476
  1. package models
  2. import (
  3. "eta/eta_index_lib/models/mgo"
  4. "eta/eta_index_lib/utils"
  5. "fmt"
  6. "github.com/beego/beego/v2/client/orm"
  7. "go.mongodb.org/mongo-driver/bson"
  8. "strings"
  9. "time"
  10. )
  11. var (
  12. ThsHfPeriodArr = []int{1, 3, 5, 10, 15, 30, 60} // 时间周期
  13. ThsHfCPSArr = []string{"no", "forward1", "forward2", "forward3", "forward4", "backward1", "backward2", "backward3", "backward4"} // 复权方式
  14. ThsHfFillArr = []string{"Original", "Previous", "Blank"} // 非交易间隔处理
  15. )
  16. // BaseFromThsHfIndex 同花顺高频数据
  17. type BaseFromThsHfIndex struct {
  18. BaseFromThsHfIndexId int `orm:"column(base_from_ths_hf_index_id);pk"`
  19. BaseFromThsHfClassifyId int `description:"分类ID"`
  20. IndexCode string `description:"指标编码"`
  21. IndexName string `description:"指标名称"`
  22. Unit string `description:"单位"`
  23. Source string `description:"数据来源"`
  24. Frequency string `description:"频度"`
  25. StartDate time.Time `description:"开始日期(至时分秒)"`
  26. EndDate time.Time `description:"结束日期(至时分秒)"`
  27. Describe string `description:"指标描述"`
  28. Sort int `description:"排序"`
  29. IsStop int `description:"是否停更:0-否;1-停更"`
  30. TerminalCode string `description:"所属终端编码"`
  31. StockCode string `description:"证券代码"`
  32. Indicator string `description:"同花顺指标代码"`
  33. ApiPars string `description:"API请求参数"`
  34. LatestValue float64 `description:"最新值"`
  35. SysUserId int `description:"创建人ID"`
  36. SysUserRealName string `description:"创建人姓名"`
  37. CreateTime time.Time `description:"创建时间"`
  38. ModifyTime time.Time `description:"修改时间"`
  39. }
  40. func (m *BaseFromThsHfIndex) TableName() string {
  41. return "base_from_ths_hf_index"
  42. }
  43. type BaseFromThsHfIndexCols struct {
  44. PrimaryId string
  45. BaseFromThsHfClassifyId string
  46. IndexCode string
  47. IndexName string
  48. Unit string
  49. Source string
  50. Frequency string
  51. StartDate string
  52. EndDate string
  53. Describe string
  54. Sort string
  55. IsStop string
  56. TerminalCode string
  57. StockCode string
  58. Indicator string
  59. ApiPars string
  60. LatestValue string
  61. SysUserId string
  62. SysUserRealName string
  63. CreateTime string
  64. ModifyTime string
  65. }
  66. func (m *BaseFromThsHfIndex) Cols() BaseFromThsHfIndexCols {
  67. return BaseFromThsHfIndexCols{
  68. PrimaryId: "base_from_ths_hf_index_id",
  69. BaseFromThsHfClassifyId: "base_from_ths_hf_classify_id",
  70. IndexCode: "index_code",
  71. IndexName: "index_name",
  72. Unit: "unit",
  73. Source: "source",
  74. Frequency: "frequency",
  75. StartDate: "start_date",
  76. EndDate: "end_date",
  77. Describe: "describe",
  78. Sort: "sort",
  79. IsStop: "is_stop",
  80. TerminalCode: "terminal_code",
  81. StockCode: "stock_code",
  82. Indicator: "indicator",
  83. ApiPars: "api_pars",
  84. LatestValue: "latest_value",
  85. SysUserId: "sys_user_id",
  86. SysUserRealName: "sys_user_real_name",
  87. CreateTime: "create_time",
  88. ModifyTime: "modify_time",
  89. }
  90. }
  91. func (m *BaseFromThsHfIndex) Create() (err error) {
  92. o := orm.NewOrm()
  93. id, err := o.Insert(m)
  94. if err != nil {
  95. return
  96. }
  97. m.BaseFromThsHfIndexId = int(id)
  98. return
  99. }
  100. func (m *BaseFromThsHfIndex) CreateMulti(items []*BaseFromThsHfIndex) (err error) {
  101. if len(items) == 0 {
  102. return
  103. }
  104. o := orm.NewOrm()
  105. _, err = o.InsertMulti(len(items), items)
  106. return
  107. }
  108. func (m *BaseFromThsHfIndex) Update(cols []string) (err error) {
  109. o := orm.NewOrm()
  110. _, err = o.Update(m, cols...)
  111. return
  112. }
  113. func (m *BaseFromThsHfIndex) Remove() (err error) {
  114. o := orm.NewOrm()
  115. sql := fmt.Sprintf(`DELETE FROM %s WHERE %s = ? LIMIT 1`, m.TableName(), m.Cols().PrimaryId)
  116. _, err = o.Raw(sql, m.BaseFromThsHfIndexId).Exec()
  117. return
  118. }
  119. func (m *BaseFromThsHfIndex) MultiRemove(ids []int) (err error) {
  120. if len(ids) == 0 {
  121. return
  122. }
  123. o := orm.NewOrm()
  124. sql := fmt.Sprintf(`DELETE FROM %s WHERE %s IN (%s)`, m.TableName(), m.Cols().PrimaryId, utils.GetOrmInReplace(len(ids)))
  125. _, err = o.Raw(sql, ids).Exec()
  126. return
  127. }
  128. func (m *BaseFromThsHfIndex) RemoveByCondition(condition string, pars []interface{}) (err error) {
  129. if condition == "" {
  130. return
  131. }
  132. o := orm.NewOrm()
  133. sql := fmt.Sprintf(`DELETE FROM %s WHERE %s`, m.TableName(), condition)
  134. _, err = o.Raw(sql, pars).Exec()
  135. return
  136. }
  137. func (m *BaseFromThsHfIndex) GetItemById(id int) (item *BaseFromThsHfIndex, err error) {
  138. o := orm.NewOrm()
  139. sql := fmt.Sprintf(`SELECT * FROM %s WHERE %s = ? LIMIT 1`, m.TableName(), m.Cols().PrimaryId)
  140. err = o.Raw(sql, id).QueryRow(&item)
  141. return
  142. }
  143. func (m *BaseFromThsHfIndex) GetItemByCondition(condition string, pars []interface{}, orderRule string) (item *BaseFromThsHfIndex, err error) {
  144. o := orm.NewOrm()
  145. order := ``
  146. if orderRule != "" {
  147. order = ` ORDER BY ` + orderRule
  148. }
  149. sql := fmt.Sprintf(`SELECT * FROM %s WHERE 1=1 %s %s LIMIT 1`, m.TableName(), condition, order)
  150. err = o.Raw(sql, pars).QueryRow(&item)
  151. return
  152. }
  153. func (m *BaseFromThsHfIndex) GetCountByCondition(condition string, pars []interface{}) (count int, err error) {
  154. o := orm.NewOrm()
  155. sql := fmt.Sprintf(`SELECT COUNT(1) FROM %s WHERE 1=1 %s`, m.TableName(), condition)
  156. err = o.Raw(sql, pars).QueryRow(&count)
  157. return
  158. }
  159. func (m *BaseFromThsHfIndex) GetItemsByCondition(condition string, pars []interface{}, fieldArr []string, orderRule string) (items []*BaseFromThsHfIndex, err error) {
  160. o := orm.NewOrm()
  161. fields := strings.Join(fieldArr, ",")
  162. if len(fieldArr) == 0 {
  163. fields = `*`
  164. }
  165. order := fmt.Sprintf(`ORDER BY %s DESC`, m.Cols().CreateTime)
  166. if orderRule != "" {
  167. order = ` ORDER BY ` + orderRule
  168. }
  169. sql := fmt.Sprintf(`SELECT %s FROM %s WHERE 1=1 %s %s`, fields, m.TableName(), condition, order)
  170. _, err = o.Raw(sql, pars).QueryRows(&items)
  171. return
  172. }
  173. func (m *BaseFromThsHfIndex) GetPageItemsByCondition(condition string, pars []interface{}, fieldArr []string, orderRule string, startSize, pageSize int) (items []*BaseFromThsHfIndex, err error) {
  174. o := orm.NewOrm()
  175. fields := strings.Join(fieldArr, ",")
  176. if len(fieldArr) == 0 {
  177. fields = `*`
  178. }
  179. order := fmt.Sprintf(`ORDER BY %s DESC`, m.Cols().CreateTime)
  180. if orderRule != "" {
  181. order = ` ORDER BY ` + orderRule
  182. }
  183. sql := fmt.Sprintf(`SELECT %s FROM %s WHERE 1=1 %s %s LIMIT ?,?`, fields, m.TableName(), condition, order)
  184. _, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&items)
  185. return
  186. }
  187. // BaseFromThsHfIndexItem 同花顺高频数据信息
  188. type BaseFromThsHfIndexItem struct {
  189. IndexId int `description:"同花顺高频数据ID"`
  190. IndexCode string `description:"指标编码"`
  191. IndexName string `description:"指标名称"`
  192. Unit string `description:"单位"`
  193. Source string `description:"数据来源"`
  194. Frequency string `description:"频度"`
  195. StartDate string `description:"开始日期(至时分秒)"`
  196. EndDate string `description:"结束日期(至时分秒)"`
  197. Describe string `description:"指标描述"`
  198. Sort int `description:"排序"`
  199. CreateTime string `description:"创建时间"`
  200. ModifyTime string `description:"修改时间"`
  201. }
  202. func (m *BaseFromThsHfIndex) Format2Item() (item *BaseFromThsHfIndexItem) {
  203. item = new(BaseFromThsHfIndexItem)
  204. item.IndexId = m.BaseFromThsHfIndexId
  205. item.IndexCode = m.IndexCode
  206. item.IndexName = m.IndexName
  207. item.Unit = m.Unit
  208. item.Source = m.Source
  209. item.Frequency = m.Frequency
  210. item.StartDate = utils.TimeTransferString(utils.FormatDateTime, m.StartDate)
  211. item.EndDate = utils.TimeTransferString(utils.FormatDateTime, m.EndDate)
  212. item.Describe = m.Describe
  213. item.Sort = m.Sort
  214. item.CreateTime = utils.TimeTransferString(utils.FormatDateTime, m.CreateTime)
  215. item.ModifyTime = utils.TimeTransferString(utils.FormatDateTime, m.ModifyTime)
  216. return
  217. }
  218. // ThsHfSearchEdbReq 搜索指标请求体
  219. type ThsHfSearchEdbReq struct {
  220. StockCode string `form:"StockCode" description:"证券代码" `
  221. EdbCode string `form:"EdbCode" description:"指标代码"`
  222. StartTime string `form:"StartTime" description:"每日数据开始时间"`
  223. EndTime string `form:"EndTime" description:"每日数据结束时间"`
  224. Interval int `form:"Interval" description:"时间周期"`
  225. Fill string `form:"Fill" description:"非交易间隔处理"`
  226. CPS string `form:"CPS" description:"复权方式"`
  227. BaseDate string `form:"BaseDate" description:"复权基点"`
  228. }
  229. // ThsHfApiResp 同花顺接口响应
  230. type ThsHfApiResp struct {
  231. ErrorCode int `json:"errorcode"`
  232. ErrMsg string `json:"errmsg"`
  233. Tables []struct {
  234. ThsCode string `json:"thscode"`
  235. Time []string `json:"time"`
  236. Table map[string][]float64 `json:"table"`
  237. } `json:"tables"`
  238. }
  239. // ThsHfAppResp 同花顺公用机响应
  240. type ThsHfAppResp struct {
  241. ErrorCode int `json:"errorcode"`
  242. ErrMsg string `json:"errmsg"`
  243. //Data []interface{} `json:"data"`
  244. Data []struct {
  245. Time string `json:"time"`
  246. ThsCode string `json:"thscode"`
  247. Open float64 `json:"open"`
  248. Close float64 `json:"close"`
  249. } `json:"data"`
  250. }
  251. // ThsHfIndexWithData 同花顺高频指标
  252. type ThsHfIndexWithData struct {
  253. StockCode string `description:"证券代码"`
  254. EdbCode string `description:"指标代码"`
  255. IndexData []*ThsHfIndexData `description:"指标数据"`
  256. }
  257. // ThsHfIndexData 同花顺高频指标数据
  258. type ThsHfIndexData struct {
  259. DataTime time.Time `description:"数据时间(2006-01-02 15:04)"`
  260. Value float64 `description:"数据值"`
  261. }
  262. type ThsHfBaseAddIndexItem struct {
  263. ClassifyId int `description:"分类ID"`
  264. Unit string `description:"单位"`
  265. IndexName string `description:"指标名称"`
  266. Frequency string `description:"频度"`
  267. StockCode string `description:"证券代码"`
  268. EdbCode string `description:"指标代码"`
  269. }
  270. // ThsHfBaseAddReq 新增至数据源请求
  271. type ThsHfBaseAddReq struct {
  272. StartTime string `description:"每日数据开始时间"`
  273. EndTime string `description:"每日数据结束时间"`
  274. Interval int `description:"时间周期"`
  275. Fill string `description:"非交易间隔处理"`
  276. CPS string `description:"复权方式"`
  277. BaseDate string `description:"复权基点"`
  278. SysAdminId int `description:"创建人ID"`
  279. SysAdminName string `description:"创建人姓名"`
  280. ThsHfBaseAddIndexItem `description:"指标信息"`
  281. }
  282. // CreateIndexAndData 新增指标和数据
  283. //func (m *BaseFromThsHfIndex) CreateIndexAndData(indexItem *BaseFromThsHfIndex, indexData []*BaseFromThsHfData) (err error) {
  284. // o := orm.NewOrm()
  285. // tx, e := o.Begin()
  286. // if e != nil {
  287. // err = fmt.Errorf("tx begin err: %v", e)
  288. // return
  289. // }
  290. // defer func() {
  291. // if err != nil {
  292. // _ = tx.Rollback()
  293. // return
  294. // }
  295. // _ = tx.Commit()
  296. // }()
  297. //
  298. // lastId, e := tx.Insert(indexItem)
  299. // if e != nil {
  300. // err = fmt.Errorf("insert index err: %v", e)
  301. // return
  302. // }
  303. // indexId := int(lastId)
  304. // indexItem.BaseFromThsHfIndexId = indexId
  305. //
  306. // if len(indexData) == 0 {
  307. // return
  308. // }
  309. // for _, v := range indexData {
  310. // v.BaseFromThsHfIndexId = indexId
  311. // }
  312. // if _, e = tx.InsertMulti(200, indexData); e != nil {
  313. // err = fmt.Errorf("insert index data err: %v", e)
  314. // return
  315. // }
  316. // return
  317. //}
  318. // ThsHfBaseRefreshReq 数据源刷新请求
  319. type ThsHfBaseRefreshReq struct {
  320. BaseIndexCode string `description:"源指标编码"`
  321. RefreshType int `description:"刷新类型: 1-最近6小时; 2-全部刷新"`
  322. }
  323. // ThsHfEdbAddReq 新增至指标库
  324. type ThsHfEdbAddReq struct {
  325. ConvertRule ThsHfIndexConvert2EdbRule
  326. NewIndex *ThsHfIndexMultiSave2EdbPreItem `description:"新增指标"`
  327. }
  328. type ThsHfIndexConvert2EdbRule struct {
  329. ConvertType int `description:"转换类型: 1-指定时间值; 2-区间计算值"`
  330. ConvertFixed struct {
  331. FixedDay int `description:"指定时间值日期: 1-当日; 2-前一日"`
  332. FixedTime string `description:"指定时间值时点(HH:mm:ss)"`
  333. } `description:"指定时间值"`
  334. ConvertArea struct {
  335. StartDay int `description:"起始时间日期: 1-当日; 2-前一日"`
  336. StartTime string `description:"起始时间时点(HH:mm:ss)"`
  337. EndDay int `description:"截止时间日期: 1-当日; 2-前一日"`
  338. EndTime string `description:"截止时间时点(HH:mm:ss)"`
  339. CalculateType int `description:"计算类型: 1-区间均值; 2-最大值; 3-最小值"`
  340. } `description:"区间计算值"`
  341. }
  342. // ThsHfIndexMultiSave2EdbPreItem 批量新增指标库信息
  343. type ThsHfIndexMultiSave2EdbPreItem struct {
  344. IndexId int `description:"指标ID"`
  345. IndexCode string `description:"指标编码"`
  346. IndexName string `description:"原指标名称"`
  347. NewIndexName string `description:"新指标名称"`
  348. StockCode string `description:"证券代码"`
  349. EdbCode string `description:"指标代码"`
  350. Unit string `description:"单位"`
  351. Frequency string `description:"原频度"`
  352. NewFrequency string `description:"新频度(固定日度)"`
  353. ClassifyId int `description:"指标库分类ID"`
  354. SysAdminId int `description:"创建人ID"`
  355. SysAdminName string `description:"创建人姓名"`
  356. Tips string `description:"提示信息"`
  357. ErrMsg string `description:"错误信息"`
  358. }
  359. // GetEdbInfoMaxAndMinInfo
  360. // @Description: 获取指标的最新数据记录信息
  361. // @author: Roc
  362. // @receiver m
  363. // @datetime 2024-07-02 14:50:50
  364. // @param edbCode string
  365. // @return item *EdbInfoMaxAndMinInfo
  366. // @return err error
  367. func (m BaseFromThsHfIndex) GetEdbInfoMaxAndMinInfo(edbCode string) (item *EdbInfoMaxAndMinInfo, err error) {
  368. if utils.UseMongo {
  369. return m.getEdbInfoMaxAndMinInfoByMongo(edbCode)
  370. }
  371. return m.getEdbInfoMaxAndMinInfoByMysql(edbCode)
  372. }
  373. // getEdbInfoMaxAndMinInfoByMongo
  374. // @Description: 获取指标的最新数据记录信息(从mongo中获取)
  375. // @author: Roc
  376. // @receiver m
  377. // @datetime 2024-07-02 14:41:20
  378. // @param edbCode string
  379. // @return item *EdbInfoMaxAndMinInfo
  380. // @return err error
  381. func (m BaseFromThsHfIndex) getEdbInfoMaxAndMinInfoByMongo(edbCode string) (item *EdbInfoMaxAndMinInfo, err error) {
  382. mogDataObj := new(mgo.BaseFromThsHfData)
  383. pipeline := []bson.M{
  384. {"$match": bson.M{"index_code": edbCode}},
  385. {"$group": bson.M{
  386. "_id": nil,
  387. "min_date": bson.M{"$min": "$data_time"},
  388. "max_date": bson.M{"$max": "$data_time"},
  389. "min_value": bson.M{"$min": "$value"},
  390. "max_value": bson.M{"$max": "$value"},
  391. }},
  392. {"$project": bson.M{"_id": 0}}, // 可选,如果不需要_id字段
  393. }
  394. result, err := mogDataObj.GetEdbInfoMaxAndMinInfo(pipeline)
  395. if err != nil {
  396. fmt.Println("BaseFromThsHfIndex GetEdbInfoMaxAndMinInfo Err:" + err.Error())
  397. return
  398. }
  399. if !result.MaxDate.IsZero() {
  400. whereQuery := bson.M{"index_code": edbCode, "data_time": result.MaxDate}
  401. selectParam := bson.D{{"value", 1}, {"_id", 0}}
  402. latestValue, tmpErr := mogDataObj.GetLatestValue(whereQuery, selectParam)
  403. if tmpErr != nil {
  404. err = tmpErr
  405. return
  406. }
  407. result.LatestValue = latestValue.Value
  408. result.EndValue = latestValue.Value
  409. }
  410. item = &EdbInfoMaxAndMinInfo{
  411. MinDate: result.MinDate.Format(utils.FormatDateTime),
  412. MaxDate: result.MaxDate.Format(utils.FormatDateTime),
  413. MinValue: result.MinValue,
  414. MaxValue: result.MaxValue,
  415. LatestValue: result.LatestValue,
  416. LatestDate: result.LatestDate.Format(utils.FormatDateTime),
  417. EndValue: result.EndValue,
  418. }
  419. return
  420. }
  421. // getEdbInfoMaxAndMinInfoByMysql
  422. // @Description: 获取指标的最新数据记录信息(从mysql中获取)
  423. func (m BaseFromThsHfIndex) getEdbInfoMaxAndMinInfoByMysql(edbCode string) (item *EdbInfoMaxAndMinInfo, err error) {
  424. dataObj := BaseFromThsHfData{}
  425. result, err := dataObj.GetIndexMinMax(edbCode)
  426. if err != nil {
  427. return
  428. }
  429. item = &EdbInfoMaxAndMinInfo{
  430. MinDate: result.MinDate,
  431. MaxDate: result.MaxDate,
  432. MinValue: result.MinValue,
  433. MaxValue: result.MaxValue,
  434. LatestValue: result.LatestValue,
  435. LatestDate: result.LatestDate,
  436. EndValue: result.EndValue,
  437. }
  438. return
  439. }