base_from_google_travel.go 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. package models
  2. import (
  3. "fmt"
  4. "github.com/beego/beego/v2/client/orm"
  5. "hongze/hongze_edb_lib/utils"
  6. "strconv"
  7. "strings"
  8. "time"
  9. )
  10. type BaseFromChangesVisitorsCovid struct {
  11. Id int `orm:"column(id);pk"`
  12. Entity string `description:"国家"`
  13. Code string `description:"国家编码"`
  14. EdbCode string `description:"指标ID"`
  15. Day string `description:"统计的日期"`
  16. RetailAndRecreation string `description:"零售和娱乐场所"`
  17. GroceryAndPharmacy string `description:"杂货店和药房"`
  18. Residential string `description:"居家"`
  19. TransitStations string `description:"公共交通车站"`
  20. Parks string `description:"公园"`
  21. Workplaces string `description:"工作场所"`
  22. Total string `description:"总计"`
  23. CreateTime string `description:"创建时间"`
  24. ModifyTime string `description:"修改时间"`
  25. }
  26. func GetBaseFromChangesVisitorsCovidDataAllByIndexCode(indexCode string) (list []*BaseFromChangesVisitorsCovid, err error) {
  27. o := orm.NewOrm()
  28. sql := `SELECT * FROM base_from_changes_visitors_covid WHERE edb_code=? `
  29. _, err = o.Raw(sql, indexCode).QueryRows(&list)
  30. return
  31. }
  32. func GetBaseFromChangesVisitorsCovidDataByTradeCode(condition string, pars []interface{}) (item []*BaseFromChangesVisitorsCovid, err error) {
  33. sql := ` SELECT * FROM base_from_changes_visitors_covid WHERE 1=1 `
  34. o := orm.NewOrm()
  35. if condition != "" {
  36. sql += condition
  37. }
  38. sql += ` ORDER BY day DESC `
  39. _, err = o.Raw(sql, pars).QueryRows(&item)
  40. return
  41. }
  42. // 新增谷歌出行指标数据
  43. func AddEdbDataGoogleTravel(edbCode string) (err error) {
  44. o := orm.NewOrm()
  45. dataAll, err := GetBaseFromChangesVisitorsCovidDataAllByIndexCode(edbCode)
  46. if err != nil && err.Error() != utils.ErrNoRow() {
  47. return
  48. }
  49. var isAdd bool
  50. addSql := ` INSERT INTO edb_data_google_travel(edb_info_id,edb_code,data_time,value,create_time,modify_time,data_timestamp) values `
  51. for _, sv := range dataAll {
  52. eDate := sv.Day
  53. dataTime, err := time.ParseInLocation(utils.FormatDate, eDate, time.Local)
  54. if err != nil {
  55. fmt.Println("time.Parse Err:" + eDate)
  56. return err
  57. }
  58. timestamp := dataTime.UnixNano() / 1e6
  59. timeStr := fmt.Sprintf("%d", timestamp)
  60. addSql += GetAddSql("0", edbCode, eDate, timeStr, sv.Total)
  61. isAdd = true
  62. }
  63. if isAdd {
  64. addSql = strings.TrimRight(addSql, ",")
  65. utils.FileLog.Info("addSql:" + addSql)
  66. _, err = o.Raw(addSql).Exec()
  67. if err != nil {
  68. return err
  69. }
  70. }
  71. return
  72. }
  73. // 刷新大商所指标数据
  74. func RefreshEdbDataGoogleTravel(edbInfoId int, edbCode, startDate string) (err error) {
  75. source := utils.DATA_SOURCE_GOOGLE_TRAVEL
  76. o := orm.NewOrm()
  77. if err != nil {
  78. return
  79. }
  80. edbInfoIdStr := strconv.Itoa(edbInfoId)
  81. //计算数据
  82. var condition string
  83. var pars []interface{}
  84. if edbCode != "" {
  85. condition += " AND edb_code=? "
  86. pars = append(pars, edbCode)
  87. }
  88. if startDate != "" {
  89. condition += " AND day>=? "
  90. pars = append(pars, startDate)
  91. }
  92. dataList, err := GetBaseFromChangesVisitorsCovidDataByTradeCode(condition, pars)
  93. if err != nil {
  94. return err
  95. }
  96. // 真实数据的最大日期 , 插入规则配置的日期
  97. var realDataMaxDate, edbDataInsertConfigDate time.Time
  98. var edbDataInsertConfig *EdbDataInsertConfig
  99. var isFindConfigDateRealData bool //是否找到配置日期的实际数据的值
  100. {
  101. edbDataInsertConfig, err = GetEdbDataInsertConfigByEdbId(edbInfoId)
  102. if err != nil && err.Error() != utils.ErrNoRow() {
  103. return
  104. }
  105. if edbDataInsertConfig != nil {
  106. edbDataInsertConfigDate = edbDataInsertConfig.Date
  107. }
  108. }
  109. //获取指标所有数据
  110. var existCondition string
  111. var existPars []interface{}
  112. existCondition += " AND edb_info_id=? "
  113. existPars = append(existPars, edbInfoId)
  114. if startDate != "" {
  115. existCondition += " AND data_time>=? "
  116. existPars = append(existPars, startDate)
  117. }
  118. existList, err := GetEdbDataByCondition(source, existCondition, existPars)
  119. if err != nil {
  120. return err
  121. }
  122. existMap := make(map[string]*EdbInfoSearchData)
  123. for _, v := range existList {
  124. existMap[v.DataTime] = v
  125. }
  126. addSql := ` INSERT INTO edb_data_google_travel(edb_info_id,edb_code,data_time,value,create_time,modify_time,data_timestamp) values `
  127. var isAdd bool
  128. for _, v := range dataList {
  129. value := v.Total
  130. item := v
  131. itemValue := value
  132. eDate := item.Day
  133. dataTime, err := time.ParseInLocation(utils.FormatDate, eDate, time.Local)
  134. if err != nil {
  135. return err
  136. }
  137. if _, ok := existMap[v.Day]; !ok {
  138. sValue := itemValue
  139. if sValue != "" {
  140. timestamp := dataTime.UnixNano() / 1e6
  141. timeStr := fmt.Sprintf("%d", timestamp)
  142. saveValue := sValue
  143. if findItem, ok := existMap[eDate]; !ok {
  144. addSql += GetAddSql(edbInfoIdStr, edbCode, eDate, timeStr, saveValue)
  145. isAdd = true
  146. } else {
  147. if findItem != nil && utils.SubFloatToString(findItem.Value, 30) != sValue {
  148. err = ModifyEdbDataById(source, findItem.EdbDataId, sValue)
  149. if err != nil {
  150. return err
  151. }
  152. }
  153. }
  154. }
  155. }
  156. // 下面代码主要目的是处理掉手动插入的数据判断
  157. {
  158. if realDataMaxDate.IsZero() || dataTime.After(realDataMaxDate) {
  159. realDataMaxDate = dataTime
  160. }
  161. if edbDataInsertConfigDate.IsZero() || dataTime.Equal(edbDataInsertConfigDate) {
  162. isFindConfigDateRealData = true
  163. }
  164. }
  165. }
  166. // 处理手工数据补充的配置
  167. HandleConfigInsertEdbData(realDataMaxDate, edbDataInsertConfig, edbInfoId, source, existMap, isFindConfigDateRealData)
  168. if isAdd {
  169. addSql = strings.TrimRight(addSql, ",")
  170. _, err = o.Raw(addSql).Exec()
  171. if err != nil {
  172. return err
  173. }
  174. }
  175. return
  176. }