221 lines
5.7 KiB
Go
221 lines
5.7 KiB
Go
package dao
|
||
|
||
import (
|
||
"gorm.io/gorm"
|
||
"gorm.io/gorm/clause"
|
||
"hepa-calc-admin-api/api/model"
|
||
"hepa-calc-admin-api/api/requests"
|
||
"hepa-calc-admin-api/global"
|
||
)
|
||
|
||
type UserCouponDao struct {
|
||
}
|
||
|
||
// GetUserCouponById 获取数据-id
|
||
func (r *UserCouponDao) GetUserCouponById(UserCouponId int64) (m *model.UserCoupon, err error) {
|
||
err = global.Db.First(&m, UserCouponId).Error
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
return m, nil
|
||
}
|
||
|
||
// GetUserCouponPreloadById 获取数据-加载全部关联-id
|
||
func (r *UserCouponDao) GetUserCouponPreloadById(UserCouponId int64) (m *model.UserCoupon, err error) {
|
||
err = global.Db.Preload(clause.Associations).First(&m, UserCouponId).Error
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
return m, nil
|
||
}
|
||
|
||
// DeleteUserCoupon 删除
|
||
func (r *UserCouponDao) DeleteUserCoupon(tx *gorm.DB, maps interface{}) error {
|
||
err := tx.Where(maps).Delete(&model.UserCoupon{}).Error
|
||
if err != nil {
|
||
return err
|
||
}
|
||
return nil
|
||
}
|
||
|
||
// DeleteUserCouponById 删除-id
|
||
func (r *UserCouponDao) DeleteUserCouponById(tx *gorm.DB, UserCouponId int64) error {
|
||
if err := tx.Delete(&model.UserCoupon{}, UserCouponId).Error; err != nil {
|
||
return err
|
||
}
|
||
return nil
|
||
}
|
||
|
||
// EditUserCoupon 修改
|
||
func (r *UserCouponDao) EditUserCoupon(tx *gorm.DB, maps interface{}, data interface{}) error {
|
||
err := tx.Model(&model.UserCoupon{}).Where(maps).Updates(data).Error
|
||
if err != nil {
|
||
return err
|
||
}
|
||
return nil
|
||
}
|
||
|
||
// EditUserCouponById 修改-id
|
||
func (r *UserCouponDao) EditUserCouponById(tx *gorm.DB, UserCouponId int64, data interface{}) error {
|
||
err := tx.Model(&model.UserCoupon{}).Where("user_coupon_id = ?", UserCouponId).Updates(data).Error
|
||
if err != nil {
|
||
return err
|
||
}
|
||
return nil
|
||
}
|
||
|
||
// GetUserCouponList 获取列表
|
||
func (r *UserCouponDao) GetUserCouponList(maps interface{}) (m []*model.UserCoupon, err error) {
|
||
err = global.Db.Where(maps).Find(&m).Error
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
return m, nil
|
||
}
|
||
|
||
// GetUserCouponPreloadList 获取列表-加载全部关联
|
||
func (r *UserCouponDao) GetUserCouponPreloadList(maps interface{}) (m []*model.UserCoupon, err error) {
|
||
err = global.Db.Preload(clause.Associations).Where(maps).Find(&m).Error
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
return m, nil
|
||
}
|
||
|
||
// GetUserCouponCount 获取数量
|
||
func (r *UserCouponDao) GetUserCouponCount(maps interface{}) (total int64, err error) {
|
||
err = global.Db.Model(&model.UserCoupon{}).Where(maps).Count(&total).Error
|
||
if err != nil {
|
||
return 0, err
|
||
}
|
||
return total, nil
|
||
}
|
||
|
||
// GetUserCouponListRand 获取列表-随机
|
||
func (r *UserCouponDao) GetUserCouponListRand(maps interface{}, limit int) (m []*model.UserCoupon, err error) {
|
||
err = global.Db.Where(maps).Order("rand()").Limit(limit).Find(&m).Error
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
return m, nil
|
||
}
|
||
|
||
// AddUserCoupon 新增
|
||
func (r *UserCouponDao) AddUserCoupon(tx *gorm.DB, model *model.UserCoupon) (*model.UserCoupon, error) {
|
||
if err := tx.Create(model).Error; err != nil {
|
||
return nil, err
|
||
}
|
||
return model, nil
|
||
}
|
||
|
||
// GetUserCoupon 获取
|
||
func (r *UserCouponDao) GetUserCoupon(maps interface{}) (m *model.UserCoupon, err error) {
|
||
err = global.Db.Where(maps).First(&m).Error
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
return m, nil
|
||
}
|
||
|
||
// GetUserCouponListByValidTime 获取列表-今天开始时间/过期时间
|
||
func (r *UserCouponDao) GetUserCouponListByValidTime(maps interface{}, startTime, endTime string) (m []*model.UserCoupon, 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
|
||
}
|
||
|
||
// GetUserCouponPageSearch 获取列表-分页
|
||
func (r *UserCouponDao) GetUserCouponPageSearch(req requests.GetUserCouponPage, page, pageSize int) (m []*model.UserCoupon, total int64, err error) {
|
||
var totalRecords int64
|
||
|
||
// 构建查询条件
|
||
query := global.Db.Model(&model.UserCoupon{})
|
||
|
||
// 用户
|
||
query = query.Preload("User")
|
||
|
||
// 优惠卷
|
||
query = query.Preload("Coupon")
|
||
|
||
// 优惠券id
|
||
if req.CouponId != "" {
|
||
query = query.Where("coupon_id = ?", req.CouponId)
|
||
}
|
||
|
||
// 用户id
|
||
if req.UserId != nil {
|
||
query = query.Where("user_id = ?", req.UserId)
|
||
}
|
||
|
||
// 优惠券id
|
||
if req.CouponId != "" {
|
||
query = query.Where("coupon_id = ?", req.CouponId)
|
||
}
|
||
|
||
// 状态(0:未使用 1:已使用 3:已过期)
|
||
if req.UserCouponStatus != nil {
|
||
query = query.Where("user_coupon_status = ?", req.UserCouponStatus)
|
||
}
|
||
|
||
// 用户名称
|
||
if req.UserName != "" {
|
||
subQuery := global.Db.Model(&model.User{}).
|
||
Select("user_id").
|
||
Where("user_name LIKE ?", "%"+req.UserName+"%")
|
||
|
||
query = query.Where(gorm.Expr("user_id IN (?)", subQuery))
|
||
}
|
||
|
||
// 用户手机号
|
||
if req.UserMobile != "" {
|
||
subQuery := global.Db.Model(&model.User{}).
|
||
Select("user_id").
|
||
Where("mobile LIKE ?", "%"+req.UserMobile+"%")
|
||
|
||
query = query.Where(gorm.Expr("user_id IN (?)", subQuery))
|
||
}
|
||
|
||
// 优惠券名称
|
||
if req.CouponName != "" {
|
||
subQuery := global.Db.Model(&model.Coupon{}).
|
||
Select("coupon_id").
|
||
Where("coupon_name LIKE ?", "%"+req.CouponName+"%")
|
||
|
||
query = query.Where(gorm.Expr("coupon_id IN (?)", subQuery))
|
||
}
|
||
|
||
// 优惠券类型
|
||
if req.CouponType != nil {
|
||
subQuery := global.Db.Model(&model.Coupon{}).
|
||
Select("coupon_id").
|
||
Where("coupon_type = ?", req.CouponType)
|
||
|
||
query = query.Where(gorm.Expr("coupon_id IN (?)", subQuery))
|
||
}
|
||
|
||
// 适用范围
|
||
if req.ApplicationScope != nil {
|
||
subQuery := global.Db.Model(&model.Coupon{}).
|
||
Select("coupon_id").
|
||
Where("application_scope = ?", req.ApplicationScope)
|
||
|
||
query = query.Where(gorm.Expr("coupon_id IN (?)", subQuery))
|
||
}
|
||
|
||
// 排序
|
||
query = query.Order("created_at desc")
|
||
|
||
// 查询总数量
|
||
if err := query.Count(&totalRecords).Error; err != nil {
|
||
return nil, 0, err
|
||
}
|
||
|
||
err = query.Scopes(model.Paginate(page, pageSize)).Find(&m).Error
|
||
if err != nil {
|
||
return nil, 0, err
|
||
}
|
||
return m, totalRecords, nil
|
||
}
|