gongzuming
2024-09-10 cee5cbacf74ace746e69aaf2a213fefe5c93ef91
src/main/java/com/mzl/flower/service/impl/coupon/CouponTemplateServiceImpl2.java
@@ -10,14 +10,15 @@
import com.mzl.flower.constant.Constants;
import com.mzl.flower.dto.request.coupon.*;
import com.mzl.flower.dto.response.coupon.CouponPointStatisVO;
import com.mzl.flower.dto.response.coupon.CouponTemplateCustomerVO;
import com.mzl.flower.dto.response.coupon.CouponTemplateVO;
import com.mzl.flower.entity.coupon.CouponRecordDO;
import com.mzl.flower.entity.coupon.CouponTemplateCustomerDO;
import com.mzl.flower.entity.coupon.CouponTemplateDO;
import com.mzl.flower.entity.customer.Customer;
import com.mzl.flower.entity.menber.Member;
import com.mzl.flower.entity.point.CustomerPoint;
import com.mzl.flower.entity.point.CustomerPointDetail;
import com.mzl.flower.entity.point.PointGoodsRecord;
import com.mzl.flower.enums.*;
import com.mzl.flower.mapper.coupon.CouponTemplateMapper;
import com.mzl.flower.mapper.coupon.CouponTemplateMapperCustom;
@@ -27,8 +28,7 @@
import com.mzl.flower.service.coupon.CouponRecordService;
import com.mzl.flower.service.coupon.CouponTemplateCustomerService;
import com.mzl.flower.service.coupon.CouponTemplateService2;
import com.mzl.flower.service.payment.OrderService;
import com.mzl.flower.utils.UUIDGenerator;
import com.mzl.flower.service.menber.MemberService;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
@@ -38,7 +38,9 @@
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
/**
@@ -73,6 +75,11 @@
    @Autowired
    private CustomerPointDetailMapper customerPointDetailMapper;
    @Autowired
    private MemberService memberService;
    @Transactional
    @Override
    public boolean createCouponTemplate(CreateCouponTemplateBO dto) {
@@ -92,6 +99,8 @@
            final List<CouponTemplateCustomerDO> couponCustomList = dto.getPointCostomIdList().stream().map(customId -> {
                CouponTemplateCustomerDO customReDO = new CouponTemplateCustomerDO();
                // 解决本地不加ID不报错,线上报错问题
                customReDO.setId(IdUtil.simpleUUID());
                customReDO.setCouponId(couponTemplateDO.getId());
                customReDO.setCustomId(customId);
                return customReDO;
@@ -124,6 +133,8 @@
            final List<CouponTemplateCustomerDO> couponCustomList = dto.getPointCostomIdList().stream().map(customId -> {
                CouponTemplateCustomerDO customReDO = new CouponTemplateCustomerDO();
                // 解决本地不加ID不报错,线上报错问题
                customReDO.setId(IdUtil.simpleUUID());
                customReDO.setCouponId(couponTemplateDO.getId());
                customReDO.setCustomId(customId);
                return customReDO;
@@ -144,13 +155,29 @@
    @Override
    public List<CouponTemplateVO> getList(QueryCouponDTO dto) {
        List<CouponTemplateVO> list = couponTemplateMapperCustom.getList(dto);
        List<CouponTemplateVO> list =new ArrayList<>();
        if(StringUtils.isNotBlank(dto.getCategory())
                && dto.getCategory().equals(CouponCategoryEnum.MEMBER.getStatus())
        ){
            // 会员优惠券
            list = couponTemplateMapperCustom.getVipList(dto);
        }else{
            list = couponTemplateMapperCustom.getList(dto);
        }
        return list;
    }
    @Override
    public Page<CouponTemplateVO> getPage(Page page, QueryCouponDTO dto) {
        List<CouponTemplateVO> result = couponTemplateMapperCustom.getCouponTemplatePage(page, dto);
        List<CouponTemplateVO> result =new ArrayList<>();
        if(StringUtils.isNotBlank(dto.getCategory())
                && dto.getCategory().equals(CouponCategoryEnum.MEMBER.getStatus())
        ){
            // 会员优惠券
            result = couponTemplateMapperCustom.getCouponTemplateVipPage(page, dto);
        }else{
            result = couponTemplateMapperCustom.getCouponTemplatePage(page, dto);
        }
        return page.setRecords(result);
    }
@@ -160,7 +187,25 @@
        dto.setId(id);
        final List<CouponTemplateVO> list = getList(dto);
        if (CollectionUtils.isNotEmpty(list)) {
            return list.get(0);
            CouponTemplateVO vo =list.get(0);
            // 用户获取
            if(StringUtils.isNotBlank(vo.getCategory())
                  &&  vo.getCategory().equals(CouponCategoryEnum.USER.getStatus())){
                List<CouponTemplateCustomerVO> customerList= couponTemplateMapperCustom.getCouponCustomerList(id);
                vo.setCustomerList(customerList);
            }
            // 会员等级
            if(StringUtils.isNotBlank(vo.getCategory()) && null!=vo.getMemberId()
                    &&  vo.getCategory().equals(CouponCategoryEnum.MEMBER.getStatus())){
                final Member member = memberService.getById(vo.getMemberId());
                if(null!=member){
                    vo.setMember(member);
                    vo.setMemberName(member.getName());
                }
            }
            return vo;
        }
        return null;
    }
@@ -181,7 +226,7 @@
        // 判断当前的优惠券的用户类型是指定用户还是全部用户,如果是指定用户的情况下需要发布优惠券
        if (StringUtils.isNotBlank(couponTemplateDO.getGetUserType())
                && StringUtils.isNotBlank(couponTemplateDO.getCategory())
                && couponTemplateDO.getGetUserType().equals(CouponGetUserTypeEnum.POINT.getType())
                && couponTemplateDO.getGetUserType().equals(CouponGetUserTypeEnum.TARGET.getType())
                && couponTemplateDO.getCategory().equals(CouponCategoryEnum.USER.getStatus())
        ) {
@@ -215,6 +260,7 @@
                List<CouponRecordDO> couponUsageDOList = couponTemplateCustomerDOList.stream().map(pointCustomRe -> {
                    CouponRecordDO couponRecordDO = new CouponRecordDO();
                    couponRecordDO.setId(IdUtil.simpleUUID());
                    couponRecordDO.setCouponId(pointCustomRe.getCouponId());
                    couponRecordDO.setCustomerId(pointCustomRe.getCustomId());
                    final Customer customer = customerMapper.selectById(pointCustomRe.getCustomId());
@@ -233,19 +279,26 @@
                    couponRecordDO.setCouponCode(couponTemplateDO.getCouponCode());
                    couponRecordDO.setCouponName(couponTemplateDO.getCouponName());
                    //  优惠券种类
                    couponRecordDO.setCategory(couponTemplateDO.getGetUserType());
                    couponRecordDO.setCategory(couponTemplateDO.getCategory());
                    couponRecordDO.setMinOrderAmount(couponTemplateDO.getMinOrderAmount());
                    couponRecordDO.setCouponDiscountValue(couponTemplateDO.getCouponDiscountValue());
                    couponRecordDO.setGetType(couponTemplateDO.getGetType());
                    couponRecordDO.setCouponDiscountType(couponTemplateDO.getCouponDiscountType());
                    couponRecordDO.setGetUserType(couponTemplateDO.getGetUserType());
                    couponRecordDO.setPoint(couponTemplateDO.getPoint());
                    couponRecordDO.setMemberId(couponRecordDO.getMemberId());
                    // 创建相关信息
                    couponRecordDO.create(SecurityUtils.getUserId());
                    return couponRecordDO;
                }).collect(Collectors.toList());
                    // 查看当前优惠券是不是已经存在,存在的话则不能添加,防止同一人员重复下发同一张指定的优惠券
                    if(!couponRecordService.checkUserCouponExists(pointCustomRe.getCouponId(),pointCustomRe.getCustomId())){
                        return couponRecordDO;
                    }else{
                        return null;
                    }
                }).filter(Objects::nonNull).collect(Collectors.toList());
                // 批量保存优惠券信息
                couponRecordService.saveBatch(couponUsageDOList);
@@ -382,4 +435,10 @@
        customerPointDetailMapper.insert(detail);
    }
    @Override
    public List<CouponTemplateVO> getActivityEffectList(QueryActivityEffectCouponDTO dto) {
        return couponTemplateMapperCustom.getActivityEffectList(dto);
    }
}