ソースを参照

美维大屏修改5

wwh 1 年間 前
コミット
a02c203f55

+ 69 - 0
huimv-eartag2-platform/huimv-eartag2-manage2/pom.xml

@@ -110,6 +110,75 @@
             <groupId>org.springframework.boot</groupId>
             <artifactId>spring-boot-starter-websocket</artifactId>
         </dependency>
+
+        <!--sftp文件上传-->
+        <dependency>
+            <groupId>com.jcraft</groupId>
+            <artifactId>jsch</artifactId>
+            <version>0.1.54</version>
+        </dependency>
+        <dependency>
+            <groupId>junit</groupId>
+            <artifactId>junit</artifactId>
+        </dependency>
+
+        <dependency>
+            <groupId>org.jxls</groupId>
+            <artifactId>jxls</artifactId>
+            <version>2.6.0</version>
+        </dependency>
+        <dependency>
+            <groupId>org.jxls</groupId>
+            <artifactId>jxls-poi</artifactId>
+            <version>1.1.0</version>
+        </dependency>
+
+        <dependency>
+            <groupId>org.apache.poi</groupId>
+            <artifactId>poi</artifactId>
+            <version>3.15</version>
+        </dependency>
+
+        <!-- poi 读取word doc-->
+        <dependency>
+            <groupId>org.apache.poi</groupId>
+            <artifactId>poi-scratchpad</artifactId>
+            <version>3.15</version>
+        </dependency>
+
+        <!-- poi 读取word docx-->
+        <!--        <dependency>-->
+        <!--            <groupId>fr.opensagres.xdocreport</groupId>-->
+        <!--            <artifactId>xdocreport</artifactId>-->
+        <!--            <version>1.0.6</version>-->
+        <!--        </dependency>-->
+
+        <!-- poi xml-->
+        <dependency>
+            <groupId>org.apache.poi</groupId>
+            <artifactId>poi-ooxml</artifactId>
+            <version>3.15</version>
+        </dependency>
+
+        <dependency>
+            <groupId>org.apache.poi</groupId>
+            <artifactId>poi-ooxml-schemas</artifactId>
+            <version>3.15</version>
+        </dependency>
+
+        <dependency>
+            <groupId>org.projectlombok</groupId>
+            <artifactId>lombok</artifactId>
+            <version>1.18.12</version>
+            <scope>provided</scope>
+        </dependency>
+
+
+        <dependency>
+            <groupId>fr.opensagres.xdocreport</groupId>
+            <artifactId>org.apache.poi.xwpf.converter.xhtml</artifactId>
+            <version>1.0.6</version>
+        </dependency>
     </dependencies>
 
     <build>

+ 9 - 0
huimv-eartag2-platform/huimv-eartag2-manage2/src/main/java/com/huimv/eartag2/manage2/controller/EartagDeviceRegisterController.java

@@ -3,9 +3,11 @@ package com.huimv.eartag2.manage2.controller;
 
 import com.huimv.eartag2.common.utils.Result;
 import com.huimv.eartag2.manage2.service.EartagDeviceRegisterService;
+import com.huimv.eartag2.manage2.utils.PdfUtil;
 import org.springframework.web.bind.annotation.*;
 
 import javax.annotation.Resource;
+import javax.servlet.http.HttpServletResponse;
 import java.util.List;
 import java.util.Map;
 
@@ -95,5 +97,12 @@ public class EartagDeviceRegisterController {
     public Result getDeviceCode(@RequestParam(name = "farmId") String farmCode){
         return eartagDeviceRegisterService.getDeviceCode(farmCode);
     }
+
+    @GetMapping("/printData")
+    public void printChi(HttpServletResponse response,@RequestParam(name = "eartag") String eartag) throws Exception {
+        eartagDeviceRegisterService.getData(eartag);
+        String path = "G://"+eartag+".xls";
+        PdfUtil.returnPdfStream3(response, path, eartag);
+    }
 }
 

+ 2 - 0
huimv-eartag2-platform/huimv-eartag2-manage2/src/main/java/com/huimv/eartag2/manage2/service/EartagDeviceRegisterService.java

@@ -32,4 +32,6 @@ public interface EartagDeviceRegisterService extends IService<EartagDeviceRegist
     Result getDeviceCode(String farmCode);
 
     Result setFarmer(List<Map<String, Integer>> maps);
