Browse Source

Merge remote-tracking branch 'origin/qianshan_data_bureau' into qianshan_data_bureau

Administrator 3 years ago
parent
commit
f4b1033a37

+ 26 - 0
boman-api/boman-api-web-core/src/main/java/com/boman/web/core/api/RemoteAttendanceService.java

@@ -0,0 +1,26 @@
+package com.boman.web.core.api;
+
+import com.boman.domain.constant.ServiceNameConstants;
+import com.boman.domain.dto.AjaxResult;
+import org.springframework.cloud.openfeign.FeignClient;
+import org.springframework.web.bind.annotation.PostMapping;
+import org.springframework.web.bind.annotation.RequestBody;
+
+import java.util.Map;
+
+/**
+ * @author shiqian
+ * @date 2021年07月27日 15:50
+ **/
+@FeignClient(contextId = "remoteAttendanceService", value = ServiceNameConstants.WEB_CORE_SERVICE)
+public interface RemoteAttendanceService {
+
+    /**
+     * 按月统计人员考勤信息
+     *
+     * @param map 传入时间 年-月
+     * @return AjaxResult
+     */
+    @PostMapping("/attendance/statistics")
+    AjaxResult statisticsByMonth(@RequestBody Map<String, Object> map);
+}

+ 171 - 63
boman-common/boman-common-core/src/main/java/com/boman/common/core/utils/poi/ExcelUtil.java

@@ -17,7 +17,6 @@ import com.boman.common.core.utils.reflect.ReflectUtils;
 import com.boman.domain.GenTableColumn;
 import com.boman.domain.SysDictData;
 import com.boman.domain.constant.FormDataConstant;
-import com.boman.domain.constant.MaskConstant;
 import org.apache.commons.lang3.BooleanUtils;
 import org.apache.poi.ss.usermodel.*;
 import org.apache.poi.ss.util.CellRangeAddressList;
@@ -397,19 +396,19 @@ public class ExcelUtil<T>
     /**
      * 功能描述: 通用的导出
      *
-     * @param response  返回数据
-     * @param list      导出数据集合
-     * @param sheetName 工作表的名称
-     * @param columns   所有的列
-     * @param empty   所有的列
-     * @return void
+     * @param response           返回数据
+     * @param list               导出数据集合
+     * @param sheetName          工作表的名称
+     * @param listVisibleColumns 列表可见
+     * @param empty              所有的列
+     * @param result             true直接导出结果 false需要检查列...
      */
     public void exportExcelCommon(HttpServletResponse response, List<Map<String, Object>> list
-            , String sheetName, List<GenTableColumn> columns, Boolean empty) throws IOException {
+            , String sheetName, List<GenTableColumn> listVisibleColumns, Boolean empty, Boolean result) throws IOException {
         response.setContentType("application/vnd.ms-excel");
         response.setCharacterEncoding("utf-8");
-        this.initJSONObject(list, sheetName, Type.EXPORT);
-        exportExcelCommon(response.getOutputStream(), list, columns, empty);
+        initJSONObject(list, sheetName, Type.EXPORT);
+        exportExcelCommon(response.getOutputStream(), list, listVisibleColumns, empty, result);
     }
 
 
@@ -433,16 +432,15 @@ public class ExcelUtil<T>
     /**
      * 功能描述: 通用导出
      *
-     * @param outputStream outputStream
-     * @param columns      columns
-     * @param empty        true=>只带表头的excel,不含数据, false=>带数据的excel
-     * @return void
+     * @param outputStream       outputStream
+     * @param listVisibleColumns listVisibleColumns
+     * @param empty              true=>只带表头的excel,不含数据, false=>带数据的excel
+     * @param result             true直接导出结果, false需要检查列...
      */
