edb_data_base.go 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. package models
  2. import (
  3. "errors"
  4. "eta/eta_index_lib/utils"
  5. "fmt"
  6. "github.com/beego/beego/v2/client/orm"
  7. "strconv"
  8. "time"
  9. )
  10. // 指标检索数据
  11. type EdbDataItem struct {
  12. EdbCode string `description:"指标编码"`
  13. StartDate string `description:"起始日期"`
  14. EndDate string `description:"终止日期"`
  15. EdbName string `description:"指标名称"`
  16. Unit string `description:"单位"`
  17. Frequency string `description:"频率"`
  18. DataList []*EdbData
  19. }
  20. type EdbData struct {
  21. EdbDataId int `orm:"column(edb_data_id);pk"`
  22. EdbInfoId int
  23. EdbCode string
  24. DataTime string
  25. Value string
  26. Status int
  27. CreateTime time.Time
  28. ModifyTime time.Time
  29. DataTimestamp int64
  30. }
  31. func GetAddSql(edbInfoId, edbCode, dataTime, timestampStr string, value string) (addSql string) {
  32. nowStr := time.Now().Format(utils.FormatDateTime)
  33. addSql += "("
  34. addSql += edbInfoId + "," + "'" + edbCode + "'" + "," + "'" + dataTime + "'" + "," + value + "," + "'" + nowStr + "'" +
  35. "," + "'" + nowStr + "'"
  36. addSql += "," + "'" + timestampStr + "'"
  37. addSql += "),"
  38. return
  39. }
  40. type AddEdbInfoReq struct {
  41. EdbCode string `description:"指标编码"`
  42. Source int `description:"指标来源ID"`
  43. StockCode string `description:"证券代码"`
  44. Frequency string `description:"频度"`
  45. }
  46. // GetEdbInfoCountByCondition 获取指标数量
  47. func GetEdbInfoCountByCondition(condition string, pars []interface{}) (count int, err error) {
  48. o := orm.NewOrm()
  49. sql := ` SELECT COUNT(1) AS count FROM edb_info WHERE 1=1 `
  50. if condition != "" {
  51. sql += condition
  52. }
  53. err = o.Raw(sql, pars).QueryRow(&count)
  54. return
  55. }
  56. // GetEdbDataAllByEdbCode 根据指标编码获取指标数据列表
  57. func GetEdbDataAllByEdbCode(edbCode string, source, subSource, limit int) (items []*EdbInfoSearchData, err error) {
  58. var pars []interface{}
  59. pars = append(pars, edbCode)
  60. o := orm.NewOrm()
  61. tableName := GetEdbDataTableName(source, subSource)
  62. sql := ` SELECT * FROM %s WHERE edb_code=? ORDER BY data_time DESC`
  63. if limit > 0 {
  64. sql += ` LIMIT ? `
  65. pars = append(pars, limit)
  66. }
  67. sql = fmt.Sprintf(sql, tableName)
  68. _, err = o.Raw(sql, pars).QueryRows(&items)
  69. return
  70. }
  71. func GetEdbDataByCondition(source, subSource int, condition string, pars []interface{}) (items []*EdbInfoSearchData, err error) {
  72. o := orm.NewOrm()
  73. tableName := GetEdbDataTableName(source, subSource)
  74. sql := ` SELECT * FROM %s WHERE 1=1 `
  75. if condition != "" {
  76. sql += condition
  77. }
  78. sql += ` ORDER BY data_time DESC `
  79. sql = fmt.Sprintf(sql, tableName)
  80. _, err = o.Raw(sql, pars).QueryRows(&items)
  81. return
  82. }
  83. // GetEdbDataByDate 根据数据日期获取指标数据
  84. func GetEdbDataByDate(source, subSource int, edbCode, dataTime string) (item *EdbInfoSearchData, err error) {
  85. o := orm.NewOrm()
  86. tableName := GetEdbDataTableName(source, subSource)
  87. sql := ` SELECT * FROM %s WHERE 1=1 AND edb_code = ? AND data_time =? ORDER BY data_time DESC `
  88. sql = fmt.Sprintf(sql, tableName)
  89. err = o.Raw(sql, edbCode, dataTime).QueryRow(&item)
  90. return
  91. }
  92. func ModifyEdbDataById(source, subSource, edbDataId int, value string) (err error) {
  93. o := orm.NewOrm()
  94. tableName := GetEdbDataTableName(source, subSource)
  95. sql := ` UPDATE %s SET value=?,modify_time=NOW() WHERE edb_data_id=? `
  96. sql = fmt.Sprintf(sql, tableName)
  97. _, err = o.Raw(sql, value, edbDataId).Exec()
  98. return
  99. }
  100. func DeleteEdbDataById(source, subSource, edbDataId int) (err error) {
  101. sql := ` DELETE FROM %s WHERE edb_data_id=? `
  102. tableName := GetEdbDataTableName(source, subSource)
  103. sql = fmt.Sprintf(sql, tableName)
  104. o := orm.NewOrm()
  105. _, err = o.Raw(sql, edbDataId).Exec()
  106. return
  107. }
  108. type RefreshEdbInfoReq struct {
  109. EdbInfoId int `description:"指标ID"`
  110. EdbCode string `description:"指标编码"`
  111. StartDate string `description:"开始日期"`
  112. Source int `description:"指标来源ID"`
  113. }
  114. // GetAllEdbDataList 获取所有的指标数据列表
  115. func GetAllEdbDataList(edbInfoId, source, subSource int) (existDataList []*EdbData, err error) {
  116. o := orm.NewOrm()
  117. dataTableName := GetEdbDataTableName(source, subSource)
  118. fmt.Println("dataTableName:", dataTableName)
  119. sql := `SELECT * FROM %s WHERE edb_info_id=? order by data_time asc`
  120. sql = fmt.Sprintf(sql, dataTableName)
  121. _, err = o.Raw(sql, edbInfoId).QueryRows(&existDataList)
  122. return
  123. }
  124. // GetAllEdbDataListByTo 获取所有的指标数据列表
  125. func GetAllEdbDataListByTo(to orm.TxOrmer, edbInfoId, source, subSource int) (existDataList []*EdbData, err error) {
  126. dataTableName := GetEdbDataTableName(source, subSource)
  127. fmt.Println("dataTableName:", dataTableName)
  128. sql := `SELECT * FROM %s WHERE edb_info_id=? order by data_time asc`
  129. sql = fmt.Sprintf(sql, dataTableName)
  130. _, err = to.Raw(sql, edbInfoId).QueryRows(&existDataList)
  131. return
  132. }
  133. // GetFinalLastByTo 获取所有的指标数据列表
  134. func GetFinalLastByTo(to orm.TxOrmer, edbInfoId, source, subSource int, latestDate string) (finalLast EdbInfoSearchData, err error) {
  135. dataTableName := GetEdbDataTableName(source, subSource)
  136. sql := fmt.Sprintf(` SELECT data_time , value FROM %s WHERE edb_info_id=? and data_time<=? ORDER BY data_time DESC `, dataTableName)
  137. err = to.Raw(sql, edbInfoId, latestDate).QueryRow(&finalLast)
  138. if err != nil && err.Error() != utils.ErrNoRow() {
  139. return
  140. }
  141. return
  142. }
  143. // 新版本
  144. type EdbDataV1 struct {
  145. EdbDataId int `orm:"column(edb_data_id);pk"`
  146. EdbInfoId int
  147. EdbCode string
  148. DataTime string
  149. Value string
  150. Status int
  151. CreateTime time.Time
  152. ModifyTime time.Time
  153. DataTimestamp int64
  154. }
  155. type EdbDataList struct {
  156. EdbDataId int `description:" 指标数据ID"`
  157. EdbInfoId int `description:"指标ID"`
  158. DataTime string //`json:"-" description:"数据日期"`
  159. DataTimestamp int64 `description:"数据日期"`
  160. Value float64 `description:"数据值"`
  161. }
  162. func GetEdbDataList(source, subSource, endInfoId int, startDate, endDate string) (list []*EdbDataList, err error) {
  163. tableName := GetEdbDataTableName(source, subSource)
  164. if tableName == "" {
  165. err = errors.New("无效的渠道:" + strconv.Itoa(source))
  166. list = make([]*EdbDataList, 0)
  167. return list, err
  168. }
  169. var pars []interface{}
  170. sql := `SELECT edb_data_id,edb_info_id,data_time,value,data_timestamp FROM %s WHERE edb_info_id=? `
  171. if startDate != "" {
  172. sql += ` AND data_time>=? `
  173. pars = append(pars, startDate)
  174. }
  175. if endDate != "" {
  176. sql += ` AND data_time<=? `
  177. pars = append(pars, endDate)
  178. }
  179. sql += ` ORDER BY data_time ASC `
  180. sql = fmt.Sprintf(sql, tableName)
  181. o := orm.NewOrm()
  182. _, err = o.Raw(sql, endInfoId, pars).QueryRows(&list)
  183. return
  184. }