|  | @@ -2,23 +2,29 @@ package com.ym.mec.biz.service.impl;
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |  import com.alibaba.fastjson.JSON;
 | 
	
		
			
				|  |  |  import com.alibaba.fastjson.JSONObject;
 | 
	
		
			
				|  |  | +import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
 | 
	
		
			
				|  |  | +import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
 | 
	
		
			
				|  |  |  import com.ym.mec.biz.dal.dao.*;
 | 
	
		
			
				|  |  |  import com.ym.mec.biz.dal.dto.BasicUserDto;
 | 
	
		
			
				|  |  |  import com.ym.mec.biz.dal.dto.ComplementGoodsDto;
 | 
	
		
			
				|  |  |  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.GoodsSub;
 | 
	
		
			
				|  |  |  import com.ym.mec.biz.dal.entity.Organization;
 | 
	
		
			
				|  |  |  import com.ym.mec.biz.dal.entity.SellOrder;
 | 
	
		
			
				|  |  |  import com.ym.mec.biz.dal.enums.*;
 | 
	
		
			
				|  |  | +import com.ym.mec.biz.dal.mapper.GoodsSubMapper;
 | 
	
		
			
				|  |  |  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.dto.PmsProductDto;
 | 
	
		
			
				|  |  |  import com.ym.mec.common.dto.PmsProductQueryParamDto;
 | 
	
		
			
				|  |  |  import com.ym.mec.common.entity.UploadReturnBean;
 | 
	
		
			
				|  |  |  import com.ym.mec.common.exception.BizException;
 | 
	
		
			
				|  |  | +import com.ym.mec.common.page.PageInfo;
 | 
	
		
			
				|  |  |  import com.ym.mec.common.service.IdGeneratorService;
 | 
	
		
			
				|  |  |  import com.ym.mec.common.service.impl.BaseServiceImpl;
 | 
	
		
			
				|  |  |  import com.ym.mec.common.tenant.TenantContextHolder;
 | 
	
	
		
			
				|  | @@ -32,6 +38,7 @@ import org.apache.commons.lang3.StringUtils;
 | 
	
		
			
				|  |  |  import org.apache.poi.ss.usermodel.PictureData;
 | 
	
		
			
				|  |  |  import org.slf4j.Logger;
 | 
	
		
			
				|  |  |  import org.slf4j.LoggerFactory;
 | 
	
		
			
				|  |  | +import org.springframework.beans.BeanUtils;
 | 
	
		
			
				|  |  |  import org.springframework.beans.factory.annotation.Autowired;
 | 
	
		
			
				|  |  |  import org.springframework.core.io.ClassPathResource;
 | 
	
		
			
				|  |  |  import org.springframework.stereotype.Service;
 | 
	
	
		
			
				|  | @@ -73,6 +80,8 @@ public class GoodsServiceImpl extends BaseServiceImpl<Integer, Goods>  implement
 | 
	
		
			
				|  |  |  	@Resource
 | 
	
		
			
				|  |  |  	private MallFeignService mallFeignService;
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | +	@Autowired
 | 
	
		
			
				|  |  | +	private GoodsSubMapper goodsSubMapper;
 | 
	
		
			
				|  |  |  	@Override
 | 
	
		
			
				|  |  |  	public BaseDAO<Integer, Goods> getDAO() {
 | 
	
		
			
				|  |  |  		return goodsDao;
 | 
	
	
		
			
				|  | @@ -162,20 +171,17 @@ public class GoodsServiceImpl extends BaseServiceImpl<Integer, Goods>  implement
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |  	@Override
 | 
	
		
			
				|  |  |  	@Transactional(rollbackFor = Exception.class)
 | 
	
		
			
				|  |  | -	public void addGoods(Goods goods, Integer operatorId) {
 | 
	
		
			
				|  |  | +	public void addGoods(GoodsWrapper.Goods goods, Integer operatorId) {
 | 
	
		
			
				|  |  |  		if(StringUtils.isBlank(goods.getSn())){
 | 
	
		
			
				|  |  |  			throw new BizException("请指定商品货号");
 | 
	
		
			
				|  |  |  		}
 | 
	
		
			
				|  |  | -		if(StringUtils.isEmpty(goods.getComplementGoodsJson())){
 | 
	
		
			
				|  |  | -			throw new BizException("请指定子商品");
 | 
	
		
			
				|  |  | -		}
 | 
	
		
			
				|  |  |  		Goods existsGood = goodsDao.lockBySn(goods.getSn());
 | 
	
		
			
				|  |  |  		if(Objects.nonNull(existsGood)){
 | 
	
		
			
				|  |  |  			throw new BizException("商品货号重复");
 | 
	
		
			
				|  |  |  		}
 | 
	
		
			
				|  |  | -		List<ComplementGoodsDto> complementGoods = JSON.parseArray(goods.getComplementGoodsJson(), ComplementGoodsDto.class);
 | 
	
		
			
				|  |  | +		List<GoodsWrapper.GoodsSub> goodsSubList = goods.getGoodsSubList();
 | 
	
		
			
				|  |  |  		//获取最小的库存数
 | 
	
		
			
				|  |  | -		String skuIds = complementGoods.stream().map(e -> e.getSkuStockId().toString()).collect(Collectors.joining(","));
 | 
	
		
			
				|  |  | +		String skuIds = goodsSubList.stream().map(e -> e.getSku().toString()).collect(Collectors.joining(","));
 | 
	
		
			
				|  |  |  		PmsProductQueryParamDto paramDto = new PmsProductQueryParamDto();
 | 
	
		
			
				|  |  |  		paramDto.setSkuStockIds(skuIds);
 | 
	
		
			
				|  |  |  		paramDto.setPublishStatus(1);
 | 
	
	
		
			
				|  | @@ -189,7 +195,14 @@ public class GoodsServiceImpl extends BaseServiceImpl<Integer, Goods>  implement
 | 
	
		
			
				|  |  |  		Integer stock = productList.stream().map(PmsProductDto::getStock).min(Integer::compareTo).get();
 | 
	
		
			
				|  |  |  		goods.setStockCount(stock);
 | 
	
		
			
				|  |  |  		goods.setSellCount(0);
 | 
	
		
			
				|  |  | -		goodsDao.insert(goods);
 | 
	
		
			
				|  |  | +		Goods goodsRecord = new Goods();
 | 
	
		
			
				|  |  | +		BeanUtils.copyProperties(goods,goodsRecord);
 | 
	
		
			
				|  |  | +		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()));
 | 
	
		
			
				|  |  | +		goodsSubMapper.saveBatch(goodsSubs);
 | 
	
		
			
				|  |  |  	}
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |  	@Override
 | 
	
	
		
			
				|  | @@ -245,6 +258,13 @@ public class GoodsServiceImpl extends BaseServiceImpl<Integer, Goods>  implement
 | 
	
		
			
				|  |  |  					throw new BizException("{}等商品还在销售中", goodsNames);
 | 
	
		
			
				|  |  |  				}
 | 
	
		
			
				|  |  |  			}
 | 
	
		
			
				|  |  | +			List<GoodsSub> goodsSubs = queryGoodsSub(goodsId);
 | 
	
		
			
				|  |  | +			if (!CollectionUtils.isEmpty(goodsSubs) && status == 1) {
 | 
	
		
			
				|  |  | +				long falseSize = goodsSubs.stream().map(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());
 | 
	
	
		
			
				|  | @@ -259,6 +279,14 @@ public class GoodsServiceImpl extends BaseServiceImpl<Integer, Goods>  implement
 | 
	
		
			
				|  |  |  		goodsDao.update(goods);
 | 
	
		
			
				|  |  |  	}
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | +	private List<GoodsSub> queryGoodsSub(Integer goodsId) {
 | 
	
		
			
				|  |  | +		// 从管乐迷商城组合的商品
 | 
	
		
			
				|  |  | +		QueryWrapper<GoodsSub> queryWrapper = new QueryWrapper<>();
 | 
	
		
			
				|  |  | +		queryWrapper.eq("goods_id_", goodsId);
 | 
	
		
			
				|  |  | +		List<GoodsSub> goodsSubs = goodsSubMapper.selectList(queryWrapper);
 | 
	
		
			
				|  |  | +		return goodsSubs;
 | 
	
		
			
				|  |  | +	}
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  |  	@Override
 | 
	
		
			
				|  |  |  	public List<Goods> findGoodsBySubId(GoodsQuery goodsQuery) {
 | 
	
		
			
				|  |  |  		return goodsDao.findGoodsBySubId(goodsQuery);
 | 
	
	
		
			
				|  | @@ -729,7 +757,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()),","));
 | 
	
	
		
			
				|  | @@ -749,7 +777,11 @@ public class GoodsServiceImpl extends BaseServiceImpl<Integer, Goods>  implement
 | 
	
		
			
				|  |  |  		if(StringUtils.isNotEmpty(goods.getFreeFeeShowOrganId())){
 | 
	
		
			
				|  |  |  			goods.setFreeFeeShowOrganName(StringUtils.join(organizationDao.findByOrganIds(goods.getFreeFeeShowOrganId()),","));
 | 
	
		
			
				|  |  |  		}
 | 
	
		
			
				|  |  | -		return goods;
 | 
	
		
			
				|  |  | +		List<GoodsSub> goodsSubs = queryGoodsSub(goodsId);
 | 
	
		
			
				|  |  | +		GoodsWrapper.Goods detail = new GoodsWrapper.Goods();
 | 
	
		
			
				|  |  | +		BeanUtils.copyProperties(goods, detail);
 | 
	
		
			
				|  |  | +		detail.setGoodsSubList(JSON.parseArray(JSON.toJSONString(goodsSubs), GoodsWrapper.GoodsSub.class));
 | 
	
		
			
				|  |  | +		return detail;
 | 
	
		
			
				|  |  |  	}
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |  	@Override
 | 
	
	
		
			
				|  | @@ -762,4 +794,48 @@ 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.setStatus(null);
 | 
	
		
			
				|  |  | +		BeanUtils.copyProperties(goods, goodsInfo);
 | 
	
		
			
				|  |  | +		if (!goodsSubList.isEmpty()) {
 | 
	
		
			
				|  |  | +			goodsInfo.setComplementGoodsIdList(null);
 | 
	
		
			
				|  |  | +			UpdateWrapper<GoodsSub> delWrapper = new UpdateWrapper<>();
 | 
	
		
			
				|  |  | +			delWrapper.eq("goods_id_", goods.getId());
 | 
	
		
			
				|  |  | +			goodsSubMapper.delete(delWrapper);
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +			List<GoodsSub> goodsSubs = JSON.parseArray(JSON.toJSONString(goodsSubList), GoodsSub.class);
 | 
	
		
			
				|  |  | +			goodsSubs.forEach(next -> next.setGoodsId(goods.getId()));
 | 
	
		
			
				|  |  | +			goodsSubMapper.saveBatch(goodsSubs);
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +			BigDecimal organCostPrice = goodsSubList.stream().map(GoodsWrapper.GoodsSub::getGoodsPrice).reduce(BigDecimal.ZERO, BigDecimal::add);
 | 
	
		
			
				|  |  | +			goodsInfo.setOrganCostPrice(organCostPrice);
 | 
	
		
			
				|  |  | +		}
 | 
	
		
			
				|  |  | +		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();
 | 
	
		
			
				|  |  | +		return mallFeignService.getProductList(dto);
 | 
	
		
			
				|  |  | +	}
 | 
	
		
			
				|  |  |  }
 |