-    public void exportExcelCommon(OutputStream outputStream, List<Map<String, Object>> list, List<GenTableColumn> columns, Boolean empty) {
+    public void exportExcelCommon(OutputStream outputStream, List<Map<String, Object>> list, List<GenTableColumn> listVisibleColumns
+            , Boolean empty, Boolean result) {
         try {
-            // 写入各个字段的列头名称 列表可见
-            List<GenTableColumn> listVisibleColumns = filterData(columns, 4, MaskConstant.LIST_VISIBLE::equals);
-
+//            list = sortMapByKey(list);
             // 取出一共有多少个sheet.
             double sheetNo = Math.ceil(list.size() / sheetSize);
             for (int index = 0; index <= sheetNo; index++) {
@@ -451,13 +449,26 @@ public class ExcelUtil<T>
                 // 产生一行
                 Row row = sheet.createRow(0);
                 int columnIndex = 0;
-                for (GenTableColumn column : listVisibleColumns) {
-                    createCellByColumn(column, row, columnIndex++);
-                }
 
-                if (BooleanUtils.isFalse(empty)) {
-                    if (Type.EXPORT.equals(type)) {
-                        fillExcelDataByJSONObject(index, row, listVisibleColumns);
+                // 直接导出结果,无需检查列
+                if (BooleanUtils.isTrue(result)) {
+                    Map<String, Object> map = list.get(0);
+                    for (Map.Entry<String, Object> entry : map.entrySet()) {
+                        String columnName = entry.getKey();
+                        columnName = covertHead(columnName);
+                        createCellByMap(columnName, row, columnIndex++);
+                    }
+
+                    if (BooleanUtils.isFalse(empty) && Type.EXPORT.equals(type)) {
+                        fillDataByListMap(index);
+                    }
+                } else {
+                    for (GenTableColumn column : listVisibleColumns) {
+                        createCellByColumn(column, row, columnIndex++);
+                    }
+
+                    if (BooleanUtils.isFalse(empty) && Type.EXPORT.equals(type)) {
+                        fillDataByColumns(index, row, listVisibleColumns);
 //                        addStatisticsRow();
                     }
                 }
@@ -467,19 +478,42 @@ public class ExcelUtil<T>
         } catch (Exception e) {
             log.error("导出Excel异常{}", e.getMessage());
         } finally {
-            if (wb != null) {
-                try {
-                    wb.close();
-                } catch (IOException e1) {
-                    e1.printStackTrace();
-                }
+            close(outputStream);
+        }
+    }
+
+    private String covertHead(String columnName) {
+        switch (columnName){
+            case "userName":
+                return "用户名";
+            case "deptName":
+                return "部门名称";
+            case "date":
+                return "日期";
+            case "attendanceTableLeaveSum":
+                return "早退";
+            case "attendanceTableLeaveOrSum":
+                return "请假";
+            case "attendanceTableLateSum":
+                return "迟到";
+            default:
+                return "";
+        }
+    }
+
+    private void close(OutputStream outputStream) {
+        if (wb != null) {
+            try {
+                wb.close();
+            } catch (IOException e1) {
+                e1.printStackTrace();
             }
-            if (outputStream != null) {
-                try {
-                    outputStream.close();
-                } catch (IOException e1) {
-                    e1.printStackTrace();
-                }
+        }
+        if (outputStream != null) {
+            try {
+                outputStream.close();
+            } catch (IOException e1) {
+                e1.printStackTrace();
             }
         }
     }
@@ -536,28 +570,7 @@ public class ExcelUtil<T>
         }
         finally
         {
-            if (wb != null)
-            {
-                try
-                {
-                    wb.close();
-                }
-                catch (IOException e1)
-                {
-                    e1.printStackTrace();
-                }
-            }
-            if (outputStream != null)
-            {
-                try
-                {
-                    outputStream.close();
-                }
-                catch (IOException e1)
-                {
-                    e1.printStackTrace();
-                }
-            }
+            close(outputStream);
         }
     }
 
@@ -595,14 +608,30 @@ public class ExcelUtil<T>
      * @param index 序号
      * @param row   单元格行
      */
-    public void fillExcelDataByJSONObject(int index, Row row, List<GenTableColumn> listVisibleColumns) {
+    public void fillDataByColumns(int index, Row row, List<GenTableColumn> listVisibleColumns) {
         int startNo = index * sheetSize;
         int endNo = Math.min(startNo + sheetSize, jsonObjectList.size());
         for (int i = startNo; i < endNo; i++) {
             row = sheet.createRow(i + 1 - startNo);
             Map<String, Object> jsonObject = jsonObjectList.get(i);
 
-            addCellByJSONObject(row, jsonObject, listVisibleColumns);
+            addCellByColumns(row, jsonObject, listVisibleColumns);
+        }
+    }
+
+
+    /**
+     * 填充excel数据
+     *  @param index 序号
+     *
+     */
+    public void fillDataByListMap(int index) {
+        int startNo = index * sheetSize;
+        int endNo = Math.min(startNo + sheetSize, jsonObjectList.size());
+        for (int i = startNo; i < endNo; i++) {
+            Row row = sheet.createRow(i + 1 - startNo);
+            Map<String, Object> jsonObject = jsonObjectList.get(i);
+            addCellByMap(row, jsonObject);
         }
     }
 
@@ -715,6 +744,32 @@ public class ExcelUtil<T>
         }
     }
 
+
+    /**
+     * 创建单元格
+     */
+    public void createCellByMap(String columnComment, Row row, int columnIndex) {
+        // 创建列
+        Cell cell = row.createCell(columnIndex);
+        // 写入列信息
+        cell.setCellValue(columnComment);
+        setDataValidationByColumnComment(columnComment, columnIndex);
+
+        CellStyle style = wb.createCellStyle();
+        style.cloneStyleFrom(styles.get("data"));
+        style.setAlignment(HorizontalAlignment.CENTER);
+        style.setVerticalAlignment(VerticalAlignment.CENTER);
+//            style.setFillForegroundColor(IndexedColors.GREY_50_PERCENT.getIndex());
+//            style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
+        Font headerFont = wb.createFont();
+        headerFont.setFontName("Arial");
+        headerFont.setFontHeightInPoints((short) 15);
+        headerFont.setBold(true);
+//        headerFont.setColor(IndexedColors.RED.getIndex());
+        style.setFont(headerFont);
+        cell.setCellStyle(style);
+    }
+
     /**
      * 设置单元格信息
      *
@@ -813,8 +868,19 @@ public class ExcelUtil<T>
             // 设置列宽
             sheet.setColumnWidth(columnIndex, (int) ((16 + 0.72) * 256));
         }
+    }
 
-
+    /**
+     * 创建表格样式
+     */
+    public void setDataValidationByColumnComment(String columnComment, int columnIndex) {
+        if (columnComment.contains("注:")) {
+            sheet.setColumnWidth(columnIndex, 6000);
+        } else {
+            // 设置列宽
+            sheet.setColumnWidth(columnIndex, (int) ((16 + 0.72) * 256));
+        }
+    }
         // 如果设置了提示信息则鼠标放上去提示.
 //        if (StringUtils.isNotEmpty(attr.prompt())) {
 //            // 这里默认设了2-101列提示.
@@ -825,12 +891,12 @@ public class ExcelUtil<T>
 //            // 这里默认设了2-101列只能选择不能输入.
 //            setXSSFValidation(sheet, attr.combo(), 1, 100, columnIndex, columnIndex);
 //        }
-    }
+    
 
     /**
      * 添加单元格
      */
-    public void addCellByJSONObject(Row row, Map<String, Object> jsonObject, List<GenTableColumn> listVisibleColumns) {
+    public void addCellByColumns(Row row, Map<String, Object> jsonObject, List<GenTableColumn> listVisibleColumns) {
         Cell cell;
         try {
             int columnIndex = 0;
@@ -897,6 +963,27 @@ public class ExcelUtil<T>
 
     }
 
+    /**
+     * 添加单元格
+     */
+    public void addCellByMap(Row row, Map<String, Object> jsonObject) {
+        Cell cell;
+        try {
+            int columnIndex = 0;
+            for (Map.Entry<String, Object> entry : jsonObject.entrySet()) {
+                Object value = entry.getValue();
+                // 设置行高
+                row.setHeight((short) 600);
+                // 创建cell
+                cell = row.createCell(columnIndex++);
+                cell.setCellStyle(styles.get("data"));
+                cell.setCellValue(String.valueOf(value));
+            }
+        } catch (Exception e) {
+            log.error("导出Excel失败: {}", e);
+        }
+    }
+
     /**
      * 添加单元格
      */
@@ -1423,4 +1510,25 @@ public class ExcelUtil<T>
         return sysDictData.getDictLabel();
     }
 
+    /**
+     * 使用 Map按key进行排序
+     *
+     * @param mapList
+     * @return mapList
+     */
+    public static List<Map<String, Object>> sortMapByKey(List<Map<String, Object>> mapList) {
+        if (mapList == null || mapList.isEmpty()) {
+            return null;
+        }
+
+        List<Map<String, Object>> result = new ArrayList<>(mapList.size());
+        for (Map<String, Object> map : mapList) {
+            Map<String, Object> sortMap = new TreeMap<>(map);
+
+            result.add(sortMap);
+        }
+
+        return result;
+    }
+
 }

+ 21 - 0
boman-modules/boman-file/src/main/java/com/boman/file/controller/ExcelController.java

@@ -60,5 +60,26 @@ public class ExcelController {
     }
 
 
+    /**
+     * 功能描述: 导出数据,只是sql不一样,其余的都是通用接口的内容
+     *
+     * @param response response
+     * @param dto      tableName
+     *                 empty(true=>只带表头的excel,不含数据, false=>带数据的excel)
+     *                 condition 查询条件
+     * @return com.boman.domain.dto.AjaxResult
+     */
+    @PostMapping("/export/statisticsByMonth")
+    public AjaxResult statisticsByMonth(HttpServletResponse response, @RequestBody ExportExcelDto dto) {
+        try {
+            return fileService.statisticsByMonth(response, dto);
+        } catch (Exception e) {
+            e.printStackTrace();
+        }
+
+        return AjaxResult.error("失败");
+    }
+
+
 
 }

+ 11 - 0
boman-modules/boman-file/src/main/java/com/boman/file/service/ISysFileService.java

@@ -61,4 +61,15 @@ public interface ISysFileService
      * @return com.boman.domain.dto.AjaxResult
      */
     AjaxResult exportExcelCommon(HttpServletResponse response, ExportExcelDto dto);
+
+    /**
+     * 功能描述: 导出数据,只是sql不一样,其余的都是通用接口的内容
+     *
+     * @param response response
+     * @param dto      tableName
+     *                 empty(true=>只带表头的excel,不含数据, false=>带数据的excel)
+     *                 condition 查询条件
+     * @return com.boman.domain.dto.AjaxResult
+     */
+    AjaxResult statisticsByMonth(HttpServletResponse response, ExportExcelDto dto);
 }

