| | |
| | | package com.ruoyi.lavorissue.service.impl; |
| | | |
| | | import com.alibaba.excel.EasyExcel; |
| | | import com.alibaba.excel.ExcelWriter; |
| | | import com.alibaba.excel.write.metadata.WriteSheet; |
| | | import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; |
| | | import com.baomidou.mybatisplus.core.metadata.IPage; |
| | | import com.baomidou.mybatisplus.extension.plugins.pagination.Page; |
| | | import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; |
| | | import com.ruoyi.approve.utils.StartAndEndDateDto; |
| | | import com.ruoyi.common.utils.excel.ExcelUtils; |
| | | import com.ruoyi.common.utils.SecurityUtils; |
| | | import com.ruoyi.common.utils.excel.ConfigurableMergeStrategy; |
| | | import com.ruoyi.common.utils.excel.CustomCellStyleHandler; |
| | | import com.ruoyi.lavorissue.dto.StatisticsLaborIssue; |
| | | import com.ruoyi.lavorissue.mapper.LavorIssueMapper; |
| | | import com.ruoyi.lavorissue.pojo.LaborIssue; |
| | | import com.ruoyi.lavorissue.service.LavorIssueService; |
| | | import com.ruoyi.project.system.domain.SysDept; |
| | | import com.ruoyi.project.system.domain.SysDictData; |
| | | import com.ruoyi.project.system.domain.SysDictType; |
| | | import com.ruoyi.project.system.mapper.SysDeptMapper; |
| | | import com.ruoyi.project.system.mapper.SysDictDataMapper; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.apache.poi.ss.util.CellRangeAddress; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.stereotype.Service; |
| | | import org.springframework.util.CollectionUtils; |
| | | |
| | | import javax.servlet.http.HttpServletResponse; |
| | | import java.io.IOException; |
| | | import java.io.UnsupportedEncodingException; |
| | | import java.net.URLEncoder; |
| | | import java.text.SimpleDateFormat; |
| | | import java.util.*; |
| | | import java.util.concurrent.atomic.AtomicInteger; |
| | | import java.util.stream.Collectors; |
| | | |
| | | /** |
| | |
| | | private SysDictDataMapper sysDictDataMapper; |
| | | |
| | | @Override |
| | | public void exportCopy(HttpServletResponse response) { |
| | | public void exportCopy(HttpServletResponse response) throws UnsupportedEncodingException { |
| | | List<SysDictData> sys_lavor_issue = sysDictDataMapper.selectDictDataByType("sys_lavor_issue"); |
| | | if(CollectionUtils.isEmpty(sys_lavor_issue)){ |
| | | throw new RuntimeException("字典数据为空"); |
| | | } |
| | | List<Object> head = new ArrayList<>(); |
| | | head.add("部门"); |
| | | head.add(ExcelUtils.COLUMN_MERGE); |
| | | head.add("企业管理科"); |
| | | head.add(ExcelUtils.COLUMN_MERGE); |
| | | head.add(""); |
| | | for (int i = 0; i < sys_lavor_issue.size(); i++) { |
| | | head.add(ExcelUtils.COLUMN_MERGE); |
| | | } |
| | | |
| | | List<Object> head1 = new ArrayList<>(); |
| | | head1.add("开始年/月"); |
| | | head1.add(ExcelUtils.COLUMN_MERGE); |
| | | head1.add("结束年/月"); |
| | | head1.add(ExcelUtils.COLUMN_MERGE); |
| | | head1.add(""); |
| | | for (int i = 0; i < sys_lavor_issue.size(); i++) { |
| | | head1.add(ExcelUtils.COLUMN_MERGE); |
| | | } |
| | | |
| | | List<Object> head2 = new ArrayList<>(); |
| | | head2.add("2025"); |
| | | head2.add("1"); |
| | | head2.add("2025"); |
| | | head2.add("3"); |
| | | head2.add("企业管理科2025年1月-2025年3月劳保发放计划表"); |
| | | for (int i = 0; i < sys_lavor_issue.size(); i++) { |
| | | head2.add(ExcelUtils.COLUMN_MERGE); |
| | | } |
| | | |
| | | List<Object> head3 = new ArrayList<>(); |
| | | head3.add("部门名称"); |
| | | head3.add(ExcelUtils.COLUMN_MERGE); |
| | | head3.add("姓名"); |
| | | head3.add("工号"); |
| | | for (SysDictData sysDictData : sys_lavor_issue) { |
| | | head3.add(sysDictData.getDictLabel()); |
| | | } |
| | | head3.add("签名"); |
| | | |
| | | // 填充数据 |
| | | List<Object> head4 = new ArrayList<>(); |
| | | List<LaborIssue> laborIssues = lavorIssueMapper.selectList(null); |
| | | if(CollectionUtils.isEmpty(laborIssues)){ |
| | | List<LaborIssue> laborIssues = lavorIssueMapper.list(new LaborIssue()); |
| | | if (CollectionUtils.isEmpty(laborIssues)) { |
| | | throw new RuntimeException("数据为空"); |
| | | } |
| | | Map<String, List<LaborIssue>> laborIssueMap = laborIssues.stream() |
| | | .collect(Collectors.groupingBy(LaborIssue::getStaffNo)); |
| | | response.setContentType("application/vnd.ms-excel"); |
| | | response.setCharacterEncoding("UTF-8"); |
| | | // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系 |
| | | String fileName = URLEncoder.encode("外部装箱单", "UTF-8"); |
| | | response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx"); |
| | | |
| | | Map<String, List<LaborIssue>> collect = laborIssues.stream().collect(Collectors.groupingBy(LaborIssue::getStaffNo)); |
| | | collect.forEach((k,v)->{ |
| | | head4.add(v.get(0).getDeptName()); |
| | | head4.add(ExcelUtils.COLUMN_MERGE); |
| | | head4.add(v.get(0).getStaffName()); |
| | | head4.add(v.get(0).getStaffNo()); |
| | | // 判断字典数据 |
| | | for (SysDictData sysDictData : sys_lavor_issue) { |
| | | long sum = v.stream().filter(laborIssue -> laborIssue.getDictId().equals(sysDictData.getDictValue())).mapToLong(LaborIssue::getNum).sum(); |
| | | head4.add(sum); |
| | | try { |
| | | //新建ExcelWriter |
| | | ExcelWriter excelWriter = EasyExcel.write(response.getOutputStream()) |
| | | .registerWriteHandler(new CustomCellStyleHandler()) |
| | | .registerWriteHandler(new ConfigurableMergeStrategy(exportExternalPackingListGetMerge(4,sys_lavor_issue.size(),laborIssueMap.size()))) |
| | | .build(); |
| | | |
| | | List<List<List<String>>> data = completeExternalPackingList(laborIssueMap, sys_lavor_issue); |
| | | |
| | | for (int i = 0; i < data.size(); i++) { |
| | | List<List<String>> list = data.get(i); |
| | | |
| | | //获取sheet0对象 |
| | | WriteSheet mainSheet = EasyExcel.writerSheet(i, "外部装箱单" + i).build(); |
| | | //向sheet0写入数据 传入空list这样只导出表头 |
| | | excelWriter.write(list, mainSheet); |
| | | } |
| | | |
| | | }); |
| | | //关闭流 |
| | | excelWriter.finish(); |
| | | } catch (IOException e) { |
| | | throw new RuntimeException("导出失败"); |
| | | } |
| | | } |
| | | |
| | | List<List<Object>> sheetDataList = new ArrayList<>(); |
| | | sheetDataList.add(head); |
| | | sheetDataList.add(head1); |
| | | sheetDataList.add(head2); |
| | | sheetDataList.add(head3); |
| | | sheetDataList.add(head4); |
| | | ExcelUtils.export(response, "劳保发放计划表", sheetDataList); |
| | | @Autowired |
| | | private SysDeptMapper sysDeptMapper; |
| | | |
| | | |
| | | // 处理外部装箱单数据 |
| | | private List<List<List<String>>> completeExternalPackingList(Map<String, List<LaborIssue>> listMap,List<SysDictData> sys_lavor_issue) { |
| | | List<List<List<String>>> data = new ArrayList<>(); |
| | | int num = sys_lavor_issue.size(); |
| | | |
| | | List<List<String>> item = new ArrayList<>(); |
| | | List<String> list = new ArrayList<>(); |
| | | list.add("部门"); |
| | | list.add(""); |
| | | list.add("企业管理科"); |
| | | list.add(""); |
| | | list.add("企业管理科2025年1月-2025年3月劳保发放计划表"); |
| | | for (int i = 1; i <= num; i++){ |
| | | list.add(""); |
| | | } |
| | | item.add(list); |
| | | |
| | | List<String> list1 = new ArrayList<>(); |
| | | list1.add("开始年/月"); |
| | | list1.add(""); |
| | | list1.add("结束年/月"); |
| | | list1.add(""); |
| | | for (int i = 0; i <= num; i++){ |
| | | list1.add(""); |
| | | } |
| | | item.add(list1); |
| | | |
| | | List<String> list2 = new ArrayList<>(); |
| | | list2.add("2025"); |
| | | list2.add("1"); |
| | | list2.add("2025"); |
| | | list2.add("3"); |
| | | for (int i = 0; i <= num; i++){ |
| | | list.add(""); |
| | | } |
| | | item.add(list2); |
| | | |
| | | List<String> list3 = new ArrayList<>(); |
| | | list3.add("部门名称"); |
| | | list3.add(""); |
| | | list3.add("姓名"); |
| | | list3.add("工号"); |
| | | for (SysDictData sysDictData : sys_lavor_issue) { |
| | | list3.add(sysDictData.getDictLabel()); |
| | | } |
| | | list3.add("签名"); |
| | | item.add(list3); |
| | | |
| | | // 填值 |
| | | SysDept sysDept = sysDeptMapper.selectDeptById(SecurityUtils.getLoginUser().getTenantId()); |
| | | List<String> sumList = new ArrayList<>(); |
| | | AtomicInteger i = new AtomicInteger(); |
| | | listMap.forEach((key, value) -> { |
| | | List<String> list4 = new ArrayList<>(); |
| | | list4.add(sysDept.getDeptName()); |
| | | list4.add(""); |
| | | list4.add(value.get(0).getStaffName()); |
| | | list4.add(value.get(0).getStaffNo()); |
| | | int j = 0; |
| | | for (SysDictData sysDictData : sys_lavor_issue) { |
| | | list4.add(value.stream().filter(laborIssue -> laborIssue.getDictId().equals(sysDictData.getDictValue())) |
| | | .mapToLong(LaborIssue::getNum) |
| | | .sum() |
| | | + ""); |
| | | if(i.get() == 0){ |
| | | sumList.add(value.stream().filter(laborIssue -> laborIssue.getDictId().equals(sysDictData.getDictValue())) |
| | | .mapToLong(LaborIssue::getNum) |
| | | .sum() |
| | | + ""); |
| | | }else { |
| | | sumList.set(j,(Long.parseLong(sumList.get(j)) + value.stream().filter(laborIssue -> laborIssue.getDictId().equals(sysDictData.getDictValue())) |
| | | .mapToLong(LaborIssue::getNum) |
| | | .sum()) |
| | | + ""); |
| | | } |
| | | j++; |
| | | } |
| | | i.getAndIncrement(); |
| | | item.add(list4); |
| | | }); |
| | | List<String> list5 = new ArrayList<>(); |
| | | list5.add("合计"); |
| | | list5.add(""); |
| | | list5.add(""); |
| | | list5.add(""); |
| | | for (int h = 0; h < sumList.size(); h++){ |
| | | list5.add(sumList.get(h)); |
| | | } |
| | | item.add(list5); |
| | | data.add(item); |
| | | return data; |
| | | } |
| | | |
| | | /** |
| | | * 外部装箱单合并单元格 |
| | | * |
| | | * @return |
| | | */ |
| | | private List<CellRangeAddress> exportExternalPackingListGetMerge(Integer rowNum,Integer num,Integer listSize) { |
| | | List<CellRangeAddress> mergeRegions = new ArrayList<>(); |
| | | mergeRegions.add(new CellRangeAddress(0, 0, 0, 1)); // 合并A1:Q2 |
| | | mergeRegions.add(new CellRangeAddress(0, 0, 2, 3)); // 合并A4:Q4 |
| | | |
| | | mergeRegions.add(new CellRangeAddress(0, 2, rowNum, rowNum + num)); // 合并A5:B6 |
| | | mergeRegions.add(new CellRangeAddress(1, 1, 0, 1)); // 合并C5:E6 |
| | | mergeRegions.add(new CellRangeAddress(1, 1, 2, 3)); // 合并F5:G6 |
| | | mergeRegions.add(new CellRangeAddress(3, 3, 0, 1)); // 合并H5:I6 |
| | | for (int i = 1; i <= listSize; i++) { |
| | | mergeRegions.add(new CellRangeAddress(3 + i, 3 + i, 0, 1)); |
| | | } |
| | | mergeRegions.add(new CellRangeAddress(rowNum + listSize, rowNum + listSize, 0, 3)); |
| | | return mergeRegions; |
| | | } |
| | | |
| | | public Date getLastDayOfMonth(Date date) { |