| | |
| | | import java.math.BigDecimal; |
| | | import java.net.URLEncoder; |
| | | import java.nio.charset.StandardCharsets; |
| | | import java.util.Date; |
| | | import java.util.List; |
| | | import java.util.Objects; |
| | | import java.util.stream.Collectors; |
| | |
| | | @Override |
| | | public Result add(CalculateReportForm form) { |
| | | CalculateReport entity = CalculateReportForm.getEntityByForm(form, null); |
| | | Date now = new Date(); |
| | | entity.setCreateTime(now); |
| | | entity.setUpdateTime(now); |
| | | if(baseMapper.insert(entity) > 0) { |
| | | return Result.ok("添加成功"); |
| | | } |
| | |
| | | public Result update(CalculateReportForm form) { |
| | | |
| | | CalculateReport entity = baseMapper.selectById(form.getId()); |
| | | |
| | | // 为空抛IllegalArgumentException,做全局异常处理 |
| | | Assert.notNull(entity, "记录不存在"); |
| | | BeanUtils.copyProperties(form, entity); |
| | | Date now = new Date(); |
| | | entity.setUpdateTime(now); |
| | | if (baseMapper.updateById(entity) > 0) { |
| | | 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(100 + 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(baseMapper.exportData(contractId)); |
| | | .doWrite(list); |
| | | } |
| | | } |