| | |
| | | import com.doumee.dao.web.dto.CouponDTO; |
| | | import com.doumee.dao.web.dto.MemberCouponDTO; |
| | | import com.doumee.dao.web.request.PayDetailRequest; |
| | | import com.doumee.dao.web.response.MemberCouponResponse; |
| | | import com.doumee.dao.web.response.goods.OrderGoodsCalculateResponse; |
| | | import com.doumee.service.business.MemberCouponService; |
| | | import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; |
| | |
| | | import com.github.yulichang.wrapper.MPJLambdaWrapper; |
| | | import org.apache.commons.lang3.StringUtils; |
| | | import org.apache.shiro.SecurityUtils; |
| | | import org.springframework.beans.BeanUtils; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.stereotype.Service; |
| | | |
| | |
| | | |
| | | @Autowired |
| | | private MemberCouponJoinMapper memberCouponJoinMapper; |
| | | |
| | | @Autowired |
| | | private LabelsMapper labelsMapper; |
| | | |
| | | |
| | | @Autowired |
| | | private CouponMapper couponMapper; |
| | |
| | | * @return |
| | | */ |
| | | @Override |
| | | public IPage<CouponDTO> memberCouponPage(IPage pages, BigDecimal price, Integer memberId,Integer couponType) { |
| | | IPage<CouponDTO> page = memberCouponMapper.memberCouponPage(pages,new QueryWrapper<CouponDTO>() |
| | | public IPage<MemberCouponResponse> memberCouponPage(IPage pages, BigDecimal price, Integer memberId,Integer couponType,Integer status) { |
| | | IPage<MemberCouponResponse> page = memberCouponMapper.memberCouponPage(pages,new QueryWrapper<MemberCouponResponse>() |
| | | .eq("m.MEMBER_ID",memberId) |
| | | .eq("m.TYPE",Constants.ONE) |
| | | .eq("m.STATUS",Constants.ZERO) |
| | | .apply(Objects.nonNull(status)&&Constants.equalsInteger(status,Constants.ZERO),"m.STATUS = 0 and m.END_DATE >= now()" ) |
| | | .apply(Objects.nonNull(status)&&Constants.equalsInteger(status,Constants.ONE),"m.STATUS = 1" ) |
| | | .apply(Objects.nonNull(status)&&Constants.equalsInteger(status,Constants.TWO),"m.STATUS = 0 and m.END_DATE < now() " ) |
| | | .eq("TYPE",couponType) |
| | | .apply(!Objects.isNull(price)," m.LIMIT_PRICE >= "+price+" ") |
| | | .apply(" now() between m.START_DATE and m.END_DATE ") |
| | |
| | | } |
| | | |
| | | |
| | | @Override |
| | | public MemberCouponResponse getMemberCouponDetail(Integer id){ |
| | | MemberCoupon memberCoupon = memberCouponJoinMapper.selectById(id); |
| | | if(Objects.isNull(memberCoupon)){ |
| | | throw new BusinessException(ResponseStatus.DATA_EMPTY); |
| | | } |
| | | MemberCouponResponse memberCouponResponse = new MemberCouponResponse(); |
| | | BeanUtils.copyProperties(memberCoupon,memberCouponResponse); |
| | | if(StringUtils.isNotBlank(memberCouponResponse.getApplyIds())){ |
| | | if(Constants.equalsInteger(memberCouponResponse.getApplyType(),Constants.ONE)){ |
| | | List<Labels> labelsList = labelsMapper.selectList(new QueryWrapper<Labels>().lambda() |
| | | .eq(Labels::getIsdeleted,Constants.ZERO) |
| | | .eq(Labels::getType,Constants.LabelsType.GOODSTYPE.getKey()) |
| | | .apply(" find_in_set( id , '"+memberCouponResponse.getApplyIds()+"' ) ") |
| | | ); |
| | | if(CollectionUtils.isNotEmpty(labelsList)){ |
| | | memberCouponResponse.setRelationInfoList(labelsList.stream().map(i->i.getName()).collect(Collectors.toList())); |
| | | } |
| | | }else if(Constants.equalsInteger(memberCouponResponse.getApplyType(),Constants.TWO)){ |
| | | List<Goods> labelsList = goodsMapper.selectList(new QueryWrapper<Goods>().lambda() |
| | | .eq(Goods::getIsdeleted,Constants.ZERO) |
| | | .apply(" find_in_set( id , '"+memberCouponResponse.getApplyIds()+"' ) ") |
| | | ); |
| | | if(CollectionUtils.isNotEmpty(labelsList)){ |
| | | memberCouponResponse.setRelationInfoList(labelsList.stream().map(i->i.getName()).collect(Collectors.toList())); |
| | | } |
| | | } |
| | | } |
| | | |
| | | return memberCouponResponse; |
| | | } |
| | | |
| | | |
| | | |
| | |
| | | if(CollectionUtils.isEmpty(goodsSkuList)||!Constants.equalsInteger(goodsSkuList.size(),requestList.size())){ |
| | | throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(),"商品SKU信息错误,请刷新重试"); |
| | | } |
| | | |
| | | //查询商品列表 |
| | | List<Goods> goodsList = goodsMapper.selectJoinList(Goods.class, |
| | | new MPJLambdaWrapper<Goods>() |
| | |
| | | if(Objects.isNull(goodsList)){ |
| | | return memberCouponList; |
| | | } |
| | | |
| | | //获取选择的商品总金额 与 单个商品金额 |
| | | BigDecimal totalAmount = BigDecimal.ZERO; |
| | | for (Goods goods:goodsList) { |
| | |
| | | } |
| | | |
| | | |
| | | public void couponIsValid(List<PayDetailRequest> requestList,MemberCoupon memberCoupon){ |
| | | if(Objects.isNull(memberCoupon) |
| | | || Constants.equalsInteger(memberCoupon.getStatus(),Constants.ONE) |
| | | ){ |
| | | throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(),"优惠券信息无效或已被使用"); |
| | | } |
| | | if(memberCoupon.getStartDate().getTime() > System.currentTimeMillis()){ |
| | | throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(),"优惠券未生效"); |
| | | } |
| | | if(memberCoupon.getEndDate().getTime() < System.currentTimeMillis()){ |
| | | throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(),"优惠券已过期"); |
| | | } |
| | | List<GoodsSku> goodsSkuList = goodsSkuMapper.selectList(new QueryWrapper<GoodsSku>() |
| | | .lambda() |
| | | .eq(GoodsSku::getIsdeleted,Constants.ZERO) |
| | | .in(GoodsSku::getId,requestList.stream().map(i->i.getGoodsSkuId()).collect(Collectors.toList())) |
| | | ); |
| | | if(CollectionUtils.isEmpty(goodsSkuList)||!Constants.equalsInteger(goodsSkuList.size(),requestList.size())){ |
| | | throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(),"商品SKU信息错误,请刷新重试"); |
| | | } |
| | | //查询商品列表 |
| | | List<Goods> goodsList = goodsMapper.selectJoinList(Goods.class, |
| | | new MPJLambdaWrapper<Goods>() |
| | | .selectAs(GoodsSku::getPrice,Goods::getSkuPrice) |
| | | .selectAs(GoodsSku::getId,Goods::getSkuId) |
| | | .leftJoin(GoodsSku.class,GoodsSku::getGoodsId,Goods::getId) |
| | | .in(GoodsSku::getId,requestList.stream().map(i->i.getGoodsSkuId()).collect(Collectors.toList()))); |
| | | if(Objects.isNull(goodsList)){ |
| | | throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(),"商品SKU商品信息错误,请刷新重试"); |
| | | } |
| | | //获取选择的商品总金额 与 单个商品金额 |
| | | BigDecimal totalAmount = BigDecimal.ZERO; |
| | | for (Goods goods:goodsList) { |
| | | List<PayDetailRequest> request = requestList.stream().filter(i->Constants.equalsInteger(i.getGoodsSkuId(), goods.getSkuId())).collect(Collectors.toList()); |
| | | if(CollectionUtils.isNotEmpty(request)){ |
| | | goods.setSkuAmount(goods.getSkuPrice().multiply(new BigDecimal(request.get(Constants.ZERO).getGoodsNum()+""))); |
| | | totalAmount = totalAmount.add(goods.getSkuAmount()); |
| | | } |
| | | } |
| | | List<OrderGoodsCalculateResponse> goodsCalculateList = ListUtil.copyProperties(goodsList,OrderGoodsCalculateResponse::new); |
| | | if(totalAmount.compareTo(memberCoupon.getLimitPrice())<Constants.ZERO){ |
| | | throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(),"当前商品金额未满足可使用门槛"); |
| | | } |
| | | //适用类型:0=全场;1=品类;2=指定商品 |
| | | if(Constants.equalsInteger(memberCoupon.getApplyType(),Constants.ZERO)){ |
| | | memberCoupon.setValidAmount(memberCoupon.getPrice()); |
| | | }else{ |
| | | List<String> applyIdList = Arrays.asList(memberCoupon.getApplyIds().split(",")); |
| | | //获取有效可以使用优惠券的数据 |
| | | isVaildUse(applyIdList,goodsCalculateList, |
| | | Constants.equalsInteger(memberCoupon.getApplyType(),Constants.ONE)?Constants.ONE:Constants.ZERO); |
| | | BigDecimal skuAmount = goodsCalculateList.stream().filter(i->i.getUseCoupon()).map(i->i.getSkuAmount()).reduce(BigDecimal.ZERO,BigDecimal::add); |
| | | if(skuAmount.compareTo(memberCoupon.getLimitPrice())>=Constants.ZERO){ |
| | | //根据金额计算实际折扣金额 |
| | | BigDecimal couponPrice = skuAmount.multiply(memberCoupon.getPrice()).divide(new BigDecimal("100"),2, RoundingMode.HALF_UP); |
| | | //折扣金额大于最大可折扣金额 则使用最大可折扣金额 |
| | | memberCoupon.setValidAmount(couponPrice.compareTo(memberCoupon.getMaxPrice())>Constants.ZERO?memberCoupon.getMaxPrice():couponPrice); |
| | | } |
| | | } |
| | | } |
| | | |
| | | |
| | | |
| | | |
| | | |
| | | |
| | | |
| | | /** |
| | | * 计算当前已选择优惠券每个商品占比金额 |
| | | * @param coupon 优惠券信息 |