base_from_google_travel.go 7.1 KB

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