优惠券新增编辑代码优化之策略模式应用

概要

优化一次屎山代码

技术名词解释

策略模式

原来代码片

/**
     * 添加 可添加多条
     * 根据当前登录用户和店铺id,判断该用户是否用新增该店铺优惠券的权限
     * 根据不同的优惠方式,进行不同的必填校验
     *
     * @param couponInfoEntity
     */
    @ApiOperation(value = "优惠券-添加", notes = "优惠券-添加")
    @PostMapping(value = "/add")
    public Result<?> add(@RequestBody CouponInfoEntity couponInfoEntity) {
   
        AppLoginUser sysUser = (AppLoginUser) SecurityUtils.getSubject().getPrincipal();
        //先判断单个的信息,再检查批量信息
        if (StringUtils.isEmpty(couponInfoEntity.getShopId())) {
   
            return Result.error("未传入店铺id");
        } else {
   
            //判断当前用户是否是该店铺的著管理员。才有新增优惠券的权限,
            int roleflag = getRoleByuserIdAndShopId(sysUser.getId(), couponInfoEntity.getShopId());
            if (ROLE_TYPE_CYGLY == roleflag) {
   
                return Result.error("该用户为子管理员,无新增优惠券权限!");
            } else if (ROLE_TYPE_WBD == roleflag) {
   
                return Result.error("该用户与店铺没有绑定信息,无新增优惠券权限!");
            } else if (ROLE_TYPE_WXX == roleflag) {
   
                return Result.error("无法查询到该用户与店铺的绑定信息,无新增优惠券权限!");
            }
        }
        //商户类型
        if (StringUtils.isEmpty(couponInfoEntity.getCouponType())) {
   
            return Result.error("请选择商户类型");
        }
        //优惠方式
        if (StringUtils.isEmpty(couponInfoEntity.getCouponWay())) {
   
            return Result.error("请选择优惠方式");
        }
        //适用类型
        if (StringUtils.isEmpty(couponInfoEntity.getApplicableType())) {
   
            return Result.error("请至少选择一种适用类型");
        }
        //优惠时段
        if (null == couponInfoEntity.getStartTime() || null == couponInfoEntity.getEndTime()) {
   
            return Result.error("请选择优惠时段");
        } else {
   
            try {
   
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd");
                couponInfoEntity.setStartTime(sdf.parse(sdf1.format(couponInfoEntity.getStartTime()) + " 00:00:00"));
                couponInfoEntity.setEndTime(sdf.parse(sdf1.format(couponInfoEntity.getEndTime()) + " 23:59:59"));
            } catch (Exception e) {
   
                e.printStackTrace();
            }
        }
        if (null == couponInfoEntity.getCouponInfoDetailList() || couponInfoEntity.getCouponInfoDetailList().size() < 1) {
   
            return Result.error("请填写优惠信息");
        }
        //遍历校验多条优惠券信息,如果没有问题,则直接添加,有问题则进行提示
        boolean flag = true;
        //错误信息
        List<String> errorMsgList = new ArrayList<>();
        //要新增的结果集
        List<CouponInfo> addList = new ArrayList<CouponInfo>();
        //遍历多条数据
        for (int i = 0; i < couponInfoEntity.getCouponInfoDetailList().size(); i++) {
   
            CouponInfo couponInfo = new CouponInfo();
            BeanUtils.copyProperties(couponInfoEntity, couponInfo);
            CouponInfoDetail detail = couponInfoEntity.getCouponInfoDetailList().get(i);
            if (YHFS_JM.equals(couponInfoEntity.getCouponWay())) {
   
                //判断减免优惠满、减免优惠减 两个字段是否都有值
                if (null == detail.getReductionFill() || null == detail.getReductionReduce()) {
   
                    return Result.error("请填写满减金额");
                } else if (detail.getReductionFill() <= detail.getReductionReduce()) {
   
                    return Result.error("满减金额不能大于等于总金额");
                } else if (detail.getReductionFill() < 0 || detail.getReductionReduce() < 0) {
   
                    return Result.error("金额不能小于0");
                } else {
   
                    couponInfo.setReductionFill(detail.getReductionFill());
                    couponInfo.setReductionReduce(detail.getReductionReduce());
                }
                //优惠方式 2 折扣
            } else if (YHFS_ZK.equals(couponInfoEntity.getCouponWay())) {
   
                if (null == detail.getDiscountStart() || null == detail.getDiscountEnd()) {
   
                    return Result.error("请填写折扣金额区间");
                } else if (detail.getDiscountStart() >= detail.getDiscountEnd()) {
   
                    return Result.error("折扣区间开始金额不能大于等于区间结束金额");
                } else if (detail.getDiscountStart() < 0 || detail.getDiscountEnd() < 0) {
   
                    return Result.error("金额不能小于0");
                } else {
   
                    couponInfo.setDiscountStart(detail.getDiscountStart());
                    couponInfo.setDiscountEnd(detail.getDiscountEnd());
                }
                if (null == detail.getDiscount()) {
   
                    return Result.error("请填写折扣");
                } else if (10 < detail.getDiscount()) {
   
                    return Result.error("折扣不能大于10");
                } else if (0 >= detail.getDiscount()) {
   
                    return Result.error("折扣不能小于等于0");
                } else {
   
                    couponInfo.setDiscount(detail.getDiscount());
                }
                //优惠方式 3 全免
            } else if (YHFS_QM.equals(couponInfoEntity.getCouponWay())) {
   
                if (null == detail.getFreeStart() || null == detail.getFreeEnd()) {
   
                    return Result.error("请填写全免金额区间");
                } else if (detail.getFreeStart() >= detail.getFreeEnd()) {
   
                    return Result.error("全免区间开始金额不能大于等于结束金额");
                } else {
   
                    couponInfo.setFreeStart(detail.getFreeStart());
                    couponInfo.setFreeEnd(detail.getFreeEnd());
                }
            }
            //发放总量
            if (null == detail.getProvideNum()) {
   
                return Result.error("请填写发放总量");
                //发放总量必须大于0
            } else if (0 >= detail.getProvideNum()) {
   
                return Result.error("优惠券发放总量必须大于0");
            } else {
   
                couponInfo.setProvideNum(detail.getProvideNum());
            }
            //单人限领
            if (null == couponInfoEntity.getReceiveNumPerPeople()) {
   
                return Result.error("请填写单人限领量");
                //判断单人限领量不能超过发放总量
            } else if (detail.getProvideNum() < couponInfoEntity.getReceiveNumPerPeople()) {
   
                return Result.error("单人限领量须小于发放总量");
            } else if (couponInfoEntity.getReceiveNumPerPeople
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值