WangHan
2025-04-02 a8ba678a3fe5a39da2c732014cebbb66e408e97c
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
package com.consum.base.controller;
 
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import org.apache.poi.ss.usermodel.Workbook;
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 com.consum.base.BaseController;
import com.consum.base.core.utils.CommonUtil;
import com.consum.base.pojo.LDeptFormScrappedParam;
import com.consum.base.pojo.query.LDeptFormScrappedQry;
import com.consum.base.service.DepFormScrappedService;
import com.consum.model.po.FinSysTenantUser;
import com.walker.db.page.GenericPager;
import com.walker.infrastructure.utils.CollectionUtils;
import com.walker.infrastructure.utils.DateUtils;
import com.walker.infrastructure.utils.StringUtils;
import com.walker.web.ResponseValue;
 
import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.TemplateExportParams;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.ReflectUtil;
 
/**
 * @ClassName DepFormScrappedController
 * @Author cy
 * @Date 2023/11/30
 * @Description
 * @Version 1.0
 **/
// @Api(value = "部门报废", tags = "部门报废")
@RestController
@RequestMapping("/pc/l/wh/form/scrapped")
public class DepFormScrappedController extends BaseController {
    @Resource
    private DepFormScrappedService depFormScrappedService;
 
    // @ApiOperation(value = "新增报废单", notes = "新增报废单")
    @PostMapping("/deptAdd")
    public ResponseValue deptAdd() throws Exception {
        LDeptFormScrappedParam param = CommonUtil.getObjFromReqBody(LDeptFormScrappedParam.class);
        LDeptFormScrappedParam query = new LDeptFormScrappedParam();
        CommonUtil.copyProperties(param, query);
        param = query;
 
        FinSysTenantUser sysInfo = this.getSysInfo();
 
        depFormScrappedService.add(param, this.getCurrentUser(), sysInfo);
        return ResponseValue.success();
    }
 
    /**
     * @Description 列表查询 1.查询报废单 2.查询报废单物品
     * @Date
     */
    // @ApiOperation(value = "列表查询", notes = "列表查询")
//    @ApiImplicitParams({
//        @ApiImplicitParam(name = "param", value = "查询条件", dataType = "LWhFormScrappedQry", paramType = "query")})
    @GetMapping("/deptList")
    public ResponseValue queryList() {
        LDeptFormScrappedQry param = CommonUtil.getObjFromReq(LDeptFormScrappedQry.class);
        LDeptFormScrappedQry query = new LDeptFormScrappedQry();
        CommonUtil.copyProperties(param, query);
        param = query;
 
        FinSysTenantUser sysInfo = getSysInfo();
        if (sysInfo == null) {
            return ResponseValue.error("登录用户信息不存在");
        }
        param.setAgencyId(sysInfo.getTenantId());
 
        GenericPager<Map<String, Object>> mapGenericPager = depFormScrappedService.queryList(param);
        // 处理一下数据
        List<Map<String, Object>> datas = mapGenericPager.getDatas();
        if (CollectionUtils.isEmpty(datas)) {
            return ResponseValue.success(mapGenericPager);
        }
        List<Map<String, Object>> maps = mergeData(datas);
        ReflectUtil.setFieldValue(mapGenericPager, "datas", maps);
        return ResponseValue.success(mapGenericPager);
    }
 
    private static List<Map<String, Object>> mergeData(List<Map<String, Object>> dataList) {
        Map<Long, Map<String, Object>> mergedDataMap = new HashMap<>();
        // 遍历原始数据进行合并
        for (Map<String, Object> data : dataList) {
            Long id = Convert.toLong(data.get("id"));
            HashMap hashMap = new HashMap();
            hashMap.put("baseGoodsTemplateId", data.get("operatorName"));
            hashMap.put("baseGoodsTemplateName", data.get("baseGoodsTemplateName"));
            hashMap.put("totalCount", data.get("totalCount"));
            hashMap.put("unit", data.get("unit"));
 
            if (!mergedDataMap.containsKey(id)) {
                // 如果id在合并数据中不存在,则新增一条记录
                Map<String, Object> mergedMap = new HashMap<>();
                mergedMap.put("departmentName", data.get("departmentName"));
                mergedMap.put("dealTime", data.get("dealTime"));
                mergedMap.put("businessFormCode", data.get("businessFormCode"));
                mergedMap.put("operatorName", data.get("operatorName"));
                mergedMap.put("id", data.get("id"));
                ArrayList<HashMap> goodsList = new ArrayList<>();
                goodsList.add(hashMap);
                mergedMap.put("goods", goodsList);
                mergedDataMap.put(id, mergedMap);
            } else {
                // 如果id在合并数据中已存在,则将k3追加到已存在记录的k3列表中
                Map<String, Object> mergedMap = mergedDataMap.get(id);
                ArrayList<HashMap> goodsList = (ArrayList<HashMap>)mergedMap.get("goods");
                goodsList.add(hashMap);
            }
        }
        // 将合并后的数据转换为List返回
        return new ArrayList<>(mergedDataMap.values());
    }
 
