edb_data_base.go 7.9 KB

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