base_from_fubao.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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. o := orm.NewOrm()
  59. // 真实数据的最大日期 , 插入规则配置的日期
  60. var realDataMaxDate, edbDataInsertConfigDate time.Time
  61. var edbDataInsertConfig *EdbDataInsertConfig
  62. var isFindConfigDateRealData bool //是否找到配置日期的实际数据的值
  63. {
  64. edbDataInsertConfig, err = GetEdbDataInsertConfigByEdbId(edbInfoId)
  65. if err != nil && err.Error() != utils.ErrNoRow() {
  66. return
  67. }
  68. if edbDataInsertConfig != nil {
  69. edbDataInsertConfigDate = edbDataInsertConfig.Date
  70. }
  71. }
  72. var existCondition string
  73. var existPars []interface{}
  74. existCondition += " AND edb_info_id=? "
  75. existPars = append(existPars, edbInfoId)
  76. //if startDate != "" {
  77. // existCondition += " AND data_time>=? "
  78. // existPars = append(existPars, startDate)
  79. //}
  80. existList, err := GetEdbDataByCondition(source, existCondition, existPars)
  81. if err != nil {
  82. return err
  83. }
  84. existMap := make(map[string]*EdbInfoSearchData)
  85. for _, v := range existList {
  86. existMap[v.DataTime] = v
  87. }
  88. fmt.Println("existMap start")
  89. fmt.Println(existMap)
  90. fmt.Println("existMap end")
  91. edbInfoIdStr := strconv.Itoa(edbInfoId)
  92. addSql := ` INSERT INTO edb_data_fubao(edb_info_id,edb_code,data_time,value,create_time,modify_time,data_timestamp) values `
  93. var isAdd bool
  94. for _, dataItem := range fubaoData.ReturnValue.Data {
  95. dataTime, err := time.ParseInLocation(utils.FormatDate, dataItem.Datadate, time.Local)
  96. if err != nil {
  97. return err
  98. }
  99. if findItem, ok := existMap[dataItem.Datadate]; !ok {
  100. timestamp := dataTime.UnixNano() / 1e6
  101. timeStr := fmt.Sprintf("%d", timestamp)
  102. addSql += GetAddSql(edbInfoIdStr, edbCode, dataItem.Datadate, timeStr, dataItem.PriceStr)
  103. isAdd = true
  104. } else {
  105. fmt.Println("update start")
  106. fmt.Println(findItem.Value)
  107. fmt.Println(dataItem.PriceStr)
  108. fmt.Println(utils.SubFloatToString(findItem.Value, 30))
  109. fmt.Println("update end")
  110. if findItem != nil && utils.SubFloatToString(findItem.Value, 30) != dataItem.PriceStr {
  111. err = ModifyEdbDataById(source, findItem.EdbDataId, dataItem.PriceStr)
  112. if err != nil {
  113. return err
  114. }
  115. }
  116. }
  117. // 下面代码主要目的是处理掉手动插入的数据判断
  118. {
  119. if realDataMaxDate.IsZero() || dataTime.After(realDataMaxDate) {
  120. realDataMaxDate = dataTime
  121. }
  122. if edbDataInsertConfigDate.IsZero() || dataTime.Equal(edbDataInsertConfigDate) {
  123. isFindConfigDateRealData = true
  124. }
  125. }
  126. }
  127. // 处理手工数据补充的配置
  128. HandleConfigInsertEdbData(realDataMaxDate, edbDataInsertConfig, edbInfoId, source, existMap, isFindConfigDateRealData)
  129. if isAdd {
  130. addSql = strings.TrimRight(addSql, ",")
  131. _, err = o.Raw(addSql).Exec()
  132. if err != nil {
  133. fmt.Println("RefreshEdbDataFromFubao Err", err.Error())
  134. return
  135. }
  136. }
  137. return
  138. }