base_from_pb_finance.go 4.4 KB

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