fei.wang
2024-04-15 61747a14819075bf6da2c1597b6c22e22e026686
src/main/java/com/flow/controller/ZongInvoicesController.java
@@ -5,23 +5,45 @@
package com.flow.controller;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.flow.mapper.zongInvoicesMapper;
import com.flow.pojo.Manager;
import com.flow.pojo.OperationLog;
import com.flow.pojo.TbFuwuqi;
import com.flow.pojo.Zonginvoices;
import com.flow.service.zongInvoicesService;
import com.flow.util.result;
import com.flow.util.resultutil;
import com.github.pagehelper.PageInfo;
import java.io.*;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Random;
import java.util.UUID;
import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@RestController
@RequestMapping({"/hxzk"})
public class ZongInvoicesController {
    @Autowired
    zongInvoicesService zongInvoicesService;
    @Autowired
    zongInvoicesMapper zongInvoicesMapper;
    public ZongInvoicesController() {
    }
@@ -48,8 +70,208 @@
        return this.zongInvoicesService.findsqnum(zonginvoices);
    }
    @PostMapping({"UpdateKaiPiao"})
    public ModelAndView UpdateKaiPiao(Zonginvoices zonginvoices) {
        this.zongInvoicesService.UpdateKaiPiao(zonginvoices);
//        QueryWrapper queryWrapper = new QueryWrapper();
//        queryWrapper.eq("id", tbFuwuqi.getId());
//        TbFuwuqi tbFuwuqi1 = tbFuwuqiMapper.selectOne(queryWrapper);
//        OperationLog operationLog = new OperationLog();
//        operationLog.setCaozuotype("修改");
//        operationLog.setCaozuocontent("修改服务器:"+tbFuwuqi1.getServername());
//        operationLogService.addOperationLog(operationLog);
//        Date date = new Date();
//        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//        operationLog.setCaozuodate(formatter.format(date));
//        operationLogMapper.insert(operationLog);
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("/page/ChongZhi.jsp");
        return modelAndView;
    }
    @PostMapping({"DeleteKaiPiao"})
    public int DeleteKaiPiao(String kaipiaoId) {
        return this.zongInvoicesService.DeleteKaiPiao(kaipiaoId);
    }
    @PostMapping({"adddanhao"})
    public int adddanhao(Zonginvoices zonginvoices) {
        return this.zongInvoicesService.adddanhao(zonginvoices);
    }
    @PostMapping({"ZonginvoicesSearch"})
    public result<List<Zonginvoices>> tableSearch(Zonginvoices manager, int page, int limit) throws Exception {
        PageInfo<Zonginvoices> cz = this.zongInvoicesService.tableSearch(manager, page, limit);
//        for(int i = 0; i < cz.getSize(); ++i) {
//            if (((Manager)cz.getList().get(i)).getCompany() != null) {
//                SimpleDateFormat date2 = new SimpleDateFormat("yyyy-MM-dd");
//                Date date = new Date();
//                Date endDay = date2.parse(cz.getList().get(i).getEnddate());//结束时间
//                Long starTime = date.getTime();
//                Long endTime = endDay.getTime();
//                Long num11 = endTime - starTime;//时间戳
//
////                if ((num11/24/60/60/1000)<0){
////                    ((Manager)cz.getList().get(i)).setRemainingdays("0天");
////                }else{
////                    ((Manager)cz.getList().get(i)).setRemainingdays(+num11/24/60/60/1000+"天");
////                }
////                this.cardService.UpdateCard((Card)cz.getList().get(i));
////                this.cardService.UpdateCard((Card)cz.getList().get(i));
//            }
//        }
        return resultutil.returnSuccess(cz.getTotal(), cz.getList());
    }
    /**
     *  文档上传
     */
    @ResponseBody
    @PostMapping("/saveDocument")
    public ModelAndView saveDocument(MultipartFile file, HttpServletRequest request,Zonginvoices zonginvoices) throws IOException {
        System.out.println("打印数据=========》》》》》》》》》》》》》》》");
        String filename = this.AddXcd(file,request);
        System.out.println(filename );
//        System.out.println("doc=" + doc);
        //1.获取原始文件名
//        String uploadFileName = file.getOriginalFilename();
//        System.out.println("要上传的原始文件名字是:" + uploadFileName);
//        //2.截取文件扩展名
//        String extendName = uploadFileName.substring(uploadFileName.lastIndexOf(".") + 1, uploadFileName.length());
//        //3.把文件加上随机数,防止文件重复
//        String uuid = UUID.randomUUID().toString().replace("-", "").toUpperCase();
//        //4.生成的新的服务器文件名
//        String newfileName = uuid + "." + extendName;
//        System.out.println("生成的新的服务器文件名是:" + newfileName); //防止服务器文件名重名
//        //5.获取真实的服务器文件上传地址 (文件的存储位置)
//        String filePath = request.getServletContext().getRealPath("WEB-INF") + "\\" + newfileName;
//        System.out.println("服务器运行目录,最终上传的路径:" + filePath);
//        //6.执行上传
//        file.transferTo(new File(filePath));
//        System.out.println("文件上传结束!");
//        //调用文件上传工具类 上传文件
        zonginvoices.setDanhao(filename);
        this.zongInvoicesService.UpdateKaiPiao(zonginvoices);
//       FileUpLoadUtils.Fileupload(file, request, doc, docService);
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("/page/KaiPiaoJiLu.jsp");
        return modelAndView;
//        return t
    }
    public static String AddXcd(MultipartFile file,HttpServletRequest request) throws IOException {
        // 获取图片的原始文件名称
        String oldName=file.getOriginalFilename();
        //获取服务器路径:upload    getRealPath:获取一个相对路径的真实路径
//        String path="D:\\HxzkFlow\\HxzkFlow\\HxzkFlow\\src\\webapp\\WEB-INF";
        String path="C:\\Users\\Administrator\\Desktop\\nginx\\html\\apache-tomcat-7.0.70\\webapps\\HxzkFlow\\WEB-INF";
        //文件改名================================
        //1.截取后缀格式.png .jpg
        String suffix=null;
        if (oldName.length()>0){
            suffix= oldName.substring(oldName.lastIndexOf("."));
        }
        //2.生成新的文件名
        SimpleDateFormat dateFormat=new SimpleDateFormat("yyyyMMddHHmmssSSS");
        String dateStr=dateFormat.format(new Date());
        //生成随机数
        Random random=new Random();
        int max=1000000;
        int min=100000;
        int rand= random.nextInt(max);
        while (rand<min){
            rand= random.nextInt(max);
        }
        //生成新的文件名称
        String newName=dateStr.concat(rand+"").concat(suffix);
        /*File.separator:是系统自动判定分隔符(不同的操作系统中 路径分隔符不一样  但是java语言是跨平台的 所以就需要 统一 分隔符)*/
        File file1=new File(path+File.separator+newName);
        //把我们上次的文件通过transferTo写入本地文件
        file.transferTo(file1);
        return newName;
    }
    @ResponseBody
    @RequestMapping("/down")
    public void down( Integer id,HttpServletRequest request, HttpServletResponse response) throws Exception{
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("sqnum", id);
        Zonginvoices zonginvoices =  zongInvoicesMapper.selectOne(queryWrapper);
        //模拟文件,myfile.txt为需要下载的文件
        String fileName = request.getSession().getServletContext().getRealPath("WEB-INF")+"/"+zonginvoices.getDanhao();
        //获取输入流
        InputStream bis = new BufferedInputStream(new FileInputStream(new File(fileName)));
        //假如以中文名下载的话
        String filename = zonginvoices.getDanhao();
        //转码,免得文件名中文乱码
        filename = URLEncoder.encode(filename,"UTF-8");
        //设置文件下载头
        response.addHeader("Content-Disposition", "attachment;filename=" + filename);
        //1.设置文件ContentType类型,这样设置,会自动判断下载文件类型
        response.setContentType("multipart/form-data");
        BufferedOutputStream out = new BufferedOutputStream(response.getOutputStream());
        int len = 0;
        while((len = bis.read()) != -1){
            out.write(len);
            out.flush();
        }
        out.close();
    }
    /**
     * 下载文档
     */
    @ResponseBody
    @RequestMapping("/downloadDocument")
    public ResponseEntity<byte[]> downloadDocument(Integer id, HttpServletRequest request) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("sqnum", id);
        Zonginvoices zonginvoices =  zongInvoicesMapper.selectOne(queryWrapper);
        //获得该doc对象
