base_from_bloomberg_index.go 11 KB

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