base_from_pb_finance.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. func AddEdbDataFromPbFinance(edbCode string, item *EdbDataFromPb) (err error) {
  11. o := orm.NewOrm()
  12. if len(item.Date) > 0 {
  13. var isAdd bool
  14. dateMap := item.Date
  15. addSql := ` INSERT INTO edb_data_pb(edb_info_id,edb_code,data_time,value,create_time,modify_time,ticker,field,data_timestamp) values `
  16. nowStr := time.Now().Format(utils.FormatDateTime)
  17. for k, v := range dateMap {
  18. timeStr := fmt.Sprintf("%d", v)
  19. v = v / 1000
  20. t := time.Unix(v, 0)
  21. dateTime := t.Format(utils.FormatDate)
  22. val := item.Value[k]
  23. field := item.Field[k]
  24. ticker := item.Ticker[k]
  25. addSql += "("
  26. addSql += "0," + "'" + edbCode + "'" + "," + "'" + dateTime + "'" + "," + utils.SubFloatToString(val, 20) + "," + "'" + nowStr + "'" +
  27. "," + "'" + nowStr + "'" + "," + "'" + ticker + "'" + "," + "'" + field + "'" + "," + "'" + timeStr + "'"
  28. addSql += "),"
  29. isAdd = true
  30. }
  31. if isAdd {
  32. addSql = strings.TrimRight(addSql, ",")
  33. _, err = o.Raw(addSql).Exec()
  34. if err != nil {
  35. return
  36. }
  37. }
  38. }
  39. return
  40. }
  41. // 刷新彭博指标数据
  42. func RefreshEdbDataFromPbFinance(edbInfoId int, edbCode, startDate string, item *EdbDataFromPb) (err error) {
  43. o := orm.NewOrm()
  44. source := utils.DATA_SOURCE_PB_FINANCE
  45. // 真实数据的最大日期 , 插入规则配置的日期
  46. var realDataMaxDate, edbDataInsertConfigDate time.Time
  47. var edbDataInsertConfig *EdbDataInsertConfig
  48. var isFindConfigDateRealData bool //是否找到配置日期的实际数据的值
  49. {
  50. edbDataInsertConfig, err = GetEdbDataInsertConfigByEdbId(edbInfoId)
  51. if err != nil && err.Error() != utils.ErrNoRow() {
  52. return
  53. }
  54. if edbDataInsertConfig != nil {
  55. edbDataInsertConfigDate = edbDataInsertConfig.Date
  56. }
  57. }
  58. var condition string
  59. var pars []interface{}
  60. condition += " AND edb_info_id=? "
  61. pars = append(pars, edbInfoId)
  62. condition += " AND data_time>=? "
  63. pars = append(pars, startDate)
  64. existList, err := GetEdbDataByCondition(source, condition, pars)
  65. existMap := make(map[string]*EdbInfoSearchData)
  66. for _, v := range existList {
  67. existMap[v.DataTime] = v
  68. }
  69. // 彭博财务编码
  70. edbCodeArr := strings.Split(edbCode, "&&")
  71. fieldCode := edbCodeArr[1]
  72. edbInfoIdStr := strconv.Itoa(edbInfoId)
  73. addMap := make(map[string]string)
  74. if len(item.Date) > 0 {
  75. dateMap := item.Date
  76. var isAdd bool
  77. addSql := ` INSERT INTO edb_data_pb(edb_info_id,edb_code,data_time,value,create_time,modify_time,ticker,field,data_timestamp) values `
  78. nowStr := time.Now().Format(utils.FormatDateTime)
  79. for k, v := range dateMap {
  80. timeStr := fmt.Sprintf("%d", v)
  81. v = v / 1000
  82. t := time.Unix(v, 0)
  83. dateTime := t.Format(utils.FormatDate)
  84. t, _ = time.ParseInLocation(utils.FormatDate, dateTime, time.Local) //这里的目的是为了北京时间,以防万一,还是重新转一下吧
  85. val := item.Value[k]
  86. field := item.Field[k]
  87. ticker := item.Ticker[k]
  88. saveValue := utils.SubFloatToString(val, 30)
  89. if field == fieldCode {
  90. if findItem, ok := existMap[dateTime]; !ok {
  91. if _, addOk := addMap[dateTime]; !addOk {
  92. addSql += "("
  93. addSql += edbInfoIdStr + "," + "'" + edbCode + "'" + "," + "'" + dateTime + "'" + "," + saveValue + "," + "'" + nowStr + "'" +
  94. "," + "'" + nowStr + "'" + "," + "'" + ticker + "'" + "," + "'" + field + "'" + "," + "'" + timeStr + "'"
  95. addSql += "),"
  96. isAdd = true
  97. addMap[dateTime] = saveValue
  98. }
  99. } else {
  100. if findItem != nil && utils.SubFloatToString(findItem.Value, 30) != saveValue {
  101. err = ModifyEdbDataById(source, findItem.EdbDataId, saveValue)
  102. if err != nil {
  103. return err
  104. }
  105. }
  106. }
  107. // 下面代码主要目的是处理掉手动插入的数据判断
  108. {
  109. if realDataMaxDate.IsZero() || t.After(realDataMaxDate) {
  110. realDataMaxDate = t
  111. }
  112. if edbDataInsertConfigDate.IsZero() || t.Equal(edbDataInsertConfigDate) {
  113. isFindConfigDateRealData = true
  114. }
  115. }
  116. }
  117. }
  118. // 处理手工数据补充的配置
  119. HandleConfigInsertEdbData(realDataMaxDate, edbDataInsertConfig, edbInfoId, source, existMap, isFindConfigDateRealData)
  120. if isAdd {
  121. addSql = strings.TrimRight(addSql, ",")
  122. _, err = o.Raw(addSql).Exec()
  123. if err != nil {
  124. fmt.Println("RefreshAllEdbDataByPbFinance add Err", err.Error())
  125. return
  126. }
  127. }
  128. }
  129. return
  130. }