varmaxhomAdvertising[]models.MaxhomAdvertising
varmaxhomAdvertisingQtymodels.MaxhomAdvertisingQty
varmaxhomAdvQtymodels.MaxhomAdvertisingQty
db.Where(where).Order(“sort asc”).Find(&maxhomAdvertising)
nowDate:= time.Now().Format(“2006-01-02”)//当前日期
for_,item:=rangemaxhomAdvertising {
ifdb.Where(“advertising_id = ? and date = ?”, item.Id, nowDate).First(&maxhomAdvQty).RecordNotFound() {
maxhomAdvertisingQty.AdvertisingId= item.Id
maxhomAdvertisingQty.Qty=1
maxhomAdvertisingQty.Date= nowDate
db.Create(&maxhomAdvertisingQty)
}else{
db.Model(&models.MaxhomAdvertisingQty{}).Where(“advertising_id = ? and date = ?”, item.Id, nowDate).Updates(map[string]interface{}{“qty”: gorm.Expr(“qty + ?”,1)})
}
}
utils.ReturnData(c, http.StatusOK,map[string]interface{}{
“recordList”: maxhomAdvertising,
“response”: success(),
})
处理报错:
直接去掉 结构体的ID
修改成没有ID
typeMaxhomAdvertisingQtystruct{
Date string`gorm:”column:date;default:NULL;comment:’日期'”`
AdvertisingIdint `gorm:”column:advertising_id;default:0;comment:’广告ID'”`
Qty int `gorm:”column:qty;default:0;comment:’点击数量'”`
}
func(m *MaxhomAdvertisingQty)TableName()string{
return”maxhom_advertising_qty”
}