edb_data_base.go 8.7 KB

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