edb_data_base.go 6.9 KB

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