|
@@ -0,0 +1,120 @@
|
|
|
+package com.fjhx.service.jd.impl;
|
|
|
+
|
|
|
+import cn.hutool.core.bean.BeanUtil;
|
|
|
+import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
|
|
+import com.fjhx.base.Condition;
|
|
|
+import com.fjhx.entity.jd.JdSalesData;
|
|
|
+import com.fjhx.entity.product.ProductInfo;
|
|
|
+import com.fjhx.entity.stock.Stock;
|
|
|
+import com.fjhx.entity.warehouse.Warehouse;
|
|
|
+import com.fjhx.params.jd.JdSalesDataEx;
|
|
|
+import com.fjhx.params.jd.JdSalesDataExcelVo;
|
|
|
+import com.fjhx.mapper.jd.JdSalesDataMapper;
|
|
|
+import com.fjhx.params.jd.JdSalesDataVo;
|
|
|
+import com.fjhx.service.jd.JdSalesDataService;
|
|
|
+import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
|
+import com.fjhx.service.product.ProductInfoService;
|
|
|
+import com.fjhx.service.stock.StockService;
|
|
|
+import com.fjhx.utils.Assert;
|
|
|
+import com.fjhx.utils.wrapperUtil.IWrapper;
|
|
|
+import org.springblade.core.excel.util.ExcelUtil;
|
|
|
+import org.springblade.core.log.exception.ServiceException;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+import org.springframework.transaction.annotation.Transactional;
|
|
|
+import org.springframework.web.multipart.MultipartFile;
|
|
|
+
|
|
|
+import java.math.BigDecimal;
|
|
|
+import java.util.*;
|
|
|
+import java.util.stream.Collectors;
|
|
|
+
|
|
|
+/**
|
|
|
+ * <p>
|
|
|
+ * 京东销售数据 服务实现类
|
|
|
+ * </p>
|
|
|
+ *
|
|
|
+ * @author ${author}
|
|
|
+ * @since 2023-03-16
|
|
|
+ */
|
|
|
+@Service
|
|
|
+public class JdSalesDataServiceImpl extends ServiceImpl<JdSalesDataMapper, JdSalesData> implements JdSalesDataService {
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ ProductInfoService productInfoService;
|
|
|
+ @Autowired
|
|
|
+ StockService stockService;
|
|
|
+
|
|
|
+ @Override
|
|
|
+ @Transactional(rollbackFor = Exception.class)
|
|
|
+ public void excelImport(MultipartFile file) {
|
|
|
+ // 读取excel数据
|
|
|
+ List<JdSalesDataExcelVo> list = ExcelUtil.read(file, JdSalesDataExcelVo.class);
|
|
|
+ //获取所有产品id并判断是否存在
|
|
|
+ List<Long> productIds = list.stream().map(JdSalesDataExcelVo::getProductId).distinct().collect(Collectors.toList());
|
|
|
+ List<ProductInfo> purchases = productInfoService.listByIds(productIds);
|
|
|
+ Assert.notEmpty(purchases, "excel中的所有产品都无法在数据库中搜索到");
|
|
|
+ //检查产品表中是否存在商品
|
|
|
+ if (purchases.size() != productIds.size()) {
|
|
|
+ List<Long> collect = purchases.stream().map(ProductInfo::getId).distinct().collect(Collectors.toList());
|
|
|
+ List<Long> difference = productIds.stream().filter(item -> !collect.contains(item)).collect(Collectors.toList());
|
|
|
+ throw new ServiceException("数据库中未找到以下产品" + difference);
|
|
|
+ }
|
|
|
+ //检查仓库中是否存在商品
|
|
|
+ List<Stock> stockList = stockService.list(q -> q.in(Stock::getGoodsId, productIds).eq(Stock::getWarehouseId, Warehouse.JD_SALES_DATA_WAREHOUSE_ID));
|
|
|
+ if (stockList.size() != productIds.size()) {
|
|
|
+ List<Long> collect = stockList.stream().map(Stock::getGoodsId).distinct().collect(Collectors.toList());
|
|
|
+ List<Long> difference = productIds.stream().filter(item -> !collect.contains(item)).collect(Collectors.toList());
|
|
|
+ throw new ServiceException("仓库中不存在以下产品" + difference);
|
|
|
+ }
|
|
|
+ //根据全国昨日出库商品件数 更新 京东仓库库存数量
|
|
|
+ List<Stock> stockList1 = new ArrayList<>();
|
|
|
+ List<JdSalesData> jdSalesDataList = new ArrayList<>();
|
|
|
+ StringBuilder stringBuilder = new StringBuilder();
|
|
|
+
|
|
|
+ Map<Long, List<Stock>> stockMap = stockList.stream().collect(Collectors.groupingBy(Stock::getGoodsId));
|
|
|
+ for (JdSalesDataExcelVo jdSalesDataExcelVo : list) {
|
|
|
+ JdSalesData jdSalesData = new JdSalesData();
|
|
|
+
|
|
|
+ Long productId = jdSalesDataExcelVo.getProductId();
|
|
|
+ Stock stock = stockMap.get(productId).get(0);
|
|
|
+ //计算仓库库存-全国昨日出库商品件数 后剩余库存 是否大于0
|
|
|
+ BigDecimal quantity = stock.getQuantity().subtract(jdSalesDataExcelVo.getYesterdayQuantity());
|
|
|
+ if (quantity.compareTo(BigDecimal.ZERO) < 0) {
|
|
|
+ stringBuilder.append(productId);
|
|
|
+ } else {
|
|
|
+ stock.setQuantity(quantity);
|
|
|
+ }
|
|
|
+ stockList1.add(stock);
|
|
|
+ }
|
|
|
+ if(stringBuilder.length()>0){
|
|
|
+ throw new ServiceException("仓库中以下产品库存不足" + stringBuilder);
|
|
|
+ }
|
|
|
+ stockService.updateBatchById(stockList1);
|
|
|
+ List<JdSalesData> jdSalesDataList1 = BeanUtil.copyToList(list, JdSalesData.class);
|
|
|
+ saveBatch(jdSalesDataList1);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Page<JdSalesDataEx> getPage(Condition condition) {
|
|
|
+
|
|
|
+ IWrapper<JdSalesData> wrapper = IWrapper.getWrapper(condition);
|
|
|
+
|
|
|
+ return baseMapper.getPage(condition.getPage(), wrapper);
|
|
|
+ }
|
|
|
+//
|
|
|
+// @Override
|
|
|
+// public void add(JdSalesDataVo jdSalesDataVo) {
|
|
|
+// save(jdSalesDataVo);
|
|
|
+// }
|
|
|
+//
|
|
|
+// @Override
|
|
|
+// public void edit(JdSalesDataVo jdSalesDataVo) {
|
|
|
+// updateById(jdSalesDataVo);
|
|
|
+// }
|
|
|
+//
|
|
|
+ @Override
|
|
|
+ public void delete(JdSalesDataVo jdSalesDataVo) {
|
|
|
+ removeById(jdSalesDataVo.getId());
|
|
|
+ }
|
|
|
+
|
|
|
+}
|