yanghb
2024-12-24 e3cf8e216328e113d7cf05cba34f5a0af3865338
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
package com.zzg.web.controller.state;
 
import com.alibaba.excel.EasyExcelFactory;
import com.alibaba.excel.write.builder.ExcelWriterBuilder;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.github.pagehelper.PageInfo;
import com.zzg.common.constant.state.UrlConstants;
import com.zzg.common.core.domain.AjaxResult;
import com.zzg.common.core.domain.entity.state.StateSettlement;
import com.zzg.common.core.domain.entity.system.SysDept;
import com.zzg.common.enums.SettledProcessEnum;
import com.zzg.common.enums.SubmitStatusEnum;
import com.zzg.common.exception.GlobalException;
import com.zzg.common.utils.file.FileUtils;
import com.zzg.system.convert.StateProjectConvert;
import com.zzg.system.convert.easyExcel.MultiDropdownWriteHandler;
import com.zzg.system.domain.bo.*;
import com.zzg.system.domain.vo.*;
import com.zzg.system.service.state.StateHouseholdService;
import com.zzg.system.service.state.StateSettlementService;
import com.zzg.system.service.system.ISysDeptService;
import com.zzg.web.core.enums.TemplateFileTypeEnum;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
 
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.Method;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;
 
import static com.zzg.system.service.state.impl.StateSettlementImpl.extractNumber;
 
@RequiredArgsConstructor
@RestController
@Slf4j
public class StateSettlementController {
 
    private final StateSettlementService settlementService;
    private final ISysDeptService deptService;
 
    @PostMapping(UrlConstants.STATE_SETTLEMENT_SAVE)
    public AjaxResult<List<StateSettlement>> saveSettlement(@RequestBody List<StateSettlement> stateSettlement) {
        settlementService.saveOrUpdateSettlement(stateSettlement);
        return AjaxResult.success(stateSettlement);
    }
 
    @GetMapping(UrlConstants.STATE_SETTLEMENT_DEL)
    public AjaxResult<Boolean> delSettlement(@RequestParam String settlementId) {
        settlementService.deleteSettlement(settlementId);
        return AjaxResult.success(true);
    }
 
    //安置详情
    @PostMapping(UrlConstants.STATE_SETTLEMENT_DETAIL_LIST)
    public AjaxResult<SettlementDetailPageVO> listSettlementDetail(@RequestBody SettlementDetailBO settlementBO) {
        return AjaxResult.success(settlementService.listDetailData(settlementBO));
    }
 
    //安置详情
    @PostMapping(UrlConstants.STATE_SETTLEMENT_DETAIL_UPDATE_LIST)
    public AjaxResult<SettlementDetailPageVO> listSettlementDetailUpdate(@RequestBody SettlementDetailBO settlementBO) {
        return AjaxResult.success(settlementService.listDetailUpdateData(settlementBO));
    }
 
    //外层项目实施数据
    @GetMapping(UrlConstants.STATE_SETTLEMENT_SUM)
    public AjaxResult<SettlementSumVO> sumSettled(@RequestParam String projectId) {
        return AjaxResult.success(settlementService.sumSettleData(projectId));
    }
 
    @PostMapping(UrlConstants.STATE_SETTLEMENT_LIST)
    public AjaxResult<PageInfo<HouseholdVO>> listSettlement(@RequestBody SettlementBO settlementBO) {
        return AjaxResult.success(settlementService.listSettleData(settlementBO));
    }
 
