base_from_fubao.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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. type FuBaoData struct {
  11. ReturnValue struct {
  12. Product struct {
  13. AlikeName string `json:"alikeName"`
  14. DiffName string `json:"diffName"`
  15. PropValueStr string `json:"propValueStr"`
  16. Pattern string `json:"pattern"`
  17. } `json:"product"`
  18. Data []struct {
  19. PriceStr string `json:"priceStr"`
  20. Uad string `json:"uad"`
  21. Datadate string `json:"datadate"`
  22. } `json:"data"`
  23. } `json:"returnValue"`
  24. Code int `json:"code"`
  25. }
  26. // 新增富宝指标数据
  27. func AddEdbDataFromFubao(edbCode string, fubaoData *FuBaoData) (err error) {
  28. o := orm.NewOrm()
  29. var isAdd bool
  30. addSql := ` INSERT INTO edb_data_fubao(edb_info_id,edb_code,data_time,value,create_time,modify_time,data_timestamp) values `
  31. for _, dataItem := range fubaoData.ReturnValue.Data {
  32. dataTime, err := time.ParseInLocation(utils.FormatDate, dataItem.Datadate, time.Local)
  33. if err != nil {
  34. return err
  35. }
  36. eDate := dataTime.Format(utils.FormatDate)
  37. if err != nil {
  38. return err
  39. }
  40. timestamp := dataTime.UnixNano() / 1e6
  41. timeStr := fmt.Sprintf("%d", timestamp)
  42. addSql += GetAddSql("0", edbCode, eDate, timeStr, dataItem.PriceStr)
  43. isAdd = true
  44. }
  45. if isAdd {
  46. addSql = strings.TrimRight(addSql, ",")
  47. _, err = o.Raw(addSql).Exec()
  48. if err != nil {
  49. fmt.Println("RefreshAllEdbDataByFubao add Err", err.Error())
  50. return
  51. }
  52. }
  53. return
  54. }
  55. // 刷新路透指标数据
  56. func RefreshEdbDataFromFubao(edbInfoId int, edbCode, startDate string, fubaoData *FuBaoData) (err error) {
  57. source := utils.DATA_SOURCE_FUBAO
  58. subSource := utils.DATA_SUB_SOURCE_EDB
  59. o := orm.NewOrm()
  60. // 真实数据的最大日期 , 插入规则配置的日期
  61. var realDataMaxDate, edbDataInsertConfigDate time.Time
  62. var edbDataInsertConfig *EdbDataInsertConfig
  63. var isFindConfigDateRealData bool //是否找到配置日期的实际数据的值
  64. {
  65. edbDataInsertConfig, err = GetEdbDataInsertConfigByEdbId(edbInfoId)
  66. if err != nil && err.Error() != utils.ErrNoRow() {
  67. return
  68. }
  69. if edbDataInsertConfig != nil {
  70. edbDataInsertConfigDate = edbDataInsertConfig.Date
  71. }
  72. }
  73. var existCondition string
  74. var existPars []interface{}
  75. existCondition += " AND edb_info_id=? "
  76. existPars = append(existPars, edbInfoId)
  77. //if startDate != "" {
  78. // existCondition += " AND data_time>=? "
  79. // existPars = append(existPars, startDate)
  80. //}
  81. existList, err := GetEdbDataByCondition(source, subSource, existCondition, existPars)
  82. if err != nil {
  83. return err
  84. }
  85. existMap := make(map[string]*EdbInfoSearchData)
  86. for _, v := range existList {
  87. existMap[v.DataTime] = v
  88. }
  89. fmt.Println("existMap start")
  90. fmt.Println(existMap)
  91. fmt.Println("existMap end")
  92. edbInfoIdStr := strconv.Itoa(edbInfoId)
  93. addSql := ` INSERT INTO edb_data_fubao(edb_info_id,edb_code,data_time,value,create_time,modify_time,data_timestamp) values `
  94. var isAdd bool
  95. for _, dataItem := range fubaoData.ReturnValue.Data {
  96. dataTime, err := time.ParseInLocation(utils.FormatDate, dataItem.Datadate, time.Local)
  97. if err != nil {
  98. return err
  99. }
  100. if findItem, ok := existMap[dataItem.Datadate]; !ok {
  101. timestamp := dataTime.UnixNano() / 1e6
  102. timeStr := fmt.Sprintf("%d", timestamp)
  103. addSql += GetAddSql(edbInfoIdStr, edbCode, dataItem.Datadate, timeStr, dataItem.PriceStr)
  104. isAdd = true
  105. } else {
  106. fmt.Println("update start")
  107. fmt.Println(findItem.Value)
  108. fmt.Println(dataItem.PriceStr)
  109. fmt.Println(utils.SubFloatToString(findItem.Value, 30))
  110. fmt.Println("update end")
  111. if findItem != nil && utils.SubFloatToString(findItem.Value, 30) != dataItem.PriceStr {
  112. err = ModifyEdbDataById(source, subSource, findItem.EdbDataId, dataItem.PriceStr)
  113. if err != nil {
  114. return err
  115. }
  116. }
  117. }
  118. // 下面代码主要目的是处理掉手动插入的数据判断
  119. {
  120. if realDataMaxDate.IsZero() || dataTime.After(realDataMaxDate) {
  121. realDataMaxDate = dataTime
  122. }
  123. if edbDataInsertConfigDate.IsZero() || dataTime.Equal(edbDataInsertConfigDate) {
  124. isFindConfigDateRealData = true
  125. }
  126. }
  127. }
  128. // 处理手工数据补充的配置
  129. HandleConfigInsertEdbData(realDataMaxDate, edbDataInsertConfig, edbInfoId, source, subSource, existMap, isFindConfigDateRealData)
  130. if isAdd {
  131. addSql = strings.TrimRight(addSql, ",")
  132. _, err = o.Raw(addSql).Exec()
  133. if err != nil {
  134. fmt.Println("RefreshEdbDataFromFubao Err", err.Error())
  135. return
  136. }
  137. }
  138. return
  139. }