龚焕茏
2024-07-30 9c2dab107551b64b3fe16e1c7c178a4c15cacb5b
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
package com.ycl.platform.service.impl;
 
import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ycl.platform.domain.entity.CalculateRecord;
import com.ycl.platform.domain.entity.CalculateReport;
import com.ycl.platform.domain.excel.CalculateExport;
import com.ycl.platform.domain.form.CalculateReportBackfillForm;
import com.ycl.platform.domain.form.CalculateReportForm;
import com.ycl.platform.domain.query.CalculateReportQuery;
import com.ycl.platform.domain.vo.CalculateReportDetailVO;
import com.ycl.platform.domain.vo.CalculateReportVO;
import com.ycl.platform.mapper.CalculateRecordMapper;
import com.ycl.platform.mapper.CalculateReportMapper;
import com.ycl.platform.service.CalculateReportService;
import com.ycl.system.Result;
import com.ycl.system.page.PageUtil;
import enumeration.general.CalculateReportStatusEnum;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;
 
import java.math.BigDecimal;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
 
/**
 * 核算报告 服务实现类
 *
 * @author xp
 * @since 2024-04-23
 */
@Service
@RequiredArgsConstructor
public class CalculateReportServiceImpl extends ServiceImpl<CalculateReportMapper, CalculateReport> implements CalculateReportService {
 
    private final CalculateReportMapper calculateReportMapper;
    private final CalculateRecordMapper calculateRecordMapper;
 
    /**
     * 添加
     * @param form
     * @return
     */
    @Override
    public Result add(CalculateReportForm form) {
        CalculateReport entity = CalculateReportForm.getEntityByForm(form, null);
        if(baseMapper.insert(entity) > 0) {
            return Result.ok("添加成功");
        }
        return Result.error("添加失败");
    }
 
    /**
     * 修改
     * @param form
     * @return
     */
    @Override
    public Result update(CalculateReportForm form) {
 
        CalculateReport entity = baseMapper.selectById(form.getId());
 
        // 为空抛IllegalArgumentException,做全局异常处理
        Assert.notNull(entity, "记录不存在");
        BeanUtils.copyProperties(form, entity);
        if (baseMapper.updateById(entity) > 0) {
            return Result.ok("修改成功");
        }
        return Result.error("修改失败");
    }
 
    /**
     * 批量删除
     * @param ids
     * @return
     */
    @Override
    public Result remove(List<Long> ids) {
        if(baseMapper.deleteBatchIds(ids) > 0) {
            return Result.ok("删除成功");
        }
        return Result.error("删除失败");
    }
 
    /**
     * id删除
     * @param id
     * @return
     */
    @Override
    public Result removeById(Long id) {
        if(baseMapper.deleteById(id) > 0) {
            return Result.ok("删除成功");
        }
        return Result.error("删除失败");
    }
 
    /**
     * 分页查询
     * @param query
     * @return
     */
    @Override
    public Result page(CalculateReportQuery query) {
        IPage<CalculateReportVO> page = PageUtil.getPage(query, CalculateReportVO.class);
        baseMapper.page(query, page);
        page.getRecords().stream().forEach(item -> {
            if (Objects.isNull(item)) {
                item.setCanPublish(Boolean.FALSE);
            } else {
                item.setCanPublish(Boolean.TRUE);
            }
        });
        return Result.ok().data(page.getRecords()).total(page.getTotal());
    }
 
    /**
     * 根据id查找
     * @param id
     * @return
     */
    @Override
    public Result detail(Long id) {
        // 明细列表
        CalculateReportDetailVO detail = baseMapper.getById(id);
        if (Objects.nonNull(detail) && ! CollectionUtils.isEmpty(detail.getRecordList())) {
            if (CalculateReportStatusEnum.NOT_PUBLISH.equals(detail.getStatus())) {
                detail.getRecordList().get(0).setLatest(Boolean.TRUE);
            } else {
                detail.getRecordList().get(0).setLatest(Boolean.FALSE);
            }
        }
        return Result.ok().data(detail);
    }
 
    /**
     * 列表
     * @return
     */
    @Override
    public Result all() {
        List<CalculateReport> entities = baseMapper.selectList(null);
        List<CalculateReportVO> vos = entities.stream()
                .map(entity -> CalculateReportVO.getVoByEntity(entity, null))
                .collect(Collectors.toList());
        return Result.ok().data(vos);
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result backfill(CalculateReportBackfillForm form) {
        CalculateReport report = baseMapper.selectById(form.getId());
        if (Objects.isNull(report)) {
            throw new RuntimeException("该核算报告不存在");
        }
        if (CalculateReportStatusEnum.PUBLISHED.equals(report.getStatus())) {
            throw new RuntimeException("最新一次核算报告已经发布,无法修改");
        }
        CalculateReportBackfillForm.RecordForm latestRecord = form.getRecordList().get(0);
        CalculateRecord beforeRecord = calculateRecordMapper.selectById(latestRecord.getId());
        if (Objects.isNull(beforeRecord)) {
            throw new RuntimeException("最近一次核算记录不存在");
        }
        // 计算得到扣款总额(只算最近一条)
        BigDecimal totalDeduct = report.getDeductMoney().subtract(latestRecord.getDeductMoney()).add(latestRecord.getDeductMoney());
        report.setDeductMoney(totalDeduct);
        baseMapper.updateById(report);
 
        beforeRecord.setDeductMoney(latestRecord.getDeductMoney());
        calculateRecordMapper.updateById(beforeRecord);
        return Result.ok("操作成功");
    }
 
    @Override
    public Result updatePublishStatus(Long reportId) {
        CalculateReport report = baseMapper.selectById(reportId);
        if (Objects.isNull(report)) {
            throw new RuntimeException("该核算报告不存在");
        }
        if (CalculateReportStatusEnum.NOT_PUBLISH.equals(report.getStatus())) {
            report.setStatus(CalculateReportStatusEnum.PUBLISHED);
        } else if (CalculateReportStatusEnum.PUBLISHED.equals(report.getStatus())) {
            report.setStatus(CalculateReportStatusEnum.NOT_PUBLISH);
        }
        baseMapper.updateById(report);
        return Result.ok("操作成功");
    }
 
    @Override
    @SneakyThrows
    public void export(Integer contractId, HttpServletResponse response) {
        // 获取数据
        List<CalculateExport> list = baseMapper.exportData(contractId);
        CalculateExport calculateExport = new CalculateExport();
        calculateExport.setRuleName("合计");
        calculateExport.setNum(list.stream().mapToInt(CalculateExport::getNum).sum());
        calculateExport.setScore(list.stream().mapToInt(CalculateExport::getScore).sum());
        list.add(calculateExport);
        // 输出文件
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setCharacterEncoding("utf-8");
        String fileName = URLEncoder.encode("核算报告", StandardCharsets.UTF_8).replace("\\+", "%20");
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
        EasyExcel.write(response.getOutputStream(), CalculateExport.class)
                .sheet("核算报告")
                .doWrite(list);
    }
}