edb_info.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. package data_manage
  2. import (
  3. "eta/eta_hub/utils"
  4. "fmt"
  5. "strings"
  6. "time"
  7. "github.com/beego/beego/v2/client/orm"
  8. "github.com/rdlucklib/rdluck_tools/paging"
  9. )
  10. type EdbInfo struct {
  11. EdbInfoId int `orm:"column(edb_info_id);pk"`
  12. EdbInfoType int `description:"指标类型,0:普通指标,1:预测指标"`
  13. SourceName string `description:"来源名称"`
  14. Source int `description:"来源ID"`
  15. EdbCode string `description:"指标编码"`
  16. EdbName string `description:"指标名称"`
  17. EdbNameEn string `description:"英文指标名称"`
  18. EdbNameSource string `description:"指标名称来源"`
  19. Frequency string `description:"频率"`
  20. Unit string `description:"单位"`
  21. UnitEn string `description:"英文单位"`
  22. StartDate string `description:"起始日期"`
  23. EndDate string `description:"终止日期"`
  24. EndValue float64 `description:"终止值"`
  25. ClassifyId int `description:"分类ID"`
  26. SysUserId int `description:"创建人ID"`
  27. SysUserRealName string `description:"创建人姓名"`
  28. UniqueCode string `description:"指标唯一编码"`
  29. CreateTime time.Time `description:"创建时间"`
  30. ModifyTime time.Time `description:"更新时间"`
  31. MinValue float64 `description:"指标最小值"`
  32. MaxValue float64 `description:"指标最大值"`
  33. CalculateFormula string `description:"计算公式"`
  34. EdbType int `description:"指标类型:1:基础指标,2:计算指标"`
  35. Sort int `description:"排序字段"`
  36. LatestDate string `description:"数据最新日期"`
  37. LatestValue float64 `description:"数据最新值"`
  38. MoveType int `description:"移动方式:1:领先(默认),2:滞后"`
  39. MoveFrequency string `description:"移动频度"`
  40. NoUpdate int8 `description:"是否停止更新,0:继续更新;1:停止更新"`
  41. ServerUrl string `description:"服务器地址"`
  42. ChartImage string `description:"图表图片"`
  43. Calendar string `description:"公历/农历" orm:"default(公历);"`
  44. DataDateType string `orm:"column(data_date_type);size(255);null;default(交易日)"`
  45. ManualSave int `description:"是否有手动保存过上下限: 0-否; 1-是"`
  46. TerminalCode string `description:"终端编码,用于配置在机器上"`
  47. DataUpdateTime string `description:"最近一次数据发生变化的时间"`
  48. ErDataUpdateDate time.Time `description:"本次更新,数据发生变化的最早日期"`
  49. SourceIndexName string `description:"数据源中的指标名称"`
  50. SubSource int `description:"子数据来源:0:经济数据库,1:日期序列"`
  51. SubSourceName string `description:"子数据来源名称"`
  52. IndicatorCode string `description:"指标代码"`
  53. StockCode string `description:"证券代码"`
  54. Extra string `description:"指标的额外配置"`
  55. }
  56. func (m *EdbInfo) GetItemsByCondition(cond string, pars []interface{}, fieldArr []string, orderRule string) (items []*EdbInfo, err error) {
  57. o := orm.NewOrmUsingDB("data")
  58. fields := strings.Join(fieldArr, ",")
  59. if len(fieldArr) == 0 {
  60. fields = `*`
  61. }
  62. order := `ORDER BY create_time DESC`
  63. if orderRule != "" {
  64. order = ` ORDER BY ` + orderRule
  65. }
  66. sql := fmt.Sprintf(`SELECT %s FROM edb_info WHERE 1=1 %s %s`, fields, cond, order)
  67. _, err = o.Raw(sql, pars).QueryRows(&items)
  68. return
  69. }
  70. func (m *EdbInfo) GetItemsPageByCondition(cond string, pars []interface{}, fieldArr []string, orderRule string, startSize, pageSize int) (items []*EdbInfo, err error) {
  71. o := orm.NewOrmUsingDB("data")
  72. fields := strings.Join(fieldArr, ",")
  73. if len(fieldArr) == 0 {
  74. fields = `*`
  75. }
  76. order := `ORDER BY create_time DESC`
  77. if orderRule != "" {
  78. order = ` ORDER BY ` + orderRule
  79. }
  80. sql := fmt.Sprintf(`SELECT %s FROM edb_info WHERE 1=1 %s %s`, fields, cond, order)
  81. sql += fmt.Sprintf(` LIMIT %d,%d`, startSize, pageSize)
  82. _, err = o.Raw(sql, pars).QueryRows(&items)
  83. return
  84. }
  85. func (m *EdbInfo) GetItemsTotal(cond string, pars []interface{}) (total int, err error) {
  86. o := orm.NewOrmUsingDB("data")
  87. sql := fmt.Sprintf(`SELECT COUNT(*) FROM edb_info WHERE 1=1 %s`, cond)
  88. err = o.Raw(sql, pars).QueryRow(&total)
  89. return
  90. }
  91. func (m *EdbInfo) GetItemByUniCode(code string) (item *EdbInfo, err error) {
  92. o := orm.NewOrmUsingDB("data")
  93. sql := fmt.Sprintf(`SELECT * FROM edb_info WHERE unique_code = ? LIMIT 1`)
  94. err = o.Raw(sql, code).QueryRow(&item)
  95. return
  96. }
  97. func (m *EdbInfo) GetItemByEdbCode(code string) (item *EdbInfo, err error) {
  98. o := orm.NewOrmUsingDB("data")
  99. sql := fmt.Sprintf(`SELECT * FROM edb_info WHERE edb_code = ? LIMIT 1`)
  100. err = o.Raw(sql, code).QueryRow(&item)
  101. return
  102. }
  103. // EdbInfoItem 指标信息
  104. type EdbInfoItem struct {
  105. EdbInfoId int `description:"指标信息ID"`
  106. UniqueCode string `description:"指标唯一编码"`
  107. EdbCode string `description:"指标编码"`
  108. EdbInfoType int `description:"指标类型: 0-普通指标; 1-预测指标"`
  109. EdbType int `description:"指标类型: 1-基础指标; 2-计算指标"`
  110. EdbName string `description:"指标名称"`
  111. ClassifyId int `description:"分类ID"`
  112. Source int `description:"来源ID"`
  113. SourceName string `description:"来源名称"`
  114. Frequency string `description:"频率"`
  115. Unit string `description:"单位"`
  116. StartDate string `description:"起始日期"`
  117. EndDate string `description:"终止日期"`
  118. MinValue float64 `description:"指标最小值"`
  119. MaxValue float64 `description:"指标最大值"`
  120. LatestDate string `description:"数据最新日期"`
  121. LatestValue float64 `description:"数据最新值"`
  122. ChartImage string `description:"图表图片"`
  123. Calendar string `description:"公历/农历"`
  124. DataDateType string `description:"数据日期类型: 交易日/自然日"`
  125. Sort int `description:"排序"`
  126. NoUpdate int `description:"是否停更: 0-继续更新; 1-停止更新"`
  127. CreateTime string `description:"创建时间"`
  128. ModifyTime string `description:"更新时间"`
  129. SysUserRealName string `description:"创建人姓名"`
  130. ErDataUpdateDate string `description:"本次更新,数据发生变化的最早日期"`
  131. //EdbData []*EdbDataItem `description:"指标数据"`
  132. }
  133. type EdbInfoItemResp struct {
  134. List []*EdbInfoItem `description:"指标列表"`
  135. Paging *paging.PagingItem `description:"分页"`
  136. }
  137. func FormatEdbInfo2Item(origin *EdbInfo) (item *EdbInfoItem) {
  138. if origin == nil {
  139. return
  140. }
  141. item = new(EdbInfoItem)
  142. item.EdbInfoId = origin.EdbInfoId
  143. item.EdbInfoType = origin.EdbInfoType
  144. item.SourceName = origin.SourceName
  145. item.Source = origin.Source
  146. item.EdbCode = origin.EdbCode
  147. item.EdbName = origin.EdbName
  148. item.Frequency = origin.Frequency
  149. item.Unit = origin.Unit
  150. item.StartDate = origin.StartDate
  151. item.EndDate = origin.EndDate
  152. item.ClassifyId = origin.ClassifyId
  153. item.UniqueCode = origin.UniqueCode
  154. item.MinValue = origin.MinValue
  155. item.MaxValue = origin.MaxValue
  156. item.EdbType = origin.EdbType
  157. item.Sort = origin.Sort
  158. item.LatestDate = origin.LatestDate
  159. item.LatestValue = origin.LatestValue
  160. item.NoUpdate = int(origin.NoUpdate)
  161. item.ChartImage = origin.ChartImage
  162. item.Calendar = origin.Calendar
  163. item.DataDateType = origin.DataDateType
  164. item.SysUserRealName = origin.SysUserRealName
  165. item.ErDataUpdateDate = utils.TimeTransferString(utils.FormatDate, origin.ErDataUpdateDate)
  166. item.CreateTime = utils.TimeTransferString(utils.FormatDateTime, origin.CreateTime)
  167. item.ModifyTime = utils.TimeTransferString(utils.FormatDateTime, origin.ModifyTime)
  168. //item.EdbData = edbData
  169. return
  170. }
  171. // TraceEdbInfoResp 指标追溯数据返回
  172. type TraceEdbInfoMoreResp struct {
  173. EdbInfoId int `description:"指标id"`
  174. EdbCode string `description:"指标编码"`
  175. EdbInfoType int `description:"指标类型: 0-普通指标; 1-预测指标"`
  176. EdbName string `description:"指标名称"`
  177. EdbType int `description:"指标类型: 1-基础指标; 2-计算指标"`
  178. RuleTitle string `description:"指标规则"`
  179. UniqueCode string `description:"唯一编码"`
  180. Frequency string `description:"频率"`
  181. Unit string `description:"单位"`
  182. EndDate string `description:"终止日期"`
  183. EndValue float64 `description:"终止值"`
  184. LatestDate string `description:"最新日期"`
  185. LatestValue float64 `description:"最新值"`
  186. Child []TraceEdbInfoMoreResp `description:"下级来源"`
  187. EdbInfo *EdbInfo `description:"指标信息" json:"-"`
  188. }
  189. type TraceEdbInfoResp struct {
  190. EdbInfoId int `description:"指标id"`
  191. EdbCode string `description:"指标编码"`
  192. EdbInfoType int `description:"指标类型: 0-普通指标; 1-预测指标"`
  193. EdbName string `description:"指标名称"`
  194. EdbType int `description:"指标类型: 1-基础指标; 2-计算指标"`
  195. RuleTitle string `description:"指标规则"`
  196. UniqueCode string `description:"唯一编码"`
  197. ClassifyId int `description:"分类ID"`
  198. Child []TraceEdbInfoResp `description:"下级来源"`
  199. EdbInfo *EdbInfo `description:"指标信息" json:"-"`
  200. }
  201. func GetEdbInfoById(edbInfoId int) (item *EdbInfo, err error) {
  202. o := orm.NewOrmUsingDB("data")
  203. sql := ` SELECT * FROM edb_info WHERE edb_info_id=? `
  204. err = o.Raw(sql, edbInfoId).QueryRow(&item)
  205. return
  206. }
  207. // GetEdbInfoByIdList 根据指标id集合 获取 指标列表
  208. func GetEdbInfoByIdList(edbInfoIdList []int) (items []*EdbInfo, err error) {
  209. num := len(edbInfoIdList)
  210. if num <= 0 {
  211. return
  212. }
  213. o := orm.NewOrmUsingDB("data")
  214. sql := ` SELECT * FROM edb_info WHERE edb_info_id in (` + utils.GetOrmInReplace(num) + `) `
  215. _, err = o.Raw(sql, edbInfoIdList).QueryRows(&items)
  216. return
  217. }
  218. // GetEdbInfoByEdbCodeList 根据指标code集合获取指标列表
  219. func GetEdbInfoByEdbCodeList(source int, edbCodeList []string) (items []*EdbInfo, err error) {
  220. num := len(edbCodeList)
  221. if num <= 0 {
  222. return
  223. }
  224. o := orm.NewOrmUsingDB("data")
  225. sql := ` SELECT * FROM edb_info WHERE source=? AND edb_code in (` + utils.GetOrmInReplace(num) + `) `
  226. _, err = o.Raw(sql, source, edbCodeList).QueryRows(&items)
  227. return
  228. }