#
mrzhssss
2022-03-24 4c1e8761e3fa0516d5e6d316e838e83c8e0f5edf
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
package zy.cloud.wms.manager.controller;
 
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.write.style.column.LongestMatchColumnWidthStyleStrategy;
import com.baomidou.mybatisplus.plugins.Page;
import com.core.annotations.ManagerAuth;
import com.core.common.Cools;
import com.core.common.R;
import com.core.exception.CoolException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import zy.cloud.wms.common.utils.VersionUtils;
import zy.cloud.wms.common.web.BaseController;
import zy.cloud.wms.manager.entity.LocDetl;
import zy.cloud.wms.manager.entity.Mat;
import zy.cloud.wms.manager.entity.Pakin;
import zy.cloud.wms.manager.service.LocDetlService;
import zy.cloud.wms.manager.service.MatService;
import zy.cloud.wms.manager.service.PakinService;
 
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;
 
/**
 * Created by vincent on 2021/4/2
 */
@RestController
public class StatisController extends BaseController {
 
    @Autowired
    private MatService matService;
    @Autowired
    private LocDetlService locDetlService;
    @Autowired
    private PakinService pakinService;
 
    // 库存统计 ------------------------------------------------------------------------------------------
 
    @RequestMapping(value = "/stock/statis/auth")
    @ManagerAuth
    public R stockStatis(@RequestParam(defaultValue = "1")Integer curr,
                    @RequestParam(defaultValue = "10")Integer limit,
                    @RequestParam Map<String, Object> param) {
        Long hostId = getHostId();
        if (hostId != null) {
            param.put("host_id", hostId);
        }
        Page<LocDetl> stockStatis = locDetlService.getStockStatis(toPage(curr, limit, param, LocDetl.class));
        for (LocDetl locDetl : stockStatis.getRecords()) {
            Mat mat = matService.selectByMatnr(hostId, locDetl.getMatnr());
            if (mat != null) {
                VersionUtils.setLocDetl(locDetl, mat);
            }
        }
        return R.ok().add(stockStatis);
    }
 
    @RequestMapping(value = "/stock/statis/export")
    public void stockStatisExport(HttpServletResponse response, @RequestParam String token) throws IOException {
        Long hostId = getHostId(token);
        List<LocDetl> excel = locDetlService.getStockStatisExcel(hostId);
        for (LocDetl locDetl : excel) {
            Mat mat = matService.selectById(locDetl.getMatnr());
            if (mat != null) {
                VersionUtils.setLocDetl(locDetl, mat);
            }
        }
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        String fileName = URLEncoder.encode("库存明细统计报表", "UTF-8");
        response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
        EasyExcel.write(response.getOutputStream(), LocDetl.class)
                .registerWriteHandler(new LongestMatchColumnWidthStyleStrategy())
                .sheet("sheet1")
                .doWrite(excel);
    }
 
    // 月入库统计 ------------------------------------------------------------------------------------------
 
    @RequestMapping(value = "/pakin/statis/auth")
    @ManagerAuth
    public R pakinStatis(@RequestParam(defaultValue = "1")Integer curr,
                         @RequestParam(defaultValue = "10")Integer limit,
                         @RequestParam Map<String, Object> param) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        if (Cools.isEmpty(param.get("year"))) {
            param.remove("month");
            param.put("year", calendar.get(Calendar.YEAR));
        }
        if (Cools.isEmpty(param.get("month"))) {
            param.put("month", calendar.get(Calendar.MONTH) + 1);
        }
        Long hostId = getHostId();
        if (hostId != null) {
            param.put("host_id", hostId);
        }
        Page<Pakin> pakinStatis = pakinService.getPakinStatis(toPage(curr, limit, param, Pakin.class));
        for (Pakin pakin : pakinStatis.getRecords()) {
            Mat mat = matService.selectByMatnr(hostId, pakin.getMatnr());
            if (mat != null) {
                VersionUtils.setPakin(pakin, mat);
            }
        }
        return R.ok().add(pakinStatis);
    }
 
    @RequestMapping(value = "/pakin/statis/export")
    public void pakinStatisExport(@RequestParam("year") Integer year,
                                  @RequestParam("month") Integer month,
                                  @RequestParam String token,
                                  HttpServletResponse response) throws IOException {
        if (Cools.isEmpty(year, month)) {
            throw new CoolException("年月不能为空");
        }
        Long hostId = getHostId(token);
        List<Pakin> excel = pakinService.getPakinStatisExcel(year, month, hostId);
        for (Pakin pakin : excel) {
            Mat mat = matService.selectByMatnr(hostId, pakin.getMatnr());
            if (mat != null) {
                VersionUtils.setPakin(pakin, mat);
            }
        }
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        String fileName = URLEncoder.encode(year+"年" + month + "月入库统计报表", "UTF-8");
        response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
        EasyExcel.write(response.getOutputStream(), Pakin.class)
                .registerWriteHandler(new LongestMatchColumnWidthStyleStrategy())
                .sheet(year + "-" + month)
                .doWrite(excel);
    }
 
}