Selaa lähdekoodia

新增项目月份数据接口与导出

anderx 1 vuosi sitten
vanhempi
commit
900fdf6244

+ 204 - 2
ruoyi-common/src/main/java/com/ruoyi/common/utils/poi/ExcelUtil.java

@@ -994,6 +994,9 @@ public class ExcelUtil<T>
         return cell;
     }
 
+
+
+
     /**
      * 设置单元格信息
      *
@@ -1840,15 +1843,22 @@ public class ExcelUtil<T>
     public void exportProjectMonthClockExcel(HttpServletResponse response,List<Map<String, Object>> listData, String projectName, String departmentName, String companyName,
                                              Integer year,Integer month) {
         title=StringUtils.format("{}年{}月工时记录表",year,month);
-        sheetName="导出表格";
+        sheetName="tab1";
         response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
         response.setCharacterEncoding("utf-8");
         this.wb = new SXSSFWorkbook(500);
         this.sheet = wb.createSheet();
         wb.setSheetName(0, sheetName);
+        this.styles = createProjectStyles(wb);
+        sheet.setColumnWidth(0, 16*256);
+        Map<String, Object> stringObjectMap = listData.get(0);
+        List<Map<String, Object>> map =  (List<Map<String, Object>>) stringObjectMap.get("data");
         try
         {
-//            writeSheet();
+            addTitle(map.size());
+            addSubHead(map.size(),projectName,departmentName,companyName);
+            addTable(map);
+            addTableDuration(listData);
             wb.write(response.getOutputStream());
         }
         catch (Exception e)
@@ -1861,4 +1871,196 @@ public class ExcelUtil<T>
         }
 
     }
+
+    private void addTableDuration(List<Map<String, Object>> map) {
+        int index=0;
+        for (Map<String, Object> stringObjectMap : map) {
+            int start=rownum;
+            Row row = sheet.createRow(rownum++);
+            Cell nameCell = row.createCell(index++);
+            nameCell.setCellStyle(styles.get("data"));
+            nameCell.setCellValue(stringObjectMap.get("name").toString());
+            Cell typeCell = row.createCell(index++);
+            typeCell.setCellStyle(styles.get("data"));
+            typeCell.setCellValue("研发工时");
+            List<Map<String, Object>> map2 = (List<Map<String, Object>>) stringObjectMap.get("data");
+            for (Map<String, Object> objectMap : map2) {
+                Cell cell = row.createCell(index++);
+                if (objectMap.get("projectDuration")!=null){
+                    BigDecimal projectDuration = (BigDecimal) objectMap.get("projectDuration");
+                    cell.setCellValue(projectDuration.stripTrailingZeros().toPlainString());
+                    cell.setCellStyle(styles.get("data"));
+                }
+            }
+            Cell projectDuration = row.createCell(index++);
+            BigDecimal projectDuration1 = (BigDecimal) stringObjectMap.get("projectDuration");
+            projectDuration.setCellValue(projectDuration1.stripTrailingZeros().toPlainString());
+            projectDuration.setCellStyle(styles.get("data"));
+            Cell projectProportion = row.createCell(index);
+            BigDecimal proportion = (BigDecimal) stringObjectMap.get("projectProportion");
+            proportion = proportion.multiply(new BigDecimal(100));
+            projectProportion.setCellValue(proportion.stripTrailingZeros().toPlainString()+"%");
+            projectProportion.setCellStyle(styles.get("data"));
+            Row row2 = sheet.createRow(rownum++);
+            index=0;
+            Cell typeCell1 = row2.createCell(index++);
+            typeCell1.setCellStyle(styles.get("data"));
+            Cell typeCell2 = row2.createCell(index++);
+            typeCell2.setCellStyle(styles.get("data"));
+            typeCell2.setCellValue("非研发工时");
+            for (Map<String, Object> objectMap : map2) {
+                Cell Cell = row2.createCell(index++);
+                if (objectMap.get("notDuration")!=null){
+                    BigDecimal notDuration = (BigDecimal) objectMap.get("notDuration");
+                    Cell.setCellValue(notDuration.stripTrailingZeros().toPlainString());
+                    Cell.setCellStyle(styles.get("data"));
+                }
+            }
+            Cell notDuration = row2.createCell(index++);
+            BigDecimal notDuration1 = (BigDecimal) stringObjectMap.get("notDuration");
+            notDuration.setCellValue(notDuration1.stripTrailingZeros().toPlainString());
+            notDuration.setCellStyle(styles.get("data"));
+            Cell notProportion = row2.createCell(index);
+            BigDecimal notP = (BigDecimal) stringObjectMap.get("notProportion");
+            notP = notP.multiply(new BigDecimal(100));
+            notProportion.setCellValue(notP.stripTrailingZeros().toPlainString()+"%");
+            notProportion.setCellStyle(styles.get("data"));
+            sheet.addMergedRegion(new CellRangeAddress(start, start+1, 0, 0));
+            index=0;
+        }
+
+    }
+
+    private void addTable(List<Map<String, Object>> map) {
+        List<String> row1= new ArrayList<>();
+        List<String> row2= new ArrayList<>();
+
+        row1.add("姓名");
+        row1.add("星期");
+        row2.add("姓名");
+        row2.add("日期");
+        for (Map<String, Object> objectMap : map) {
+            row1.add(objectMap.get("week").toString());
+            row2.add(objectMap.get("date").toString());
+        }
+        row1.add("正常出勤");
+        row1.add("分摊率");
+        row2.add("工时");
+        row2.add("分摊率");
+        addRowTable(row1);
+        addRowTable(row2);
+        sheet.setColumnWidth(row1.size()-2, 14*256);
+        sheet.addMergedRegion(new CellRangeAddress(2, 3, 0, 0));
+        sheet.addMergedRegion(new CellRangeAddress(2, 3, row1.size()-1, row1.size()-1));
+    }
+
+    private void addSubHead(int size, String projectName, String departmentName, String companyName) {
+        Row titleRow = sheet.createRow(rownum++);
+        titleRow.setHeightInPoints(20);
+        int    titleLastCol=3+size;
+        for (int i = 0; i <=titleLastCol; i++){
+            Cell titleCell = titleRow.createCell(i);
+            CellStyle title1 = styles.get("subHead");
+            if (i==0){
+                String str= StringUtils.format("单位: {}                             " +
+                        "所属部门: {}                            " +
+                        "课题名称: {}",companyName,departmentName,projectName);
+                titleCell.setCellValue(str);
+            }
+            titleCell.setCellStyle(title1);
+        }
+        sheet.addMergedRegion(new CellRangeAddress(titleRow.getRowNum(), titleRow.getRowNum(), 0, titleLastCol));
+    }
+
+
+    private void  addTitle(Integer size) {
+        Row titleRow = sheet.createRow(rownum == 0 ? rownum++ : 0);
+        titleRow.setHeightInPoints(30);
+        int    titleLastCol=3+size;
+        for (int i = 0; i <=titleLastCol; i++){
+            Cell titleCell = titleRow.createCell(i);
+            if (i==0){
+                titleCell.setCellValue(title);
+            }
+            titleCell.setCellStyle(styles.get("title"));
+        }
+        sheet.addMergedRegion(new CellRangeAddress(titleRow.getRowNum(), titleRow.getRowNum(), 0, titleLastCol));
+
+    }
+
+    private void addRowTable(List<String> row) {
+        Row RowTable = sheet.createRow( rownum++ );
+        RowTable.setHeightInPoints(20);
+        int index = 0;
+        for (String s : row) {
+            Cell Cell = RowTable.createCell(index);
+            CellStyle title1 = styles.get("subHead");
+            Font titleFont = wb.createFont();
+            titleFont.setFontHeightInPoints((short) 12);
+            title1.setFont(titleFont);
+            Cell.setCellStyle(title1);
+            Cell.setCellValue(s);
+            index++;
+        }
+    }
+
+    private Map<String, CellStyle> createProjectStyles(Workbook wb) {
+        // 写入各条记录,每条记录对应excel表中的一行
+        Map<String, CellStyle> styles = new HashMap<String, CellStyle>();
+        CellStyle style = wb.createCellStyle();
+        style.setAlignment(HorizontalAlignment.CENTER);
+        style.setVerticalAlignment(VerticalAlignment.CENTER);
+        style.setBorderTop(BorderStyle.THIN);
+        style.setBorderBottom(BorderStyle.THIN);
+        style.setBorderLeft(BorderStyle.THIN);
+        style.setBorderRight(BorderStyle.THIN);
+        Font titleFont = wb.createFont();
+        titleFont.setFontName("Arial");
+        titleFont.setFontHeightInPoints((short) 16);
+        titleFont.setBold(true);
+        style.setFont(titleFont);
+        styles.put("title", style);
+
+        style = wb.createCellStyle();
+        style.setAlignment(HorizontalAlignment.CENTER);
+        style.setVerticalAlignment(VerticalAlignment.CENTER);
+        style.setBorderTop(BorderStyle.THIN);
+        style.setBorderBottom(BorderStyle.THIN);
+        style.setBorderLeft(BorderStyle.THIN);
+        style.setBorderRight(BorderStyle.THIN);
+        Font subHeadFont = wb.createFont();
+        subHeadFont.setFontName("Arial");
+        subHeadFont.setFontHeightInPoints((short) 14);
+        subHeadFont.setBold(true);
+        style.setFont(titleFont);
+        styles.put("subHead", style);
+
+        style = wb.createCellStyle();
+        style.setAlignment(HorizontalAlignment.CENTER);
+        style.setVerticalAlignment(VerticalAlignment.CENTER);
+        style.setBorderTop(BorderStyle.THIN);
+        style.setBorderBottom(BorderStyle.THIN);
+        style.setBorderLeft(BorderStyle.THIN);
+        style.setBorderRight(BorderStyle.THIN);
+        style.setBottomBorderColor(IndexedColors.GREY_50_PERCENT.getIndex());
+        Font dataFont = wb.createFont();
+        dataFont.setFontName("Arial");
+        dataFont.setFontHeightInPoints((short) 10);
+        style.setFont(dataFont);
+        styles.put("data", style);
+
+        style = wb.createCellStyle();
+        style.setAlignment(HorizontalAlignment.CENTER);
+        style.setVerticalAlignment(VerticalAlignment.CENTER);
+        style.setBorderTop(BorderStyle.THIN);
+        style.setBorderBottom(BorderStyle.THIN);
+        style.setBorderLeft(BorderStyle.THIN);
+        style.setBorderRight(BorderStyle.THIN);
+        Font totalFont = wb.createFont();
+        totalFont.setFontName("Arial");
+        totalFont.setFontHeightInPoints((short) 10);
+        style.setFont(totalFont);
+        styles.put("total", style);
+        return styles;
+    }
 }

+ 33 - 31
ruoyi-system/src/main/java/com/ruoyi/project/service/impl/ProjectStaffRecordServiceImpl.java

@@ -777,38 +777,28 @@ public class ProjectStaffRecordServiceImpl implements ProjectStaffRecordService
 
     @Override
     public List<Map<String,Object>> getMonthClock(Integer id, String yearMonth) {
-        ProjectTask projectTask = projectTaskMapper.selectByPrimaryKey(Long.valueOf(id));
-        SysDept sysDept = deptService.selectDeptByPid(projectTask.getId());
-        String projectName=projectTask.getName();
-        String departmentName=sysDept.getDeptName();
-        String companyName=sysDept.getCompanyName();
-        if (sysDept.getCompanyId()==null){
-            companyName=sysDept.getDeptName();
-        }
         LocalDate startDate =  LocalDate.parse(yearMonth, DateTimeFormatter.ofPattern("yyyy-MM-dd"));
         startDate=startDate.withDayOfMonth(1);
         int year=startDate.getYear();
         int month=startDate.getMonthValue();
         int lastMonth = month;
         if(startDate.getMonthValue()== Month.DECEMBER.getValue()){
-            month=1;
+            lastMonth=1;
             year=startDate.getYear()+1;
         }else {
             lastMonth=startDate.getMonthValue()+1;
         }
         LocalDate endDate = LocalDate.of(year,lastMonth,1);
         List<ProjectStaffRecord> monthClock = projectStaffRecordMapper.getMonthClock(id, startDate, endDate);
-        monthClock = monthClock.stream().filter(e -> e.getAid() == 48).collect(Collectors.toList());
+//        monthClock = monthClock.stream().filter(e -> e.getAid() == 48).collect(Collectors.toList());
         LocalDate lastDay = startDate.with(TemporalAdjusters.lastDayOfMonth());
-        //获取当月每天与星期
-        List<Map<String, Object>> list = getDaysMap(lastDay, startDate);
         List<Map<String,Object>> listData = new ArrayList<>();
         //将数据与表头匹配
         for (ProjectStaffRecord e : monthClock) {
             //先匹配是否存在这个人,不存证则需要新增
             LocalDate recordTime =  e.getRecordTime().toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
             if (listData.isEmpty()){
-                pushDate(e, list, recordTime, listData);
+                pushDate(e, getDaysMap(lastDay, startDate), recordTime, listData);
             }else {
                 boolean flag = false;
                 for (Map<String, Object> m : listData) {
@@ -820,31 +810,34 @@ public class ProjectStaffRecordServiceImpl implements ProjectStaffRecordService
                         for (Map<String, Object> map2 : maps) {
                             Integer date = (Integer) map2.get("date");
                             Integer dayOfMonth = recordTime.getDayOfMonth();
+
                             //比对日期,如果日期有参数相加,没有则赋值
                             if (dayOfMonth.equals(date)) {
-                                Double o = (Double) map2.get("projectDuration");
-                                if (o==null){
-                                    map2.put("projectDuration", e.getDuration());
+                                BigDecimal projectDuration = (BigDecimal) map2.get("projectDuration");
+                                BigDecimal added = projectDuration.add(BigDecimal.valueOf(e.getDuration()));
+                                map2.put("projectDuration", added);
+                                if (added.compareTo(BigDecimal.valueOf(8))>=0){
+                                    map2.put("notDuration", BigDecimal.valueOf(0));
                                 }else {
-                                    map2.put("projectDuration", o+e.getDuration());
+                                    map2.put("notDuration", BigDecimal.valueOf(8).subtract(added));
                                 }
-                                map2.put("notDuration", 8- e.getDuration()>0?8- e.getDuration():0);
                                 break;
                             }
+                            if (date==1){
+                                System.out.println("name="+name);
+                                System.out.println("map="+map2);
+                            }
                         }
+
                     }
                 }
                 if (!flag){
-                    pushDate(e, list, recordTime, listData);
+                    pushDate(e, getDaysMap(lastDay, startDate), recordTime, listData);
                 }
             }
         }
         //将数据统计,计算出所有工时与分摊率
         pushDurationAndProportion(listData);
-        System.out.println("projectName:"+projectName);
-        System.out.println("depName:"+departmentName);
-        System.out.println("companyName:"+companyName);
-
         return listData;
     }
 
@@ -891,9 +884,8 @@ public class ProjectStaffRecordServiceImpl implements ProjectStaffRecordService
     private List<Map<String, Object>> getDaysMap(LocalDate lastDay, LocalDate startDate) {
         List<Map<String,Object>> list =new ArrayList<>();
         //先计算出月份表头
-        for (int i = 1; i < lastDay.getDayOfMonth(); i++){
-            startDate = startDate.plusDays(1);
-            Map<String,Object> map = new HashMap<>();
+        for (int i = 1; i <= lastDay.getDayOfMonth(); i++){
+            Map<String,Object> map = new LinkedHashMap<>();
             map.put("date", startDate.getDayOfMonth());
             DayOfWeek dayOfWeek = startDate.getDayOfWeek();
             switch (dayOfWeek){
@@ -919,9 +911,10 @@ public class ProjectStaffRecordServiceImpl implements ProjectStaffRecordService
                     map.put("week","日");
                     break;
             }
-            map.put("projectDuration", null);
-            map.put("notDuration", null);
+            map.put("projectDuration", BigDecimal.valueOf(0));
+            map.put("notDuration", BigDecimal.valueOf(8));
             list.add(map);
+            startDate = startDate.plusDays(1);
         }
         return list;
     }
@@ -933,13 +926,22 @@ public class ProjectStaffRecordServiceImpl implements ProjectStaffRecordService
         for (Map<String, Object> map2 : list) {
             Integer date = (Integer) map2.get("date");
             if (recordTime.getDayOfMonth()==date) {
-                map2.put("projectDuration", e.getDuration());
-                map2.put("notDuration", 8- e.getDuration()>0?8- e.getDuration():0);
+                BigDecimal projectDuration = BigDecimal.valueOf(e.getDuration());
+                map2.put("projectDuration", projectDuration);
+                if (projectDuration.compareTo(BigDecimal.valueOf(8))>=0){
+                    map2.put("notDuration", BigDecimal.valueOf(0));
+                }else {
+                    map2.put("notDuration", BigDecimal.valueOf(8).subtract(projectDuration));
+                }
+                break;
             }
         }
-        map.put("data", list);
+        map.put("data", new ArrayList<>(list));
         listData.add(map);
     }
 
 
+
+
+
 }