| | |
| | | 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.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.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.BeanUtils; |
| | | 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; |
| | | |
| | | /** |
| | | * @author :yys |
| | |
| | | |
| | | @Override |
| | | public IPage<LaborIssue> listPage(Page page, LaborIssue laborIssue) { |
| | | IPage<LaborIssue> listPage = lavorIssueMapper.listPage(page, laborIssue); |
| | | return listPage; |
| | | StartAndEndDateDto startAndEndDateDto = getStartAndEndDateDto(laborIssue.getSeason(), laborIssue.getIssueDate()); |
| | | laborIssue.setStartDate(startAndEndDateDto.getStartDate()); |
| | | laborIssue.setEndDate(startAndEndDateDto.getEndDate()); |
| | | IPage<LaborIssue> laborIssueIPage = lavorIssueMapper.listPage(page, laborIssue); |
| | | return laborIssueIPage; |
| | | } |
| | | |
| | | public StartAndEndDateDto getStartAndEndDateDto(Integer season,Date payDate){ |
| | | SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); |
| | | StartAndEndDateDto startAndEndDateDto = new StartAndEndDateDto(); |
| | | if(season != null){ |
| | | Calendar calendar = Calendar.getInstance(); |
| | | int currentYear = calendar.get(Calendar.YEAR); |
| | | startAndEndDateDto.setYear(currentYear); |
| | | switch (season){ |
| | | case 1: |
| | | startAndEndDateDto.setStartDate(currentYear + "-01-01"); |
| | | startAndEndDateDto.setEndDate(currentYear + "-03-31"); |
| | | startAndEndDateDto.setStartMonth(1); |
| | | startAndEndDateDto.setEndMonth(3); |
| | | break; |
| | | case 2: |
| | | startAndEndDateDto.setStartDate(currentYear + "-04-01"); |
| | | startAndEndDateDto.setEndDate(currentYear + "-06-30"); |
| | | startAndEndDateDto.setStartMonth(4); |
| | | startAndEndDateDto.setEndMonth(6); |
| | | break; |
| | | case 3: |
| | | startAndEndDateDto.setStartDate(currentYear + "-07-01"); |
| | | startAndEndDateDto.setEndDate(currentYear + "-09-30"); |
| | | startAndEndDateDto.setStartMonth(7); |
| | | startAndEndDateDto.setEndMonth(9); |
| | | break; |
| | | case 4: |
| | | startAndEndDateDto.setStartDate(currentYear + "-10-01"); |
| | | startAndEndDateDto.setEndDate(currentYear + "-12-31"); |
| | | startAndEndDateDto.setStartMonth(10); |
| | | startAndEndDateDto.setEndMonth(12); |
| | | break; |
| | | } |
| | | } |
| | | if(payDate != null){ |
| | | Date lastDayOfMonth = getLastDayOfMonth(payDate); |
| | | startAndEndDateDto.setStartDate(sdf.format(payDate)); |
| | | startAndEndDateDto.setEndDate(sdf.format(lastDayOfMonth)); |
| | | } |
| | | return startAndEndDateDto; |
| | | } |
| | | |
| | | @Override |
| | | public StatisticsLaborIssue statistics(StatisticsLaborIssue req) throws Exception{ |
| | | SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); |
| | | StatisticsLaborIssue statisticsLaborIssue = new StatisticsLaborIssue(); |
| | | StartAndEndDateDto startAndEndDateDto = getStartAndEndDateDto(req.getSeason(), req.getIssueDate()); |
| | | LambdaQueryWrapper<LaborIssue> laborIssueLambdaQueryWrapper = new LambdaQueryWrapper<>(); |
| | | if(req.getSeason() != null || req.getIssueDate() != null){ |
| | | laborIssueLambdaQueryWrapper.ge(LaborIssue::getIssueDate, startAndEndDateDto.getStartDate()) |
| | | .le(LaborIssue::getIssueDate, startAndEndDateDto.getEndDate()); |
| | | } |
| | | List<LaborIssue> laborIssues = lavorIssueMapper.selectList(laborIssueLambdaQueryWrapper); |
| | | if(!CollectionUtils.isEmpty(laborIssues)){ |
| | | Long sum = laborIssues.stream() |
| | | .filter(laborIssue -> laborIssue.getAdoptedDate() != null) |
| | | .mapToLong(LaborIssue::getNum) |
| | | .sum(); |
| | | statisticsLaborIssue.setYlqNum(sum); |
| | | statisticsLaborIssue.setWlqNum(laborIssues.stream() |
| | | .filter(laborIssue -> laborIssue.getAdoptedDate() == null) |
| | | .mapToLong(LaborIssue::getNum) |
| | | .sum()); |
| | | Date currentDate = new Date(); |
| | | Date parse = sdf.parse(sdf.format(currentDate)); |
| | | statisticsLaborIssue.setCsylqNum(laborIssues.stream() |
| | | .filter(laborIssue -> laborIssue.getIssueDate() != null |
| | | && laborIssue.getAdoptedDate() != null |
| | | && (laborIssue.getIssueDate().before(laborIssue.getAdoptedDate()))) |
| | | .mapToLong(LaborIssue::getNum) |
| | | .sum()); |
| | | statisticsLaborIssue.setCswlqNum(laborIssues.stream() |
| | | .filter(laborIssue -> laborIssue.getIssueDate() != null |
| | | && laborIssue.getIssueDate().before(parse) |
| | | && laborIssue.getAdoptedDate() == null) |
| | | .mapToLong(LaborIssue::getNum) |
| | | .sum()); |
| | | } |
| | | return statisticsLaborIssue; |
| | | } |
| | | |
| | | @Autowired |
| | | private SysDictDataMapper sysDictDataMapper; |
| | | |
| | | @Override |
| | | public void exportCopy(HttpServletResponse response, LaborIssue laborIssue) throws UnsupportedEncodingException { |
| | | List<SysDictData> sys_lavor_issue = sysDictDataMapper.selectDictDataByType("sys_lavor_issue"); |
| | | if(CollectionUtils.isEmpty(sys_lavor_issue)){ |
| | | throw new RuntimeException("字典数据为空"); |
| | | } |
| | | StartAndEndDateDto startAndEndDateDto = getStartAndEndDateDto(laborIssue.getSeason(), laborIssue.getIssueDate()); |
| | | BeanUtils.copyProperties(startAndEndDateDto, laborIssue); |
| | | List<LaborIssue> laborIssues = lavorIssueMapper.list(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"); |
| | | |
| | | 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,laborIssue); |
| | | |
| | | 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("导出失败"); |
| | | } |
| | | } |
| | | |
| | | @Autowired |
| | | private SysDeptMapper sysDeptMapper; |
| | | |
| | | |
| | | // 处理外部装箱单数据 |
| | | private List<List<List<String>>> completeExternalPackingList(Map<String, List<LaborIssue>> listMap,List<SysDictData> sys_lavor_issue,LaborIssue obj) { |
| | | 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("企业管理科" + obj.getYear() + "年" + obj.getStartMonth() + "月-" + obj.getYear() + "年" + obj.getEndMonth() +"月劳保发放计划表"); |
| | | 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(obj.getYear().toString()); |
| | | list2.add(obj.getStartMonth().toString()); |
| | | list2.add(obj.getYear().toString()); |
| | | list2.add(obj.getEndMonth().toString()); |
| | | 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) { |
| | | Calendar calendar = Calendar.getInstance(); |
| | | calendar.setTime(date); // 设置传入的Date |
| | | calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH)); // 设置为当月最后一天 |
| | | return calendar.getTime(); // 返回Date对象 |
| | | } |
| | | } |