edb_data_base.go 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. package models
  2. import (
  3. "eta/eta_index_lib/utils"
  4. "fmt"
  5. "github.com/beego/beego/v2/client/orm"
  6. "strings"
  7. "time"
  8. )
  9. // 指标检索数据
  10. type EdbDataItem struct {
  11. EdbCode string `description:"指标编码"`
  12. StartDate string `description:"起始日期"`
  13. EndDate string `description:"终止日期"`
  14. EdbName string `description:"指标名称"`
  15. Unit string `description:"单位"`
  16. Frequency string `description:"频率"`
  17. DataList []*EdbData
  18. }
  19. type EdbData struct {
  20. EdbDataId int `orm:"column(edb_data_id);pk"`
  21. EdbInfoId int
  22. EdbCode string
  23. DataTime string
  24. Value string
  25. Status int
  26. CreateTime time.Time
  27. ModifyTime time.Time
  28. DataTimestamp int64
  29. }
  30. func GetAddSql(edbInfoId, edbCode, dataTime, timestampStr string, value string) (addSql string) {
  31. nowStr := time.Now().Format(utils.FormatDateTime)
  32. addSql += "("
  33. addSql += edbInfoId + "," + "'" + edbCode + "'" + "," + "'" + dataTime + "'" + "," + value + "," + "'" + nowStr + "'" +
  34. "," + "'" + nowStr + "'"
  35. addSql += "," + "'" + timestampStr + "'"
  36. addSql += "),"
  37. return
  38. }
  39. type AddEdbInfoReq struct {
  40. EdbCode string `description:"指标编码"`
  41. Source int `description:"指标来源ID"`
  42. StockCode string `description:"证券代码"`
  43. Frequency string `description:"频度"`
  44. ExtraPars 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 DelEdbDataByMysql(to orm.TxOrmer, edbInfoId int, tableName string, removeDateList []string) (err error) {
  163. if len(removeDateList) <= 0 {
  164. return
  165. }
  166. // 移除不存在的日期数据
  167. removeDateStr := strings.Join(removeDateList, `","`)
  168. removeDateStr = `"` + removeDateStr + `"`
  169. sql := fmt.Sprintf(` DELETE FROM %s WHERE edb_info_id = ? and data_time in (%s) `, tableName, removeDateStr)
  170. _, err = to.Raw(sql, edbInfoId).Exec()
  171. return
  172. }
  173. // GetEdbDataList
  174. // @Description:
  175. // @author: Roc
  176. // @datetime 2024-05-08 16:10:49
  177. // @param source int
  178. // @param subSource int
  179. // @param endInfoId int
  180. // @param startDate string
  181. // @param endDate string
  182. // @return list []*EdbDataList
  183. // @return err error
  184. func GetEdbDataList(source, subSource, endInfoId int, startDate, endDate string) (list []*EdbDataList, err error) {
  185. list = make([]*EdbDataList, 0)
  186. tmpDataList, err := GetEdbDataListAll(source, subSource, FindEdbDataListAllCond{
  187. EdbInfoId: endInfoId,
  188. StartDataTime: startDate,
  189. StartDataTimeCond: ">=",
  190. EndDataTime: endDate,
  191. EndDataTimeCond: "<=",
  192. }, 1)
  193. if err != nil {
  194. return
  195. }
  196. // 数据拼接
  197. for _, v := range tmpDataList {
  198. list = append(list, &EdbDataList{
  199. EdbDataId: v.EdbDataId,
  200. EdbInfoId: v.EdbInfoId,
  201. DataTime: v.DataTime,
  202. DataTimestamp: v.DataTimestamp,
  203. Value: v.Value,
  204. })
  205. }
  206. return
  207. }
  208. type AddEdbBaseInfoReq struct {
  209. EdbCode string `description:"指标编码"`
  210. EdbName string `description:"指标名称"`
  211. Unit string `description:"单位"`
  212. ClassifyId int `description:"所属分类"`
  213. SysUserId int `description:"用户id"`
  214. SysUserRealName string `description:"用户真实名称"`
  215. }
  216. // EditEdbBaseInfoReq 编辑基础指标请求参数
  217. type EditEdbBaseInfoReq struct {
  218. SysUserId int `description:"用户id"`
  219. SysUserRealName string `description:"用户真实名称"`
  220. EdbInfoId int `description:"指标id"`
  221. EdbName string `description:"指标名称"`
  222. EdbNameEn string `description:"英文指标名称"`
  223. Frequency string `description:"频率"`
  224. Unit string `description:"单位"`
  225. UnitEn string `description:"英文单位"`
  226. ClassifyId int `description:"分类id"`
  227. }