diff --git a/src/dao/egg_access_total_dao.go b/src/dao/egg_access_total_dao.go index 824248a..96e4ed8 100644 --- a/src/dao/egg_access_total_dao.go +++ b/src/dao/egg_access_total_dao.go @@ -5,4 +5,5 @@ import "code.fnuoos.com/EggPlanet/egg_models.git/src/model" type EggAccessTotalDao interface { //TODO:: You can add specific method definitions here EggAccessTotalGetByPage(year, week, page, limit int) ([]model.EggAccessTotal, error) + EggAccessTotalAddOrDefault(uid int64, year, week int) (int64, error) } diff --git a/src/implement/egg_access_total_implement.go b/src/implement/egg_access_total_implement.go index 2284ab8..89b7dbb 100644 --- a/src/implement/egg_access_total_implement.go +++ b/src/implement/egg_access_total_implement.go @@ -23,3 +23,33 @@ func (e EggAccessTotalDb) EggAccessTotalGetByPage(year, week, page, limit int) ( } return m, nil } + +func (e EggAccessTotalDb) EggAccessTotalAddOrDefault(uid int64, year, week int) (int64, error) { + session := e.Db.NewSession() + session.Begin() + var m model.EggAccessTotal + var affected int64 + exist, err := session.Where("uid = ?", uid).And("year = ?", year).And("week = ?", week).Get(&m) + if err != nil { + return 0, zhios_order_relate_logx.Error(err.Error()) + } + if !exist { + // 不存在则创建 + m.Total = 1 + m.Uid = uid + m.Year = year + m.Week = week + affected, err = session.Insert(m) + if err != nil { + return 0, zhios_order_relate_logx.Error(err.Error()) + } + } else { + m.Total++ + affected, err = session.Where("id = ?", m.Id).MustCols("total").Update(m) + if err != nil { + return 0, zhios_order_relate_logx.Error(err.Error()) + } + } + session.Commit() + return affected, nil +}