    @PostMapping(UrlConstants.STATE_SETTLEMENT_EXPORT)
    public AjaxResult<Void> exportSettlement(@RequestBody SettlementBO settlementBO, HttpServletResponse response) {
        try {
            List<HouseholdVO> result = settlementService.exportSettleData(settlementBO, response);
            if (Objects.equals(settlementBO.getIsSettled(), SettledProcessEnum.NOT_SETTLE.getValue())) {
                List<NotResettlementHouseholdExportVO> exportVOList = StateProjectConvert.INSTANCE.toNotResettlementHouseholdExportVO(result);
                List<String> deptIdList = exportVOList.stream()
                        .map(NotResettlementHouseholdExportVO::getDeptId)
                        .collect(Collectors.toList());
                Map<String, SysDept> stringSysDeptMap = deptService.selectMapDeptById(deptIdList);
                Collections.reverse(exportVOList);
                for (int i = 0; i < exportVOList.size(); i++) {
                    NotResettlementHouseholdExportVO exportVO = exportVOList.get(i);
                    if (StringUtils.isNoneBlank(exportVO.getAgreeMoveStr())) {
                        if (exportVO.getAgreeMoveStr().contains("同意")) {
                            exportVO.setAgreeMoveStr("是");
                        } else if (exportVO.getAgreeMoveStr().contains("拒绝")) {
                            exportVO.setAgreeMoveStr("否");
                        }
                    } else {
                        exportVO.setAgreeMoveStr("否");
                    }
                    exportVO.setIndex(String.valueOf(i + 1));
                    SysDept sysDept = stringSysDeptMap.get(exportVO.getDeptId());
                    if (Objects.nonNull(sysDept)) {
                        exportVO.setBelongingStreetTown(sysDept.getDeptName());
                    }
                }
 
                FileUtils.setExcelResponseHeader(response, "未安置情况导出.xlsx");
                EasyExcelFactory.write(response.getOutputStream(), NotResettlementHouseholdExportVO.class)
                        .sheet("sheet")
                        .doWrite(exportVOList);
            } else if (Objects.equals(settlementBO.getIsSettled(), SettledProcessEnum.SETTLE.getValue())) {
                List<ResettlementHouseholdExportVO> exportVOList = StateProjectConvert.INSTANCE.toResettlementHouseholdExportVO(result);
                List<String> deptIdList = exportVOList.stream()
                        .map(ResettlementHouseholdExportVO::getDeptId)
                        .collect(Collectors.toList());
                Map<String, SysDept> stringSysDeptMap = deptService.selectMapDeptById(deptIdList);
                exportVOList.sort((o1, o2) -> {
                    int num1 = extractNumber(o1.getSettleName());
                    int num2 = extractNumber(o2.getSettleName());
                    return Integer.compare(num1, num2);
                });
//                Collections.reverse(exportVOList);
                for (int i = 0; i < exportVOList.size(); i++) {
                    ResettlementHouseholdExportVO exportVO = exportVOList.get(i);
                    if (StringUtils.isNoneBlank(exportVO.getAgreeMoveStr())) {
                        if (exportVO.getAgreeMoveStr().contains("同意")) {
                            exportVO.setAgreeMoveStr("是");
                        } else if (exportVO.getAgreeMoveStr().contains("拒绝")) {
                            exportVO.setAgreeMoveStr("否");
                        }
                    } else {
                        exportVO.setAgreeMoveStr("否");
                    }
                    exportVO.setIndex(String.valueOf(i + 1));
                    exportVO.setSettleStatusStr(SubmitStatusEnum.getTextByValue(exportVO.getSettleStatus()));
                    SysDept sysDept = stringSysDeptMap.get(exportVO.getDeptId());
                    if (Objects.nonNull(sysDept)) {
                        exportVO.setBelongingStreetTown(sysDept.getDeptName());
                    }
                }
 
                FileUtils.setExcelResponseHeader(response, "已安置情况导出.xlsx");
                EasyExcelFactory.write(response.getOutputStream(), ResettlementHouseholdExportVO.class)
                        .sheet("sheet")
                        .doWrite(exportVOList);
            }
            response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE);
            return AjaxResult.success();
        } catch (IOException e) {
            return AjaxResult.error();
        }
    }
 
    @GetMapping(UrlConstants.STATE_SETTLEMENT_IMPORT_TEMPLATE)
    public void importStateHouseholdTemplate(@RequestParam Integer compensationType, HttpServletResponse response) {
 
        try {
            TemplateFileTypeEnum pathByFileType;
            if (1 == compensationType) {
                pathByFileType = TemplateFileTypeEnum.getEnumByFileType(4);
            } else if (2 == compensationType) {
                pathByFileType = TemplateFileTypeEnum.getEnumByFileType(7);
            } else {
                throw new GlobalException("补偿类型错误");
            }
            Method generateExampleDataMethod = pathByFileType.getClazz().getMethod("generateExampleData");
            Method generateHeaderDataMethod = pathByFileType.getClazz().getMethod("generateHeaderData");
            Object exampleData = generateExampleDataMethod.invoke(null);
            Object headerData = generateHeaderDataMethod.invoke(null);
 
            FileUtils.setExcelResponseHeader(response, pathByFileType.getFileName());
            ExcelWriterBuilder write = EasyExcelFactory.write(response.getOutputStream(), pathByFileType.getClazz());
            if (Objects.nonNull(headerData)) {
                write.registerWriteHandler((MultiDropdownWriteHandler) headerData);
            }
            write.sheet("sheet")
                    .doWrite(Collections.singletonList(exampleData));
            response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE);
        } catch (Exception e) {
            log.error("下载文件失败", e);
        }
    }
 
 
    @PostMapping(UrlConstants.STATE_SETTLEMENT_IMPORT)
    public AjaxResult<Void> importSettlement(ImportSettlementBO importSettlementBO) throws IOException {
        settlementService.stateSettlementImport(importSettlementBO);
        return AjaxResult.success();
 
    }
 
    @PostMapping(UrlConstants.STATE_SETTLEMENT_LIST_EXPORT)
    public AjaxResult<Void> exportSettlementDetail(@RequestBody(required = false) StateExecutionBO executionBO, HttpServletResponse response) {
        try {
            List<StateExecutionDetailVO> exportVOList = settlementService.exportExecution(executionBO);
            List<StateExecutionDetailExportVO> detailExportVOList = StateProjectConvert.INSTANCE.toExportVO(exportVOList);
 
            FileUtils.setExcelResponseHeader(response, "实施明细.xlsx");
 
            EasyExcelFactory.write(response.getOutputStream(), StateExecutionDetailExportVO.class)
                    .sheet("sheet")
                    .doWrite(detailExportVOList);
            response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE);
            return AjaxResult.success();
        } catch (IOException e) {
            return AjaxResult.error();
        }
    }
 
    //项目实施明细
    @PostMapping(UrlConstants.STATE_EXECUTION_DETAIL)
    public AjaxResult<PageInfo<StateExecutionDetailVO>> listExecution(@RequestBody(required = false) StateExecutionBO executionBO) {
        return AjaxResult.success(settlementService.listExecution(executionBO));
    }
 
    //给项目展示使用
    @PostMapping(UrlConstants.STATE_EXECUTION_SUM)
    public AjaxResult<Map<String, Map<Integer, ProjectExecutionSumVO>>> listProjectExecution(@RequestBody List<String> projectIdList) {
        return AjaxResult.success(settlementService.listProjectExecutionByProjectId(projectIdList, null));
    }
 
    @PostMapping(UrlConstants.STATE_SETTLEMENT_EXECUTION_WORKFLOW_SUBMIT)
    public AjaxResult<Boolean> submitSettle(@RequestBody WorkFlowSubmitBO workFlowSubmitBO) {
        if (Objects.isNull(workFlowSubmitBO.getSettlementIdList()) || workFlowSubmitBO.getSettlementIdList().isEmpty()) {
            return AjaxResult.success(false);
        }
        List<StateSettlement> agreementList = settlementService.listByIds(workFlowSubmitBO.getSettlementIdList());
        if (CollectionUtils.isEmpty(agreementList)) {
            return AjaxResult.success(false);
        }
        agreementList.forEach(stateAgreement -> {
            //需要显示文件名,直接存json
            stateAgreement.setSettleName(workFlowSubmitBO.getBatchName());
            stateAgreement.setFileUrl(workFlowSubmitBO.getFileBO());
            stateAgreement.setAuditStatus(SubmitStatusEnum.PENDING_REVIEW.getValue());
        });
        settlementService.workflowSubmit(workFlowSubmitBO.getSettlementIdList());
        return AjaxResult.success(settlementService.saveOrUpdateBatch(agreementList));
    }
 
    /**
     * @param projectId
     * @param componsationType
     * @return
     */
    @GetMapping(UrlConstants.STATE_SETTLEMENT_GENERATE_SETTLE_BATCH)
    public AjaxResult<String> generateSettleBatch(@RequestParam String projectId, @RequestParam(required = false) String componsationType) {
        return AjaxResult.success(settlementService.generateBatchName(projectId, componsationType));
    }
 
    @GetMapping(UrlConstants.STATE_SETTLEMENT_BATCH_NAME_LIST)
    public AjaxResult<List<String>> listBatchName(@RequestParam String projectId) {
        return AjaxResult.success(settlementService.listBatchName(projectId));
    }
 
}