+
+    void getData(String eartag) throws Exception;
 }

+ 16 - 4
huimv-eartag2-platform/huimv-eartag2-manage2/src/main/java/com/huimv/eartag2/manage2/service/impl/EartagDeviceRegisterServiceImpl.java

@@ -6,9 +6,12 @@ import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
 import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
 import com.baomidou.mybatisplus.core.toolkit.StringUtils;
 import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
+import com.huimv.eartag2.manage2.mapper.EartagData2Mapper;
 import com.huimv.eartag2.manage2.pojo.BaseFarmer;
+import com.huimv.eartag2.manage2.pojo.EartagData2;
 import com.huimv.eartag2.manage2.pojo.EartagDeviceRegister;
 import com.huimv.eartag2.manage2.utils.IDUtil;
+import com.huimv.eartag2.manage2.utils.Print;
 import com.huimv.eartag2.manage2.utils.ResultUtil;
 import com.huimv.eartag2.common.utils.Result;
 import com.huimv.eartag2.common.utils.ResultCode;
@@ -23,10 +26,7 @@ import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 
 import javax.annotation.Resource;
-import java.util.ArrayList;
-import java.util.Comparator;
-import java.util.List;
-import java.util.Map;
+import java.util.*;
 
 /**
  * <p>
@@ -51,6 +51,8 @@ public class EartagDeviceRegisterServiceImpl extends ServiceImpl<EartagDeviceReg
     private IDUtil idUtil;
     @Autowired
     private ICacheService cacheService;
+    @Autowired
+    private EartagData2Mapper data2Mapper;
 
 
     @Override
@@ -90,6 +92,16 @@ public class EartagDeviceRegisterServiceImpl extends ServiceImpl<EartagDeviceReg
     }
 
     @Override
+    public void getData(String eartag ) throws Exception {
+        Calendar calendar = Calendar.getInstance();
+        calendar.set(Calendar.DATE, calendar.get(Calendar.DATE) - 7);
+        QueryWrapper<EartagData2> queryWrapper = new QueryWrapper<>();
+        queryWrapper.eq("earmark", eartag).ge("add_time", calendar.getTime()).orderByAsc("add_time");
+        List<EartagData2> eartagData2s = data2Mapper.selectList(queryWrapper);
+        Print.printDuck(eartagData2s);
+    }
+
+    @Override
     public Result remove(String ids) {
         List<Integer> convertList = idUtil.convert(ids);
         //{删除芯片id缓存}

+ 344 - 0
huimv-eartag2-platform/huimv-eartag2-manage2/src/main/java/com/huimv/eartag2/manage2/utils/PdfUtil.java

@@ -0,0 +1,344 @@
+package com.huimv.eartag2.manage2.utils;
+
+//import cn.bt.common.utils.StringUtils;
+//import cn.bt.modules.process.instance.enums.InstanceStatusEnum;
+//import com.itextpdf.text.*;
+//import com.itextpdf.text.pdf.*;
+
+import cn.hutool.core.io.IoUtil;
+
+import javax.servlet.http.HttpServletResponse;
+import java.io.BufferedInputStream;
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.InputStream;
+
+/**
+ * @Author: xp
+ * @Date: 2021/7/14
+ */
+public class PdfUtil {
+
+    /**
+     * 返回PDF流
+     *
+     * @param response 相应设置
+     * @param pathName 水印文件路径和名称
+     * @throws Exception 异常
+     */
+    /*public static void returnPdfStream(HttpServletResponse response, String pathName) throws Exception {
+        response.setContentType("application/pdf");
+
+        File file = new File(pathName);
+        if (file.exists()) {
+            FileInputStream in = new FileInputStream(file);
+            OutputStream out = response.getOutputStream();
+            byte[] b = new byte[1024 * 4];
+            int n;
+            while ((n = in.read(b)) > -1) {
+                out.write(b, 0, n);
+            }
+            out.flush();
+            in.close();
+            out.close();
+        }
+    }*/
+    public static void returnPdfStream2(HttpServletResponse response, String pathName, String subsidyNane) throws Exception {
+        InputStream in = null;
+        try {
+            File file = new File(pathName);
+             in = new BufferedInputStream(new FileInputStream(file));
+            String filename = new String((subsidyNane+".pdf").getBytes(), "ISO8859_1");
+            response.setContentType("application/binary;charset=ISO8859_1");
+            response.setHeader("Content-disposition", "attachment; filename=" + filename);// 组装附件名称和格式
+            IoUtil.copy(in, response.getOutputStream());
+        } catch (Exception e) {
+            System.out.println("------------ 这里除了问题!!!-----------");
+            System.out.println(e);
+        }finally {
+            if (in != null) {
+                in.close();
+            }
+        }
+    }
+
+    public static void returnPdfStream3(HttpServletResponse response, String pathName, String subsidyNane) throws Exception {
+        InputStream in = null;
+        try {
+            File file = new File(pathName);
+            in = new BufferedInputStream(new FileInputStream(file));
+            String filename = new String((subsidyNane+".xls").getBytes(), "ISO8859_1");
+            response.setContentType("application/binary;charset=ISO8859_1");
+            response.setHeader("Content-disposition", "attachment; filename=" + filename);// 组装附件名称和格式
+            IoUtil.copy(in, response.getOutputStream());
+        } catch (Exception e) {
+            System.out.println("------------ 这里除了问题!!!-----------");
+            System.out.println(e);
+        }finally {
+            if (in != null) {
+                in.close();
+            }
+        }
+    }
+
+    /**
+     * 返回水印图片路径
+     *
+     * @param instanceStatus 流程状态
+     * @return 水印图片路径
+     */
+//    public static String returnWatermarkPath(int instanceStatus) {
+//        String watermarkPath = "ioffice-api/src/main/resources/static/watermark";
+//        if (InstanceStatusEnum.E_0.getCode().equals(instanceStatus)) {
+//            watermarkPath = watermarkPath + "/已作废.png";
+//
+//        } else if (InstanceStatusEnum.E_2.getCode().equals(instanceStatus)) {
+//            watermarkPath = watermarkPath + "/审批通过.png";
+//
+//        } else {
+//            watermarkPath = watermarkPath + "/审批中.png";
+//        }
+//        return watermarkPath;
+//    }
+//
+//    /**
+//     * 建立一个书写器(Writer)与document对象关联,通过书写器(Writer)可以将文档写入到磁盘中。
+//     *
+//     * @param pdfPath     保存路径
+//     * @param document    文档
+//     * @param pdfPathName 文件保存路径和名称
+//     * @return 书写器(Writer)
+//     * @throws Exception
+//     */
+//    public static PdfWriter createPdfWriter(String pdfPath, Document document, String pdfPathName) throws Exception {
+//
+//        //判断文件夹是否存在
+//        File file = new File(pdfPath);
+//        if (!file.exists()) {
+//            file.mkdir();
+//        }
+//        file = new File(pdfPathName);
+//
+//        PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(file));
+//        writer.setViewerPreferences(PdfWriter.PageModeUseThumbs);
+//
+//        return writer;
+//    }
+//
+//    /**
+//     * 设置第一标题内容
+//     *
+//     * @param title    第一标题
+//     * @param document 文档
+//     * @throws Exception 异常
+//     */
+//    public static void setPdfFirstTitle(String title, Document document) throws Exception {
+//        Paragraph paragraph = new Paragraph(title, getPdfChineseFont(0));
+//        paragraph.setAlignment(Element.ALIGN_CENTER);
+//        paragraph.setSpacingAfter(2);
+//        document.add(paragraph);
+//    }
+//
+//    /**
+//     * 设置第二标题内容
+//     *
+//     * @param title           第二标题
+//     * @param document        文档
+//     * @param alignment       对齐方式
+//     * @param firstLineIndent 第一行缩进
+//     * @param spacingAfter    之后间隔
+//     * @throws Exception
+//     */
+//    public static void setPdfSecondTitle(String title, Document document, int alignment, int firstLineIndent, int spacingAfter) throws Exception {
+//        Paragraph lsh = new Paragraph(title, getPdfChineseFont(1));
+//        lsh.setAlignment(alignment);
+//        lsh.setFirstLineIndent(firstLineIndent);
+//        lsh.setSpacingAfter(spacingAfter);
+//        document.add(lsh);
+//    }
+//
+//    /**
+//     * 设置表格内容 并将表格加入文档中
+//     *
+//     * @param dates    数据
+//     * @param document 文档
+//     * @param table    表格
+//     * @throws Exception 异常
+//     */
+//    public static void setPdfTableContent(Object[][] dates, Document document, PdfPTable table) throws Exception {
+//        for (int i = 0; i < dates.length; i++) {
+//            for (int j = 0; j < dates[i].length; j++) {
+//                //表格的单元格
+//                PdfPCell pdfCell = new PdfPCell();
+//                //设置表格行高
+//                if (i > 0 && i < 3) {
+//                    pdfCell.setMinimumHeight(50);
+//                } else {
+//                    pdfCell.setMinimumHeight(25);
+//                }
+//                Paragraph paragraph = new Paragraph(dates[i][j] + "", getPdfChineseFont(1));
+//                pdfCell.setPhrase(paragraph);
+//
+//                setCellStyle(pdfCell);
+//                table.addCell(pdfCell);
+//            }
+//        }
+//
+//        document.add(table);
+//    }
+//
+//    /**
+//     * 设置水印
+//     *
+//     * @param inputFile 要这设置水印的文件路径
+//     * @param imageFile 水印图片路径
+//     * @param tag       标记
+//     * @return 生成水印文件的路径
+//     */
+//    public static String imageWaterMark(String inputFile, String imageFile, String tag) throws Exception {
+//        String[] spe = separatePath(inputFile);
+//        String outputFile = spe[0] + tag + "." + spe[1];
+//
+//        PdfReader reader = new PdfReader(inputFile);
+//        PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(outputFile));
+//
+//        int total = reader.getNumberOfPages() + 1;
+//
+//        Image image = Image.getInstance(imageFile);
+//        //坐标
+//        image.setAbsolutePosition(389, 730);
+//        //自定义大小
+//        image.scaleAbsolute(80, 80);
+//        //旋转 弧度
+//        image.setRotation(-55);
+//
+//        //旋转 角度
+//        image.setRotationDegrees(0);
+//        //依照比例缩放
+//        image.scalePercent(50);
+//
+//        PdfGState gs = new PdfGState();
+//        // 设置透明度为0.2
+//        gs.setFillOpacity(0.2f);
+//
+//
+//        PdfContentByte under;
+//        //给每一页加水印
+//        for (int i = 1; i < total; i++) {
+//            under = stamper.getUnderContent(i);
+//            under.beginText();
+//            // 添加水印图片
+//            under.addImage(image);
+//            under.setGState(gs);
+//        }
+//        stamper.close();
+//        reader.close();
+//        return outputFile;
+//    }
+//
+//    /**
+//     * 拆分原文件路径
+//     *
+//     * @param path 原文件路径
+//     * @return 拆分原路径的数组
+//     */
+//    public static String[] separatePath(String path) {
+//        if (StringUtils.isBlank(path)) {
+//            return null;
+//        }
+//        String[] sep = path.split("\\.");
+//        return new String[]{sep[0], sep[1]};
+//    }
+//
+//    /**
+//     * 设置表格单元格样式
+//     *
+//     * @param pdfCell 单元格对象
+//     */
+//    public static void setCellStyle(PdfPCell pdfCell) {
+//        pdfCell.setHorizontalAlignment(Element.ALIGN_CENTER);
+//        pdfCell.setVerticalAlignment(Element.ALIGN_MIDDLE);
+//        pdfCell.setBackgroundColor(new BaseColor(0xdd7e6b));
+//        pdfCell.setBorderWidthTop(0.1f);
+//        pdfCell.setBorderWidthBottom(0.1f);
+//        pdfCell.setBorderWidthLeft(0.1f);
+//        pdfCell.setBorderWidthRight(0.1f);
+//    }
+//
+//    /**
+//     * 设置pdf字体及大小
+//     *
+//     * @param type 标题或者内容 0-内容 1-标题
+//     * @return 字体
+//     * @throws Exception 异常
+//     */
+//    public static Font getPdfChineseFont(int type) throws Exception {
+//        // 使用系统字体
+//        BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
+//        Font font;
+//        if (type == 1) {
+//            font = new Font(bfChinese);
+//        } else {
+//            font = new Font(bfChinese, 18, Font.BOLD);
+//        }
+//
+//        return font;
+//    }
+//
+//    /**
+//     * 删除文件夹
+//     *
+//     * @param folderPath 文件路基
+//     */
+//    public static void delFolder(String folderPath) {
+//        // 删除完里面所有内容
+//        delAllFile(folderPath);
+//        String filePath = folderPath;
+//        filePath = filePath.toString();
+//        java.io.File myFilePath = new java.io.File(filePath);
+//        // 删除空文件夹
+//        myFilePath.delete();
+//    }
+//
+//    /**
+//     * 删除指定文件夹下所有文件
+//     *
+//     * @param path 文件路基
+//     * @return 是否成功
+//     */
+//    public static boolean delAllFile(String path) {
+//        boolean flag = false;
+//        File file = new File(path);
+//        String[] tempList = file.list();
+//        File temp = null;
+//        for (int i = 0; i < tempList.length; i++) {
+//            if (path.endsWith(File.separator)) {
+//                temp = new File(path + tempList[i]);
+//            } else {
+//                temp = new File(path + File.separator + tempList[i]);
+//            }
+//            if (temp.isFile()) {
+//                temp.delete();
+//            }
+//            if (temp.isDirectory()) {
+//                // 先删除文件夹里面的文件
+//                delAllFile(path + "/" + tempList[i]);
+//                // 再删除空文件夹
+//                delFolder(path + "/" + tempList[i]);
+//                flag = true;
+//            }
+//        }
+//        return flag;
+//    }
+//    public static boolean delAllFile(String path) {
+//        boolean flag = false;
+//        File file = new File(path);
+//        File temp = null;
+//
+//            if (temp.isFile()) {
+//                temp.delete();
+//            }
+//        return flag;
+//    }
+}
+

