fei.wang
2024-05-13 19fe5a3d816184549e8ffa011c39f218734da8d5
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//
 
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 com.tencentcloudapi.common.exception.TencentCloudSDKException;
import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
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() {
    }
 
    @GetMapping({"zongkaipiao"})
    public result<List<Zonginvoices>> findinvoicesjilu(Integer page, Integer limit) {
        PageInfo<Zonginvoices> cz = this.zongInvoicesService.findinvoicesjilu(page, limit);
        return resultutil.returnSuccess(cz.getTotal(), cz.getList());
    }
 
    @GetMapping({"zongkaipiaoZi"})
    public result<List<Zonginvoices>> findinvoicesjilu(Integer page, Integer limit, Zonginvoices zonginvoices) {
        PageInfo<Zonginvoices> cz = this.zongInvoicesService.findinvoicesjilu(page, limit, zonginvoices);
        return resultutil.returnSuccess(cz.getTotal(), cz.getList());
    }
 
    @PostMapping({"zongfpadd"})
    public int zongfpadd(Zonginvoices zonginvoices) throws TencentCloudSDKException {
        return this.zongInvoicesService.zongfpadd(zonginvoices);
    }
 
    @PostMapping({"findsqnum"})
    public Zonginvoices findsqnum(Zonginvoices zonginvoices) {
        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;
    }
}