base_from_pb_finance.go 4.8 KB

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