liupeng 4 years ago
parent
commit
40d26534a2

+ 14 - 4
code/sapparent/sapcms/src/main/java/org/fouram/controller/ApiController.java

@@ -257,13 +257,23 @@ public class ApiController extends BaseController {
 	@ResponseBody
 	public Object exportUserReportExcel(HttpServletResponse response, @RequestBody FindUsersDTO dto) {
 		try {
+			if (StringUtils.isBlank(dto.getStartDate())) {
+				return ResultUtil.error("开始日期不可以为空");
+			}
+			if (StringUtils.isBlank(dto.getEndDate())) {
+				return ResultUtil.error("结束日期不可以为空");
+			}
+			String haveFilePath = "/public/excel/userReport.xlsx";
+			// 明细
+			String mxFilePath = "/public/excel/userReport" + DateUtil.getTimeStamp() + ".xlsx";
 			Object object = sapCheckService.findUserReports(dto.getStartDate(), dto.getEndDate(), dto.getDepartId(),
 					dto.getName(), dto.getState(), null, null);
 			List<Map<String, Object>> dataList = (List<Map<String, Object>>) object;
-			String haveFilePath = "/public/excel/userReport.xlsx";
-			String filePath = "/public/excel/userReport" + DateUtil.getTimeStamp() + ".xlsx";
-			ExcelExportUtil.exportExcelFile(getRootPath() + haveFilePath, dataList, getRootPath() + filePath, 1);
-			return ResultUtil.success(filePath, "操作成功");
+			ExcelExportUtil.exportExcelFile(getRootPath() + haveFilePath, dataList, getRootPath() + mxFilePath, 1, 0);
+			// 汇总
+			String allFilePath = "/public/excel/userReport" + DateUtil.getTimeStamp() + ".xlsx";
+			ExcelExportUtil.exportExcelFile(getRootPath() + mxFilePath, dataList, getRootPath() + allFilePath, 1, 1);
+			return ResultUtil.success(allFilePath, "操作成功");
 		} catch (Exception e) {
 			LoggerUtil.error(e);
 			return ResultUtil.error(ResultConstant.WEB_ERR_MSG);

BIN
code/sapparent/sapcms/src/main/webapp/public/excel/userReport.xlsx


+ 29 - 9
code/sapparent/sapservice/src/main/java/org/fouram/core/util/DateUtil.java

@@ -3,11 +3,14 @@ package org.fouram.core.util;
 import java.text.DateFormat;
 import java.text.ParseException;
 import java.text.SimpleDateFormat;
+import java.util.ArrayList;
 import java.util.Calendar;
 import java.util.Date;
 import java.util.GregorianCalendar;
+import java.util.List;
 
 import org.apache.commons.lang.time.DurationFormatUtils;
+import org.apache.commons.lang3.StringUtils;
 
 public class DateUtil {
 
@@ -308,8 +311,8 @@ public class DateUtil {
 		return day < dayArr[month - 1] ? constellationArr[month - 1] : constellationArr[month];
 	}
 
-	public static void main(String[] args) throws ParseException {
-		System.out.println(getLastDay("2020-05"));
+	public static void main(String[] args) throws Exception {
+		System.out.println(getSubDateList("2020-06-01", "2020-06-11"));
 	}
 
 	// 根据开始日期 增加 月份后 算出 日期(yyyy-mm)
@@ -442,22 +445,39 @@ public class DateUtil {
 	}
 
 	public static String getLastDay(String yearMonth) {
-		int year = Integer.parseInt(yearMonth.split("-")[0]);  //年
-		int month = Integer.parseInt(yearMonth.split("-")[1]); //月
+		int year = Integer.parseInt(yearMonth.split("-")[0]); // 
+		int month = Integer.parseInt(yearMonth.split("-")[1]); // 
 		Calendar cal = Calendar.getInstance();
 		// 设置年份
 		cal.set(Calendar.YEAR, year);
 		// 设置月份
 		// cal.set(Calendar.MONTH, month - 1);
-		cal.set(Calendar.MONTH, month); //设置当前月的上一个月
+		cal.set(Calendar.MONTH, month); // 设置当前月的上一个月
 		// 获取某月最大天数
-		//int lastDay = cal.getActualMaximum(Calendar.DATE);
-		int lastDay = cal.getMinimum(Calendar.DATE); //获取月份中的最小值,即第一天
+		// int lastDay = cal.getActualMaximum(Calendar.DATE);
+		int lastDay = cal.getMinimum(Calendar.DATE); // 获取月份中的最小值,即第一天
 		// 设置日历中月份的最大天数
-		//cal.set(Calendar.DAY_OF_MONTH, lastDay);
-		cal.set(Calendar.DAY_OF_MONTH, lastDay - 1); //上月的第一天减去1就是当月的最后一天
+		// cal.set(Calendar.DAY_OF_MONTH, lastDay);
+		cal.set(Calendar.DAY_OF_MONTH, lastDay - 1); // 上月的第一天减去1就是当月的最后一天
 		// 格式化日期
 		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
 		return sdf.format(cal.getTime());
 	}
+
+	public static List<String> getSubDateList(String startDate, String endDate) throws Exception {
+		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
+		List<String> list = new ArrayList<>(); // 保存日期的集合
+		Date date_start = sdf.parse(startDate);
+		Date date_end = sdf.parse(endDate);
+		Date date = date_start;
+		Calendar cd = Calendar.getInstance();// 用Calendar 进行日期比较判断
+		while (date.getTime() <= date_end.getTime()) {
+			list.add(sdf.format(date));
+			cd.setTime(date);
+			cd.add(Calendar.DATE, 1);// 增加一天 放入集合
+			date = cd.getTime();
+		}
+		StringUtils.strip(list.toString(), "[]");// 去掉符号[]
+		return list;
+	}
 }

+ 7 - 2
code/sapparent/sapservice/src/main/java/org/fouram/core/util/ExcelExportUtil.java

@@ -35,7 +35,7 @@ public class ExcelExportUtil {
 		// 设置表格标题栏的样式
 		XSSFCellStyle titleStyle = workbook.createCellStyle();
 //		titleStyle.setFillBackgroundColor(color);
-		 titleStyle.setFillForegroundColor(HSSFColor.BRIGHT_GREEN.index);
+		titleStyle.setFillForegroundColor(HSSFColor.BRIGHT_GREEN.index);
 		titleStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
 		titleStyle.setBorderBottom(BorderStyle.THIN);
 		titleStyle.setBorderLeft(BorderStyle.THIN);
@@ -138,6 +138,11 @@ public class ExcelExportUtil {
 
 	public static void exportExcelFile(String haveFilePath, List<Map<String, Object>> dataList, String filePath,
 			int index) {
+		exportExcelFile(haveFilePath, dataList, filePath, index, 0);
+	}
+
+	public static void exportExcelFile(String haveFilePath, List<Map<String, Object>> dataList, String filePath,
+			int index, int sheetIndex) {
 		// 声明一个工作薄
 		File f = new File(haveFilePath);
 		XSSFWorkbook workbook = null;
@@ -148,7 +153,7 @@ public class ExcelExportUtil {
 			e.printStackTrace();
 		}
 		// 生成一个表格
-		XSSFSheet sheet = workbook.getSheetAt(0);
+		XSSFSheet sheet = workbook.getSheetAt(sheetIndex);
 		// 设置表格默认列宽度为15个字节
 		sheet.setDefaultColumnWidth(15);
 

+ 1 - 6
code/sapparent/sapservice/src/main/java/org/fouram/mapper/SapCheckMapper.xml

@@ -76,12 +76,7 @@
 		order by a.userId ASC, b.checkinDay asc;
 	</insert>
 	<select id="selectReportDayList" resultType="Map">
-		SELECT a.userId, a.name, 
-		replace(group_concat(case when a.checkinDay = '2020-05-11' then a.result else '' end ),',','') as '2020-05-11',
-		replace(group_concat(case when a.checkinDay = '2020-05-12' then a.result else '' end ),',','') as '2020-05-12',
-		replace(group_concat(case when a.checkinDay = '2020-05-13' then a.result else '' end ),',','') as '2020-05-13',
-		replace(group_concat(case when a.checkinDay = '2020-05-14' then a.result else '' end ),',','') as '2020-05-14',
-		replace(group_concat(case when a.checkinDay = '2020-05-15' then a.result else '' end ),',','') as '2020-05-15'
+		SELECT a.userId, a.name, ${reportDaySql}
 		from sap_check_report a, sap_checkdate b 
 		where a.checkinDay=b.checkinDay and a.checkinDay >= #{startDay} and a.checkinDay &lt;= #{endDay}
 		group by a.userId, a.name;

+ 32 - 6
code/sapparent/sapservice/src/main/java/org/fouram/service/SapCheckService.java

@@ -64,7 +64,7 @@ public class SapCheckService extends BaseService {
 		update("SapCheckMapper.saveCheckoutStart1", null);
 		update("SapCheckMapper.saveCheckoutStart2", null);
 		update("SapCheckMapper.saveCheckoutEnd", null);
-		
+
 		// 删除重复数据
 		updateRepeatCheckDelete();
 	}
@@ -240,9 +240,35 @@ public class SapCheckService extends BaseService {
 		}
 		return (List<Map<String, String>>) findList("SapCheckMapper.findUserReports", pd);
 	}
-	
-	public Long findUserReportTotal(String startDate, String endDate, String departId, String name,
-			String state) throws Exception {
+
+	@SuppressWarnings("unchecked")
+	public List<Map<String, String>> selectReportDayList(String startDate, String endDate, String departId, String name)
+			throws Exception {
+		if (StringUtils.isNotBlank(departId) && departId.endsWith(",")) {
+			departId = departId.substring(0, departId.length() - 1);
+		}
+		Map<String, String> pd = Maps.newHashMap();
+		pd.put("startDate", startDate);
+		pd.put("endDate", endDate);
+		pd.put("departId", departId);
+		pd.put("name", name);
+		pd.put("reportDaySql", getReportDaySql(startDate, endDate));
+		return (List<Map<String, String>>) findList("SapCheckMapper.selectReportDayList", pd);
+	}
+
+	private String getReportDaySql(String startDate, String endDate) throws Exception {
+		String baseSql = "replace(group_concat(case when a.checkinDay = '%s' then a.result else '' end ),',','') as '%s',";
+		List<String> days = DateUtil.getSubDateList(startDate, endDate);
+		String reportDaySql = "";
+		for (String day : days) {
+			reportDaySql += String.format(baseSql, day, day);
+		}
+		reportDaySql = reportDaySql.substring(0, reportDaySql.length() - 1);
+		return reportDaySql;
+	}
+
+	public Long findUserReportTotal(String startDate, String endDate, String departId, String name, String state)
+			throws Exception {
 		if (StringUtils.isNotBlank(departId) && departId.endsWith(",")) {
 			departId = departId.substring(0, departId.length() - 1);
 		}
@@ -261,8 +287,8 @@ public class SapCheckService extends BaseService {
 		pd.put("state", state);
 		return (Long) findObject("SapCheckMapper.findUserReportTotal", pd);
 	}
-	
-	public static JSONObject getPageResult(List<? extends Object> list, Object total){
+
+	public static JSONObject getPageResult(List<? extends Object> list, Object total) {
 		JSONObject object = new JSONObject();
 		object.put("total", total);
 		object.put("data", list);

+ 1 - 1
code/sapparent/sapservice/src/main/resources/spring/mybatis-config.xml

@@ -12,7 +12,7 @@
 		<!-- 驼峰转换 -->
 		<setting name="mapUnderscoreToCamelCase" value="true"/>
 		<!-- 打印查询语句 -->
-		<!-- <setting name="logImpl" value="STDOUT_LOGGING" /> -->
+		<setting name="logImpl" value="STDOUT_LOGGING" />
 	</settings>
 
 	<typeAliases>