base_from_bloomberg_index.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. package data_manage
  2. import (
  3. "eta/eta_api/utils"
  4. "fmt"
  5. "github.com/beego/beego/v2/client/orm"
  6. "github.com/rdlucklib/rdluck_tools/paging"
  7. "strings"
  8. "time"
  9. )
  10. // BaseFromBloombergIndex 彭博原始指标表
  11. type BaseFromBloombergIndex struct {
  12. BaseFromBloombergIndexId int `orm:"column(base_from_bloomberg_index_id);pk"`
  13. IndexCode string `description:"指标编码"`
  14. IndexName string `description:"指标名称"`
  15. Unit string `description:"单位"`
  16. Source int `description:"数据来源"`
  17. Frequency string `description:"频度"`
  18. StartDate time.Time `description:"开始日期"`
  19. EndDate time.Time `description:"结束日期"`
  20. Describe string `description:"指标描述"`
  21. Sort int `description:"排序"`
  22. IsStop int `description:"是否停更:0-否;1-停更"`
  23. EdbExist int `description:"指标库是否已添加:0-否;1-是"`
  24. TerminalCode string `description:"所属终端编码"`
  25. FilePath string `description:"文件存储路径"`
  26. CreateTime time.Time `description:"创建时间"`
  27. ModifyTime time.Time `description:"修改时间"`
  28. }
  29. var BaseFromBloombergIndexCols = struct {
  30. BaseFromBloombergIndexId string
  31. IndexCode string
  32. IndexName string
  33. Unit string
  34. Source string
  35. Frequency string
  36. StartDate string
  37. EndDate string
  38. Describe string
  39. Sort string
  40. IsStop string
  41. EdbExist string
  42. TerminalCode string
  43. FilePath string
  44. CreateTime string
  45. ModifyTime string
  46. }{
  47. BaseFromBloombergIndexId: "base_from_bloomberg_index_id",
  48. IndexCode: "index_code",
  49. IndexName: "index_name",
  50. Unit: "unit",
  51. Source: "source",
  52. Frequency: "frequency",
  53. StartDate: "start_date",
  54. EndDate: "end_date",
  55. Describe: "describe",
  56. Sort: "sort",
  57. IsStop: "is_stop",
  58. EdbExist: "edb_exist",
  59. TerminalCode: "terminal_code",
  60. FilePath: "file_path",
  61. CreateTime: "create_time",
  62. ModifyTime: "modify_time",
  63. }
  64. func (m *BaseFromBloombergIndex) TableName() string {
  65. return "base_from_bloomberg_index"
  66. }
  67. func (m *BaseFromBloombergIndex) PrimaryId() string {
  68. return BaseFromBloombergIndexCols.BaseFromBloombergIndexId
  69. }
  70. func (m *BaseFromBloombergIndex) Create() (err error) {
  71. o := orm.NewOrmUsingDB("data")
  72. id, err := o.Insert(m)
  73. if err != nil {
  74. return
  75. }
  76. m.BaseFromBloombergIndexId = int(id)
  77. return
  78. }
  79. func (m *BaseFromBloombergIndex) CreateMulti(items []*BaseFromBloombergIndex) (err error) {
  80. if len(items) == 0 {
  81. return
  82. }
  83. o := orm.NewOrmUsingDB("data")
  84. _, err = o.InsertMulti(len(items), items)
  85. return
  86. }
  87. func (m *BaseFromBloombergIndex) Update(cols []string) (err error) {
  88. o := orm.NewOrmUsingDB("data")
  89. _, err = o.Update(m, cols...)
  90. return
  91. }
  92. func (m *BaseFromBloombergIndex) Del() (err error) {
  93. o := orm.NewOrmUsingDB("data")
  94. sql := fmt.Sprintf(`DELETE FROM %s WHERE %s = ? LIMIT 1`, m.TableName(), m.PrimaryId())
  95. _, err = o.Raw(sql, m.BaseFromBloombergIndexId).Exec()
  96. return
  97. }
  98. func (m *BaseFromBloombergIndex) MultiDel(menuIds []int) (err error) {
  99. if len(menuIds) == 0 {
  100. return
  101. }
  102. o := orm.NewOrmUsingDB("data")
  103. sql := fmt.Sprintf(`DELETE FROM %s WHERE %s IN (%s)`, m.TableName(), m.PrimaryId(), utils.GetOrmInReplace(len(menuIds)))
  104. _, err = o.Raw(sql, menuIds).Exec()
  105. return
  106. }
  107. func (m *BaseFromBloombergIndex) GetItemById(id int) (item *BaseFromBloombergIndex, err error) {
  108. o := orm.NewOrmUsingDB("data")
  109. sql := fmt.Sprintf(`SELECT * FROM %s WHERE %s = ? LIMIT 1`, m.TableName(), m.PrimaryId())
  110. err = o.Raw(sql, id).QueryRow(&item)
  111. return
  112. }
  113. func (m *BaseFromBloombergIndex) GetItemByCondition(condition string, pars []interface{}, orderRule string) (item *BaseFromBloombergIndex, err error) {
  114. o := orm.NewOrmUsingDB("data")
  115. order := ``
  116. if orderRule != "" {
  117. order = ` ORDER BY ` + orderRule
  118. }
  119. sql := fmt.Sprintf(`SELECT * FROM %s WHERE 1=1 %s %s LIMIT 1`, m.TableName(), condition, order)
  120. err = o.Raw(sql, pars).QueryRow(&item)
  121. return
  122. }
  123. func (m *BaseFromBloombergIndex) GetCountByCondition(condition string, pars []interface{}) (count int, err error) {
  124. o := orm.NewOrmUsingDB("data")
  125. sql := fmt.Sprintf(`SELECT COUNT(1) FROM %s WHERE 1=1 %s`, m.TableName(), condition)
  126. err = o.Raw(sql, pars).QueryRow(&count)
  127. return
  128. }
  129. func (m *BaseFromBloombergIndex) GetItemsByCondition(condition string, pars []interface{}, fieldArr []string, orderRule string, limit int) (items []*BaseFromBloombergIndex, err error) {
  130. o := orm.NewOrmUsingDB("data")
  131. fields := strings.Join(fieldArr, ",")
  132. if len(fieldArr) == 0 {
  133. fields = `*`
  134. }
  135. order := `ORDER BY create_time DESC`
  136. if orderRule != "" {
  137. order = ` ORDER BY ` + orderRule
  138. }
  139. sql := fmt.Sprintf(`SELECT %s FROM %s WHERE 1=1 %s %s`, fields, m.TableName(), condition, order)
  140. if limit > 0 {
  141. sql += fmt.Sprintf(` LIMIT %d`, limit)
  142. }
  143. _, err = o.Raw(sql, pars).QueryRows(&items)
  144. return
  145. }
  146. func (m *BaseFromBloombergIndex) GetPageItemsByCondition(condition string, pars []interface{}, fieldArr []string, orderRule string, startSize, pageSize int) (items []*BaseFromBloombergIndex, err error) {
  147. o := orm.NewOrmUsingDB("data")
  148. fields := strings.Join(fieldArr, ",")
  149. if len(fieldArr) == 0 {
  150. fields = `*`
  151. }
  152. order := `ORDER BY create_time DESC`
  153. if orderRule != "" {
  154. order = ` ORDER BY ` + orderRule
  155. }
  156. sql := fmt.Sprintf(`SELECT %s FROM %s WHERE 1=1 %s %s LIMIT ?,?`, fields, m.TableName(), condition, order)
  157. _, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&items)
  158. return
  159. }
  160. // BaseFromBloombergIndexItem 彭博原始指标信息
  161. type BaseFromBloombergIndexItem struct {
  162. BaseFromBloombergIndexId int
  163. IndexCode string `description:"指标编码"`
  164. IndexName string `description:"指标名称"`
  165. Unit string `description:"单位"`
  166. Source int `description:"数据来源"`
  167. Frequency string `description:"频度"`
  168. StartDate string `description:"开始日期"`
  169. EndDate string `description:"结束日期"`
  170. Describe string `description:"指标描述"`
  171. Sort int `description:"排序"`
  172. IsStop int `description:"是否停更:0-否;1-停更"`
  173. EdbExist int `description:"指标库是否已添加:0-否;1-是"`
  174. CreateTime string `description:"创建时间"`
  175. ModifyTime string `description:"修改时间"`
  176. EdbInfoId int `description:"指标库ID"`
  177. EdbUniqueCode string `description:"指标库唯一编码"`
  178. EdbClassifyId int `description:"指标库分类ID"`
  179. }
  180. func FormatBaseFromBloombergIndex2Item(origin *BaseFromBloombergIndex) (item *BaseFromBloombergIndexItem) {
  181. if origin == nil {
  182. return
  183. }
  184. item = new(BaseFromBloombergIndexItem)
  185. item.BaseFromBloombergIndexId = origin.BaseFromBloombergIndexId
  186. item.IndexCode = origin.IndexCode
  187. item.IndexName = origin.IndexName
  188. item.Unit = origin.Unit
  189. item.Source = origin.Source
  190. item.Frequency = origin.Frequency
  191. item.StartDate = utils.TimeTransferString(utils.FormatDate, origin.StartDate)
  192. item.EndDate = utils.TimeTransferString(utils.FormatDate, origin.EndDate)
  193. item.Describe = origin.Describe
  194. item.Sort = origin.Sort
  195. item.IsStop = origin.IsStop
  196. //item.EdbExist = origin.EdbExist
  197. item.CreateTime = utils.TimeTransferString(utils.FormatDateTime, origin.CreateTime)
  198. item.ModifyTime = utils.TimeTransferString(utils.FormatDateTime, origin.ModifyTime)
  199. return
  200. }
  201. // BloombergSourceListReq 指标列表筛选
  202. type BloombergSourceListReq struct {
  203. PageSize int `form:"PageSize"`
  204. CurrentIndex int `form:"CurrentIndex"`
  205. Frequency string `form:"Frequency" description:"频度"`
  206. Keywords string `form:"Keywords" description:"指标ID/指标名称"`
  207. ListAll bool `form:"ListAll" description:"列表全选"`
  208. SortField int `form:"SortField" description:"排序字段: 0-默认; 1-开始时间; 2-最新时间; 3-更新时间"`
  209. SortRule int `form:"SortRule" description:"排序方式: 0-默认; 1-正序; 2-倒序"`
  210. }
  211. // BloombergSourceListResp 指标列表响应体
  212. type BloombergSourceListResp struct {
  213. List []*BaseFromBloombergIndexItem
  214. Paging *paging.PagingItem `description:"分页数据"`
  215. }
  216. // UpdateEdbExist 标记已添加指标库
  217. func (m *BaseFromBloombergIndex) UpdateEdbExist(indexCode string) (err error) {
  218. o := orm.NewOrmUsingDB("data")
  219. sql := fmt.Sprintf(`UPDATE %s SET %s = ? WHERE %s = ? LIMIT 1`, m.TableName(), BaseFromBloombergIndexCols.EdbExist, BaseFromBloombergIndexCols.IndexCode)
  220. _, err = o.Raw(sql, 1, indexCode).Exec()
  221. return
  222. }
  223. // BloombergSourceBatchAddCheckReq Bloomberg批量添加校验
  224. type BloombergSourceBatchAddCheckReq struct {
  225. BloombergSourceListReq
  226. IndexCodes []string `form:"IndexCodes" description:"全选为false时, 该数组为选中; 全选为true时, 该数组为不选的指标"`
  227. }
  228. // --------------------------------------------- 以下为测试用 ---------------------------------------------
  229. // BaseFromBloombergData 彭博原始指标表
  230. //type BaseFromBloombergData struct {
  231. // BaseFromBloombergDataId int `orm:"column(base_from_bloomberg_data_id);pk"`
  232. // BaseFromBloombergIndexId int `description:"指标ID"`
  233. // IndexCode string `description:"指标编码"`
  234. // DataTime time.Time `description:"数据日期"`
  235. // Value float64 `description:"数据值"`
  236. // CreateTime time.Time `description:"创建时间"`
  237. // ModifyTime time.Time `description:"修改时间"`
  238. // DataTimestamp int `description:"数据日期时间戳"`
  239. //}
  240. //
  241. //func (m *BaseFromBloombergData) TableName() string {
  242. // return "base_from_bloomberg_data"
  243. //}
  244. //
  245. //func (m *BaseFromBloombergData) Create() (err error) {
  246. // o := orm.NewOrmUsingDB("data")
  247. // id, err := o.Insert(m)
  248. // if err != nil {
  249. // return
  250. // }
  251. // m.BaseFromBloombergDataId = int(id)
  252. // return
  253. //}
  254. //
  255. //func (m *BaseFromBloombergData) CreateMulti(items []*BaseFromBloombergData) (err error) {
  256. // if len(items) == 0 {
  257. // return
  258. // }
  259. // o := orm.NewOrmUsingDB("data")
  260. // _, err = o.InsertMulti(len(items), items)
  261. // return
  262. //}