|
@@ -1,17 +1,25 @@
|
|
package cn.rankin.productservice.service;
|
|
package cn.rankin.productservice.service;
|
|
|
|
|
|
|
|
+import cn.rankin.common.utils.enums.BaseStatusEnum;
|
|
import cn.rankin.data.api.product.dto.PackageProductDTO;
|
|
import cn.rankin.data.api.product.dto.PackageProductDTO;
|
|
|
|
+import cn.rankin.data.api.product.entity.Goods;
|
|
import cn.rankin.data.api.product.entity.MerchantProduct;
|
|
import cn.rankin.data.api.product.entity.MerchantProduct;
|
|
|
|
+import cn.rankin.data.api.product.entity.PackageProductRelation;
|
|
import cn.rankin.data.api.product.entity.Product;
|
|
import cn.rankin.data.api.product.entity.Product;
|
|
import cn.rankin.productservice.repository.GoodsRepository;
|
|
import cn.rankin.productservice.repository.GoodsRepository;
|
|
import cn.rankin.productservice.repository.MerchantProductRepository;
|
|
import cn.rankin.productservice.repository.MerchantProductRepository;
|
|
|
|
+import cn.rankin.productservice.repository.PackageProductRelationRepository;
|
|
import cn.rankin.productservice.repository.ProductRepository;
|
|
import cn.rankin.productservice.repository.ProductRepository;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.stereotype.Service;
|
|
import org.springframework.stereotype.Service;
|
|
|
|
+import org.springframework.util.CollectionUtils;
|
|
|
|
|
|
import javax.transaction.Transactional;
|
|
import javax.transaction.Transactional;
|
|
|
|
+import java.math.BigDecimal;
|
|
import java.util.ArrayList;
|
|
import java.util.ArrayList;
|
|
|
|
+import java.util.HashMap;
|
|
import java.util.List;
|
|
import java.util.List;
|
|
|
|
+import java.util.Map;
|
|
|
|
|
|
@Service
|
|
@Service
|
|
public class PackageChangeService {
|
|
public class PackageChangeService {
|
|
@@ -25,12 +33,141 @@ public class PackageChangeService {
|
|
@Autowired
|
|
@Autowired
|
|
private ProductRepository productRepository;
|
|
private ProductRepository productRepository;
|
|
|
|
|
|
|
|
+ @Autowired
|
|
|
|
+ private PackageProductRelationRepository packageProductRelationRepository;
|
|
|
|
+
|
|
|
|
+ // 课程包进行调整时需要将上架渠道的课程包内的商品进行增删修改
|
|
@Transactional
|
|
@Transactional
|
|
- public void updatePackageInsideGoods(String pkgId, List<PackageProductDTO> dtoList) {
|
|
|
|
|
|
+ public void updatePackageInsideGoods(String pkgId, List<PackageProductDTO> productDTOList) {
|
|
List<String> productIdList = new ArrayList<>();
|
|
List<String> productIdList = new ArrayList<>();
|
|
- dtoList.forEach(dto -> productIdList.add(dto.getPid()));
|
|
|
|
|
|
+ productDTOList.forEach(dto -> productIdList.add(dto.getPid()));
|
|
|
|
|
|
List<Product> productList = productRepository.findByPids(productIdList);
|
|
List<Product> productList = productRepository.findByPids(productIdList);
|
|
|
|
+ Map<String, Product> productMap = new HashMap<>();
|
|
|
|
+ productList.forEach(product -> productMap.put(product.getPid(), product));
|
|
|
|
+
|
|
List<MerchantProduct> merchantProductList = merchantProductRepository.findByPid(pkgId);
|
|
List<MerchantProduct> merchantProductList = merchantProductRepository.findByPid(pkgId);
|
|
|
|
+ if (CollectionUtils.isEmpty(merchantProductList)) {
|
|
|
|
+ return;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ // 看哪些渠道上线了这款产品
|
|
|
|
+ List<String> merchantIdList = new ArrayList<>();
|
|
|
|
+ merchantProductList.forEach(merchantProduct -> merchantIdList.add(merchantProduct.getMerchantId()));
|
|
|
|
+
|
|
|
|
+ // 把goods拿出来
|
|
|
|
+ List<Goods> goodsList = goodsRepository.findByPkgIdAndMerchantIds(pkgId, merchantIdList);
|
|
|
|
+ Map<String, List<Goods>> merchantGoodsMap = new HashMap<>();
|
|
|
|
+ for (Goods goods : goodsList) {
|
|
|
|
+ String merchantId = goods.getMerchantId();
|
|
|
|
+ List<Goods> tmp = merchantGoodsMap.get(merchantId);
|
|
|
|
+ if (tmp == null) {
|
|
|
|
+ tmp = new ArrayList<>();
|
|
|
|
+ tmp.add(goods);
|
|
|
|
+ merchantGoodsMap.put(merchantId, tmp);
|
|
|
|
+ }else {
|
|
|
|
+ tmp.add(goods);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ // 最后需要更新的处理之后的list
|
|
|
|
+ List<Goods> finalGoodsList = new ArrayList<>();
|
|
|
|
+ // 按渠道处理
|
|
|
|
+ for (String merchantId : merchantIdList) {
|
|
|
|
+ Map<String, Goods> productIdGoodsMap = new HashMap<>();
|
|
|
|
+
|
|
|
|
+ List<Goods> merchantGoodsList = merchantGoodsMap.get(merchantId) ;
|
|
|
|
+ if (merchantGoodsList == null) {
|
|
|
|
+ merchantGoodsList = new ArrayList<>();
|
|
|
|
+ }else {
|
|
|
|
+ for (Goods goods : merchantGoodsList) {
|
|
|
|
+ String productId = goods.getId();
|
|
|
|
+ productIdGoodsMap.put(productId, goods);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ for (PackageProductDTO productDTO : productDTOList) {
|
|
|
|
+ String productId = productDTO.getPid();
|
|
|
|
+ Goods goods = productIdGoodsMap.get(productId);
|
|
|
|
+ if (goods == null) {
|
|
|
|
+ goods = new Goods();
|
|
|
|
+ Product product = productMap.get(productId);
|
|
|
|
+ if (product == null) {
|
|
|
|
+ continue;
|
|
|
|
+ }
|
|
|
|
+ // 初始化goods信息
|
|
|
|
+ goods.setCpId(product.getCpId());
|
|
|
|
+ goods.setPid(productId);
|
|
|
|
+ goods.setCpPrice(productDTO.getCpPrice());
|
|
|
|
+ goods.setTerminalPrice(BigDecimal.ZERO);
|
|
|
|
+ goods.setTerminalPrice(BigDecimal.ZERO);
|
|
|
|
+ goods.setDuration(0);
|
|
|
|
+ goods.setMerchantId(merchantId);
|
|
|
|
+ goods.setPkgId(pkgId);
|
|
|
|
+ goods.setType(product.getType());
|
|
|
|
+ merchantGoodsList.add(goods);
|
|
|
|
+ }else {
|
|
|
|
+ goods.setCpPrice(productDTO.getCpPrice());
|
|
|
|
+ goods.setStatus(BaseStatusEnum.NORMAL);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ for (Goods goods : merchantGoodsList) {
|
|
|
|
+ String productId = goods.getPid();
|
|
|
|
+ if (!productIdList.contains(productId)) {
|
|
|
|
+ goods.setStatus(BaseStatusEnum.DEL);
|
|
|
|
+ continue;
|
|
|
|
+ }
|
|
|
|
+ Integer index = productIdList.indexOf(productId);
|
|
|
|
+ goods.setSort(index);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ finalGoodsList.addAll(merchantGoodsList);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ // 最后保存
|
|
|
|
+ goodsRepository.save(finalGoodsList);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ // 课程包上架时需要将课程包内的商品进行相应的添加操作
|
|
|
|
+ @Transactional
|
|
|
|
+ public void addPackageInsideGoods(String pkgId, String merchantId) {
|
|
|
|
+ List<PackageProductRelation> productRelationList = packageProductRelationRepository.findByPkgId(pkgId);
|
|
|
|
+ if (CollectionUtils.isEmpty(productRelationList)) {
|
|
|
|
+ return;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ List<String> productIdList = new ArrayList<>();
|
|
|
|
+ productRelationList.forEach(relation -> productIdList.add(relation.getPid()));
|
|
|
|
+ List<Product> productList = productRepository.findByPids(productIdList);
|
|
|
|
+
|
|
|
|
+ Map<String, Product> productMap = new HashMap<>();
|
|
|
|
+ productList.forEach(product -> productMap.put(product.getPid(), product));
|
|
|
|
+
|
|
|
|
+ List<Goods> goodsList = new ArrayList<>();
|
|
|
|
+ Integer sort = 0;
|
|
|
|
+ for (PackageProductRelation relation : productRelationList) {
|
|
|
|
+ String productId = relation.getPid();
|
|
|
|
+ Product product = productMap.get(productId);
|
|
|
|
+ if (product == null) {
|
|
|
|
+ continue;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ Goods goods = new Goods();
|
|
|
|
+ goods.setCpId(product.getCpId());
|
|
|
|
+ goods.setPid(productId);
|
|
|
|
+ goods.setCpPrice(relation.getCpPrice());
|
|
|
|
+ goods.setTerminalPrice(BigDecimal.ZERO);
|
|
|
|
+ goods.setTerminalPrice(BigDecimal.ZERO);
|
|
|
|
+ goods.setDuration(0);
|
|
|
|
+ goods.setMerchantId(merchantId);
|
|
|
|
+ goods.setPkgId(pkgId);
|
|
|
|
+ goods.setType(product.getType());
|
|
|
|
+
|
|
|
|
+ goodsList.add(goods);
|
|
|
|
+ sort ++;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ goodsRepository.save(goodsList);
|
|
}
|
|
}
|
|
}
|
|
}
|