    /**
     * @Description 根据id查询详情
     */
    // @ApiOperation(value = "根据id查询详情", notes = "根据id查询详情")
    // @ApiImplicitParams({@ApiImplicitParam(name = "id", value = "报废单id", dataType = "Long", paramType = "query")})
    @GetMapping("/deptDetail")
    public ResponseValue getById(Long id) {
        if (id == null) {
            return ResponseValue.error("报废单id为空");
        }
        return ResponseValue.success(this.depFormScrappedService.getById(id));
    }
 
    // @ApiOperation(value = "导出报废单", notes = "导出报废单")
    // @ApiImplicitParams({@ApiImplicitParam(name = "id", value = "报废单id", dataType = "Long", paramType = "query")})
    @GetMapping("/deptList/export")
    public ResponseValue<String> export(Long id, HttpServletResponse response) throws Exception {
        if (id == null) {
            throw new RuntimeException("报废单id为空");
        }
        FinSysTenantUser sysInfo = getSysInfo();
        if (sysInfo == null) {
            throw new RuntimeException("登录用户信息不存在");
        }
        List<Map<String, Object>> export = depFormScrappedService.export(id, sysInfo.getTenantId());
        if (CollectionUtils.isEmpty(export)) {
            throw new RuntimeException("数据为空");
        }
        TemplateExportParams exportParams = new TemplateExportParams("import/deptScrapped.xlsx");
        exportParams.setHeadingStartRow(2);
        Map<String, Object> scrappedInfoExcelTemplate = export.get(0);
 
        Long dealTime = Convert.toLong(scrappedInfoExcelTemplate.get("dealTime"));
        String operatorName = Convert.toStr(scrappedInfoExcelTemplate.get("operatorName"));
        String businessFormCode = Convert.toStr(scrappedInfoExcelTemplate.get("businessFormCode"));
 
        Map<String, Object> map = new HashMap<>();
        map.put("code", businessFormCode);
        map.put("date", DateUtils.toShowDate(dealTime));
        map.put("name", operatorName);
        map.put("dataList", export);
 
        Workbook workbook = ExcelExportUtil.exportExcel(exportParams, map);
        // 设置响应头
//        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8");
//        response.setHeader("Content-disposition", "attachment;filename=" + URLEncoder.encode("部门报废单.xlsx", "utf-8"));
//        try (OutputStream outputStream = response.getOutputStream()) {
//            workbook.write(outputStream);
//            workbook.close();
//        }
        String filePath = downLoadExcel("采购入库单", workbook);
        return ResponseValue.success("导出成功", filePath);
 
    }
 
    @GetMapping("/deptListByModel")
    public ResponseValue export() {
        LDeptFormScrappedQry param = CommonUtil.getObjFromReq(LDeptFormScrappedQry.class);
        LDeptFormScrappedQry query = new LDeptFormScrappedQry();
        CommonUtil.copyProperties(param, query);
        param = query;
 
        FinSysTenantUser sysInfo = getSysInfo();
        if (sysInfo == null) {
            throw new RuntimeException("登录用户信息不存在");
        }
        /*当前登录人只能看到自己机构下的列表*/
        String tenantId = sysInfo.getTenantId();
        String paramAgencyId = param.getAgencyId();
        if (StringUtils.isEmpty(paramAgencyId) || !paramAgencyId.startsWith(tenantId)) {
            param.setAgencyId(tenantId);
        }
 
        List<Map<String, Object>> export = depFormScrappedService.listByModel(param);
        return ResponseValue.success(export);
    }
 
    @GetMapping("/deptListByModel2")
    public ResponseValue deptListByModel2() {
        LDeptFormScrappedQry param = CommonUtil.getObjFromReq(LDeptFormScrappedQry.class);
        LDeptFormScrappedQry query = new LDeptFormScrappedQry();
        CommonUtil.copyProperties(param, query);
        param = query;
 
        FinSysTenantUser sysInfo = getSysInfo();
        if (sysInfo == null) {
            throw new RuntimeException("登录用户信息不存在");
        }
        /*当前登录人只能看到自己机构下的列表*/
        String tenantId = sysInfo.getTenantId();
        String paramAgencyId = param.getAgencyId();
        if (StringUtils.isEmpty(paramAgencyId) || !paramAgencyId.startsWith(tenantId)) {
            param.setAgencyId(tenantId);
        }
        return ResponseValue.success(depFormScrappedService.listByModel2(param));
    }
 
}