//        Doc doc = docService.getById(id);
        //用文件下载工具类 下载文件
        ResponseEntity<byte[]> responseEntity = this.FileDownLoad(id, zonginvoices, request);
        return responseEntity;
    }
    public static ResponseEntity<byte[]> FileDownLoad(Integer id, Zonginvoices zonginvoices ,HttpServletRequest request) {
        //获得要下载的地址
//        String path = request.getSession().getServletContext().getRealPath("D:\\HxzkFlow\\HxzkFlow\\HxzkFlow\\src\\webapp\\WEB-INF");
        String path = request.getSession().getServletContext().getRealPath("D:\\HxzkFlow\\HxzkFlow\\HxzkFlow\\src\\webapp\\WEB-INF\\")+zonginvoices.getDanhao();
        File file = new File(path, zonginvoices.getDanhao());
        if (file.exists()) {
            try {
                //设置请求头
                HttpHeaders headers = new HttpHeaders();
                /**
                 *  1.用title(存在中文)为文件名,可能会有乱码问题
                 *
                 *  2.用fileName(不存在中文 )为文件名,不会有乱码问题
                 *  如:  String downName = new String(doc.getFileName().getBytes("utf-8"), "ISO-8859-1");
                 *  ---要保证没有乱码问题可第二种方式
                 *
                 */
                //文件下载的名字, 可能存在乱码问题
                String newFileName = zonginvoices.getDanhao().split("\\.")[1];
                String downName = new String(newFileName.getBytes("utf-8"), "ISO-8859-1");
                // 设置以附件的形式下载
                headers.setContentDispositionFormData("attachment", downName);
                // 设置文件内容以流的形式来下载
                headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
                // 用springmvc 提供的下载方式
                ResponseEntity<byte[]> responseEntity = new ResponseEntity<byte[]>(FileUtils.readFileToByteArray(file), headers, HttpStatus.OK);
                return responseEntity;
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;
    }
}