118 lines
2.9 KiB
Go
118 lines
2.9 KiB
Go
package dao
|
|
|
|
import (
|
|
"gorm.io/gorm"
|
|
"gorm.io/gorm/clause"
|
|
"hepa-calc-api/api/model"
|
|
"hepa-calc-api/global"
|
|
)
|
|
|
|
type CouponDao struct {
|
|
}
|
|
|
|
// GetCouponById 获取数据-id
|
|
func (r *CouponDao) GetCouponById(CouponId int64) (m *model.Coupon, err error) {
|
|
err = global.Db.First(&m, CouponId).Error
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return m, nil
|
|
}
|
|
|
|
// GetCouponPreloadById 获取数据-加载全部关联-id
|
|
func (r *CouponDao) GetCouponPreloadById(CouponId int64) (m *model.Coupon, err error) {
|
|
err = global.Db.Preload(clause.Associations).First(&m, CouponId).Error
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return m, nil
|
|
}
|
|
|
|
// DeleteCoupon 删除
|
|
func (r *CouponDao) DeleteCoupon(tx *gorm.DB, maps interface{}) error {
|
|
err := tx.Where(maps).Delete(&model.Coupon{}).Error
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// DeleteCouponById 删除-id
|
|
func (r *CouponDao) DeleteCouponById(tx *gorm.DB, CouponId int64) error {
|
|
if err := tx.Delete(&model.Coupon{}, CouponId).Error; err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// EditCoupon 修改
|
|
func (r *CouponDao) EditCoupon(tx *gorm.DB, maps interface{}, data interface{}) error {
|
|
err := tx.Model(&model.Coupon{}).Where(maps).Updates(data).Error
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// EditCouponById 修改-id
|
|
func (r *CouponDao) EditCouponById(tx *gorm.DB, CouponId int64, data interface{}) error {
|
|
err := tx.Model(&model.Coupon{}).Where("coupon_id = ?", CouponId).Updates(data).Error
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// GetCouponList 获取列表
|
|
func (r *CouponDao) GetCouponList(maps interface{}) (m []*model.Coupon, err error) {
|
|
err = global.Db.Where(maps).Find(&m).Error
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return m, nil
|
|
}
|
|
|
|
// GetCouponCount 获取数量
|
|
func (r *CouponDao) GetCouponCount(maps interface{}) (total int64, err error) {
|
|
err = global.Db.Model(&model.Coupon{}).Where(maps).Count(&total).Error
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
return total, nil
|
|
}
|
|
|
|
// GetCouponListRand 获取列表-随机
|
|
func (r *CouponDao) GetCouponListRand(maps interface{}, limit int) (m []*model.Coupon, err error) {
|
|
err = global.Db.Where(maps).Order("rand()").Limit(limit).Find(&m).Error
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return m, nil
|
|
}
|
|
|
|
// AddCoupon 新增
|
|
func (r *CouponDao) AddCoupon(tx *gorm.DB, model *model.Coupon) (*model.Coupon, error) {
|
|
if err := tx.Create(model).Error; err != nil {
|
|
return nil, err
|
|
}
|
|
return model, nil
|
|
}
|
|
|
|
// GetCoupon 获取
|
|
func (r *CouponDao) GetCoupon(maps interface{}) (m *model.Coupon, err error) {
|
|
err = global.Db.Where(maps).First(&m).Error
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return m, nil
|
|
}
|
|
|
|
// GetCouponListByValidTime 获取列表-今天开始时间/过期时间
|
|
func (r *CouponDao) GetCouponListByValidTime(maps interface{}, startTime, endTime string) (m []*model.Coupon, err error) {
|
|
err = global.Db.Where(maps).Where("valid_end_time BETWEEN ? AND ?", startTime, endTime).Find(&m).Error
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return m, nil
|
|
}
|