+ 74 - 2
boman-modules/boman-file/src/main/java/com/boman/file/service/LocalSysFileServiceImpl.java

@@ -13,6 +13,7 @@ import com.boman.domain.dto.ExportExcelDto;
 import com.boman.domain.dto.FormDataDto;
 import com.boman.domain.dto.ImportExcelDto;
 import com.boman.file.utils.FileUploadUtils;
+import com.boman.web.core.api.RemoteAttendanceService;
 import com.boman.web.core.api.RemoteObjService;
 import org.apache.commons.lang3.BooleanUtils;
 import org.slf4j.Logger;
@@ -47,6 +48,8 @@ public class LocalSysFileServiceImpl implements ISysFileService
     private RemoteObjService remoteObjService;
     @Resource
     private RedisService redisService;
+    @Resource
+    private RemoteAttendanceService remoteAttendanceService;
 
     /**
      * 资源映射路径 前缀
@@ -66,6 +69,15 @@ public class LocalSysFileServiceImpl implements ISysFileService
     @Value("${file.path}")
     private String localFilePath;
 
+    /******************************* 考勤表中的常量  *******************************/
+
+    public static final String USERNAME= "userName";
+    public static final String DEPT_NAME= "deptName";
+    public static final String ATTENDANCE_TABLE_LEAVE_OR_SUM= "attendanceTableLeaveOrSum";
+    public static final String ATTENDANCE_TABLE_LATE_SUM= "attendanceTableLateSum";
+    public static final String ATTENDANCE_TABLE_LEAVE_SUM= "attendanceTableLeaveSum";
+    public static final String DATE= "date";
+
     /**
      * 本地文件上传接口
      * 
@@ -158,14 +170,74 @@ public class LocalSysFileServiceImpl implements ISysFileService
         }
 
         try {
-            util.exportExcelCommon(response, list, "sheet1", columns, empty);
+            util.exportExcelCommon(response, list, "sheet1", columns, empty, false);
         } catch (IOException e) {
             e.printStackTrace();
         }
         return null;
     }
 
-    /**f
+    /**
+     * 功能描述: 导出数据,只是sql不一样,其余的都是通用接口的内容
+     *
+     * @param response response
+     * @param dto      tableName
+     *                 empty(true=>只带表头的excel,不含数据, false=>带数据的excel)
+     *                 condition 查询条件
+     * @return com.boman.domain.dto.AjaxResult
+     */
+    @Override
+    public AjaxResult statisticsByMonth(HttpServletResponse response, ExportExcelDto dto) {
+
+        String tableName = dto.getTableName();
+        Boolean empty = dto.getEmpty();
+        ObjectUtils.requireNonNull(tableName, "exportExcelCommon tableName is empty");
+        ObjectUtils.requireNonNull(empty, "exportExcelCommon empty is empty");
+
+        ExcelUtil<JSONObject> util = new ExcelUtil<>(JSONObject.class);
+        List<Map<String, Object>> list = null;
+        if (BooleanUtils.isTrue(empty)) {
+            list = new ArrayList<>(0);
+        } else {
+            AjaxResult ajaxResult = remoteAttendanceService.statisticsByMonth(dto.getCondition());
+            if (AjaxResult.checkSuccess(ajaxResult)) {
+                list = ((List<Map<String, Object>>) ajaxResult.get(AjaxResult.DATA_TAG));
+                // 导出不需要user_id, 同时把map中的value换成int
+                List<Map<String, Object>> newList = new ArrayList<>(list.size());
+                for (Map<String, Object> map : list) {
+                    map.remove("user_id");
+                    for (Map.Entry<String, Object> entry : map.entrySet()) {
+                        Object value = entry.getValue();
+                        if (value instanceof Double) {
+                            Double value1 = (Double) value;
+                            entry.setValue(value1.intValue());
+                        }
+                    }
+
+                    Map<String, Object> newMap = new LinkedHashMap<>(map.size());
+                    newMap.put(USERNAME, map.get(USERNAME));
+                    newMap.put(DEPT_NAME, map.get(DEPT_NAME));
+                    newMap.put(ATTENDANCE_TABLE_LATE_SUM, map.get(ATTENDANCE_TABLE_LATE_SUM));
+                    newMap.put(ATTENDANCE_TABLE_LEAVE_SUM, map.get(ATTENDANCE_TABLE_LEAVE_SUM));
+                    newMap.put(ATTENDANCE_TABLE_LEAVE_OR_SUM, map.get(ATTENDANCE_TABLE_LEAVE_OR_SUM));
+                    newMap.put(DATE, map.get(DATE));
+                    newList.add(newMap);
+                }
+
+                list = newList;
+            }
+        }
+
+        try {
+            util.exportExcelCommon(response, list, "sheet1", null, empty, true);
+        } catch (IOException e) {
+            LOGGER.error("考勤统计导出失败,导出数据为:{}", list);
+            e.printStackTrace();
+        }
+        return null;
+    }
+
+    /**
      * 功能描述: 通用的导入接口
      *
      * @param dto@return java.util.List<com.alibaba.fastjson.JSONObject>

+ 1 - 1
boman-web-core/src/main/java/com/boman/web/core/controller/AttendanceController.java

@@ -86,7 +86,7 @@ public class AttendanceController {
      * @return
      */
     @PostMapping("/statistics")
