edb_data_base.go 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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. func GetAddSqlV2(edbInfoId, edbCode, dataTime, timestampStr string, value, nowStr string) (addSql string) {
  40. addSql += "("
  41. addSql += edbInfoId + "," + "'" + edbCode + "'" + "," + "'" + dataTime + "'" + "," + value + "," + "'" + nowStr + "'" +
  42. "," + "'" + nowStr + "'"
  43. addSql += "," + "'" + timestampStr + "'"
  44. addSql += "),"
  45. return
  46. }
  47. type AddEdbInfoReq struct {
  48. EdbCode string `description:"指标编码"`
  49. Source int `description:"指标来源ID"`
  50. StockCode string `description:"证券代码"`
  51. Frequency string `description:"频度"`
  52. ExtraPars string `description:"额外参数(如同花顺日期序列)"`
  53. }
  54. // GetEdbInfoCountByCondition 获取指标数量
  55. func GetEdbInfoCountByCondition(condition string, pars []interface{}) (count int, err error) {
  56. o := orm.NewOrm()
  57. sql := ` SELECT COUNT(1) AS count FROM edb_info WHERE 1=1 `
  58. if condition != "" {
  59. sql += condition
  60. }
  61. err = o.Raw(sql, pars).QueryRow(&count)
  62. return
  63. }
  64. // GetEdbDataAllByEdbCode 根据指标编码获取指标数据列表
  65. func GetEdbDataAllByEdbCode(edbCode string, source, subSource, limit int) (items []*EdbInfoSearchData, err error) {
  66. var pars []interface{}
  67. pars = append(pars, edbCode)
  68. o := orm.NewOrm()
  69. tableName := GetEdbDataTableName(source, subSource)
  70. sql := ` SELECT * FROM %s WHERE edb_code=? ORDER BY data_time DESC`
  71. if limit > 0 {
  72. sql += ` LIMIT ? `
  73. pars = append(pars, limit)
  74. }
  75. sql = fmt.Sprintf(sql, tableName)
  76. _, err = o.Raw(sql, pars).QueryRows(&items)
  77. return
  78. }
  79. func GetEdbDataByCondition(source, subSource int, condition string, pars []interface{}) (items []*EdbInfoSearchData, err error) {
  80. o := orm.NewOrm()
  81. tableName := GetEdbDataTableName(source, subSource)
  82. sql := ` SELECT * FROM %s WHERE 1=1 `
  83. if condition != "" {
  84. sql += condition
  85. }
  86. sql += ` ORDER BY data_time DESC `
  87. sql = fmt.Sprintf(sql, tableName)
  88. _, err = o.Raw(sql, pars).QueryRows(&items)
  89. return
  90. }
  91. // GetEdbDataByDate 根据数据日期获取指标数据
  92. func GetEdbDataByDate(source, subSource int, edbCode, dataTime string) (item *EdbInfoSearchData, err error) {
  93. o := orm.NewOrm()
  94. tableName := GetEdbDataTableName(source, subSource)
  95. sql := ` SELECT * FROM %s WHERE 1=1 AND edb_code = ? AND data_time =? ORDER BY data_time DESC `
  96. sql = fmt.Sprintf(sql, tableName)
  97. err = o.Raw(sql, edbCode, dataTime).QueryRow(&item)
  98. return
  99. }
  100. func ModifyEdbDataById(source, subSource, edbDataId int, value string) (err error) {
  101. o := orm.NewOrm()
  102. tableName := GetEdbDataTableName(source, subSource)
  103. sql := ` UPDATE %s SET value=?,modify_time=NOW() WHERE edb_data_id=? `
  104. sql = fmt.Sprintf(sql, tableName)
  105. _, err = o.Raw(sql, value, edbDataId).Exec()
  106. return
  107. }
  108. func DeleteEdbDataById(source, subSource, edbDataId int) (err error) {
  109. sql := ` DELETE FROM %s WHERE edb_data_id=? `
  110. tableName := GetEdbDataTableName(source, subSource)
  111. sql = fmt.Sprintf(sql, tableName)
  112. o := orm.NewOrm()
  113. _, err = o.Raw(sql, edbDataId).Exec()
  114. return
  115. }
  116. type RefreshEdbInfoReq struct {
  117. EdbInfoId int `description:"指标ID"`
  118. EdbCode string `description:"指标编码"`
  119. StartDate string `description:"开始日期"`
  120. Source int `description:"指标来源ID"`
  121. }
  122. // GetAllEdbDataList 获取所有的指标数据列表
  123. func GetAllEdbDataList(edbInfoId, source, subSource int) (existDataList []*EdbData, err error) {
  124. o := orm.NewOrm()
  125. dataTableName := GetEdbDataTableName(source, subSource)
  126. fmt.Println("dataTableName:", dataTableName)
  127. sql := `SELECT * FROM %s WHERE edb_info_id=? order by data_time asc`
  128. sql = fmt.Sprintf(sql, dataTableName)
  129. _, err = o.Raw(sql, edbInfoId).QueryRows(&existDataList)
  130. return
  131. }
  132. // GetAllEdbDataListByTo 获取所有的指标数据列表
  133. func GetAllEdbDataListByTo(to orm.TxOrmer, edbInfoId, source, subSource int) (existDataList []*EdbData, err error) {
  134. dataTableName := GetEdbDataTableName(source, subSource)
  135. fmt.Println("dataTableName:", dataTableName)
  136. sql := `SELECT * FROM %s WHERE edb_info_id=? order by data_time asc`
  137. sql = fmt.Sprintf(sql, dataTableName)
  138. _, err = to.Raw(sql, edbInfoId).QueryRows(&existDataList)
  139. return
  140. }
  141. // GetFinalLastByTo 获取所有的指标数据列表
  142. func GetFinalLastByTo(to orm.TxOrmer, edbInfoId, source, subSource int, latestDate string) (finalLast EdbInfoSearchData, err error) {
  143. dataTableName := GetEdbDataTableName(source, subSource)
  144. sql := fmt.Sprintf(` SELECT data_time , value FROM %s WHERE edb_info_id=? and data_time<=? ORDER BY data_time DESC `, dataTableName)
  145. err = to.Raw(sql, edbInfoId, latestDate).QueryRow(&finalLast)
  146. if err != nil && err.Error() != utils.ErrNoRow() {
  147. return
  148. }
  149. return
  150. }
  151. // 新版本
  152. type EdbDataV1 struct {
  153. EdbDataId int `orm:"column(edb_data_id);pk"`
  154. EdbInfoId int
  155. EdbCode string
  156. DataTime string
  157. Value string
  158. Status int
  159. CreateTime time.Time
  160. ModifyTime time.Time
  161. DataTimestamp int64
  162. }
  163. type EdbDataList struct {
  164. EdbDataId int `description:" 指标数据ID"`
  165. EdbInfoId int `description:"指标ID"`
  166. DataTime string //`json:"-" description:"数据日期"`
  167. DataTimestamp int64 `description:"数据日期"`
  168. Value float64 `description:"数据值"`
  169. }
  170. func DelEdbDataByMysql(to orm.TxOrmer, edbInfoId int, tableName string, removeDateList []string) (err error) {
  171. if len(removeDateList) <= 0 {
  172. return
  173. }
  174. // 移除不存在的日期数据
  175. removeDateStr := strings.Join(removeDateList, `","`)
  176. removeDateStr = `"` + removeDateStr + `"`
  177. sql := fmt.Sprintf(` DELETE FROM %s WHERE edb_info_id = ? and data_time in (%s) `, tableName, removeDateStr)
  178. _, err = to.Raw(sql, edbInfoId).Exec()
  179. return
  180. }
  181. // GetEdbDataList
  182. // @Description:
  183. // @author: Roc
  184. // @datetime 2024-05-08 16:10:49
  185. // @param source int
  186. // @param subSource int
  187. // @param endInfoId int
  188. // @param startDate string
  189. // @param endDate string
  190. // @return list []*EdbDataList
  191. // @return err error
  192. func GetEdbDataList(source, subSource, endInfoId int, startDate, endDate string) (list []*EdbDataList, err error) {
  193. list = make([]*EdbDataList, 0)
  194. tmpDataList, err := GetEdbDataListAll(source, subSource, FindEdbDataListAllCond{
  195. EdbInfoId: endInfoId,
  196. StartDataTime: startDate,
  197. StartDataTimeCond: ">=",
  198. EndDataTime: endDate,
  199. EndDataTimeCond: "<=",
  200. }, 1)
  201. if err != nil {
  202. return
  203. }
  204. // 数据拼接
  205. for _, v := range tmpDataList {
  206. list = append(list, &EdbDataList{
  207. EdbDataId: v.EdbDataId,
  208. EdbInfoId: v.EdbInfoId,
  209. DataTime: v.DataTime,
  210. DataTimestamp: v.DataTimestamp,
  211. Value: v.Value,
  212. })
  213. }
  214. return
  215. }
  216. type AddEdbBaseInfoReq struct {
  217. EdbCode string `description:"指标编码"`
  218. EdbName string `description:"指标名称"`
  219. Unit string `description:"单位"`
  220. ClassifyId int `description:"所属分类"`
  221. SysUserId int `description:"用户id"`
  222. SysUserRealName string `description:"用户真实名称"`
  223. }
  224. // EditEdbBaseInfoReq 编辑基础指标请求参数
  225. type EditEdbBaseInfoReq struct {
  226. SysUserId int `description:"用户id"`
  227. SysUserRealName string `description:"用户真实名称"`
  228. EdbInfoId int `description:"指标id"`
  229. EdbName string `description:"指标名称"`
  230. EdbNameEn string `description:"英文指标名称"`
  231. Frequency string `description:"频率"`
  232. Unit string `description:"单位"`
  233. UnitEn string `description:"英文单位"`
  234. ClassifyId int `description:"分类id"`
  235. }