jiangping
2025-06-06 c845254d8e0d83bb9458d0236a1da201bfbeae42
server/visits/dmvisit_service/src/main/java/com/doumee/service/business/impl/YwPatrolPointServiceImpl.java
@@ -22,14 +22,32 @@
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
import com.google.zxing.qrcode.encoder.ByteMatrix;
import com.google.zxing.qrcode.encoder.Encoder;
import com.google.zxing.qrcode.encoder.QRCode;
import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;
import org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import java.util.Date;
import java.util.List;
import java.util.Objects;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.URLEncoder;
import java.nio.charset.Charset;
import java.nio.file.FileSystems;
import java.nio.file.Path;
import java.util.*;
/**
 * 运维巡检点信息表Service实现
@@ -49,15 +67,21 @@
    @Override
    public Integer create(YwPatrolPoint ywPatrolPoint) {
        if(Objects.isNull(ywPatrolPoint)
                || Objects.isNull(ywPatrolPoint.getCode())
//                || Objects.isNull(ywPatrolPoint.getCode())
                || Objects.isNull(ywPatrolPoint.getName())
        ){
            throw new BusinessException(ResponseStatus.BAD_REQUEST);
        }
        if(ywPatrolPointMapper.selectCount(new QueryWrapper<YwPatrolPoint>().lambda().eq(YwPatrolPoint::getIsdeleted,Constants.ZERO)
                .eq(YwPatrolPoint::getCode,ywPatrolPoint.getCode()))>Constants.ZERO){
            throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(),"巡检点编码重复!");
        String pre =  systemDictDataBiz.queryByCode(Constants.XUNJIAN,Constants.XJ_POINT_PREFIX).getCode();
        int length = 8;
        try {
            length =  Integer.parseInt(systemDictDataBiz.queryByCode(Constants.XUNJIAN,Constants.XJ_POINT_CODE_LENGTH).getCode());
        }catch (Exception e){
        }
        long num = ywPatrolPointMapper.selectCount(new QueryWrapper<YwPatrolPoint>());
        ywPatrolPoint.setCode(pre+Constants.formartNumString (length,(num+1)));
        LoginUserInfo loginUserInfo = ywPatrolPoint.getLoginUserInfo();
        ywPatrolPoint.setCreateDate(new Date());
        ywPatrolPoint.setCreator(loginUserInfo.getId());
@@ -82,10 +106,11 @@
    @Override
    public void deleteById(Integer id, LoginUserInfo user) {
        ywPatrolPointMapper.update(new UpdateWrapper<YwPatrolPoint>().lambda().set(YwPatrolPoint::getIsdeleted,Constants.ONE)
        ywPatrolPointMapper.update(new UpdateWrapper<YwPatrolPoint>().lambda()
                .set(YwPatrolPoint::getIsdeleted,Constants.ONE)
                .set(YwPatrolPoint::getEditDate, DateUtil.getCurrDateTime())
                .set(YwPatrolPoint::getEditor,user.getId())
                .eq(YwPatrolPoint::getId,user.getId())
                .eq(YwPatrolPoint::getId,id)
        );
    }
@@ -94,7 +119,120 @@
        UpdateWrapper<YwPatrolPoint> deleteWrapper = new UpdateWrapper<>(ywPatrolPoint);
        ywPatrolPointMapper.delete(deleteWrapper);
    }
    @Override
    public void exportQrcodes(Integer id, HttpServletResponse response){
        try {
            List<File>  fileList = new ArrayList<>();
            List<YwPatrolPoint> bikesList = ywPatrolPointMapper.selectList(new QueryWrapper<YwPatrolPoint>().lambda()
                    .eq(YwPatrolPoint::getIsdeleted,Constants.ZERO)
                    .eq(id!=null,YwPatrolPoint::getId,id)
            );
            if(bikesList== null || bikesList.size() == 0){
                throw  new BusinessException(ResponseStatus.DATA_EMPTY);
            }
            // 创建临时文件的前缀和后缀
            String path =  systemDictDataBiz.queryByCode(Constants.WX_PLATFORM,Constants.WX_AUTH_URL).getCode();
            String uri =  systemDictDataBiz.queryByCode(Constants.XUNJIAN,Constants.XJ_RERIRECT_URI).getCode();
            String appId = systemDictDataBiz.queryByCode(Constants.WX_PLATFORM,Constants.WX_PLATFORM_APPID).getCode() ;
            // 创建临时文件
            for(YwPatrolPoint l : bikesList){
                if(StringUtils.isNotBlank(l.getCode())){
                    String redirectUri = uri.replace("${ywid}",l.getCode());
                    String url = path.replace("${url}",URLEncoder.encode(redirectUri)).replace("${appid}",appId);
                    File file =  generateQRCodeImage(url,100,100,l.getCode()+".png");
                    if(file!=null && file.isFile()){
                        fileList.add(file);
                    }
                }
            }
            if(fileList == null || fileList.size() == 0){
                throw  new BusinessException(ResponseStatus.DATA_EMPTY.getCode(),"对不起,下载内容为空,操作失败!");
            }
            String fileName =  "巡检点二维码导出_"+System.currentTimeMillis()+".zip" ;
            String encodeFileName =URLEncoder.encode(fileName, Charset.forName("UTF-8").toString())+".zip";
            response.setHeader("Content-Disposition","attachment;filename=" + encodeFileName);
            response.setContentType("application/octet-stream");
            response.setHeader("eva-opera-type", "download");
            response.setHeader("eva-download-filename", encodeFileName);
            packFilesToZip(fileList,response.getOutputStream());
        } catch (IOException e) {
            throw new BusinessException(ResponseStatus.EXPORT_EXCEL_ERROR, e);
        }
    }
    public static File generateQRCodeImage(String text, int width, int height, String fileName)  {
        try {
            // 创建二维码数据矩阵
            Map<EncodeHintType, Object> hints = new HashMap<>();
            hints.put(EncodeHintType.CHARACTER_SET, "UTF-8"); // 设置字符编码为UTF-8
            hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H); // 设置纠错等级为H
            hints.put(EncodeHintType.MARGIN, 0); // 设置白边为0
            BitMatrix bitMatrix = new MultiFormatWriter().encode(text, BarcodeFormat.QR_CODE , width, height, hints);
            // 保存二维码图片到文件系统
            File f = new File("temp/");
            if(!f.exists()){
                f.mkdirs();
            }
//            bitMatrix =  renderResult(bitMatrix,width,height);
            Path path = FileSystems.getDefault().getPath("temp/"+fileName);
            MatrixToImageWriter.writeToPath(bitMatrix, "PNG", path); // 保存为PNG格式的图片
            return path.toFile();
        }catch (Exception e){
            e.printStackTrace();
        }
        return  null;
    }
    public static void main(String[] args) {
        File f =generateQRCodeImage("dj少时诵诗书少时诵诗书是撒是撒是撒",100,100,UUID.randomUUID().toString()+".png");
        System.out.println(f.getAbsolutePath());
    }
    private static BitMatrix renderResult(BitMatrix input, int width, int height) {
        if (input == null) {
           return null;
        }
        int inputWidth = input.getWidth();
        int inputHeight = input.getHeight();
        // 依据用户的输入宽高,计算最后的输出宽高
        int outputWidth = Math.max(width, inputWidth);
        int outputHeight = Math.max(height, inputHeight);
        //计算缩放比例
        int multiple = Math.min(outputWidth / inputWidth, outputHeight / inputHeight);
        BitMatrix output = new BitMatrix(outputWidth, outputHeight);
        int inputY = 0;
        // 嵌套循环,将ByteMatrix的内容计算padding后转换成BitMatrix
        for (int outputY = 0; inputY < inputHeight; outputY += multiple) {
            int inputX = 0;
            for (int outputX = 0; inputX < inputWidth; outputX += multiple) {
                if (input.get(inputX, inputY)) {
                    output.setRegion(outputX, outputY, multiple, multiple);
                }
                inputX++;
            }
            inputY++;
        }
        return output;
    }
    public static void packFilesToZip(List<File> files,    ServletOutputStream os) throws IOException {
        try (ZipArchiveOutputStream zipOutputStream = new ZipArchiveOutputStream(os)) {
            for (File file : files) {
                ZipArchiveEntry entry = new ZipArchiveEntry(file.getName());
                zipOutputStream.putArchiveEntry(entry);
                try (FileInputStream fileInputStream = new FileInputStream(file)) {
                    byte[] buffer = new byte[1024];
                    int length;
                    while ((length = fileInputStream.read(buffer)) > 0) {
                        zipOutputStream.write(buffer, 0, length);
                    }
                }
                zipOutputStream.closeArchiveEntry();
            }
        }
    }
    @Override
    public void deleteByIdInBatch(List<Integer> ids, LoginUserInfo user) {
        if (CollectionUtils.isEmpty(ids)) {