-    public AjaxResult statisticsByMonth(@RequestBody Map<String,String> map) {
+    public AjaxResult statisticsByMonth(@RequestBody Map<String, Object> map) {
         return service.statisticsByMonth(map);
     }
 

+ 1 - 1
boman-web-core/src/main/java/com/boman/web/core/mapper/StandardlyMapper.java

@@ -215,7 +215,7 @@ public interface StandardlyMapper {
             " d.dept_name,t.attendance_table_username,t.user_id,DATE_FORMAT( t.create_time, '%Y-%m' )\n" +
             "limit #{pageNo}, #{pageSize}" +
             "</script>")
-    List<JSONObject> statisticsByMonth(@Param("date") String date,@Param("pageNo") Long pageNo,@Param("pageSize") Long pageSize);
+    List<JSONObject> statisticsByMonth(@Param("date") String date,@Param("pageNo") int pageNo,@Param("pageSize") int pageSize);
 
     /**
      * 查询用户某月的上班打卡时间

+ 1 - 1
boman-web-core/src/main/java/com/boman/web/core/service/attendance/AttendanceService.java

@@ -66,7 +66,7 @@ public interface AttendanceService {
      * @param map
      * @return
      */
-    AjaxResult statisticsByMonth(Map<String,String> map);
+    AjaxResult statisticsByMonth(Map<String, Object> map);
 
     /**
      * 查询用户某月的上班打卡时间

+ 5 - 5
boman-web-core/src/main/java/com/boman/web/core/service/attendance/AttendanceServiceImpl.java

@@ -316,11 +316,11 @@ public class AttendanceServiceImpl implements AttendanceService {
      * @return
      */
     @Override
-    public AjaxResult statisticsByMonth(Map<String, String> map) {
-        String date = map.get("date");
-        String page = map.get("page");
-        Long pageSize = Long.parseLong(map.get("pageSize"));
-        Long pageNo = (Long.parseLong(page) - 1) * pageSize;
+    public AjaxResult statisticsByMonth(Map<String, Object> map) {
+        String date = String.valueOf(map.get("date"));
+        int page = ((Integer) map.get("page"));
+        int pageSize = ((Integer) map.get("pageSize"));
+        int pageNo = (page - 1) * pageSize;
         List<JSONObject> jsonObjects = standardlyMapper.statisticsByMonth(date, pageNo, pageSize);
         return AjaxResult.success(jsonObjects);
     }