package service import ( "errors" "fmt" "hospital-admin-api/api/dao" "hospital-admin-api/api/dto" "hospital-admin-api/api/model" "hospital-admin-api/api/requests" "hospital-admin-api/global" "hospital-admin-api/utils" "strconv" "strings" "time" ) type CouponService struct { } // GetSystemCoupon 系统优惠卷详情 func (r *CouponService) GetSystemCoupon(couponId int64) (g *dto.CouponDto, err error) { couponDao := dao.CouponDao{} coupon, err := couponDao.GetCouponById(couponId) if err != nil || coupon == nil { return nil, errors.New(err.Error()) } // 处理返回值 g = dto.GetCouponDto(coupon) return g, nil } // AddSystemCoupon 新增系统优惠卷 func (r *CouponService) AddSystemCoupon(req requests.AddSystemCoupon) (bool, error) { // 满减 if *req.CouponType == 2 { if *req.WithAmount == 0 { return false, errors.New("请填入符合满减标准金额") } } // 数量 if *req.CouponType == 3 { if *req.MinUsableNumber == 0 { return false, errors.New("请填入单商品最小可使用数量") } } // 发放对象-近期消费 if *req.DistributionObject == 4 { if *req.DistributionWithDay == 0 { return false, errors.New("请填入发放关联天数") } } // 适用范围-全场通用 if *req.ApplicationScope == 1 { // 满减 if *req.CouponType == 2 { if *req.WithAmount == 0 { return false, errors.New("请填入符合满减标准金额") } } // 数量 if *req.CouponType == 3 { if *req.MinUsableNumber == 0 { return false, errors.New("请填入单商品最小可使用数量") } } } // 适用范围-问诊 if *req.ApplicationScope == 2 { // 关联问诊类型 if req.InquiryType == nil { return false, errors.New("请填入关联问诊类型") } } // 适用范围-按品牌适用 if *req.ApplicationScope == 3 { // 关联品牌id if req.BrandId == nil { return false, errors.New("请填入关联品牌") } } // 适用范围-单品使用 if *req.ApplicationScope == 5 { // 关联商品id if len(req.ProductId) == 0 { return false, errors.New("请填入关联商品") } productDao := dao.ProductDao{} for _, s := range req.ProductId { // 将 id 转换为 int64 类型 productId, err := strconv.ParseInt(s, 10, 64) if err != nil { return false, errors.New("新增失败") } _, err = productDao.GetProductById(productId) if err != nil { return false, errors.New("存在错误商品") } } } // 有效类型-绝对时效 if *req.ValidType == 1 { if req.ValidStartTime == nil { return false, errors.New("请填入开始使用时间") } if req.ValidEndTime == nil { return false, errors.New("请填入结束使用时间") } } // 有效类型-相对时效 if *req.ValidType == 2 { if req.ValidDays == nil { return false, errors.New("请填入有效天数") } } // 开始事务 tx := global.Db.Begin() defer func() { if r := recover(); r != nil { tx.Rollback() fmt.Println(r) } }() // 新增优惠卷表 coupon := &model.Coupon{ CouponName: req.CouponName, CouponIcon: utils.AddOssDomain(req.CouponIcon), CouponClient: *req.CouponClient, CouponType: *req.CouponType, CouponStatus: *req.CouponClient, DistributionObject: *req.DistributionObject, ApplicationScope: *req.ApplicationScope, IsMutex: *req.IsMutex, IsDisplay: *req.IsDisplay, CouponCount: *req.CouponCount, CouponPrice: *req.CouponPrice, ValidType: *req.ValidType, IsPopup: *req.IsPopup, } // 关联品牌id if req.BrandId != nil { // 将 id 转换为 int64 类型 brandId, err := strconv.ParseInt(*req.BrandId, 10, 64) if err != nil { tx.Rollback() return false, errors.New("新增失败") } coupon.BrandId = &brandId } // 时间区间 if req.ValidStartTime != nil { t, err := time.Parse("2006-01-02", *req.ValidStartTime) if err != nil { tx.Rollback() return false, errors.New("新增失败") } validStartTime := model.LocalTime(t) coupon.ValidStartTime = &validStartTime } if req.ValidEndTime != nil { t, err := time.Parse("2006-01-02", *req.ValidEndTime) if err != nil { tx.Rollback() return false, errors.New("新增失败") } ValidEndTime := model.LocalTime(t) coupon.ValidEndTime = &ValidEndTime } // 关联商品id if req.ProductId != nil { productId := strings.Join(req.ProductId, ",") if productId != "" { coupon.ProductId = productId } } // 关联问诊类型 if req.InquiryType != nil { coupon.InquiryType = *req.InquiryType } // 发放关联天数 if req.DistributionWithDay != nil { coupon.DistributionWithDay = *req.DistributionWithDay } // 关联 单商品最小可使用数量 if req.DistributionWithDay != nil { coupon.DistributionWithDay = *req.DistributionWithDay } // 符合满减标准金额 if req.WithAmount != nil { coupon.WithAmount = *req.WithAmount } // 有效天数 if req.ValidDays != nil { coupon.ValidDays = *req.ValidDays } // 再次发放间隔天数 if req.ReissueIntervalDays != nil { coupon.ReissueIntervalDays = *req.ReissueIntervalDays } // 允许再次发放 if req.IsReissuableAfterExpire != nil { coupon.IsReissuableAfterExpire = *req.IsReissuableAfterExpire } // 优惠卷描述 if req.CouponDesc != nil { coupon.CouponDesc = *req.CouponDesc } couponDao := dao.CouponDao{} coupon, err := couponDao.AddCoupon(tx, coupon) if err != nil || coupon == nil { tx.Rollback() return false, errors.New(err.Error()) } tx.Commit() return true, nil }