2024-06-14 15:45:46 +08:00

191 lines
5.3 KiB
Go
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package dao
import (
"gorm.io/gorm"
"gorm.io/gorm/clause"
"hospital-admin-api/api/model"
"hospital-admin-api/api/requests"
"hospital-admin-api/global"
"strings"
"time"
)
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
}
// 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 修改
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
}
// 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
}
// GetCouponPageSearch 获取列表-分页
func (r *CouponDao) GetCouponPageSearch(req requests.GetSystemCouponPage, page, pageSize int) (m []*model.Coupon, total int64, err error) {
var totalRecords int64
// 构建查询条件
query := global.Db.Model(&model.Coupon{})
// 优惠卷名称
if req.CouponName != "" {
query = query.Where("coupon_name LIKE ?", "%"+req.CouponName+"%")
}
// 优惠卷描述
if req.CouponDesc != "" {
query = query.Where("coupon_desc LIKE ?", "%"+req.CouponDesc+"%")
}
// 使用平台1:小程序)
if req.CouponClient != nil {
query = query.Where("coupon_client = ?", req.CouponClient)
}
// 优惠卷类型1:无门槛 2:满减 3:数量
if req.CouponType != nil {
query = query.Where("coupon_type = ?", req.CouponType)
}
// 状态1:正常 2:强制失效 3:结束 4:删除)
if req.CouponStatus != nil {
query = query.Where("coupon_status = ?", req.CouponStatus)
}
// 发放对象1:全部用户 2:新注册用户 3:会员 4:近期消费 5:近期购药 6:存量用户 7:健康包服务用户)
if req.DistributionObject != nil {
query = query.Where("distribution_object = ?", req.DistributionObject)
}
// 适用范围1:全场通用 2:问诊 3:按品牌适用 4:按类别适用 5:单品使用 6:全品类药品)
if req.ApplicationScope != nil {
query = query.Where("application_scope = ?", req.ApplicationScope)
}
// 关联问诊类型application_scope=问诊时存在生效逗号分隔1:全部 2:快速问诊 3:专家问诊 4:公益问诊 5:问诊购药 6:检测)
if req.InquiryType != "" {
query = query.Where("inquiry_type = ?", req.InquiryType)
}
// 关联品牌id如不限制品牌此项为空
if req.BrandId != "" {
query = query.Where("brand_id = ?", req.BrandId)
}
// 是否互斥0:否 1:是)互斥情况下无法和其他优惠卷同时使用
if req.IsMutex != nil {
query = query.Where("is_mutex = ?", req.IsMutex)
}
// 是否展示0:否 1:是)
if req.IsDisplay != nil {
query = query.Where("is_display = ?", req.IsDisplay)
}
// 有效类型1:绝对时效xxx-xxx时间段有效 2:相对时效 n天内有效
if req.ValidType != nil {
query = query.Where("valid_type = ?", req.ValidType)
}
// 过期之后是否允许再次发放(0:否 1:是)
if req.IsReissuableAfterExpire != nil {
query = query.Where("is_reissuable_after_expire = ?", req.IsReissuableAfterExpire)
}
// 是否首页弹窗0:否 1:是)
if req.IsPopup != nil {
query = query.Where("is_popup = ?", req.IsPopup)
}
// 创建时间
if req.CreatedAt != "" {
createdAt := strings.Split(req.CreatedAt, "&")
if len(createdAt) == 2 {
startTime, _ := time.Parse("2006-01-02", createdAt[0])
endTime, _ := time.Parse("2006-01-02", createdAt[1])
endTime = endTime.Add(23*time.Hour + 59*time.Minute + 59*time.Second)
query = query.Where("created_at 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
}
// Inc 自增
func (r *CouponDao) Inc(tx *gorm.DB, couponId int64, field string, numeral int) error {
err := tx.Model(&model.Coupon{}).Where("coupon_id = ?", couponId).UpdateColumn("coupon_take_count", gorm.Expr(field+" + ?", numeral)).Error
if err != nil {
return err
}
return nil
}
// Dec 自减
func (r *CouponDao) Dec(tx *gorm.DB, couponId int64, field string, numeral int) error {
err := tx.Model(&model.Coupon{}).Where("coupon_id = ?", couponId).UpdateColumn("coupon_take_count", gorm.Expr(field+" - ?", numeral)).Error
if err != nil {
return err
}
return nil
}