|
@@ -2,7 +2,12 @@ package cn.kdan.cloud.pdf.office.product.service.impl;
|
|
|
|
|
|
import cn.kdan.cloud.pdf.office.api.account.feign.UserApi;
|
|
|
import cn.kdan.cloud.pdf.office.api.account.utils.SubscriptionUtil;
|
|
|
+import cn.kdan.cloud.pdf.office.api.payment.feign.OrderApi;
|
|
|
+import cn.kdan.cloud.pdf.office.api.payment.vo.OrdersVO;
|
|
|
+import cn.kdan.cloud.pdf.office.api.product.vo.ListingProductVO;
|
|
|
import cn.kdan.cloud.pdf.office.common.enums.ExceptionEnum;
|
|
|
+import cn.kdan.cloud.pdf.office.common.enums.product.ProductEducationPriceEnum;
|
|
|
+import cn.kdan.cloud.pdf.office.common.pojo.ResultMap;
|
|
|
import cn.kdan.cloud.pdf.office.common.vo.UserSubscriptionInfoVO;
|
|
|
import cn.kdan.cloud.pdf.office.api.product.dto.ProductListDTO;
|
|
|
import cn.kdan.cloud.pdf.office.api.product.vo.ActivityInfoVO;
|
|
@@ -10,15 +15,16 @@ import cn.kdan.cloud.pdf.office.api.product.vo.ProductPriceVo;
|
|
|
import cn.kdan.cloud.pdf.office.api.product.vo.ProductVO;
|
|
|
import cn.kdan.cloud.pdf.office.common.exception.BackendRuntimeException;
|
|
|
import cn.kdan.cloud.pdf.office.common.utils.JsonUtils;
|
|
|
+import cn.kdan.cloud.pdf.office.common.vo.UserVO;
|
|
|
+import cn.kdan.cloud.pdf.office.product.entity.Prize;
|
|
|
import cn.kdan.cloud.pdf.office.product.entity.Product;
|
|
|
import cn.kdan.cloud.pdf.office.product.entity.RProductPay;
|
|
|
import cn.kdan.cloud.pdf.office.product.error.ErrorMessage;
|
|
|
import cn.kdan.cloud.pdf.office.product.mapper.ProductMapper;
|
|
|
-import cn.kdan.cloud.pdf.office.product.service.ActivityInformationService;
|
|
|
-import cn.kdan.cloud.pdf.office.product.service.ProductService;
|
|
|
-import cn.kdan.cloud.pdf.office.product.service.RProductPayService;
|
|
|
+import cn.kdan.cloud.pdf.office.product.service.*;
|
|
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
import com.baomidou.mybatisplus.core.metadata.IPage;
|
|
|
+import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
|
|
|
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
|
|
|
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
|
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
@@ -27,7 +33,10 @@ import lombok.extern.slf4j.Slf4j;
|
|
|
import org.springframework.beans.BeanUtils;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
-import java.util.List;
|
|
|
+import java.math.BigDecimal;
|
|
|
+import java.time.LocalDateTime;
|
|
|
+import java.util.*;
|
|
|
+import java.util.stream.Collectors;
|
|
|
|
|
|
/**
|
|
|
* @author ComPDFKit-WPH 2023-01-28
|
|
@@ -41,9 +50,14 @@ public class ProductServiceImpl extends ServiceImpl<ProductMapper, Product> impl
|
|
|
|
|
|
private final UserApi userAPi;
|
|
|
|
|
|
+ private final OrderApi orderApi;
|
|
|
|
|
|
private final RProductPayService rProductPayService;
|
|
|
|
|
|
+ private final EduEmailsService eduEmailsService;
|
|
|
+
|
|
|
+ private final PrizeService prizeService;
|
|
|
+
|
|
|
@Override
|
|
|
public IPage<ProductVO> page(ProductListDTO query) {
|
|
|
Page<Product> productPage = this.page(new Page<>(query.getPage(), query.getSize()), new LambdaQueryWrapper<Product>()
|
|
@@ -133,4 +147,199 @@ public class ProductServiceImpl extends ServiceImpl<ProductMapper, Product> impl
|
|
|
return productVO;
|
|
|
}
|
|
|
|
|
|
+ @Override
|
|
|
+ public List<ListingProductVO> getListingProducts(Integer isEducation, String platformId) {
|
|
|
+ // 获取上架中的产品
|
|
|
+ List<ListingProductVO> listingProducts = this.baseMapper.getListingProducts(platformId, null);
|
|
|
+ if (null != isEducation && isEducation == 1) {
|
|
|
+ // 返回原价和教育优惠价格
|
|
|
+ listingProducts.forEach(listingProductVO -> {
|
|
|
+ ProductEducationPriceEnum byPrice = ProductEducationPriceEnum.getByPrice(listingProductVO.getPrice());
|
|
|
+ if (null != byPrice) {
|
|
|
+ listingProductVO.setDisplayPrice(byPrice.getDisplayPrice());
|
|
|
+ } else {
|
|
|
+ log.error("产品价格设置错误,id:{}", listingProductVO.getId());
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }
|
|
|
+ return listingProducts;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public ListingProductVO getProductPriceForBuy(String productId, String userId, Integer isEducation) {
|
|
|
+ // 查询购买的商品信息
|
|
|
+ List<ListingProductVO> productVOs = this.baseMapper.getListingProducts(null, Collections.singletonList(productId));
|
|
|
+ ListingProductVO listingProductVO = productVOs.get(0);
|
|
|
+ // 查询用户+所有订单
|
|
|
+ UserVO userVO = userAPi.getById(userId).getResult();
|
|
|
+ List<OrdersVO> orders = orderApi.getOrderByUserId(userId).getResult();
|
|
|
+ if (null != isEducation && isEducation == 1) {
|
|
|
+ // 判断用户教育优惠资格
|
|
|
+ // 根据用户邮箱
|
|
|
+ Integer checkEmail = eduEmailsService.checkEmail(userVO.getEmail());
|
|
|
+ if (checkEmail == 0) {
|
|
|
+ throw new BackendRuntimeException(ErrorMessage.USER_IS_NOT_EDUCATION_USER);
|
|
|
+ }
|
|
|
+ // 查询用户所有订单。判断近一年有没有教育优惠订单
|
|
|
+ if (orders.stream().anyMatch(ordersVO -> ordersVO.getIsEducation() == 1 && ordersVO.getCreatedAt().isAfter(LocalDateTime.now().minusYears(1)))) {
|
|
|
+ throw new BackendRuntimeException(ErrorMessage.USER_HAS_EDUCATION);
|
|
|
+ }
|
|
|
+ // 校验教育用户资格通过
|
|
|
+ listingProductVO.setDisplayPrice(ProductEducationPriceEnum.getByPrice(listingProductVO.getPrice()).getDisplayPrice());
|
|
|
+ // 没有购买过的用户直接返回
|
|
|
+ if (CollectionUtils.isEmpty(orders)) {
|
|
|
+ return listingProductVO;
|
|
|
+ }
|
|
|
+ // 获取已购买的订单商品及会员权益
|
|
|
+ List<String> orderProductIds = orders.stream().map(OrdersVO::getProductId).collect(Collectors.toList());
|
|
|
+ List<ListingProductVO> orderProducts = this.baseMapper.getListingProducts(null, orderProductIds);
|
|
|
+ if (CollectionUtils.isEmpty(orderProducts)) {
|
|
|
+ return listingProductVO;
|
|
|
+ }
|
|
|
+ // 筛选订单购买的产品中有没有vip订阅类型
|
|
|
+ boolean hasVip = false;
|
|
|
+ boolean hasSVip = false;
|
|
|
+ boolean hasPermanent = false;
|
|
|
+ for (ListingProductVO orderProduct : orderProducts) {
|
|
|
+ if (orderProduct.getPaymentModel() == 1) {
|
|
|
+ hasPermanent = true;
|
|
|
+ } else {
|
|
|
+ if (orderProduct.getLevels().contains("2")) {
|
|
|
+ hasVip = true;
|
|
|
+ } else if (orderProduct.getLevels().contains("3")) {
|
|
|
+ hasSVip = true;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ // 购买svip,判断是否买断升级订阅(买且只买了永久版,购买高级版订阅)
|
|
|
+ if (listingProductVO.getLevels().contains("3")) {
|
|
|
+ // vip无法购买svip
|
|
|
+ if (hasVip) {
|
|
|
+ throw new BackendRuntimeException(ErrorMessage.USER_CANNOT_BUY_PREMIUM);
|
|
|
+ }
|
|
|
+ // svip原价提前续费
|
|
|
+ if (hasSVip) {
|
|
|
+ return listingProductVO;
|
|
|
+ }
|
|
|
+ // 买断升级订阅价格
|
|
|
+ if (hasPermanent) {
|
|
|
+ listingProductVO.setUpgradePrice(new BigDecimal("9.99"));
|
|
|
+ return listingProductVO;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ // 购买vip
|
|
|
+ if (listingProductVO.getLevels().contains("2")) {
|
|
|
+ // vip提前续费
|
|
|
+ if (hasVip) {
|
|
|
+ return listingProductVO;
|
|
|
+ }
|
|
|
+ // svip无法购买vip
|
|
|
+ if (hasSVip) {
|
|
|
+ throw new BackendRuntimeException(ErrorMessage.USER_CANNOT_BUY_VIP);
|
|
|
+ }
|
|
|
+ // 买断无法购买vip
|
|
|
+ if (hasPermanent) {
|
|
|
+ throw new BackendRuntimeException(ErrorMessage.USER_CANNOT_UPGRADE_VIP);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return listingProductVO;
|
|
|
+ } else {
|
|
|
+ // 没有购买过的用户直接返回
|
|
|
+ if (CollectionUtils.isEmpty(orders)) {
|
|
|
+ return listingProductVO;
|
|
|
+ }
|
|
|
+ // 获取已购买的订单商品及会员权益
|
|
|
+ List<String> orderProductIds = orders.stream().map(OrdersVO::getProductId).collect(Collectors.toList());
|
|
|
+ List<ListingProductVO> orderProducts = this.baseMapper.getListingProducts(null, orderProductIds);
|
|
|
+ if (CollectionUtils.isEmpty(orderProducts)) {
|
|
|
+ return listingProductVO;
|
|
|
+ }
|
|
|
+ // 筛选订单购买的产品中有没有vip订阅类型
|
|
|
+ boolean hasVip = false;
|
|
|
+ boolean hasSVip = false;
|
|
|
+ boolean hasPermanent = false;
|
|
|
+ for (ListingProductVO orderProduct : orderProducts) {
|
|
|
+ if (orderProduct.getPaymentModel() == 1) {
|
|
|
+ hasPermanent = true;
|
|
|
+ } else {
|
|
|
+ if (orderProduct.getLevels().contains("2")) {
|
|
|
+ hasVip = true;
|
|
|
+ } else if (orderProduct.getLevels().contains("3")) {
|
|
|
+ hasSVip = true;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ // 购买svip,判断是否买断升级订阅(买且只买了永久版,购买高级版订阅)
|
|
|
+ if (listingProductVO.getLevels().contains("3")) {
|
|
|
+ // vip无法购买svip
|
|
|
+ if (hasVip) {
|
|
|
+ throw new BackendRuntimeException(ErrorMessage.USER_CANNOT_BUY_PREMIUM);
|
|
|
+ }
|
|
|
+ // svip原价提前续费
|
|
|
+ if (hasSVip) {
|
|
|
+ return listingProductVO;
|
|
|
+ }
|
|
|
+ // 买断升级订阅价格
|
|
|
+ if (hasPermanent) {
|
|
|
+ listingProductVO.setUpgradePrice(new BigDecimal("19.99"));
|
|
|
+ return listingProductVO;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ // 购买vip
|
|
|
+ if (listingProductVO.getLevels().contains("2")) {
|
|
|
+ // vip原价提前续费
|
|
|
+ if (hasVip) {
|
|
|
+ return listingProductVO;
|
|
|
+ }
|
|
|
+ // svip无法购买vip
|
|
|
+ if (hasSVip) {
|
|
|
+ throw new BackendRuntimeException(ErrorMessage.USER_CANNOT_BUY_VIP);
|
|
|
+ }
|
|
|
+ // 买断无法购买vip
|
|
|
+ if (hasPermanent) {
|
|
|
+ throw new BackendRuntimeException(ErrorMessage.USER_CANNOT_UPGRADE_VIP);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return listingProductVO;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 批量获取产品价格
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public List<Map<Integer, BigDecimal>> getBatchProductPrice() {
|
|
|
+ // 2-5 10% OFF $107.99/个
|
|
|
+ //6-20 20% OFF $95.99/个
|
|
|
+ //21-50 30% OFF $83.99/个
|
|
|
+ //50+ 联系我们获取最新折扣
|
|
|
+ HashMap<Integer, BigDecimal> hashMap = new HashMap<>();
|
|
|
+ hashMap.put(1, new BigDecimal("119.99"));
|
|
|
+ hashMap.put(5, new BigDecimal("107.99"));
|
|
|
+ hashMap.put(20, new BigDecimal("95.99"));
|
|
|
+ hashMap.put(50, new BigDecimal("83.99"));
|
|
|
+ return Collections.singletonList(hashMap);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public ListingProductVO checkCoupon(String productId, String userId, String code) {
|
|
|
+ Prize prize = prizeService.getOne(new LambdaQueryWrapper<Prize>().eq(Prize::getCode, code).eq(Prize::getUserId, userId));
|
|
|
+ if (prize == null) {
|
|
|
+ log.error("优惠券不存在:{}", code);
|
|
|
+ throw new BackendRuntimeException(ErrorMessage.COUPON_NOT_EXISTS);
|
|
|
+ }
|
|
|
+ if (prize.getType() != 1) {
|
|
|
+ log.error("优惠券类型错误:{}", code);
|
|
|
+ throw new BackendRuntimeException(ErrorMessage.COUPON_TYPE_ERROR);
|
|
|
+ }
|
|
|
+ if (!prize.getProductId().contains(productId)) {
|
|
|
+ log.error("优惠券关联产品错误,productId:{}", productId);
|
|
|
+ throw new BackendRuntimeException(ErrorMessage.COUPON_PRODUCT_ERROR);
|
|
|
+ }
|
|
|
+ List<ListingProductVO> productVOs = this.baseMapper.getListingProducts(null, Collections.singletonList(productId));
|
|
|
+ ListingProductVO listingProductVO = productVOs.get(0);
|
|
|
+ listingProductVO.setDisplayPrice(listingProductVO.getDisplayPrice().subtract(new BigDecimal(prize.getAmount())));
|
|
|
+ return listingProductVO;
|
|
|
+ }
|
|
|
}
|