base_from_google_travel.go 6.2 KB

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