puzhibing
2024-01-12 aec323b302fdc60429ecf8944e40ebae1a85a7fa
ruoyi-modules/ruoyi-member/src/main/java/com/ruoyi/member/service/impl/coupon/CouponServiceImpl.java
@@ -1,6 +1,7 @@
package com.ruoyi.member.service.impl.coupon;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.exception.ServiceException;
@@ -266,12 +267,102 @@
        CouponTotal couponTotal;
        if(StringUtils.isNotBlank(mgtCouponEditDto.getCouponId())){
            coupon = this.getById(mgtCouponEditDto.getCouponId());
            couponTotal = couponTotalService.getById(mgtCouponEditDto.getCouponId());
            if(couponTotal.getSendCount()!=null&&couponTotal.getSendCount()>0){
                throw new ServiceException(AppErrorConstant.COUPON_SENT_EDIT);
//            couponTotal = couponTotalService.getById(mgtCouponEditDto.getCouponId());
//            if(couponTotal.getSendCount()!=null&&couponTotal.getSendCount()>0){
//                throw new ServiceException(AppErrorConstant.COUPON_SENT_EDIT);
//            }
            /**
             * 一:不可修改
             *  1、优惠券类型
             *  2、发放方式
             *  3、优惠券金额
             *  4、使用期限类型
             *  5、有效时间段:开始时间不能修改,结束时间可以修改
             * 二:部分可修改
             *  1、发放方式为指定发放时
             *     定时发放:已到发放时间不可修改
             *     立即发放:不可修改
             *  2、适用范围
             *      全部商品:不可修改
             *      部分商品:只能增加商品
             *  3、发放数量:只能增加
             *      不限制:不可修改
             *  4、用户可领数量:只能增加
             * 三:可修改
             *  1、优惠券名称
             *  2、宣传海报
             * 四:发放对象
             *  1、未到发放时间都可以修改
             *  2、已到发放时间,只有自定义时可修改,且只能增加用户
             */
            if(!coupon.getCouponType().equals(mgtCouponEditDto.getCouponType())){
                throw new ServiceException("不能修改优惠券类型");
            }
            couponRelUserService.deleteCouponRelByCouponId(coupon.getCouponId());
            couponRelGoodsService.deleteCouponRelByCouponId(coupon.getCouponId());
            if(!coupon.getSendType().equals(mgtCouponEditDto.getSendType())){
                throw new ServiceException("不能修改优惠券发放方式");
            }
            if(null != mgtCouponEditDto.getMoneyThreshold() && !coupon.getMoneyThreshold().equals(mgtCouponEditDto.getMoneyThreshold())){
                throw new ServiceException("不能修改优惠券金额");
            }
            if(null != mgtCouponEditDto.getDiscountMoney() && !coupon.getDiscountMoney().equals(mgtCouponEditDto.getDiscountMoney())){
                throw new ServiceException("不能修改优惠券金额");
            }
            if(null != mgtCouponEditDto.getDiscountPercent() && !coupon.getDiscountPercent().equals(mgtCouponEditDto.getDiscountPercent())){
                throw new ServiceException("不能修改优惠券折扣");
            }
            if(null != mgtCouponEditDto.getValidTimeType() && !coupon.getValidTimeType().equals(mgtCouponEditDto.getValidTimeType())){
                throw new ServiceException("不能修改优惠券适用期限类型");
            }
            if(null != mgtCouponEditDto.getValidTimeType() && coupon.getValidTimeType() == 1 && !coupon.getValidStartTime().equals(mgtCouponEditDto.getValidStartTime())){
                throw new ServiceException("不能修改优惠券有效期开始时间");
            }
            if(coupon.getSendType() == 2 && coupon.getSendTimeType() == 1 && mgtCouponEditDto.getSendTimeType() != 1){
                throw new ServiceException("不能修改优惠券发放方式");
            }
            if(coupon.getSendType() == 2 && coupon.getSendTimeType() == 2 && mgtCouponEditDto.getSendTimeType() != 2 && System.currentTimeMillis() <= coupon.getSendTime().getTime()){
                throw new ServiceException("不能修改优惠券发放方式");
            }
            if(coupon.getSendType() == 2 && coupon.getSendTimeType() == 2 && mgtCouponEditDto.getSendTimeType() == 2 && !mgtCouponEditDto.getSendTime().equals(coupon.getSendTime()) && System.currentTimeMillis() <= coupon.getSendTime().getTime()){
                throw new ServiceException("不能修改优惠券发放时间");
            }
            if(coupon.getUseScope() == 1 && !coupon.getUseScope().equals(mgtCouponEditDto.getUseScope())){
                throw new ServiceException("不能修改优惠券适用范围");
            }
            if(coupon.getUseScope() == 2 && coupon.getUseScope().equals(mgtCouponEditDto.getUseScope())){
                List<String> relGoodsIdList = mgtCouponEditDto.getRelGoodsIdList();
                List<CouponRelGoods> list = couponRelGoodsService.list(new QueryWrapper<CouponRelGoods>().eq("coupon_id", coupon.getCouponId()).eq("del_flag", 0));
                for (CouponRelGoods couponRelGoods : list) {
                    if(!relGoodsIdList.contains(couponRelGoods.getGoodsId())){
                        throw new ServiceException("不能删除优惠券适用商品");
                    }
                }
            }
            if(coupon.getSendLimitFlag() == 0 && mgtCouponEditDto.getSendLimitFlag() != 0){
                throw new ServiceException("不能修改优惠券发放数量限制");
            }
            if(coupon.getSendLimitFlag() == 1 && !coupon.getSendLimitNumber().equals(mgtCouponEditDto.getSendLimitNumber())){
                throw new ServiceException("不能修改优惠券发放数量限制");
            }
            if(coupon.getLimitNumber().compareTo(mgtCouponEditDto.getLimitNumber()) < 0){
                throw new ServiceException("只能增加优惠券领取数量");
            }
            List<CouponRelUser> list = couponRelUserService.list(new QueryWrapper<CouponRelUser>().eq("coupon_id", coupon.getCouponId()).eq("del_flag", 0));
            List<Long> relUserIdList = mgtCouponEditDto.getRelUserIdList();
            for (CouponRelUser couponRelUser : list) {
                if(!relUserIdList.contains(couponRelUser.getUserId())){
                    throw new ServiceException("只能增加优惠发放对象");
                }
            }
            if(relUserIdList.size() != list.size() && coupon.getSendType() == 2 && coupon.getSendTimeType() == 1){
                throw new ServiceException("只能修改优惠发放对象");
            }
            if(relUserIdList.size() != list.size() && coupon.getSendType() == 2 && coupon.getSendTimeType() == 2 && System.currentTimeMillis() <= coupon.getSendTime().getTime()){
                throw new ServiceException("只能修改优惠发放对象");
            }
//            couponRelUserService.deleteCouponRelByCouponId(coupon.getCouponId());
//            couponRelGoodsService.deleteCouponRelByCouponId(coupon.getCouponId());
        }else{
            coupon = new Coupon();
            String couponId = IdUtils.simpleUUID();