|
@@ -2,33 +2,46 @@ package com.ym.mec.biz.service.impl;
|
|
|
|
|
|
import com.alibaba.fastjson.JSON;
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
-import com.ym.mec.biz.dal.dao.*;
|
|
|
+import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
|
|
+import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
|
|
|
+import com.microsvc.toolkit.middleware.oss.OssPluginContext;
|
|
|
+import com.microsvc.toolkit.middleware.oss.impl.TencentOssPlugin;
|
|
|
+import com.ym.mec.biz.dal.dao.GoodsDao;
|
|
|
+import com.ym.mec.biz.dal.dao.OrganizationDao;
|
|
|
+import com.ym.mec.biz.dal.dao.TeacherDao;
|
|
|
import com.ym.mec.biz.dal.dto.BasicUserDto;
|
|
|
import com.ym.mec.biz.dal.dto.GoodsSellDto;
|
|
|
-import com.ym.mec.biz.dal.entity.Goods;
|
|
|
-import com.ym.mec.biz.dal.entity.GoodsProcurement;
|
|
|
-import com.ym.mec.biz.dal.entity.Organization;
|
|
|
-import com.ym.mec.biz.dal.entity.SellOrder;
|
|
|
+import com.ym.mec.biz.dal.entity.*;
|
|
|
import com.ym.mec.biz.dal.enums.*;
|
|
|
+import com.ym.mec.biz.dal.page.GoodsCategoryQueryInfo;
|
|
|
import com.ym.mec.biz.dal.page.GoodsQuery;
|
|
|
import com.ym.mec.biz.dal.page.GoodsQueryInfo;
|
|
|
+import com.ym.mec.biz.dal.wrapper.GoodsWrapper;
|
|
|
import com.ym.mec.biz.service.*;
|
|
|
import com.ym.mec.common.dal.BaseDAO;
|
|
|
-import com.ym.mec.common.entity.UploadReturnBean;
|
|
|
+import com.ym.mec.common.dto.*;
|
|
|
+import com.ym.mec.common.entity.GoodsSubModel;
|
|
|
+import com.ym.mec.common.entity.GoodsSubStockModel;
|
|
|
import com.ym.mec.common.exception.BizException;
|
|
|
-import com.ym.mec.common.service.IdGeneratorService;
|
|
|
+import com.ym.mec.common.page.PageInfo;
|
|
|
+import com.ym.mec.common.page.QueryInfo;
|
|
|
import com.ym.mec.common.service.impl.BaseServiceImpl;
|
|
|
import com.ym.mec.common.tenant.TenantContextHolder;
|
|
|
+import com.ym.mec.mall.MallFeignService;
|
|
|
import com.ym.mec.thirdparty.message.MessageSenderPluginContext;
|
|
|
import com.ym.mec.util.collection.MapUtil;
|
|
|
import com.ym.mec.util.excel.POIUtil;
|
|
|
import com.ym.mec.util.ini.IniFileUtil;
|
|
|
-
|
|
|
+import com.ym.mec.util.upload.UploadUtil;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.apache.commons.lang.math.NumberUtils;
|
|
|
import org.apache.commons.lang3.StringUtils;
|
|
|
import org.apache.poi.ss.usermodel.PictureData;
|
|
|
+import org.apache.poi.ss.usermodel.Workbook;
|
|
|
+import org.apache.poi.ss.usermodel.WorkbookFactory;
|
|
|
import org.slf4j.Logger;
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
-import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.boot.system.ApplicationHome;
|
|
|
import org.springframework.core.io.ClassPathResource;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
import org.springframework.transaction.annotation.Isolation;
|
|
@@ -36,41 +49,50 @@ import org.springframework.transaction.annotation.Transactional;
|
|
|
import org.springframework.util.CollectionUtils;
|
|
|
import org.springframework.web.multipart.MultipartFile;
|
|
|
|
|
|
-import java.io.ByteArrayInputStream;
|
|
|
-import java.io.InputStream;
|
|
|
+import javax.annotation.Resource;
|
|
|
+import javax.imageio.ImageIO;
|
|
|
+import java.awt.image.BufferedImage;
|
|
|
+import java.io.*;
|
|
|
import java.math.BigDecimal;
|
|
|
+import java.nio.charset.StandardCharsets;
|
|
|
+import java.nio.file.Files;
|
|
|
+import java.text.SimpleDateFormat;
|
|
|
import java.util.*;
|
|
|
-import java.util.concurrent.atomic.AtomicInteger;
|
|
|
+import java.util.function.Function;
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
+@Slf4j
|
|
|
@Service
|
|
|
public class GoodsServiceImpl extends BaseServiceImpl<Integer, Goods> implements GoodsService {
|
|
|
private static final Logger LOGGER = LoggerFactory.getLogger(CourseScheduleTeacherSalaryServiceImpl.class);
|
|
|
-
|
|
|
- @Autowired
|
|
|
+
|
|
|
+ @Resource
|
|
|
private GoodsDao goodsDao;
|
|
|
- @Autowired
|
|
|
- private UploadFileService uploadFileService;
|
|
|
- @Autowired
|
|
|
- private IdGeneratorService idGeneratorService;
|
|
|
- @Autowired
|
|
|
- private GoodsProcurementDao goodsProcurementDao;
|
|
|
- @Autowired
|
|
|
+ @Resource
|
|
|
private SysTenantConfigService sysTenantConfigService;
|
|
|
- @Autowired
|
|
|
+ @Resource
|
|
|
private SysMessageService sysMessageService;
|
|
|
- @Autowired
|
|
|
+ @Resource
|
|
|
private TeacherDao teacherDao;
|
|
|
- @Autowired
|
|
|
- private SellOrderDao sellOrderDao;
|
|
|
- @Autowired
|
|
|
+ @Resource
|
|
|
private OrganizationDao organizationDao;
|
|
|
-
|
|
|
+ @Resource
|
|
|
+ private MallFeignService mallFeignService;
|
|
|
+ @Resource
|
|
|
+ private GoodsCategoryService goodsCategoryService;
|
|
|
+ @Resource
|
|
|
+ private GoodsSubService goodsSubService;
|
|
|
+ @Resource
|
|
|
+ private OssPluginContext ossPluginContext;
|
|
|
@Override
|
|
|
public BaseDAO<Integer, Goods> getDAO() {
|
|
|
return goodsDao;
|
|
|
}
|
|
|
|
|
|
+ public GoodsDao getGoodsDao() {
|
|
|
+ return goodsDao;
|
|
|
+ }
|
|
|
+
|
|
|
@Override
|
|
|
public List<Goods> exportGoods(GoodsQueryInfo queryInfo) {
|
|
|
Map<String, Object> params = new HashMap<String, Object>();
|
|
@@ -154,27 +176,53 @@ public class GoodsServiceImpl extends BaseServiceImpl<Integer, Goods> implement
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
- @Transactional(rollbackFor = Exception.class,isolation = Isolation.READ_COMMITTED)
|
|
|
- public void addGoods(Goods goods, Integer operatorId) {
|
|
|
- if(StringUtils.isBlank(goods.getSn())){
|
|
|
- throw new BizException("请指定商品货号");
|
|
|
- }
|
|
|
- Goods existsGood = goodsDao.lockBySn(goods.getSn());
|
|
|
- if(Objects.nonNull(existsGood)){
|
|
|
- throw new BizException("商品货号重复");
|
|
|
- }else{
|
|
|
- if(Objects.isNull(goods.getStockCount())){
|
|
|
- goods.setStockCount(0);
|
|
|
+ @Transactional(rollbackFor = Exception.class)
|
|
|
+ public void addGoods(List<GoodsWrapper.Goods> goodsList, Integer operatorId) {
|
|
|
+// List<String> snList = goodsList.stream().map(GoodsWrapper.Goods::getSn).collect(Collectors.toList());
|
|
|
+// if (snList.size() != new HashSet<>(snList).size()) {
|
|
|
+// throw new BizException("商品货号重复");
|
|
|
+// }
|
|
|
+// List<Goods> bySns = goodsDao.findBySns(snList);
|
|
|
+// if (!CollectionUtils.isEmpty(bySns)) {
|
|
|
+// throw new BizException("商品货号重复");
|
|
|
+// }
|
|
|
+ for (GoodsWrapper.Goods goods : goodsList) {
|
|
|
+ if (StringUtils.isBlank(goods.getSn())) {
|
|
|
+ throw new BizException("请指定商品货号");
|
|
|
}
|
|
|
- if(Objects.isNull(goods.getTaxStockCount())){
|
|
|
- goods.setTaxStockCount(0);
|
|
|
+// Goods existsGood = goodsDao.lockBySn(goods.getSn());
|
|
|
+// if (Objects.nonNull(existsGood)) {
|
|
|
+// throw new BizException("商品货号重复");
|
|
|
+// }
|
|
|
+ List<GoodsWrapper.GoodsSub> goodsSubList = goods.getGoodsSubList();
|
|
|
+ //获取最小的库存数
|
|
|
+ String skuIds = goodsSubList.stream().map(e -> e.getSku().toString()).collect(Collectors.joining(","));
|
|
|
+ PmsProductQueryParamDto paramDto = new PmsProductQueryParamDto();
|
|
|
+ paramDto.setSkuStockIds(skuIds);
|
|
|
+// paramDto.setPublishStatus(1);
|
|
|
+ paramDto.setPageSize(1000);
|
|
|
+ paramDto.setPageNum(1);
|
|
|
+ paramDto.setJson(JSON.toJSONString(paramDto));
|
|
|
+ List<PmsProductDto> productList = mallFeignService.getProductList(paramDto).getRows();
|
|
|
+ if (CollectionUtils.isEmpty(productList)) {
|
|
|
+ throw new BizException("子商品不存在");
|
|
|
}
|
|
|
+ //获取最小库存数量
|
|
|
+ Integer stock = productList.stream().map(PmsProductDto::getStock).min(Integer::compareTo).get();
|
|
|
+ goods.setStockCount(stock);
|
|
|
goods.setSellCount(0);
|
|
|
- goodsDao.insert(goods);
|
|
|
+ Goods goodsRecord = JSON.parseObject(JSON.toJSONString(goods), Goods.class);
|
|
|
+ goodsRecord.setStatus(YesOrNoEnum.NO);
|
|
|
+ BigDecimal organCostPrice = goodsSubList.stream().map(GoodsWrapper.GoodsSub::getGoodsPrice).reduce(BigDecimal.ZERO, BigDecimal::add);
|
|
|
+ goodsRecord.setOrganCostPrice(organCostPrice);
|
|
|
+ goodsDao.insert(goodsRecord);
|
|
|
+ List<GoodsSub> goodsSubs = JSON.parseArray(JSON.toJSONString(goodsSubList), GoodsSub.class);
|
|
|
+ goodsSubs.forEach(next -> next.setGoodsId(goodsRecord.getId()));
|
|
|
+ goodsSubService.saveBatch(goodsSubs);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- @Override
|
|
|
+ /*@Override
|
|
|
@Transactional(rollbackFor = Exception.class)
|
|
|
public void addGoodsProcurement(GoodsProcurement goodsProcurement) {
|
|
|
Goods existsGood = goodsDao.get(goodsProcurement.getGoodsId());
|
|
@@ -204,7 +252,7 @@ public class GoodsServiceImpl extends BaseServiceImpl<Integer, Goods> implement
|
|
|
goodsProcurementDao.insert(goodsProcurement);
|
|
|
|
|
|
sellOrderBatchNoAllot(existsGood.getTenantId());
|
|
|
- }
|
|
|
+ }*/
|
|
|
|
|
|
@Override
|
|
|
@Transactional(rollbackFor = Exception.class)
|
|
@@ -227,6 +275,13 @@ public class GoodsServiceImpl extends BaseServiceImpl<Integer, Goods> implement
|
|
|
throw new BizException("{}等商品还在销售中", goodsNames);
|
|
|
}
|
|
|
}
|
|
|
+ List<GoodsWrapper.GoodsSub> goodsSubs = queryGoodsSubByGoodId(goodsId);
|
|
|
+ if (!CollectionUtils.isEmpty(goodsSubs) && status == 1) {
|
|
|
+ long falseSize = goodsSubs.stream().map(GoodsWrapper.GoodsSub::getGoodsStatus).filter(Boolean.FALSE::equals).count();
|
|
|
+ if (falseSize > 0) {
|
|
|
+ throw new BizException("存在已下架的子商品");
|
|
|
+ }
|
|
|
+ }
|
|
|
}else{
|
|
|
if(status==1){
|
|
|
List<Integer> goodsIds = Arrays.stream(goods.getComplementGoodsIdList().split(",")).map(s -> Integer.valueOf(s)).collect(Collectors.toList());
|
|
@@ -241,6 +296,56 @@ public class GoodsServiceImpl extends BaseServiceImpl<Integer, Goods> implement
|
|
|
goodsDao.update(goods);
|
|
|
}
|
|
|
|
|
|
+ private List<GoodsWrapper.GoodsSub> queryGoodsSubByGoodId(Integer goodsId) {
|
|
|
+ // 从管乐迷商城组合的商品
|
|
|
+ QueryWrapper<GoodsSub> queryWrapper = new QueryWrapper<>();
|
|
|
+ queryWrapper.eq("goods_id_", goodsId);
|
|
|
+ List<GoodsSub> goodsSubs = goodsSubService.getBaseMapper().selectList(queryWrapper);
|
|
|
+ if (goodsSubs.isEmpty()) {
|
|
|
+ return new ArrayList<>();
|
|
|
+ }
|
|
|
+// List<String> skuList = goodsSubs.stream().map(next -> next.getSku().toString()).distinct().collect(Collectors.toList());
|
|
|
+ List<GoodsWrapper.GoodsSub> goodsSubList = goodsSubs.stream().map(next -> {
|
|
|
+ GoodsWrapper.GoodsSub goodsSub = new GoodsWrapper.GoodsSub();
|
|
|
+ goodsSub.setId(next.getId());
|
|
|
+ goodsSub.setGoodsId(goodsId);
|
|
|
+ goodsSub.setMallGoodsId(next.getMallGoodsId());
|
|
|
+ goodsSub.setGoodsStatus(next.getGoodsStatus());
|
|
|
+ goodsSub.setSku(next.getSku());
|
|
|
+ goodsSub.setGoodsPrice(next.getGoodsPrice());
|
|
|
+ return goodsSub;
|
|
|
+ }).collect(Collectors.toList());
|
|
|
+ List<String> goodIdList = goodsSubs.stream().map(next -> next.getMallGoodsId().toString()).distinct().collect(Collectors.toList());
|
|
|
+ PmsProductQueryParamDto build = PmsProductQueryParamDto.builder()
|
|
|
+ .productIds(String.join(",", goodIdList))
|
|
|
+ .pageSize(9999)
|
|
|
+ .pageNum(1).build();
|
|
|
+ build.setJson(JSON.toJSONString(build));
|
|
|
+ List<PmsProductDto> rows = mallFeignService.getProductList(build).getRows();
|
|
|
+ Map<Integer, PmsProductDto> map = rows.stream().collect(Collectors.toMap(PmsProductDto::getSkuStockId, Function.identity()));
|
|
|
+ Map<Long, List<PmsProductDto>> groupByGoodId = rows.stream().collect(Collectors.groupingBy(PmsProductDto::getGoodsId));
|
|
|
+ goodsSubList.forEach(next -> {
|
|
|
+ PmsProductDto dto = map.get(next.getSku());
|
|
|
+ if (dto != null) {
|
|
|
+ next.setPrice(dto.getPrice());
|
|
|
+ next.setSkuCode(dto.getSkuCode());
|
|
|
+ if (1 == dto.getDeleteStatus()) {
|
|
|
+ next.setGoodsStatus(false);
|
|
|
+ } else {
|
|
|
+ next.setGoodsStatus(dto.getPublishStatus() == 1);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if(groupByGoodId.containsKey(Long.valueOf(next.getMallGoodsId()))){
|
|
|
+ PmsProductDto dto1 = groupByGoodId.get(Long.valueOf(next.getMallGoodsId())).get(0);
|
|
|
+ next.setMallGoodsName(dto1.getName());
|
|
|
+ next.setProductSn(dto1.getProductSn());
|
|
|
+ next.setBrandId(dto1.getBrandId());
|
|
|
+ next.setBrandName(dto1.getBrandName());
|
|
|
+ }
|
|
|
+ });
|
|
|
+ return goodsSubList;
|
|
|
+ }
|
|
|
+
|
|
|
@Override
|
|
|
public List<Goods> findGoodsBySubId(GoodsQuery goodsQuery) {
|
|
|
return goodsDao.findGoodsBySubId(goodsQuery);
|
|
@@ -261,6 +366,20 @@ public class GoodsServiceImpl extends BaseServiceImpl<Integer, Goods> implement
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
+ public PageInfo<Goods> queryPage(QueryInfo queryInfo) {
|
|
|
+ PageInfo<Goods> page = super.queryPage(queryInfo);
|
|
|
+
|
|
|
+ List<Goods> rows = page.getRows();
|
|
|
+ if (!rows.isEmpty()) {
|
|
|
+ Map<String, String> brandIdNameMap = this.queryGoodsBrandList().stream().collect(Collectors.toMap(next -> next.getId().toString(), BrandDto::getName));
|
|
|
+ for (Goods row : rows) {
|
|
|
+ row.setBrandName(brandIdNameMap.getOrDefault(row.getBrand(), row.getBrand()));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return page;
|
|
|
+ }
|
|
|
+
|
|
|
+/* @Override
|
|
|
@Transactional(rollbackFor = Exception.class, isolation = Isolation.READ_COMMITTED)
|
|
|
public List<Goods> importGoods(MultipartFile file, Integer operatorId) throws Exception {
|
|
|
Map<String, List<Map<String, Object>>> sheetsListMap = POIUtil.importExcel(new ByteArrayInputStream(file.getBytes()), 2, file.getOriginalFilename());
|
|
@@ -458,6 +577,373 @@ public class GoodsServiceImpl extends BaseServiceImpl<Integer, Goods> implement
|
|
|
sellOrderBatchNoAllot(TenantContextHolder.getTenantId());
|
|
|
|
|
|
return goodsList;
|
|
|
+ }*/
|
|
|
+
|
|
|
+
|
|
|
+ @Transactional(rollbackFor = Exception.class)
|
|
|
+ @Override
|
|
|
+ public String importGoodsV2(MultipartFile file, Integer userId) throws Exception {
|
|
|
+ File excel = createImportGoodsTempFile(UUID.randomUUID().toString().replaceAll("-","") + ".xlsx");
|
|
|
+ Workbook workbook = WorkbookFactory.create(file.getInputStream());
|
|
|
+ workbook.write(Files.newOutputStream(excel.toPath()));
|
|
|
+
|
|
|
+ List<String> errList = new ArrayList<>();
|
|
|
+// Map<String, List<Map<String, Object>>> sheetsListMap = POIUtil.importExcel(new ByteArrayInputStream(file.getBytes()), 2, file.getOriginalFilename());
|
|
|
+ Map<String, List<Map<String, Object>>> sheetsListMap = POIUtil.importExcel(excel, 2);
|
|
|
+ excel.deleteOnExit();
|
|
|
+ if (sheetsListMap.values().isEmpty()) {
|
|
|
+ errList.add("excel未解析到数据");
|
|
|
+ return getExportErrLogFile(errList);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 查询商品分类
|
|
|
+ GoodsCategoryQueryInfo goodsCategoryQueryInfo = new GoodsCategoryQueryInfo();
|
|
|
+ goodsCategoryQueryInfo.setDelFlag(YesOrNoEnum.NO);
|
|
|
+ goodsCategoryQueryInfo.setPage(1);
|
|
|
+ goodsCategoryQueryInfo.setRows(9999);
|
|
|
+
|
|
|
+
|
|
|
+ List<GoodsCategory> categoryList = goodsCategoryService.findAll(new HashMap<>()).stream().filter(next -> YesOrNoEnum.NO.equals(next.getDelFlag())).collect(Collectors.toList());
|
|
|
+ Map<String, Integer> categoryIdNameMap = categoryList
|
|
|
+ .stream().collect(Collectors.toMap(GoodsCategory::getName, GoodsCategory::getId));
|
|
|
+ Set<String> categoryNames = categoryIdNameMap.keySet();
|
|
|
+
|
|
|
+ // 查询员工机构
|
|
|
+ Map<String, Integer> orgNameIdMap = organizationDao.findAllOrgans(TenantContextHolder.getTenantId()).stream()
|
|
|
+ .collect(Collectors.toMap(Organization::getName, Organization::getId));
|
|
|
+
|
|
|
+
|
|
|
+ InputStream inputStream = new ClassPathResource("columnMapper.ini").getInputStream();
|
|
|
+ Map<String, String> columns = IniFileUtil.readIniFile(inputStream, TemplateTypeEnum.GOODS_GROUP.getMsg());
|
|
|
+
|
|
|
+ List<Map<String, Object>> firstSheet = sheetsListMap.entrySet().iterator().next().getValue();
|
|
|
+ List<String> skuList = firstSheet.stream().map(next -> next.get("SKU").toString()).distinct().collect(Collectors.toList());
|
|
|
+
|
|
|
+ List<PmsProductDto> subGoods;
|
|
|
+ try {
|
|
|
+ // 查询SKU对应商品
|
|
|
+ PmsProductQueryParamDto dto = PmsProductQueryParamDto.builder()
|
|
|
+ .skuStockCodes(String.join(",", skuList))
|
|
|
+ .pageNum(1)
|
|
|
+ .pageSize(9999)
|
|
|
+ .build();
|
|
|
+ dto.setJson(JSON.toJSONString(dto));
|
|
|
+ subGoods = new ArrayList<>(mallFeignService.getProductList(dto).getRows());
|
|
|
+ } catch (Exception e) {
|
|
|
+ errList.add("商城服务调用失败");
|
|
|
+ return getExportErrLogFile(errList);
|
|
|
+ }
|
|
|
+ Map<String, PmsProductDto> skuMap = new HashMap<>();
|
|
|
+ subGoods.forEach(next -> {
|
|
|
+ String key = next.getName() + "_" + next.getSkuCode();
|
|
|
+ if (!skuMap.containsKey(key)) { // 处理出现重复key问题
|
|
|
+ skuMap.put(key, next);
|
|
|
+ }
|
|
|
+ });
|
|
|
+
|
|
|
+ // excel中所有的sn
|
|
|
+// List<String> snSet = firstSheet.stream().map(next -> next.get("组合商品货号").toString()).filter(StringUtils::isNotEmpty).collect(Collectors.toList());
|
|
|
+ // 获取重复的货号
|
|
|
+// List<String> repeatSnList = snSet.stream().collect(Collectors.groupingBy(e -> e, Collectors.counting()))
|
|
|
+// .entrySet().stream().filter(e -> e.getValue() > 1)
|
|
|
+// .map(Map.Entry::getKey).collect(Collectors.toList());
|
|
|
+// List<String> existSnList = goodsDao.findBySns(snSet.stream().distinct().collect(Collectors.toList())).stream().map(Goods::getSn).distinct().collect(Collectors.toList());
|
|
|
+
|
|
|
+ Map<String, Long> brandNameIdMap = mallFeignService.getList().stream().collect(Collectors.toMap(BrandDto::getName, BrandDto::getId));
|
|
|
+
|
|
|
+ List<JSONObject> jsonObjectList = new ArrayList<>();
|
|
|
+ Map<String, byte[]> snImageMap = new HashMap<>();
|
|
|
+ for (Map.Entry<String, List<Map<String, Object>>> sheetData : sheetsListMap.entrySet()) {
|
|
|
+ List<Map<String, Object>> rows = sheetData.getValue();
|
|
|
+ if (rows.isEmpty()) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ Map<String, Object> firstRow = rows.get(0);
|
|
|
+ // 缺省字段
|
|
|
+ Set<String> templateFields = columns.keySet();
|
|
|
+ List<String> defaultField = templateFields.stream().filter(next->!firstRow.containsKey(next)).collect(Collectors.toList());
|
|
|
+ if (!defaultField.isEmpty()) {
|
|
|
+ errList.add("导入字段缺省:" + String.join(",", defaultField));
|
|
|
+ return getExportErrLogFile(errList);
|
|
|
+ }
|
|
|
+
|
|
|
+ String lineErrMsg = "错误行号:第%s行,%s";
|
|
|
+ Map<String, Object> beforeGoods = new HashMap<>();
|
|
|
+ Map<String, Set<String>> snSubGoodsMap = new HashMap<>();
|
|
|
+ for (int i = 0; i < rows.size(); i++) {
|
|
|
+ JSONObject objectMap = new JSONObject();
|
|
|
+ int rowNum = i + 2;
|
|
|
+ Map<String, Object> row = rows.get(i);
|
|
|
+ if (row.size() == 0) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ // 新商品标志
|
|
|
+ String sn = row.get("组合商品货号").toString();
|
|
|
+ if (StringUtils.isNotEmpty(sn)) {
|
|
|
+ beforeGoods.clear();
|
|
|
+ snSubGoodsMap.clear();
|
|
|
+ }
|
|
|
+ String subGoodsName = null;
|
|
|
+ String subGoodsSku = null;
|
|
|
+ List<String> lineErrList = new ArrayList<>();
|
|
|
+ for (Map.Entry<String, Object> entry : row.entrySet()) {
|
|
|
+ String fieldName = entry.getKey();
|
|
|
+ if (!templateFields.contains(fieldName)) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ String fieldCode = columns.get(fieldName);
|
|
|
+ Object value = entry.getValue();
|
|
|
+
|
|
|
+ // 非子商品字段为空,往上一行获取值
|
|
|
+ if (StringUtils.isEmpty(value.toString())) {
|
|
|
+ if ("subGoodsName".equals(fieldCode) || "sku".equals(fieldCode) || "subGoodsPrice".equals(fieldCode)) {
|
|
|
+ lineErrList.add("字段‘" + fieldName + "’为空");
|
|
|
+ } else {
|
|
|
+ if (beforeGoods.containsKey(fieldCode)) {
|
|
|
+ value = beforeGoods.get(fieldCode);
|
|
|
+ } else {
|
|
|
+ if (!("educationShowOrganName".equals(fieldCode)
|
|
|
+ || "courseFeeShowOrganName".equals(fieldCode)
|
|
|
+ || "memberFeeShowOrganName".equals(fieldCode)
|
|
|
+ || "freeFeeShowOrganName".equals(fieldCode)
|
|
|
+ || "replacementShowOrganName".equals(fieldCode))) {
|
|
|
+ lineErrList.add("字段‘" + fieldName + "’为空");
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ if ("image".equals(fieldCode)) { // 图片数组 取第一个
|
|
|
+ if (value instanceof List) {
|
|
|
+ Object data = ((List<?>) value).get(0);
|
|
|
+ if (data instanceof PictureData) {
|
|
|
+ value = ((PictureData) data).getData();
|
|
|
+ }
|
|
|
+ } else if (!(value instanceof byte[])) {
|
|
|
+ lineErrList.add("图片格式错误");
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ beforeGoods.put(fieldCode, value);
|
|
|
+ }
|
|
|
+// if ("sn".equals(fieldCode)) {
|
|
|
+// if (existSnList.contains(value.toString())) {
|
|
|
+// lineErrList.add("货号[" + value + "]已存在");
|
|
|
+// } else {
|
|
|
+// if (repeatSnList.contains(value.toString())) {
|
|
|
+// lineErrList.add("货号[" + value + "]重复");
|
|
|
+// }
|
|
|
+// }
|
|
|
+// objectMap.put(fieldCode, value);
|
|
|
+// } else
|
|
|
+ if ("marketPrice".equals(fieldCode)
|
|
|
+ || "discountPrice".equals(fieldCode)
|
|
|
+ || "groupPurchasePrice".equals(fieldCode)) {
|
|
|
+ if (NumberUtils.isNumber(value.toString())) {
|
|
|
+ BigDecimal prize = new BigDecimal(value.toString());
|
|
|
+ if (BigDecimal.ZERO.compareTo(prize) > 0) {
|
|
|
+ lineErrList.add("字段‘" + fieldName + "’不能为负数");
|
|
|
+ } else {
|
|
|
+ objectMap.put(fieldCode, value);
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ lineErrList.add("字段‘" + fieldName + "’类型错误");
|
|
|
+
|
|
|
+ }
|
|
|
+ } else if ("type".equals(fieldCode)) {
|
|
|
+ String tempValue = value.toString();
|
|
|
+ Optional<GoodsType> first = Arrays.stream(GoodsType.values()).filter(next -> next.getDesc().equals(tempValue)).findFirst();
|
|
|
+ if (first.isPresent()) {
|
|
|
+ objectMap.put(fieldCode, first.get().getCode());
|
|
|
+ } else {
|
|
|
+ lineErrList.add("字段‘" + fieldName + "’类型不支持");
|
|
|
+ }
|
|
|
+ } else if ("goodsCategoryName".equals(fieldCode)) {
|
|
|
+ if(categoryNames.contains(value.toString())){
|
|
|
+ objectMap.put("goodsCategoryId", categoryIdNameMap.get(value.toString()));
|
|
|
+ }else {
|
|
|
+ lineErrList.add("字段‘" + fieldName + "’不支持");
|
|
|
+ }
|
|
|
+ } else if ("educationShowOrganName".equals(fieldCode)
|
|
|
+ || "courseFeeShowOrganName".equals(fieldCode)
|
|
|
+ || "memberFeeShowOrganName".equals(fieldCode)
|
|
|
+ || "freeFeeShowOrganName".equals(fieldCode)
|
|
|
+ || "replacementShowOrganName".equals(fieldCode)
|
|
|
+ ) {
|
|
|
+ if (StringUtils.isNotEmpty(value.toString())) {
|
|
|
+ List<String> orgIds = new ArrayList<>();
|
|
|
+ for (String orgName : value.toString().split("[,,]")) {
|
|
|
+ if (!orgNameIdMap.containsKey(orgName)) {
|
|
|
+ lineErrList.add("字段‘" + fieldName + "’:‘" + value + "’分部不支持");
|
|
|
+ } else {
|
|
|
+ orgIds.add(orgNameIdMap.get(orgName).toString());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ String join = String.join(",", orgIds);
|
|
|
+ String fieldKey = fieldCode.replaceAll("Name", "Id");
|
|
|
+ objectMap.put(fieldKey, join);
|
|
|
+ }
|
|
|
+ } else if ("subGoodsName".equals(fieldCode)) {
|
|
|
+ subGoodsName = value.toString();
|
|
|
+ if (StringUtils.isNotEmpty(subGoodsSku)) {
|
|
|
+ String key = subGoodsName + "_" + subGoodsSku;
|
|
|
+ if (skuMap.containsKey(key)) {
|
|
|
+ PmsProductDto dto = skuMap.get(key);
|
|
|
+ objectMap.put("mallGoodsId", dto.getGoodsId().intValue());
|
|
|
+ objectMap.put("sku", dto.getSkuStockId());
|
|
|
+ objectMap.put("stock", dto.getStock());
|
|
|
+ objectMap.put("goodsStatus", dto.getPublishStatus()==1);
|
|
|
+ } else {
|
|
|
+ lineErrList.add("子商品‘" + subGoodsName + "[" + subGoodsSku + "]’不存在");
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } else if ("sku".equals(fieldCode)) {
|
|
|
+ subGoodsSku = value.toString();
|
|
|
+ if (StringUtils.isNotEmpty(subGoodsName)) {
|
|
|
+ String key = subGoodsName + "_" + subGoodsSku;
|
|
|
+ if (skuMap.containsKey(key)) {
|
|
|
+ PmsProductDto dto = skuMap.get(key);
|
|
|
+ objectMap.put("mallGoodsId", dto.getGoodsId().intValue());
|
|
|
+ objectMap.put("sku", dto.getSkuStockId());
|
|
|
+ objectMap.put("stock", dto.getStock());
|
|
|
+ objectMap.put("goodsStatus", dto.getPublishStatus()==1);
|
|
|
+ } else {
|
|
|
+ lineErrList.add("子商品‘" + subGoodsName + "[" + subGoodsSku + "]’不存在");
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } else if ("subGoodsPrice".equals(fieldCode)) {
|
|
|
+ BigDecimal subGoodsPrice = new BigDecimal(value.toString());
|
|
|
+ if (BigDecimal.ZERO.compareTo(subGoodsPrice) > 0) {
|
|
|
+ lineErrList.add("字段‘" + fieldName + "’不能为负数");
|
|
|
+ }
|
|
|
+ } else if ("brand".equals(fieldCode)) {
|
|
|
+ if (brandNameIdMap.containsKey(value.toString())) {
|
|
|
+ objectMap.put("brand", brandNameIdMap.get(value.toString()));
|
|
|
+ } else {
|
|
|
+ lineErrList.add("组合商品品牌‘" + value + "’不支持");
|
|
|
+ }
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ objectMap.put(fieldCode, value);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ // 校验分部可售是否都为空
|
|
|
+ if (!objectMap.containsKey("educationShowOrganId") &&
|
|
|
+ !objectMap.containsKey("courseFeeShowOrganId") &&
|
|
|
+ !objectMap.containsKey("memberFeeShowOrganId") &&
|
|
|
+ !objectMap.containsKey("freeFeeShowOrganId") &&
|
|
|
+ !objectMap.containsKey("replacementShowOrganId")) {
|
|
|
+ lineErrList.add("可售分部至少需要填写一个");
|
|
|
+ }
|
|
|
+ // 校验子商品sku是否重复
|
|
|
+ String tempSn = objectMap.getOrDefault("sn", "").toString();
|
|
|
+ String tempSku = objectMap.getOrDefault("sku", "").toString();
|
|
|
+ if (StringUtils.isNotEmpty(tempSn)) {
|
|
|
+ byte[] images = objectMap.getBytes("image");
|
|
|
+ snImageMap.put(tempSn, images);
|
|
|
+ if (StringUtils.isNotEmpty(tempSku)) {
|
|
|
+ Set<String> set = snSubGoodsMap.getOrDefault(tempSn, new HashSet<>());
|
|
|
+ if (set.contains(tempSku)) {
|
|
|
+ lineErrList.add("子商品SKU重复");
|
|
|
+ } else {
|
|
|
+ set.add(tempSku);
|
|
|
+ snSubGoodsMap.put(tempSn, set);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ if (!lineErrList.isEmpty()) {
|
|
|
+ errList.add(String.format(lineErrMsg, rowNum, String.join(",", lineErrList)));
|
|
|
+ }
|
|
|
+ jsonObjectList.add(objectMap);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ if (jsonObjectList.isEmpty()) {
|
|
|
+ errList.add("未解析到数据");
|
|
|
+ }
|
|
|
+
|
|
|
+ if (!errList.isEmpty()) {
|
|
|
+ // 存在错误信息,添加错误日志文件返回
|
|
|
+ return getExportErrLogFile(errList);
|
|
|
+ }
|
|
|
+
|
|
|
+ List<GoodsWrapper.Goods> goodsList = jsonObjectList.stream().map(next -> {
|
|
|
+ next.remove("image");
|
|
|
+ String jsonString = next.toJSONString();
|
|
|
+ GoodsWrapper.Goods goods = JSON.parseObject(jsonString, GoodsWrapper.Goods.class);
|
|
|
+ goods.setSellCount(0);
|
|
|
+ GoodsWrapper.GoodsSub goodsSub = JSON.parseObject(jsonString, GoodsWrapper.GoodsSub.class);
|
|
|
+ ArrayList<GoodsWrapper.GoodsSub> arrayList = new ArrayList<>();
|
|
|
+ arrayList.add(goodsSub);
|
|
|
+ goods.setGoodsSubList(arrayList);
|
|
|
+ return goods;
|
|
|
+ }).collect(Collectors.toList());
|
|
|
+
|
|
|
+ Map<String, List<GoodsWrapper.Goods>> goodsGroup = goodsList.stream().collect(Collectors.groupingBy(next -> next.getName() + "_" + next.getSn()));
|
|
|
+//
|
|
|
+//
|
|
|
+// // 数据合并
|
|
|
+ Map<Integer, Integer> skuIdMap = subGoods.stream().collect(Collectors.toMap(PmsProductDto::getSkuStockId, PmsProductDto::getStock));
|
|
|
+
|
|
|
+ for (Map.Entry<String, List<GoodsWrapper.Goods>> entry : goodsGroup.entrySet()) {
|
|
|
+ List<GoodsWrapper.Goods> value = entry.getValue();
|
|
|
+ GoodsWrapper.Goods good = value.get(0);
|
|
|
+ String sn = good.getSn();
|
|
|
+
|
|
|
+ // 图片上传
|
|
|
+ byte[] images = snImageMap.get(sn);
|
|
|
+ if (images != null && images.length > 0) {
|
|
|
+ File imgFile = createImportGoodsTempFile(UUID.randomUUID().toString().replaceAll("-","") + ".png");
|
|
|
+ BufferedImage read = ImageIO.read(new ByteArrayInputStream((images)));
|
|
|
+ ImageIO.write(read, "png", imgFile);
|
|
|
+ String url = ossPluginContext.getPluginService(TencentOssPlugin.PLUGIN_NAME).uploadFile("daya/" + UploadUtil.getFileFloder(), imgFile);
|
|
|
+ good.setImage(url);
|
|
|
+ imgFile.deleteOnExit();
|
|
|
+ }
|
|
|
+ List<GoodsWrapper.GoodsSub> goodsSubList = value.stream().map(GoodsWrapper.Goods::getGoodsSubList).flatMap(Collection::stream).collect(Collectors.toList());
|
|
|
+ Integer stock = goodsSubList.stream().map(next -> skuIdMap.get(next.getSku())).min(Integer::compareTo).orElse(0);
|
|
|
+
|
|
|
+ Goods goods = JSON.parseObject(JSON.toJSONString(good), Goods.class);
|
|
|
+ goods.setStatus(YesOrNoEnum.NO);
|
|
|
+ goods.setStockCount(stock);
|
|
|
+ goods.setGroupGoods(true);
|
|
|
+ this.insert(goods);
|
|
|
+
|
|
|
+ List<GoodsSub> subList = goodsSubList.stream().map(next -> {
|
|
|
+ GoodsSub sub = new GoodsSub();
|
|
|
+ sub.setGoodsId(goods.getId());
|
|
|
+ sub.setMallGoodsId(next.getMallGoodsId());
|
|
|
+ sub.setSku(next.getSku());
|
|
|
+ sub.setGoodsStatus(next.getGoodsStatus());
|
|
|
+ sub.setGoodsPrice(next.getGoodsPrice());
|
|
|
+ return sub;
|
|
|
+ }).collect(Collectors.toList());
|
|
|
+ goodsSubService.saveBatch(subList);
|
|
|
+ }
|
|
|
+
|
|
|
+// goodsGroup.forEach((key, value) -> {
|
|
|
+// GoodsWrapper.Goods good = value.get(0);
|
|
|
+// List<GoodsWrapper.GoodsSub> goodsSubList = value.stream().map(GoodsWrapper.Goods::getGoodsSubList).flatMap(Collection::stream).collect(Collectors.toList());
|
|
|
+// Integer stock = goodsSubList.stream().map(next -> skuIdMap.get(next.getSku())).min(Integer::compareTo).orElse(0);
|
|
|
+//
|
|
|
+// Goods goods = JSON.parseObject(JSON.toJSONString(good), Goods.class);
|
|
|
+// goods.setStatus(YesOrNoEnum.NO);
|
|
|
+// goods.setStockCount(stock);
|
|
|
+// this.insert(goods);
|
|
|
+//
|
|
|
+// List<GoodsSub> subList = goodsSubList.stream().map(next -> {
|
|
|
+// GoodsSub sub = new GoodsSub();
|
|
|
+// sub.setGoodsId(goods.getId());
|
|
|
+// sub.setMallGoodsId(next.getMallGoodsId());
|
|
|
+// sub.setSku(next.getSku());
|
|
|
+// sub.setGoodsStatus(next.getGoodsStatus());
|
|
|
+// sub.setGoodsPrice(next.getGoodsPrice());
|
|
|
+// return sub;
|
|
|
+// }).collect(Collectors.toList());
|
|
|
+// goodsSubMapper.saveBatch(subList);
|
|
|
+// });
|
|
|
+ return null;
|
|
|
}
|
|
|
|
|
|
@Override
|
|
@@ -494,7 +980,7 @@ public class GoodsServiceImpl extends BaseServiceImpl<Integer, Goods> implement
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- @Override
|
|
|
+/* @Override
|
|
|
@Transactional(rollbackFor = Exception.class, isolation = Isolation.READ_COMMITTED)
|
|
|
public void sellOrderBatchNoAllot(Integer tenantId) {
|
|
|
List<Integer> noneBatchNoSellOrderIds = sellOrderDao.getNoneBatchNoSellOrderIds(tenantId);
|
|
@@ -536,7 +1022,7 @@ public class GoodsServiceImpl extends BaseServiceImpl<Integer, Goods> implement
|
|
|
if(!CollectionUtils.isEmpty(updateSellOrders)){
|
|
|
sellOrderDao.batchUpdate(updateSellOrders);
|
|
|
}
|
|
|
- }
|
|
|
+ }*/
|
|
|
|
|
|
@Override
|
|
|
@Transactional(rollbackFor = Exception.class, isolation = Isolation.READ_COMMITTED)
|
|
@@ -546,7 +1032,37 @@ public class GoodsServiceImpl extends BaseServiceImpl<Integer, Goods> implement
|
|
|
}
|
|
|
|
|
|
List<Goods> tempGoodsList = goodsDao.lockGoods(new ArrayList<>(goodsIds));
|
|
|
- Map<Integer, Goods> idTempGoodsMap = tempGoodsList.stream().collect(Collectors.toMap(Goods::getId, g -> g));
|
|
|
+ PmsProductQueryParamDto paramDto = new PmsProductQueryParamDto();
|
|
|
+ String skuIds = goodsSubService.lambdaQuery().in(GoodsSub::getGoodsId, tempGoodsList.stream().map(Goods::getId).collect(Collectors.toList())).list()
|
|
|
+ .stream().map(e->e.getSku().toString()).collect(Collectors.joining(","));
|
|
|
+ paramDto.setSkuStockIds(skuIds);
|
|
|
+ PageInfo<PmsProductDto> productList = mallFeignService.getProductList(paramDto);
|
|
|
+ if (CollectionUtils.isEmpty(productList.getRows())) {
|
|
|
+ throw new BizException("商品不存在");
|
|
|
+ }
|
|
|
+ Map<Integer,PmsProductDto> skuIdProductMap = productList.getRows().stream().collect(Collectors.toMap(PmsProductDto::getSkuStockId, Function.identity()));
|
|
|
+ List<SellOrder> sellOrders = new ArrayList<>();
|
|
|
+ for (Goods goods : tempGoodsList) {
|
|
|
+ List<GoodsSub> goodsSubs = goodsSubService.lambdaQuery().eq(GoodsSub::getGoodsId, goods.getId()).list();
|
|
|
+ if(CollectionUtils.isEmpty(goodsSubs)){
|
|
|
+ throw new BizException("商品["+goods.getName()+"]未设置子商品");
|
|
|
+ }
|
|
|
+ for (GoodsSub goodsSub : goodsSubs) {
|
|
|
+ PmsProductDto productDto = skuIdProductMap.get(goodsSub.getSku());
|
|
|
+ if(Objects.isNull(productDto)){
|
|
|
+ throw new BizException("商品["+goods.getName()+"]子商品["+goodsSub.getSku()+"]不存在");
|
|
|
+ }
|
|
|
+ SellOrder sellOrder = new SellOrder();
|
|
|
+ sellOrder.setParentGoodsId(goods.getId());
|
|
|
+ sellOrder.setGoodsSkuId(goodsSub.getSku());
|
|
|
+ sellOrder.setNum(1);
|
|
|
+ sellOrder.setGoodsName(productDto.getName());
|
|
|
+ sellOrder.setAccountType(accountType);
|
|
|
+ sellOrder.setOrganSellCost(goodsSub.getGoodsPrice());
|
|
|
+ sellOrders.add(sellOrder);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ /*Map<Integer, Goods> idTempGoodsMap = tempGoodsList.stream().collect(Collectors.toMap(Goods::getId, g -> g));
|
|
|
List<GoodsProcurement> goodsProcurements = new ArrayList<>();
|
|
|
for (Integer goodsId : goodsIds) {
|
|
|
Goods tempGoods = idTempGoodsMap.get(goodsId);
|
|
@@ -661,12 +1177,12 @@ public class GoodsServiceImpl extends BaseServiceImpl<Integer, Goods> implement
|
|
|
}
|
|
|
sellOrders.add(sellOrder);
|
|
|
}
|
|
|
- }
|
|
|
+ }*/
|
|
|
|
|
|
return sellOrders;
|
|
|
}
|
|
|
|
|
|
- @Override
|
|
|
+/* @Override
|
|
|
@Transactional(rollbackFor = Exception.class, isolation = Isolation.READ_COMMITTED)
|
|
|
public void increaseStock(List<SellOrder> sellOrders, AccountType accountType) {
|
|
|
if(CollectionUtils.isEmpty(sellOrders)){
|
|
@@ -700,10 +1216,10 @@ public class GoodsServiceImpl extends BaseServiceImpl<Integer, Goods> implement
|
|
|
}
|
|
|
goods.setSellCount(new AtomicInteger(goods.getSellCount()).addAndGet(-sellOrder.getNum()));
|
|
|
|
|
|
-// goodsDao.update(goods);
|
|
|
-// goodsProcurementDao.update(goodsProcurement);
|
|
|
+ goodsDao.update(goods);
|
|
|
+ goodsProcurementDao.update(goodsProcurement);
|
|
|
}
|
|
|
- }
|
|
|
+ }*/
|
|
|
|
|
|
@Override
|
|
|
public List<GoodsSellDto> queryGoodsSellDtos(String goodsId) {
|
|
@@ -711,7 +1227,7 @@ public class GoodsServiceImpl extends BaseServiceImpl<Integer, Goods> implement
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
- public Goods getDetail(Integer goodsId) {
|
|
|
+ public GoodsWrapper.Goods getDetail(Integer goodsId) {
|
|
|
Goods goods = goodsDao.get(goodsId);
|
|
|
if(StringUtils.isNotEmpty(goods.getStudentShowOrganId())){
|
|
|
goods.setStudentShowOrganName(StringUtils.join(organizationDao.findByOrganIds(goods.getStudentShowOrganId()),","));
|
|
@@ -731,7 +1247,14 @@ public class GoodsServiceImpl extends BaseServiceImpl<Integer, Goods> implement
|
|
|
if(StringUtils.isNotEmpty(goods.getFreeFeeShowOrganId())){
|
|
|
goods.setFreeFeeShowOrganName(StringUtils.join(organizationDao.findByOrganIds(goods.getFreeFeeShowOrganId()),","));
|
|
|
}
|
|
|
- return goods;
|
|
|
+ GoodsWrapper.Goods detail = JSON.parseObject(JSON.toJSONString(goods), GoodsWrapper.Goods.class);
|
|
|
+ detail.setGoodsSubList(queryGoodsSubByGoodId(goodsId));
|
|
|
+ String brand = detail.getBrand();
|
|
|
+ if (StringUtils.isNotEmpty(brand) && NumberUtils.isNumber(brand)) {
|
|
|
+ Map<Long, String> brandIdNameMap = queryGoodsBrandList().stream().collect(Collectors.toMap(BrandDto::getId, BrandDto::getName));
|
|
|
+ detail.setBrandName(brandIdNameMap.getOrDefault(Long.valueOf(brand), ""));
|
|
|
+ }
|
|
|
+ return detail;
|
|
|
}
|
|
|
|
|
|
@Override
|
|
@@ -744,4 +1267,248 @@ public class GoodsServiceImpl extends BaseServiceImpl<Integer, Goods> implement
|
|
|
public int batchUpdate(List<Goods> goodsList) {
|
|
|
return goodsDao.batchUpdate(goodsList);
|
|
|
}
|
|
|
+
|
|
|
+ @Transactional(rollbackFor = Exception.class)
|
|
|
+ @Override
|
|
|
+ public void updateGoods(GoodsWrapper.Goods goods) {
|
|
|
+ Goods goodsInfo = goodsDao.getGoodsInfo(goods.getId());
|
|
|
+ if (goodsInfo == null) {
|
|
|
+ throw new BizException("参数错误");
|
|
|
+ }
|
|
|
+ List<GoodsWrapper.GoodsSub> goodsSubList = goods.getGoodsSubList();
|
|
|
+ if (StringUtils.isEmpty(goodsInfo.getComplementGoodsIdList()) && goodsSubList.isEmpty()) {
|
|
|
+ throw new BizException("子商品信息不能为空");
|
|
|
+ }
|
|
|
+ goodsInfo = JSON.parseObject(JSON.toJSONString(goods), Goods.class);
|
|
|
+ goodsInfo.setStatus(null);
|
|
|
+ if (!goodsSubList.isEmpty()) {
|
|
|
+ BigDecimal organCostPrice = goodsSubList.stream().map(GoodsWrapper.GoodsSub::getGoodsPrice).reduce(BigDecimal.ZERO, BigDecimal::add);
|
|
|
+ goodsInfo.setOrganCostPrice(organCostPrice);
|
|
|
+
|
|
|
+ //获取最小的库存数
|
|
|
+ String skuIds = goodsSubList.stream().map(e -> e.getSku().toString()).collect(Collectors.joining(","));
|
|
|
+ PmsProductQueryParamDto paramDto = new PmsProductQueryParamDto();
|
|
|
+ paramDto.setSkuStockIds(skuIds);
|
|
|
+ paramDto.setPageSize(1000);
|
|
|
+ paramDto.setPageNum(1);
|
|
|
+ paramDto.setJson(JSON.toJSONString(paramDto));
|
|
|
+ List<PmsProductDto> productList = mallFeignService.getProductList(paramDto).getRows();
|
|
|
+ if(CollectionUtils.isEmpty(productList)){
|
|
|
+ throw new BizException("子商品不存在");
|
|
|
+ }
|
|
|
+ Map<Integer, Long> skuIdGoodIdMap = productList.stream().collect(Collectors.toMap(PmsProductDto::getSkuStockId, PmsProductDto::getGoodsId));
|
|
|
+
|
|
|
+ List<GoodsSub> goodsSubs = JSON.parseArray(JSON.toJSONString(goodsSubList), GoodsSub.class);
|
|
|
+ for (GoodsSub next : goodsSubs) {
|
|
|
+ next.setGoodsId(goods.getId());
|
|
|
+ if (skuIdGoodIdMap.containsKey(next.getSku())) {
|
|
|
+ next.setMallGoodsId(skuIdGoodIdMap.get(next.getSku()).intValue());
|
|
|
+ } else {
|
|
|
+ throw new BizException("存在删除的子商品");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ UpdateWrapper<GoodsSub> delWrapper = new UpdateWrapper<>();
|
|
|
+ delWrapper.eq("goods_id_", goods.getId());
|
|
|
+ goodsSubService.getBaseMapper().delete(delWrapper);
|
|
|
+ goodsSubService.saveBatch(goodsSubs);
|
|
|
+
|
|
|
+ goodsInfo.setComplementGoodsIdList(null);
|
|
|
+ //获取最小库存数量
|
|
|
+ Integer stock = productList.stream().map(PmsProductDto::getStock).min(Integer::compareTo).get();
|
|
|
+ goodsInfo.setStockCount(stock);
|
|
|
+ }
|
|
|
+ update(goodsInfo);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public PageInfo<PmsProductDto> queryGoodsSubByPage(GoodsWrapper.GoodsSubQuery query) {
|
|
|
+ PmsProductQueryParamDto dto = PmsProductQueryParamDto.builder()
|
|
|
+ .publishStatus(query.getPublishStatus())
|
|
|
+ .keyword(query.getKeyword())
|
|
|
+ .productSn(query.getProductSn())
|
|
|
+ .productIds(query.getProductIds())
|
|
|
+ .productCategoryId(query.getProductCategoryId())
|
|
|
+ .brandId(query.getBrandId())
|
|
|
+ .pageNum(query.getPage())
|
|
|
+ .pageSize(query.getRows())
|
|
|
+ .build();
|
|
|
+ dto.setJson(JSON.toJSONString(dto));
|
|
|
+ return mallFeignService.getProductList(dto);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Transactional
|
|
|
+ @Override
|
|
|
+ public Boolean goodsStatusSynchronize(List<GoodsSubModel> goodsSubModelList) {
|
|
|
+ if (CollectionUtils.isEmpty(goodsSubModelList)) {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ log.info(Thread.currentThread().getName() + "开始同步商品状态:" + JSON.toJSONString(goodsSubModelList));
|
|
|
+ List<Integer> skuIds = goodsSubModelList.stream().map(GoodsSubModel::getSkuId).distinct().collect(Collectors.toList());
|
|
|
+ QueryWrapper<GoodsSub> queryWrapper = new QueryWrapper<>();
|
|
|
+ queryWrapper.in("sku_", skuIds);
|
|
|
+ List<GoodsSub> goodsSubs = goodsSubService.getBaseMapper().selectList(queryWrapper);
|
|
|
+ if (CollectionUtils.isEmpty(goodsSubs)) {
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ Map<Integer, Boolean> keyStatusMap = goodsSubModelList.stream().collect(Collectors.toMap(GoodsSubModel::getSkuId, GoodsSubModel::getGoodsStatus));
|
|
|
+ log.info(Thread.currentThread().getName() + "开始同步商品状态:0/" + goodsSubs.size());
|
|
|
+ List<GoodsSub> goodsSubList = goodsSubs.stream().peek(next -> next.setGoodsStatus(keyStatusMap.get(next.getSku()))).collect(Collectors.toList());
|
|
|
+ // 下架的商品
|
|
|
+ List<GoodsSub> downGoods = goodsSubList.stream().filter(next -> Boolean.FALSE.equals(next.getGoodsStatus())).collect(Collectors.toList());
|
|
|
+ if(!CollectionUtils.isEmpty(downGoods)){
|
|
|
+ List<Integer> goodIdList = downGoods.stream().map(GoodsSub::getGoodsId).distinct().collect(Collectors.toList());
|
|
|
+ goodsDao.updateStatus(goodIdList, false);
|
|
|
+ }
|
|
|
+ Map<Boolean, List<Integer>> listMap = goodsSubList.stream().collect(Collectors.groupingBy(GoodsSub::getGoodsStatus, Collectors.mapping(GoodsSub::getId, Collectors.toList())));
|
|
|
+ listMap.forEach((key, values) -> goodsSubService.getBaseMapper().updateStatus(values, key));
|
|
|
+ log.info(Thread.currentThread().getName() + "同步商品状态完成");
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 同步组合商品库存
|
|
|
+ * @param goodsSubStockModels 商品库存信息
|
|
|
+ * @return true/false
|
|
|
+ */
|
|
|
+ @Transactional(rollbackFor = Exception.class)
|
|
|
+ @Override
|
|
|
+ public Boolean goodsStockCountSynchronize(List<GoodsSubStockModel> goodsSubStockModels) {
|
|
|
+ if(goodsSubStockModels.isEmpty()){
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ List<Integer> skus = goodsSubStockModels.stream().map(GoodsSubStockModel::getSku).distinct().collect(Collectors.toList());
|
|
|
+
|
|
|
+ QueryWrapper<GoodsSub> queryWrapper = new QueryWrapper<>();
|
|
|
+ queryWrapper.in("sku_", skus);
|
|
|
+ List<GoodsSub> goodsSubs = goodsSubService.getBaseMapper().selectList(queryWrapper);
|
|
|
+ if (CollectionUtils.isEmpty(goodsSubs)) {
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+// Map<Integer, Integer> stockMap = goodsSubStockModels.stream().collect(Collectors.toMap(GoodsSubStockModel::getSku, GoodsSubStockModel::getStock));
|
|
|
+ List<Integer> goodIds = goodsSubs.stream().map(GoodsSub::getGoodsId).distinct().collect(Collectors.toList());
|
|
|
+ goodsDao.lockGoods(goodIds);
|
|
|
+// List<Goods> goodsList = goodsDao.findGoodsByIds(goodIds.stream().map(String::valueOf).collect(Collectors.joining(",")));
|
|
|
+// Map<Integer, Integer> goodStockMap = goodsList.stream().collect(Collectors.toMap(Goods::getId, Goods::getStockCount));
|
|
|
+
|
|
|
+ queryWrapper = new QueryWrapper<>();
|
|
|
+ queryWrapper.in("goods_id_", goodIds);
|
|
|
+ List<GoodsSub> allGoodsSubs = goodsSubService.getBaseMapper().selectList(queryWrapper);
|
|
|
+ List<String> allSkuIdList = allGoodsSubs.stream().map(next -> next.getSku().toString()).distinct().collect(Collectors.toList());
|
|
|
+ PmsProductQueryParamDto dto = new PmsProductQueryParamDto();
|
|
|
+ dto.setSkuStockIds(String.join(",", allSkuIdList));
|
|
|
+ dto.setPageNum(1);
|
|
|
+ dto.setPageSize(9999);
|
|
|
+ dto.setJson(JSON.toJSONString(dto));
|
|
|
+ Map<Integer, Integer> skuStockMap = mallFeignService.getProductList(dto).getRows().stream().collect(Collectors.toMap(PmsProductDto::getSkuStockId, PmsProductDto::getStock));
|
|
|
+ goodsSubStockModels.forEach(next-> skuStockMap.put(next.getSku(),next.getStock()));
|
|
|
+
|
|
|
+ Map<Integer, List<GoodsSub>> collect = allGoodsSubs.stream().collect(Collectors.groupingBy(GoodsSub::getGoodsId));
|
|
|
+
|
|
|
+ List<Goods> collect1 = collect.entrySet().stream().map(next -> {
|
|
|
+ Goods goods = new Goods();
|
|
|
+ goods.setId(next.getKey());
|
|
|
+ Integer minStock = next.getValue().stream().map(o -> skuStockMap.getOrDefault(o.getSku(), 0)).min(Integer::compareTo).get();
|
|
|
+ goods.setStockCount(minStock);
|
|
|
+ return goods;
|
|
|
+ }).collect(Collectors.toList());
|
|
|
+
|
|
|
+
|
|
|
+// List<Goods> updateGoods = new ArrayList<>();
|
|
|
+// for (GoodsSub goodsSub : goodsSubs) {
|
|
|
+// Integer goodsId = goodsSub.getGoodsId();
|
|
|
+// Integer stock = stockMap.get(goodsSub.getSku());
|
|
|
+// int updateStock = Math.min(goodStockMap.get(goodsId), stock);
|
|
|
+//
|
|
|
+// Goods goods = new Goods();
|
|
|
+// goods.setId(goodsId);
|
|
|
+// goods.setStockCount(updateStock);
|
|
|
+// updateGoods.add(goods);
|
|
|
+// }
|
|
|
+ // 处理同一个商品,多个子商品同时更新库存,取最小的库存
|
|
|
+// Map<Integer, Optional<Goods>> collect = updateGoods.stream().collect(Collectors.groupingBy(Goods::getId, Collectors.minBy(Comparator.comparingInt(Goods::getStockCount))));
|
|
|
+// List<Goods> collect1 = collect2.entrySet().stream().map(next -> {
|
|
|
+// if (next.getValue().isPresent()) {
|
|
|
+// Goods goods = new Goods();
|
|
|
+// goods.setId(next.getKey());
|
|
|
+// goods.setStockCount(next.getValue().get().getStockCount());
|
|
|
+// return goods;
|
|
|
+// }
|
|
|
+// return null;
|
|
|
+// }).filter(Objects::nonNull).collect(Collectors.toList());
|
|
|
+ goodsDao.updateStock(collect1);
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public List<ProductAttributeCategoryDto> queryGoodsTypeList() {
|
|
|
+ return mallFeignService.getProductAttributeCategoryList();
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public List<BrandDto> queryGoodsBrandList() {
|
|
|
+ return mallFeignService.getList();
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public List<ProductCategoryDto> queryGoodsCategoryList() {
|
|
|
+ return mallFeignService.listWithChildren();
|
|
|
+ }
|
|
|
+
|
|
|
+ private File createImportGoodsTempFile(String fileName) {
|
|
|
+ String basePath = new ApplicationHome(this.getClass()).getSource().getParentFile().getPath();
|
|
|
+ File file = new File(basePath + File.separator + "daya" + File.separator + "importGoods" + File.separator + fileName);
|
|
|
+ try {
|
|
|
+ if (!file.getParentFile().exists()) {
|
|
|
+ file.getParentFile().mkdirs();
|
|
|
+ }
|
|
|
+ file.createNewFile();
|
|
|
+ return file;
|
|
|
+ } catch (IOException e) {
|
|
|
+ throw new BizException("导入失败,回执文件上传失败");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private String getExportErrLogFile(List<String> errList) {
|
|
|
+ if (CollectionUtils.isEmpty(errList)) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ String format = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());
|
|
|
+ File file = createImportGoodsTempFile("商品导入回执" + format + ".csv");
|
|
|
+ try {
|
|
|
+ FileOutputStream out = new FileOutputStream(file);
|
|
|
+ byte[] uft8bom = {(byte) 0xef, (byte) 0xbb, (byte) 0xbf}; // 处理乱码
|
|
|
+ out.write(uft8bom);
|
|
|
+ BufferedWriter csvFileOutputStream = new BufferedWriter(new OutputStreamWriter(out, StandardCharsets.UTF_8));
|
|
|
+
|
|
|
+ for (String errMsg : errList) {
|
|
|
+ csvFileOutputStream.write(errMsg);
|
|
|
+ csvFileOutputStream.newLine();
|
|
|
+ }
|
|
|
+ csvFileOutputStream.flush();
|
|
|
+ csvFileOutputStream.close();
|
|
|
+ String folder = "daya/download/" + UploadUtil.getFileFloder();
|
|
|
+ return ossPluginContext.getPluginService(TencentOssPlugin.PLUGIN_NAME).uploadFile(folder, file);
|
|
|
+ } catch (IOException e) {
|
|
|
+ log.error("上传错误回执文件失败:{}",e.getMessage());
|
|
|
+ } finally {
|
|
|
+ if (file.exists()) {
|
|
|
+ file.deleteOnExit();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return "";
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Map<String,String> getBrandMap(){
|
|
|
+
|
|
|
+ try {
|
|
|
+ return mallFeignService.getList().stream().collect(Collectors.toMap(o ->o.getId().toString(), BrandDto::getName));
|
|
|
+
|
|
|
+ }catch (Exception e){
|
|
|
+ log.error("获取品牌信息失败",e);
|
|
|
+ }
|
|
|
+ return new HashMap<>();
|
|
|
+ }
|
|
|
}
|