+ 53 - 0
huimv-eartag2-platform/huimv-eartag2-manage2/src/main/java/com/huimv/eartag2/manage2/utils/Print.java

@@ -0,0 +1,53 @@
+package com.huimv.eartag2.manage2.utils;
+
+import com.huimv.eartag2.manage2.pojo.EartagData2;
+import com.huimv.eartag2.manage2.vo.EarTagData2Vo;
+import org.jxls.common.Context;
+import org.jxls.util.JxlsHelper;
+
+import java.io.FileInputStream;
+import java.io.FileOutputStream;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.text.DecimalFormat;
+import java.text.SimpleDateFormat;
+import java.util.ArrayList;
+import java.util.List;
+
+public class Print {
+    /**
+     * 打印方法  不插入图片
+     *
+     * @return
+     * @throws Exception
+     */
+    public static void printDuck(List<EartagData2> eartagData2s) throws Exception {
+
+        //准备数据
+        String path = "G://"+eartagData2s.get(0).getEarmark()+".xls";
+        //获取模板文件,你自己当前模板的位置  我这里为当前项目下
+        InputStream is = new FileInputStream("G://getData.xlsx");
+        //根据模板生成的文件保存路径  我这里保存在本地D盘
+        OutputStream os = new FileOutputStream(path);
+        //绑定数据
+        Context context = new Context();
+        DecimalFormat def = new DecimalFormat("#.00");
+        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
+        List<EarTagData2Vo> list = new ArrayList<>();
+        for (EartagData2 eartagData2 : eartagData2s) {
+            EarTagData2Vo data2Vo = new EarTagData2Vo();
+            data2Vo.setAddTime(sdf.format(eartagData2.getAddTime()));
+            data2Vo.setDeviceCode(def.format(eartagData2.getEarTemp1()));
+            data2Vo.setTypeName(eartagData2.getEarmark());
+            data2Vo.setCount1(def.format(eartagData2.getEnvTemp1()));
+            data2Vo.setCount2(eartagData2.getAct1().toString());
+            data2Vo.setCount3(eartagData2.getBat());
+            list.add(data2Vo);
+        }
+        context.putVar("list", list);
+
+        //生成
+        JxlsHelper.getInstance().processTemplate(is, os, context);
+    }
+
+}

+ 24 - 0
huimv-eartag2-platform/huimv-eartag2-manage2/src/main/java/com/huimv/eartag2/manage2/vo/EarTagData2Vo.java

@@ -0,0 +1,24 @@
+package com.huimv.eartag2.manage2.vo;
+
+import com.fasterxml.jackson.annotation.JsonFormat;
+import lombok.Data;
+
+import java.math.BigDecimal;
+import java.util.Date;
+
+@Data
+public class EarTagData2Vo {
+
+
+    private String addTime;
+
+    private String typeName;
+
+    private String deviceCode;
+
+    private String count1;
+
+    private String count2;
+
+    private Integer count3;
+}