| | |
| | | package com.ruoyi.promotion.service.impl; |
| | | |
| | | import com.baomidou.mybatisplus.extension.plugins.pagination.Page; |
| | | import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; |
| | | import com.google.common.collect.Lists; |
| | | import com.ruoyi.common.core.constant.SecurityConstants; |
| | | import com.ruoyi.common.core.enums.CouponTypeEnum; |
| | | import com.ruoyi.common.core.enums.CouponUseEnum; |
| | | import com.ruoyi.common.core.enums.DistributionMethodEnum; |
| | | import com.ruoyi.common.core.exception.ServiceException; |
| | | import com.ruoyi.common.core.utils.StringUtils; |
| | | import com.ruoyi.common.core.utils.page.BeanUtils; |
| | | import com.ruoyi.common.core.utils.page.CollUtils; |
| | | import com.ruoyi.common.core.utils.page.PageDTO; |
| | | import com.ruoyi.promotion.controller.management.dto.MgtCouponDTO; |
| | | import com.ruoyi.promotion.controller.management.dto.MgtCouponQuery; |
| | | import com.ruoyi.promotion.controller.management.dto.MgtCouponReceiveQuery; |
| | | import com.ruoyi.promotion.controller.management.dto.MgtCouponUpdDTO; |
| | | import com.ruoyi.promotion.controller.management.vo.CouponReceiveDetailVO; |
| | | import com.ruoyi.promotion.controller.management.vo.MgtCouponVO; |
| | | import com.ruoyi.promotion.domain.Coupon; |
| | | import com.ruoyi.promotion.mapper.CouponMapper; |
| | | import com.ruoyi.promotion.service.ICouponMemberService; |
| | | import com.ruoyi.promotion.service.ICouponService; |
| | | import com.ruoyi.promotion.service.async.AsyncMethodService; |
| | | import com.ruoyi.system.api.domain.CouponMember; |
| | | import com.ruoyi.system.api.domain.Member; |
| | | import com.ruoyi.system.api.domain.Order; |
| | | import com.ruoyi.system.api.domain.dto.MemberDTO; |
| | | import com.ruoyi.system.api.feignClient.MemberClient; |
| | | import com.ruoyi.system.api.feignClient.OrderClient; |
| | | import java.util.ArrayList; |
| | | import java.util.List; |
| | | import java.util.Map; |
| | | import java.util.Set; |
| | | import java.util.function.Function; |
| | | import java.util.stream.Collectors; |
| | | import lombok.RequiredArgsConstructor; |
| | | import org.springframework.stereotype.Service; |
| | | |
| | | /** |
| | |
| | | * @since 2024-05-16 |
| | | */ |
| | | @Service |
| | | @RequiredArgsConstructor |
| | | public class CouponServiceImpl extends ServiceImpl<CouponMapper, Coupon> implements ICouponService { |
| | | |
| | | |
| | | private final AsyncMethodService asyncMethodService; |
| | | private final MemberClient memberClient; |
| | | private final OrderClient orderClient; |
| | | private final ICouponMemberService couponMemberService; |
| | | |
| | | /** |
| | | * 获取优惠券列表的分页数据 |
| | | * |
| | | * @param query 管理后台-优惠券查询对象 |
| | | * @return PageDTO<MgtCouponVO> |
| | | */ |
| | | @Override |
| | | public PageDTO<MgtCouponVO> getCouponPage(MgtCouponQuery query) { |
| | | Page<Coupon> page = this.lambdaQuery() |
| | | .select(Coupon::getId, Coupon::getCouponName, Coupon::getCouponType, |
| | | Coupon::getFullReductionAmount, Coupon::getReductionAmount, |
| | | Coupon::getVoucherAmount, Coupon::getDiscountRate, |
| | | Coupon::getDistributionMethod, Coupon::getCouponStatus, |
| | | Coupon::getDescription, Coupon::getCreateTime) |
| | | .like(StringUtils.isNotBlank(query.getCouponName()), Coupon::getCouponName, |
| | | query.getCouponName()) |
| | | .eq(StringUtils.isNotNull(query.getCouponType()), Coupon::getCouponType, |
| | | query.getCouponType()) |
| | | .eq(StringUtils.isNotNull(query.getDistributionMethod()), |
| | | Coupon::getDistributionMethod, query.getDistributionMethod()) |
| | | .page(new Page<>(query.getPageCurr(), query.getPageSize())); |
| | | if (StringUtils.isEmpty(page.getRecords())) { |
| | | return PageDTO.empty(page); |
| | | } |
| | | PageDTO<MgtCouponVO> mgtCouponVOPageDTO = PageDTO.of(page, MgtCouponVO.class); |
| | | List<MgtCouponVO> collect = mgtCouponVOPageDTO.getList().stream().peek(coupon -> { |
| | | if (CouponTypeEnum.FULL_REDUCTION_COUPON.equals(coupon.getCouponType())) { |
| | | coupon.setDetails("满" + coupon.getFullReductionAmount() + "元减" |
| | | + coupon.getReductionAmount() + "元"); |
| | | } else if (CouponTypeEnum.CASH_COUPON.equals(coupon.getCouponType())) { |
| | | coupon.setDetails(coupon.getVoucherAmount().toString()); |
| | | } else if (CouponTypeEnum.COUPON.equals(coupon.getCouponType())) { |
| | | coupon.setDetails(coupon.getDiscountRate() + "折"); |
| | | } |
| | | }).collect(Collectors.toList()); |
| | | mgtCouponVOPageDTO.setList(collect); |
| | | return mgtCouponVOPageDTO; |
| | | } |
| | | |
| | | /** |
| | | * 添加优惠券 |
| | | * |
| | | * @param dto 管理后台-优惠券数据传输对象 |
| | | */ |
| | | @Override |
| | | public void saveCoupon(MgtCouponDTO dto) { |
| | | // 参数校验 |
| | | if (dto.getCouponType().equals(CouponTypeEnum.FULL_REDUCTION_COUPON)) { |
| | | if (StringUtils.isNull(dto.getFullReductionAmount()) || StringUtils.isNull( |
| | | dto.getReductionAmount())) { |
| | | throw new ServiceException("满减券的满减金额不能为空"); |
| | | } |
| | | } else if (dto.getCouponType().equals(CouponTypeEnum.CASH_COUPON) && StringUtils.isNull( |
| | | dto.getVoucherAmount())) { |
| | | throw new ServiceException("代金券的代金券金额不能为空"); |
| | | } else if (dto.getCouponType().equals(CouponTypeEnum.COUPON) && StringUtils.isNull( |
| | | dto.getDiscountRate())) { |
| | | throw new ServiceException("折扣券的折扣额度不能为空"); |
| | | } |
| | | if (dto.getDistributionMethod().equals(DistributionMethodEnum.SPECIFY_MEMBERSHIP_LEVEL) |
| | | && StringUtils.isBlank(dto.getMemberLevel())) { |
| | | throw new ServiceException("会员等级不能为空"); |
| | | } else if (dto.getDistributionMethod().equals(DistributionMethodEnum.DESIGNATED_MEMBERS) |
| | | && StringUtils.isEmpty(dto.getMemberIdList())) { |
| | | throw new ServiceException("会员id列表不能为空"); |
| | | } |
| | | Coupon coupon = BeanUtils.copyBean(dto, Coupon.class); |
| | | this.save(coupon); |
| | | List<Long> memberIdList = new ArrayList<>(); |
| | | if (dto.getDistributionMethod().equals(DistributionMethodEnum.DESIGNATED_MEMBERS)) { |
| | | memberIdList = dto.getMemberIdList(); |
| | | } else if (dto.getDistributionMethod() |
| | | .equals(DistributionMethodEnum.SPECIFY_MEMBERSHIP_LEVEL)) { |
| | | String[] memberLevelsStr = dto.getMemberLevel().replace(",", ",").split(","); |
| | | // 转为List<String>集合 |
| | | List<Member> memberList = memberClient.getMemberListByLevel( |
| | | Lists.newArrayList(memberLevelsStr), SecurityConstants.INNER).getData(); |
| | | memberIdList = memberList.stream().map(Member::getId).collect(Collectors.toList()); |
| | | } else if (dto.getDistributionMethod().equals(DistributionMethodEnum.ALL_MEMBERS)) { |
| | | List<Member> memberList = memberClient.getMemberListByLevel( |
| | | CollUtils.emptyList(), SecurityConstants.INNER).getData(); |
| | | memberIdList = memberList.stream().map(Member::getId).collect(Collectors.toList()); |
| | | } |
| | | |
| | | // 发放优惠券 |
| | | asyncMethodService.issueCoupon(coupon, memberIdList); |
| | | } |
| | | |
| | | /** |
| | | * 编辑优惠券 |
| | | * |
| | | * @param dto 管理后台-优惠券数据传输对象 |
| | | */ |
| | | @Override |
| | | public void updateCoupon(MgtCouponDTO dto) { |
| | | Coupon coupon = this.getById(dto.getId()); |
| | | if (StringUtils.isNull(coupon)) { |
| | | throw new ServiceException("优惠券不存在"); |
| | | } |
| | | coupon.setCouponName(dto.getCouponName()); |
| | | coupon.setDescription(dto.getDescription()); |
| | | this.updateById(coupon); |
| | | } |
| | | |
| | | @Override |
| | | public void updStatus(MgtCouponUpdDTO dto) { |
| | | this.lambdaUpdate() |
| | | .set(Coupon::getCouponStatus, dto.getCouponStatus()) |
| | | .eq(Coupon::getId, dto.getId()); |
| | | } |
| | | |
| | | /** |
| | | * 领取详情 |
| | | * |
| | | * @param query 管理后台-优惠券领取详情查询对象 |
| | | * @return PageDTO<CouponReceiveDetailVO> |
| | | */ |
| | | @Override |
| | | public PageDTO<CouponReceiveDetailVO> getReceiveDetail(MgtCouponReceiveQuery query) { |
| | | MemberDTO memberDTO = null; |
| | | Set<Long> memberIdSet = null; |
| | | if (StringUtils.isNotBlank(query.getRealName()) || StringUtils.isNotBlank( |
| | | query.getPhone())) { |
| | | memberDTO = new MemberDTO(); |
| | | memberDTO.setRealName(query.getRealName()); |
| | | memberDTO.setPhone(query.getPhone()); |
| | | } |
| | | if (StringUtils.isNotNull(memberDTO)) { |
| | | List<Member> data = memberClient.getMemberListByCondition(memberDTO, |
| | | SecurityConstants.INNER).getData(); |
| | | memberIdSet = data.stream().map(Member::getId).collect(Collectors.toSet()); |
| | | } |
| | | Page<CouponMember> page = couponMemberService.lambdaQuery() |
| | | .eq(StringUtils.isNotNull(query.getCouponStatus()), |
| | | CouponMember::getCouponStatus, query.getCouponStatus()) |
| | | .in(StringUtils.isNotEmpty(memberIdSet), CouponMember::getMemberId, memberIdSet) |
| | | .eq(CouponMember::getCouponId, query.getId()) |
| | | .page(new Page<>(query.getPageCurr(), query.getPageSize())); |
| | | if (StringUtils.isEmpty(page.getRecords())) { |
| | | return PageDTO.empty(page); |
| | | } |
| | | PageDTO<CouponReceiveDetailVO> couponReceiveDetailVOPageDTO = PageDTO.of(page, |
| | | CouponReceiveDetailVO.class); |
| | | // 远程调用会员服务,获取会员信息 |
| | | List<Long> memberIdList = page.getRecords().stream().map(CouponMember::getMemberId) |
| | | .collect(Collectors.toList()); |
| | | if (StringUtils.isNotEmpty(memberIdList)) { |
| | | List<Member> memberList = memberClient.getMemberListByIds(memberIdList, |
| | | SecurityConstants.INNER).getData(); |
| | | Map<Long, Member> memberMap = memberList.stream() |
| | | .collect(Collectors.toMap(Member::getId, Function.identity())); |
| | | couponReceiveDetailVOPageDTO.getList().forEach(item -> { |
| | | Member member = memberMap.get(item.getMemberId()); |
| | | item.setRealName(member.getRealName()); |
| | | item.setPhone(member.getPhone()); |
| | | }); |
| | | } |
| | | // 远程调用订单服务,获取订单信息 |
| | | List<CouponReceiveDetailVO> usedCouponList = couponReceiveDetailVOPageDTO.getList().stream() |
| | | .filter(item -> item.getCouponStatus().equals( |
| | | CouponUseEnum.USED)).collect(Collectors.toList()); |
| | | Set<Long> couponIdSet = usedCouponList.stream().map(CouponReceiveDetailVO::getCouponId) |
| | | .collect(Collectors.toSet()); |
| | | Map<String, Order> orderMap = null; |
| | | Map<Long, Member> memberMap = null; |
| | | if (StringUtils.isNotEmpty(usedCouponList)) { |
| | | List<Order> data = orderClient.getOrderByCouponIds(couponIdSet, SecurityConstants.INNER) |
| | | .getData(); |
| | | orderMap = data.stream().collect( |
| | | Collectors.toMap(order -> order.getCouponId() + "-" + order.getMemberId(), |
| | | Function.identity())); |
| | | |
| | | } |
| | | Set<Long> memberSet = couponReceiveDetailVOPageDTO.getList().stream() |
| | | .map(CouponReceiveDetailVO::getMemberId).collect( |
| | | Collectors.toSet()); |
| | | if (StringUtils.isNotEmpty(memberSet)) { |
| | | List<Member> data = memberClient.getMemberListByIds(memberSet, SecurityConstants.INNER) |
| | | .getData(); |
| | | memberMap = data.stream().collect( |
| | | Collectors.toMap(Member::getId, Function.identity())); |
| | | } |
| | | // 封装VO |
| | | for (CouponReceiveDetailVO couponReceiveDetailVO : couponReceiveDetailVOPageDTO.getList()) { |
| | | if (StringUtils.isNotEmpty(memberMap)) { |
| | | Member member = memberMap.get(couponReceiveDetailVO.getMemberId()); |
| | | if (StringUtils.isNotNull(member)) { |
| | | couponReceiveDetailVO.setRealName(member.getRealName()); |
| | | couponReceiveDetailVO.setPhone(member.getPhone()); |
| | | } |
| | | } else if (StringUtils.isNotEmpty(orderMap)) { |
| | | Order order = orderMap.get(couponReceiveDetailVO.getCouponId() + "-" |
| | | + couponReceiveDetailVO.getMemberId()); |
| | | if (StringUtils.isNotNull(order)) { |
| | | couponReceiveDetailVO.setOrderNo(order.getOrderNo()); |
| | | couponReceiveDetailVO.setCreateTime(order.getCreateTime()); |
| | | } |
| | | } else { |
| | | break; |
| | | } |
| | | } |
| | | return couponReceiveDetailVOPageDTO; |
| | | } |
| | | } |