212 lines
5.9 KiB
Go
212 lines
5.9 KiB
Go
package dao
|
||
|
||
import (
|
||
"gorm.io/gorm"
|
||
"gorm.io/gorm/clause"
|
||
"hepa-calc-api/api/model"
|
||
"hepa-calc-api/api/requests"
|
||
"hepa-calc-api/global"
|
||
"strings"
|
||
"time"
|
||
)
|
||
|
||
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
|
||
}
|
||
|
||
// 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.Where("user_id = ?", req.UserId)
|
||
|
||
// 优惠卷
|
||
query = query.Preload("Coupon", func(db *gorm.DB) *gorm.DB {
|
||
return db.Select("coupon_id", "coupon_name", "coupon_type", "coupon_status", "application_scope", "coupon_price", "valid_start_time", "valid_end_time", "coupon_desc")
|
||
})
|
||
|
||
// 优惠券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.IsWindows != nil {
|
||
query = query.Where("is_windows = ?", req.IsWindows)
|
||
}
|
||
|
||
// 使用时间
|
||
if req.CouponUseDate != "" {
|
||
couponUseDate := strings.Split(req.CouponUseDate, "&")
|
||
if len(couponUseDate) == 2 {
|
||
startTime, _ := time.Parse("2006-01-02", couponUseDate[0])
|
||
endTime, _ := time.Parse("2006-01-02", couponUseDate[1])
|
||
|
||
endTime = endTime.Add(23*time.Hour + 59*time.Minute + 59*time.Second)
|
||
|
||
query = query.Where("coupon_use_date BETWEEN ? AND ?", startTime, endTime)
|
||
}
|
||
}
|
||
|
||
// 有效开始时间
|
||
if req.ValidStartTime != "" {
|
||
validStartTime := strings.Split(req.ValidStartTime, "&")
|
||
if len(validStartTime) == 2 {
|
||
startTime, _ := time.Parse("2006-01-02", validStartTime[0])
|
||
endTime, _ := time.Parse("2006-01-02", validStartTime[1])
|
||
|
||
endTime = endTime.Add(23*time.Hour + 59*time.Minute + 59*time.Second)
|
||
|
||
query = query.Where("valid_start_time BETWEEN ? AND ?", startTime, endTime)
|
||
}
|
||
}
|
||
|
||
// 有效结束时间
|
||
if req.ValidEndTime != "" {
|
||
validEndTime := strings.Split(req.ValidEndTime, "&")
|
||
if len(validEndTime) == 2 {
|
||
startTime, _ := time.Parse("2006-01-02", validEndTime[0])
|
||
endTime, _ := time.Parse("2006-01-02", validEndTime[1])
|
||
|
||
endTime = endTime.Add(23*time.Hour + 59*time.Minute + 59*time.Second)
|
||
|
||
query = query.Where("valid_end_time BETWEEN ? AND ?", startTime, endTime)
|
||
}
|
||
}
|
||
|
||
// 排序
|
||
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
|
||
}
|
||
|
||
// 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
|
||
}
|