#
whycq
2022-09-12 964c095a69cc70115b6c5c185c0682aeb68da5ab
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
package zy.cloud.wms.manager.service.impl;
 
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.core.common.Cools;
import com.core.common.R;
import com.core.exception.CoolException;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import zy.cloud.wms.manager.entity.Item;
import zy.cloud.wms.manager.mapper.ItemReportMapper;
import zy.cloud.wms.manager.entity.ItemReport;
import zy.cloud.wms.manager.service.ItemReportService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import zy.cloud.wms.manager.service.ItemService;
 
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;
import java.util.Date;
 
@Service("itemReportService")
public class ItemReportServiceImpl extends ServiceImpl<ItemReportMapper, ItemReport> implements ItemReportService {
    @Autowired
    private ItemService itemService;
 
    /**
     * 初始化生成report
     * @param report
     * @param userId
     */
    @Override
    public void init(ItemReport report, Long userId) {
        /**
         * 控管与参数初始化
         */
        Date now = new Date();
        Item id = itemService.selectOne(new EntityWrapper<Item>()
                .eq("id", report.getItemId()));
        if (Cools.isEmpty(id)) {
            throw new CoolException("找不到该项目,请联系管理员");
        }
        report.setItemName(id.getName());
 
 
        /**
         * 如果前端返回原因,表示有异常,将状态设置为true
         */
        if (report.getDeliverReason() == null || report.getDeliverReason().equals("")) {
            report.setDeliverIssue(false);
        }else {
            report.setDeliverIssue(true);
        }
 
        if (report.getQualityReason() == null || report.getQualityReason().equals("")) {
            report.setQualityIssue(false);
        }else {
            report.setQualityIssue(true);
        }
 
        if (report.getDesignReason() == null || report.getDesignReason().equals("")) {
            report.setDesignIssue(false);
        }else {
            report.setDesignIssue(true);
        }
 
        if (report.getInstallReason() == null || report.getInstallReason().equals("")) {
            report.setInstallIssue(false);
        }else {
            report.setInstallIssue(true);
        }
        report.setUpdateTime(now);
        report.setUpdateBy(userId);
        report.setCreateTime(now);
        report.setCreateBy(userId);
        insert(report);
    }
 
    @Override
    public HttpServletResponse makeReport(Item item, ItemReport itemReport, HttpServletResponse response) {
 
        FileInputStream in = null;
        ServletOutputStream out = null;
        try {
            /**
             * 一些初始化
             */
            in = new FileInputStream("src/main/resources/excel/analysis_report.xlsx");
 
            XSSFWorkbook wb = new XSSFWorkbook(in);
            XSSFSheet mainSheet = wb.getSheet("报告");
            /**
             * 开始设置第10和11合并行的所有列, 角标从0开始,rownum9实际上等于10
             */
            XSSFRow row10 = mainSheet.getRow(9);
            row10.getCell(1).setCellValue(item.getUuid());
            row10.getCell(3).setCellValue(item.getEndTime0$());
            row10.getCell(5).setCellValue(item.getRealEndTime0$());
 
            XSSFRow row1 = mainSheet.getRow(0);
            row1.getCell(0).setCellValue(item.getName()+"订单总结分析报告");
 
            /**
             * 开始设置第12行
             */
            XSSFRow row12 = mainSheet.getRow(11);
            row12.getCell(1).setCellValue(item.getUuid());
            row12.getCell(3).setCellValue(item.getPlandeAmt());
            row12.getCell(5).setCellValue(item.getRealdeAmt());
 
            /**
             *开始设置第14行
             */
            XSSFRow row14 = mainSheet.getRow(13);
            row14.getCell(1).setCellValue(item.getName());
            row14.getCell(3).setCellValue(item.getPlaninAmt());
            row14.getCell(5).setCellValue(item.getRealinAmt());
 
            /**
             * 第16行
             */
            XSSFRow row16 = mainSheet.getRow(15);
            row16.getCell(1).setCellValue(item.getType$());
 
            /**
             * 第18行
             */
            XSSFRow row17 = mainSheet.getRow(17);
            row17.getCell(1).setCellValue(item.getSalesman());
            row17.getCell(2).setCellValue(itemReport.getIssue());
 
 
            /**
             * 第20行
             */
            XSSFRow row19 = mainSheet.getRow(19);
            row19.getCell(1).setCellValue(item.getLeader());
 
            /**
             * 第22行
             */
            XSSFRow row22 = mainSheet.getRow(21);
            row22.getCell(1).setCellValue(item.getOriginArea());
 
            /**
             * 第29行
             */
            XSSFRow row29 = mainSheet.getRow(28);
            row29.getCell(1).setCellValue(itemReport.getDeliverIssue$());
            row29.getCell(2).setCellValue(itemReport.getDeliverReason());
 
            /**
             * 第31行
             */
            XSSFRow row31 = mainSheet.getRow(30);
            row31.getCell(1).setCellValue(itemReport.getQualityIssue$());
            row31.getCell(2).setCellValue(itemReport.getQualityReason());
 
            /**
             * 33行
             */
 
            XSSFRow row33 = mainSheet.getRow(32);
            row33.getCell(1).setCellValue(itemReport.getDesignIssue$());
            row33.getCell(2).setCellValue(itemReport.getDesignReason());
            /**
             * 35行
             */
 
            XSSFRow row35 = mainSheet.getRow(34);
            row35.getCell(1).setCellValue(itemReport.getInstallIssue$());
            row35.getCell(2).setCellValue(itemReport.getInstallReason());
            /**
             * 第45行
             */
            XSSFRow row45 = mainSheet.getRow(44);
            row45.getCell(1).setCellValue(itemReport.getConclusion());
 
 
//            wb.write(out);
 
            out = response.getOutputStream();
 
            response.setContentType("application/octet-stream; charset=utf-8");
 
            response.setHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode(item.getName()+"报告.xlsx"));
 
            ServletOutputStream outputStream = response.getOutputStream();
            wb.write(outputStream);
 
 
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                in.close();
                out.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
 
        return response;
    }
 
}