zxl
6 天以前 dc181d4428c85f681d342e2ca5c7760aea5f254d
framework/src/main/java/cn/lili/modules/goods/serviceimpl/GoodsServiceImpl.java
@@ -15,9 +15,7 @@
import cn.lili.common.security.context.UserContext;
import cn.lili.common.security.enums.UserEnums;
import cn.lili.modules.goods.entity.dos.*;
import cn.lili.modules.goods.entity.dto.GoodsOperationDTO;
import cn.lili.modules.goods.entity.dto.GoodsParamsDTO;
import cn.lili.modules.goods.entity.dto.GoodsSearchParams;
import cn.lili.modules.goods.entity.dto.*;
import cn.lili.modules.goods.entity.enums.GoodsAuthEnum;
import cn.lili.modules.goods.entity.enums.GoodsStatusEnum;
import cn.lili.modules.goods.entity.vos.GoodsSkuVO;
@@ -41,6 +39,7 @@
import cn.lili.mybatis.util.PageUtil;
import cn.lili.rocketmq.RocketmqSendCallbackBuilder;
import cn.lili.rocketmq.tags.GoodsTagsEnum;
import cn.lili.utils.COSUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
@@ -48,6 +47,10 @@
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
@@ -55,6 +58,9 @@
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.net.URLEncoder;
import java.util.*;
import java.util.stream.Collectors;
@@ -121,6 +127,9 @@
    @Autowired
    private Cache<GoodsVO> cache;
    @Autowired
    private COSUtil cosUtil;
    @Override
    public List<Goods> getByBrandIds(List<String> brandIds) {
        LambdaQueryWrapper<Goods> lambdaQueryWrapper = new LambdaQueryWrapper<>();
@@ -172,7 +181,7 @@
        //检查商品
        this.checkGoods(goods);
        //向goods加入图片
        if (goodsOperationDTO.getGoodsGalleryList().size() > 0) {
        if (goodsOperationDTO.getGoodsGalleryList()!=null&& !goodsOperationDTO.getGoodsGalleryList().isEmpty()) {
            this.setGoodsGalleryParam(goodsOperationDTO.getGoodsGalleryList().get(0), goods);
        }
        //添加商品参数
@@ -201,15 +210,23 @@
        //检查商品信息
        this.checkGoods(goods);
        //向goods加入图片
        this.setGoodsGalleryParam(goodsOperationDTO.getGoodsGalleryList().get(0), goods);
        if (goodsOperationDTO.getGoodsGalleryList() != null&& !goodsOperationDTO.getGoodsGalleryList().isEmpty()) {
            this.setGoodsGalleryParam(goodsOperationDTO.getGoodsGalleryList().get(0), goods);
        }else {
            goods.setOriginal(null);
            goods.setSmall(null);
            goods.setThumbnail(null);
            goodsGalleryService.removeByGoodsId(goodsId);
        }
        //添加商品参数
        if (goodsOperationDTO.getGoodsParamsDTOList() != null && !goodsOperationDTO.getGoodsParamsDTOList().isEmpty()) {
            goods.setParams(JSONUtil.toJsonStr(goodsOperationDTO.getGoodsParamsDTOList()));
        }
        Goods previousGoods = this.getById(goodsId);
        //修改商品
        this.updateById(goods);
        //修改商品sku信息
        this.goodsSkuService.update(goods, goodsOperationDTO);
        this.goodsSkuService.update(goods, goodsOperationDTO,previousGoods);
        //添加相册
        if (goodsOperationDTO.getGoodsGalleryList() != null && !goodsOperationDTO.getGoodsGalleryList().isEmpty()) {
            this.goodsGalleryService.add(goodsOperationDTO.getGoodsGalleryList(), goods.getId());
@@ -227,6 +244,17 @@
        //缓存获取,如果没有则读取缓存
        GoodsVO goodsVO = cache.get(CachePrefix.GOODS.getPrefix() + goodsId);
        if (goodsVO != null) {
//            List<String> goodsGalleryList = goodsVO.getGoodsGalleryList();
//            if (goodsGalleryList != null && !goodsGalleryList.isEmpty()) {
//                List<String> galleryList = goodsGalleryList.stream().map(item -> {
//                    if (StringUtils.isNotBlank(item)&&!item.contains("http")) {
//                        return cosUtil.getPreviewUrl(item);
//                    }else {
//                        return item;
//                    }
//                }).collect(Collectors.toList());
//                goodsVO.setGoodsGalleryList(galleryList);
//            }
            return goodsVO;
        }
        //查询商品信息
@@ -245,6 +273,12 @@
        List<GoodsGallery> galleryList = goodsGalleryService.goodsGalleryList(goodsId);
        for (GoodsGallery goodsGallery : galleryList) {
            images.add(goodsGallery.getOriginal());
//            String original = goodsGallery.getOriginal();
//            if (StringUtils.isNotBlank(original)&&!original.contains("http")) {
//                images.add(cosUtil.getPreviewUrl(original));
//            }else {
//                images.add(goodsGallery.getOriginal());
//            }
        }
        goodsVO.setGoodsGalleryList(images);
        //商品sku赋值
@@ -281,9 +315,91 @@
        return this.page(PageUtil.initPage(goodsSearchParams), goodsSearchParams.queryWrapper());
    }
    @Override
    public void queryExportGoods(HttpServletResponse response, GoodsSearchParams searchParams) {
        List<Goods> list = this.list(searchParams.queryWrapper());
        XSSFWorkbook workbook = initGoodsExportData(list);
        try {
            // 设置响应头
            String fileName = URLEncoder.encode("商品清单", "UTF-8");
            response.setContentType("application/vnd.ms-excel;charset=UTF-8");
            response.setHeader("Content-Disposition", "attachment;filename=" + fileName + ".xlsx");
            ServletOutputStream out = response.getOutputStream();
            workbook.write(out);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                workbook.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    private XSSFWorkbook initGoodsExportData(List<Goods> list) {
        List<GoodsExportDTO> goodsExportDTOs = new ArrayList<>();
        for (Goods goods : list) {
            GoodsExportDTO goodsExportDTO = new GoodsExportDTO();
            BeanUtils.copyProperties(goods, goodsExportDTO);
            goodsExportDTOs.add(goodsExportDTO);
        }
        XSSFWorkbook workbook = new XSSFWorkbook();
        Sheet sheet = workbook.createSheet("商品清单");
        Row header = sheet.createRow(0);
        String[] headers = {"商品名称", "商品编号", "价格", "销售模式", "商品类型", "状态", "审核状态", "店铺名称"};
        for (int i = 0; i < headers.length; i++) {
            Cell cell = header.createCell(i);
            cell.setCellValue(headers[i]);
        }
        for (int i = 0; i < goodsExportDTOs.size(); i++) {
            GoodsExportDTO goodsExportDTO = goodsExportDTOs.get(i);
            Row row = sheet.createRow(i + 1);
            row.createCell(0).setCellValue(goodsExportDTO.getGoodsName());
            row.createCell(1).setCellValue(goodsExportDTO.getId());
            row.createCell(2).setCellValue(goodsExportDTO.getPrice());
            if ("RETAIL".equals(goodsExportDTO.getSalesModel())) {
                row.createCell(3).setCellValue("零售");
            } else if ("WHOLESALE".equals(goodsExportDTO.getSalesModel())) {
                row.createCell(3).setCellValue("批发");
            } else if ("PRESALE".equals(goodsExportDTO.getSalesModel())) {
                row.createCell(3).setCellValue("预售");
            } else {
                row.createCell(3).setCellValue("其他类型");
            }
            if ("PHYSICAL_GOODS".equals(goodsExportDTO.getGoodsType())) {
                row.createCell(4).setCellValue("实物商品");
            } else if ("VIRTUAL_GOODS".equals(goodsExportDTO.getGoodsType())) {
                row.createCell(4).setCellValue("虚拟商品");
            } else {
                row.createCell(4).setCellValue("电子卡券");
            }
            if ("DOWN".equals(goodsExportDTO.getMarketEnable())) {
                row.createCell(5).setCellValue("下架");
            } else if ("UPPER".equals(goodsExportDTO.getMarketEnable())) {
                row.createCell(5).setCellValue("上架");
            }
            if ("TOBEAUDITED".equals(goodsExportDTO.getAuthFlag())) {
                row.createCell(6).setCellValue("待审核");
            } else if ("PASS".equals(goodsExportDTO.getAuthFlag())) {
                row.createCell(6).setCellValue("通过");
            } else if ("REFUSE".equals(goodsExportDTO.getAuthFlag())) {
                row.createCell(6).setCellValue("拒绝");
            }
            row.createCell(7).setCellValue(goodsExportDTO.getStoreName());
        }
        return workbook;
    }
    /**
     * 商品查询
     *
     *g
     * @param goodsSearchParams 查询参数
     * @return 商品信息
     */
@@ -389,6 +505,22 @@
        queryWrapper.in(Goods::getId, goodsIds);
        List<Goods> goodsList = this.list(queryWrapper);
        this.updateGoodsStatus(goodsIds, goodsStatusEnum, goodsList);
        return result;
    }
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean goodsSort(GoodsSortDTO req) {
        boolean result;
        //检测管理员权限
        this.checkManagerAuthority();
        LambdaUpdateWrapper<Goods> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.set(Goods::getGoodsSort,req.getSort());
        updateWrapper.eq(Goods::getId, req.getGoodsId());
        result = this.update(updateWrapper);
        List<String> goodsIds = new ArrayList<>();
        goodsIds.add(req.getGoodsId());
        this.updateEsGoods(goodsIds);
        return result;
    }
@@ -541,6 +673,7 @@
        this.baseMapper.addGoodsCommentNum(commentNum, goodsId);
    }
    /**
     * 更新商品状态
     *