edb_info.go 9.4 KB

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