base_from_bloomberg.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. package models
  2. import (
  3. "eta/eta_index_lib/utils"
  4. "fmt"
  5. "github.com/beego/beego/v2/client/orm"
  6. "strconv"
  7. "strings"
  8. "time"
  9. )
  10. // BaseFromBloombergIndex Bloomberg原始指标
  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. func (m *BaseFromBloombergIndex) TableName() string {
  30. return "base_from_bloomberg_index"
  31. }
  32. func (m *BaseFromBloombergIndex) Create() (err error) {
  33. o := orm.NewOrm()
  34. id, err := o.Insert(m)
  35. if err != nil {
  36. return
  37. }
  38. m.BaseFromBloombergIndexId = int(id)
  39. return
  40. }
  41. func (m *BaseFromBloombergIndex) Update(cols []string) (err error) {
  42. o := orm.NewOrm()
  43. _, err = o.Update(m, cols...)
  44. return
  45. }
  46. func GetBaseFromBloombergIndexByCode(indexCode string) (item *BaseFromBloombergIndex, err error) {
  47. o := orm.NewOrm()
  48. sql := `SELECT * FROM base_from_bloomberg_index WHERE index_code = ? LIMIT 1`
  49. err = o.Raw(sql, indexCode).QueryRow(&item)
  50. return
  51. }
  52. // BaseFromBloombergData Bloomberg原始数据
  53. type BaseFromBloombergData struct {
  54. BaseFromBloombergDataId int `orm:"column(base_from_bloomberg_data_id);pk"`
  55. BaseFromBloombergIndexId int `description:"指标ID"`
  56. IndexCode string `description:"指标编码"`
  57. DataTime time.Time `description:"数据日期"`
  58. Value float64 `description:"数据值"`
  59. CreateTime time.Time `description:"创建时间"`
  60. ModifyTime time.Time `description:"修改时间"`
  61. DataTimestamp int `description:"数据日期时间戳"`
  62. }
  63. func (m *BaseFromBloombergData) TableName() string {
  64. return "base_from_bloomberg_data"
  65. }
  66. func GetBaseFromBloombergDataByCondition(condition string, pars []interface{}) (items []*BaseFromBloombergData, err error) {
  67. sub := `SELECT * FROM base_from_bloomberg_data WHERE 1=1 `
  68. o := orm.NewOrm()
  69. if condition != "" {
  70. sub += condition
  71. }
  72. sql := `SELECT * FROM (` + sub + ` HAVING 1 ORDER BY modify_time DESC) tmp GROUP BY data_time ORDER BY data_time DESC `
  73. _, err = o.Raw(sql, pars).QueryRows(&items)
  74. return
  75. }
  76. func MultiInsertOrUpdateBaseFromBloombergData(inserts, updates []*BaseFromBloombergData) (err error) {
  77. o := orm.NewOrm()
  78. if len(inserts) > 0 {
  79. _, e := o.InsertMulti(1000, inserts)
  80. if e != nil {
  81. err = fmt.Errorf("insert multi err: %s", e.Error())
  82. return
  83. }
  84. }
  85. if len(updates) > 0 {
  86. p, e := o.Raw("UPDATE base_from_bloomberg_data SET value = ?, modify_time = NOW() WHERE index_code = ? AND data_time = ?").Prepare()
  87. if e != nil {
  88. err = fmt.Errorf("prepare err: %s", e.Error())
  89. return
  90. }
  91. defer func() {
  92. _ = p.Close()
  93. }()
  94. for _, v := range updates {
  95. _, e = p.Exec(v.Value, v.IndexCode, v.DataTime.Format(utils.FormatDate))
  96. if e != nil {
  97. err = fmt.Errorf("update err: %s", e.Error())
  98. return
  99. }
  100. }
  101. }
  102. return
  103. }
  104. func GetBaseFromBloombergIndexMinMax(indexCode string) (item *EdbInfoMaxAndMinInfo, err error) {
  105. o := orm.NewOrm()
  106. sql := `SELECT MIN(data_time) AS min_date,MAX(data_time) AS max_date,MIN(value) AS min_value,MAX(value) AS max_value FROM base_from_bloomberg_data WHERE index_code = ? `
  107. err = o.Raw(sql, indexCode).QueryRow(&item)
  108. if err != nil {
  109. return
  110. }
  111. // 获取最新值
  112. var lastVal float64
  113. sql = ` SELECT value AS latest_value FROM base_from_bloomberg_data WHERE index_code = ? ORDER BY data_time DESC LIMIT 1 `
  114. err = o.Raw(sql, indexCode).QueryRow(&lastVal)
  115. if err != nil {
  116. return
  117. }
  118. item.LatestValue = lastVal
  119. return
  120. }
  121. func ModifyBaseFromBloombergIndexMinMax(indexCode string, item *EdbInfoMaxAndMinInfo) (err error) {
  122. o := orm.NewOrm()
  123. sql := ` UPDATE base_from_bloomberg_index SET start_date = ?, end_date = ?, modify_time = NOW() WHERE index_code = ? `
  124. _, err = o.Raw(sql, item.MinDate, item.MaxDate, indexCode).Exec()
  125. return
  126. }
  127. type BloombergData struct {
  128. InputValue float64 `orm:"column(value)" description:"值"`
  129. DataTime string `orm:"column(data_time)" description:"日期"`
  130. }
  131. func GetBloombergDataByCondition(condition string, pars []interface{}) (item []*BloombergData, err error) {
  132. sql1 := ` SELECT * FROM base_from_bloomberg_data WHERE 1=1 `
  133. o := orm.NewOrm()
  134. if condition != "" {
  135. sql1 += condition
  136. }
  137. sql := `select * from (` + sql1 + ` having 1 order by modify_time DESC ) tmp GROUP BY data_time ORDER BY data_time DESC `
  138. _, err = o.Raw(sql, pars).QueryRows(&item)
  139. return
  140. }
  141. // AddEdbDataFromBloomberg 新增Bloomberg指标数据
  142. func AddEdbDataFromBloomberg(edbCode string) (err error) {
  143. o := orm.NewOrm()
  144. var condition string
  145. var pars []interface{}
  146. if edbCode != "" {
  147. condition += " AND index_code = ? "
  148. pars = append(pars, edbCode)
  149. }
  150. bloombergDataList, err := GetBloombergDataByCondition(condition, pars)
  151. if err != nil {
  152. return
  153. }
  154. dataLen := len(bloombergDataList)
  155. existMap := make(map[string]string)
  156. if dataLen > 0 {
  157. var isAdd bool
  158. addSql := ` INSERT INTO edb_data_bloomberg (edb_info_id,edb_code,data_time,value,create_time,modify_time,data_timestamp) values `
  159. for i := 0; i < dataLen; i++ {
  160. item := bloombergDataList[i]
  161. eDate := item.DataTime
  162. sValue := utils.SubFloatToString(item.InputValue, 4)
  163. if sValue != "" {
  164. if _, ok := existMap[eDate]; !ok {
  165. dataTime, err := time.ParseInLocation(utils.FormatDate, eDate, time.Local)
  166. if err != nil {
  167. return err
  168. }
  169. timestamp := dataTime.UnixNano() / 1e6
  170. timeStr := fmt.Sprintf("%d", timestamp)
  171. addSql += GetAddSql("0", edbCode, eDate, timeStr, sValue)
  172. isAdd = true
  173. }
  174. }
  175. existMap[eDate] = eDate
  176. }
  177. if isAdd {
  178. addSql = strings.TrimRight(addSql, ",")
  179. utils.FileLog.Info("addSql:" + addSql)
  180. _, err = o.Raw(addSql).Exec()
  181. if err != nil {
  182. return err
  183. }
  184. }
  185. }
  186. return
  187. }
  188. // RefreshEdbDataFromBloomberg 刷新Bloomberg指标数据
  189. func RefreshEdbDataFromBloomberg(edbInfoId int, edbCode, startDate string) (err error) {
  190. source := utils.DATA_SOURCE_BLOOMBERG
  191. subSource := utils.DATA_SUB_SOURCE_EDB
  192. o := orm.NewOrm()
  193. if err != nil {
  194. return
  195. }
  196. edbInfoIdStr := strconv.Itoa(edbInfoId)
  197. //计算数据
  198. var condition string
  199. var pars []interface{}
  200. if edbCode != "" {
  201. condition += " AND index_code=? "
  202. pars = append(pars, edbCode)
  203. }
  204. if startDate != "" {
  205. condition += " AND data_time>=? "
  206. pars = append(pars, startDate)
  207. }
  208. bloombergDataList, err := GetBloombergDataByCondition(condition, pars)
  209. if err != nil {
  210. return
  211. }
  212. // 真实数据的最大日期 , 插入规则配置的日期
  213. var realDataMaxDate, edbDataInsertConfigDate time.Time
  214. var edbDataInsertConfig *EdbDataInsertConfig
  215. var isFindConfigDateRealData bool //是否找到配置日期的实际数据的值
  216. {
  217. edbDataInsertConfig, err = GetEdbDataInsertConfigByEdbId(edbInfoId)
  218. if err != nil && err.Error() != utils.ErrNoRow() {
  219. return
  220. }
  221. if edbDataInsertConfig != nil {
  222. edbDataInsertConfigDate = edbDataInsertConfig.Date
  223. }
  224. }
  225. var existCondition string
  226. var existPars []interface{}
  227. existCondition += " AND edb_info_id=? "
  228. existPars = append(existPars, edbInfoId)
  229. if startDate != "" {
  230. existCondition += " AND data_time>=? "
  231. existPars = append(existPars, startDate)
  232. }
  233. //获取指标所有数据
  234. existList, err := GetEdbDataByCondition(source, subSource, existCondition, existPars)
  235. if err != nil {
  236. return err
  237. }
  238. existMap := make(map[string]*EdbInfoSearchData)
  239. for _, v := range existList {
  240. existMap[v.DataTime] = v
  241. }
  242. addSql := ` INSERT INTO edb_data_bloomberg(edb_info_id,edb_code,data_time,value,create_time,modify_time,data_timestamp) values `
  243. var isAdd bool
  244. addMap := make(map[string]string)
  245. for _, v := range bloombergDataList {
  246. item := v
  247. eDate := item.DataTime
  248. sValue := utils.SubFloatToString(item.InputValue, 4)
  249. dataTime, err := time.ParseInLocation(utils.FormatDate, eDate, time.Local)
  250. if err != nil {
  251. return err
  252. }
  253. if findItem, ok := existMap[v.DataTime]; !ok {
  254. if sValue != "" {
  255. timestamp := dataTime.UnixNano() / 1e6
  256. timeStr := fmt.Sprintf("%d", timestamp)
  257. saveValue := sValue
  258. if _, addOk := addMap[eDate]; !addOk {
  259. addSql += GetAddSql(edbInfoIdStr, edbCode, eDate, timeStr, saveValue)
  260. isAdd = true
  261. }
  262. }
  263. } else {
  264. if findItem != nil && utils.SubFloatToString(findItem.Value, 4) != sValue {
  265. err = ModifyEdbDataById(source, subSource, findItem.EdbDataId, sValue)
  266. if err != nil {
  267. return err
  268. }
  269. }
  270. }
  271. addMap[v.DataTime] = v.DataTime
  272. // 下面代码主要目的是处理掉手动插入的数据判断
  273. {
  274. if realDataMaxDate.IsZero() || dataTime.After(realDataMaxDate) {
  275. realDataMaxDate = dataTime
  276. }
  277. if edbDataInsertConfigDate.IsZero() || dataTime.Equal(edbDataInsertConfigDate) {
  278. isFindConfigDateRealData = true
  279. }
  280. }
  281. }
  282. // 处理手工数据补充的配置
  283. HandleConfigInsertEdbData(realDataMaxDate, edbDataInsertConfig, edbInfoId, source, subSource, existMap, isFindConfigDateRealData)
  284. if isAdd {
  285. addSql = strings.TrimRight(addSql, ",")
  286. _, err = o.Raw(addSql).Exec()
  287. if err != nil {
  288. return err
  289. }
  290. }
  291. return
  292. }
  293. // BridgePCSGBloombergResultData 中石油新加坡-指标接口响应体
  294. type BridgePCSGBloombergResultData struct {
  295. Code int `json:"code" description:"状态码"`
  296. Msg string `json:"msg" description:"提示信息"`
  297. Data []BaseFromBloombergApiIndexAndData `json:"data" description:"返回数据"`
  298. }
  299. // BaseFromBloombergApiIndexAndData Bloomberg原始指标及数据
  300. type BaseFromBloombergApiIndexAndData struct {
  301. BaseFromBloombergIndexId int `description:"指标ID"`
  302. IndexCode string `description:"指标编码"`
  303. IndexName string `description:"指标名称"`
  304. Unit string `description:"单位"`
  305. Source string `description:"来源"`
  306. Frequency string `description:"频度"`
  307. CreateTime time.Time `description:"创建时间"`
  308. ModifyTime time.Time `description:"修改时间"`
  309. Data []BaseFromBloombergApiIndexData `description:"数据列表"`
  310. }
  311. // BaseFromBloombergApiIndexData Bloomberg原始指标数据
  312. type BaseFromBloombergApiIndexData struct {
  313. DataTime time.Time `description:"数据日期"`
  314. Value float64 `description:"数据值"`
  315. }
  316. // PCSGImportHistoryDataReq 导入历史数据
  317. type PCSGImportHistoryDataReq struct {
  318. IndexCode string `description:"指标编码"`
  319. DataMap map[time.Time]float64 `description:"数据日期/值"`
  320. TaskKey string `description:"导入数据对应的任务Key"`
  321. }