Commit ec039315 authored by nie'hong's avatar nie'hong

添加导出

parent 1346ecd3
...@@ -51,10 +51,10 @@ public interface WorkTimeOrderMapper extends BaseMapper<WorkTimeOrder> { ...@@ -51,10 +51,10 @@ public interface WorkTimeOrderMapper extends BaseMapper<WorkTimeOrder> {
/** /**
* @param year * @param year
* @param month * @param month
* @param userId * @param userIds
* @return * @return
*/ */
List<DayWorkTimeAndType> listByDateAndUserId(@Param("year") Integer year, @Param("month") Integer month, @Param("userId") Integer userId); List<DayWorkTimeAndType> listByDateAndUserId(@Param("year") Integer year, @Param("month") Integer month, @Param("userIds") List<Integer> userIds);
/** /**
......
package cn.wisenergy.mapper; package cn.wisenergy.mapper;
import cn.wisenergy.model.app.WorkUser; import cn.wisenergy.model.app.WorkUser;
import cn.wisenergy.model.dto.StatisticsTableDto;
import com.baomidou.mybatisplus.core.mapper.BaseMapper; import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import java.util.List;
import java.util.Map; import java.util.Map;
/** /**
...@@ -26,5 +28,6 @@ public interface WorkUserMapper extends BaseMapper<WorkUser> { ...@@ -26,5 +28,6 @@ public interface WorkUserMapper extends BaseMapper<WorkUser> {
Integer updateUserInfo(WorkUser user); Integer updateUserInfo(WorkUser user);
List<StatisticsTableDto> getStatisticsTableDtos(List<Integer> userIds);
} }
...@@ -47,6 +47,7 @@ ...@@ -47,6 +47,7 @@
</sql> </sql>
<resultMap id="dayWorkTimeAndType" type="cn.wisenergy.model.dto.DayWorkTimeAndType"> <resultMap id="dayWorkTimeAndType" type="cn.wisenergy.model.dto.DayWorkTimeAndType">
<result property="userId" column="user_id"/>
<result property="day" column="day(work_day)"/> <result property="day" column="day(work_day)"/>
<result property="type" column="type"/> <result property="type" column="type"/>
<result property="workTime" column="sum(work_time)"/> <result property="workTime" column="sum(work_time)"/>
...@@ -143,10 +144,13 @@ ...@@ -143,10 +144,13 @@
</select> </select>
<select id="listByDateAndUserId" resultMap="dayWorkTimeAndType"> <select id="listByDateAndUserId" resultMap="dayWorkTimeAndType">
select user_id,day(work_day),CASE when `type` = 3 then 1 when `type` = 4 then 2 else 0 end AS `type`, sum(work_time) select user_id,day(work_day),CASE when `type` = 3 then "请假" when `type` = 4 then "调休" else "正常" end AS `type`, sum(work_time)
from <include refid="table"/> from <include refid="table"/>
where year(work_day) = #{year} AND month(work_day) = #{month} AND user_id = #{userId} AND status in (2,5) where year(work_day) = #{year} AND month(work_day) = #{month} AND (status = 2 OR status=5)AND user_id IN
group by user_id,day(work_day), CASE when `type` = 3 then 1 when `type` = 4 then 2 else 0 end <foreach collection="userIds" separator="," close=")" item="userId" open="(">
#{userId}
</foreach>
group by user_id,day(work_day), CASE when `type` = 3 then "请假" when `type` = 4 then "调休" else "正常" end
order by day(work_day) order by day(work_day)
</select> </select>
......
...@@ -85,6 +85,14 @@ ...@@ -85,6 +85,14 @@
from <include refid="table"/> from <include refid="table"/>
where id = #{userId} where id = #{userId}
</select> </select>
<select id="getStatisticsTableDtos" resultType="cn.wisenergy.model.dto.StatisticsTableDto">
select u.id AS user_id, u.name AS dept_name, d.dept_name
from work_user u join work_dept d on u.dept_id=d.id
where u.id in
<foreach collection="list" open="(" close=")" separator="," item="id">
#{id}
</foreach>
</select>
<update id="updateUserInfo" parameterType="cn.wisenergy.model.app.WorkUser"> <update id="updateUserInfo" parameterType="cn.wisenergy.model.app.WorkUser">
update <include refid="table"/> update <include refid="table"/>
......
...@@ -17,6 +17,12 @@ public class DayWorkTimeAndType implements Serializable { ...@@ -17,6 +17,12 @@ public class DayWorkTimeAndType implements Serializable {
private static final long serialVersionUID = 1198786417184957375L; private static final long serialVersionUID = 1198786417184957375L;
/**
* 用户主键
*/
@ApiModelProperty(name = "userId", value = "用户主键")
private Integer userId;
/** /**
* 日期中的日 * 日期中的日
*/ */
...@@ -24,10 +30,10 @@ public class DayWorkTimeAndType implements Serializable { ...@@ -24,10 +30,10 @@ public class DayWorkTimeAndType implements Serializable {
private Integer day; private Integer day;
/** /**
* 工时类型,0:正常,1:请假,2:调休 * 工时类型,正常,请假,调休
*/ */
@ApiModelProperty(name = "type", value = "工时类型,0:正常,1:请假,2:调休") @ApiModelProperty(name = "type", value = "工时类型,正常,请假,调休")
private Integer type; private String type;
/** /**
* 工时 * 工时
......
...@@ -3,6 +3,7 @@ package cn.wisenergy.service; ...@@ -3,6 +3,7 @@ package cn.wisenergy.service;
import cn.wisenergy.model.dto.GetMonthlyCollectParam; import cn.wisenergy.model.dto.GetMonthlyCollectParam;
import cn.wisenergy.model.dto.WorkTimeAndCostCollect; import cn.wisenergy.model.dto.WorkTimeAndCostCollect;
import com.github.pagehelper.PageInfo; import com.github.pagehelper.PageInfo;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import java.util.List; import java.util.List;
...@@ -27,4 +28,12 @@ public interface StatisticsService { ...@@ -27,4 +28,12 @@ public interface StatisticsService {
* @param userId * @param userId
*/ */
List<WorkTimeAndCostCollect> getCurrentMonthWorkTimeCollect(Integer userId, String startTime); List<WorkTimeAndCostCollect> getCurrentMonthWorkTimeCollect(Integer userId, String startTime);
/**
* 导出一个部门下员工一个月中每天的工时
* @param param
* @return
*/
HSSFWorkbook exportWorkTimeExcel(GetMonthlyCollectParam param);
} }
...@@ -2,21 +2,32 @@ package cn.wisenergy.service.impl; ...@@ -2,21 +2,32 @@ package cn.wisenergy.service.impl;
import cn.hutool.core.collection.CollectionUtil; import cn.hutool.core.collection.CollectionUtil;
import cn.wisenergy.common.utils.DateUtil; import cn.wisenergy.common.utils.DateUtil;
import cn.wisenergy.common.utils.ExcelUtils;
import cn.wisenergy.common.utils.exception.BASE_RESP_CODE_ENUM; import cn.wisenergy.common.utils.exception.BASE_RESP_CODE_ENUM;
import cn.wisenergy.common.utils.exception.BaseCustomException; import cn.wisenergy.common.utils.exception.BaseCustomException;
import cn.wisenergy.mapper.WorkHolidayMapper; import cn.wisenergy.mapper.WorkHolidayMapper;
import cn.wisenergy.mapper.WorkTimeOrderMapper; import cn.wisenergy.mapper.WorkTimeOrderMapper;
import cn.wisenergy.mapper.WorkUserDeptMapper; import cn.wisenergy.mapper.WorkUserDeptMapper;
import cn.wisenergy.mapper.WorkUserMapper;
import cn.wisenergy.model.app.WorkHoliday; import cn.wisenergy.model.app.WorkHoliday;
import cn.wisenergy.model.app.WorkProject; import cn.wisenergy.model.app.WorkProject;
import cn.wisenergy.model.app.WorkUser; import cn.wisenergy.model.app.WorkUser;
import cn.wisenergy.model.dto.*; import cn.wisenergy.model.dto.*;
import cn.wisenergy.model.enums.*; import cn.wisenergy.model.enums.HolidayTypeEnum;
import cn.wisenergy.service.*; import cn.wisenergy.model.enums.ManagerEnum;
import cn.wisenergy.model.enums.WorkOrderStatus;
import cn.wisenergy.model.enums.WorkOrderType;
import cn.wisenergy.service.StatisticsService;
import cn.wisenergy.service.WorkDeptService;
import cn.wisenergy.service.WorkProjectService;
import cn.wisenergy.service.WorkUserService;
import com.alibaba.excel.util.StringUtils; import com.alibaba.excel.util.StringUtils;
import com.github.pagehelper.PageHelper; import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo; import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.VerticalAlignment;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
...@@ -50,6 +61,11 @@ public class StatisticsServiceImpl implements StatisticsService { ...@@ -50,6 +61,11 @@ public class StatisticsServiceImpl implements StatisticsService {
@Autowired @Autowired
private WorkHolidayMapper workHolidayMapper; private WorkHolidayMapper workHolidayMapper;
@Autowired
private WorkUserMapper workUserMapper;
private final static Integer EXCEL_WIDTH = 1000 * 6;
private final static Integer DEFAULT_DEPT_ID = 1; private final static Integer DEFAULT_DEPT_ID = 1;
@Override @Override
...@@ -84,28 +100,32 @@ public class StatisticsServiceImpl implements StatisticsService { ...@@ -84,28 +100,32 @@ public class StatisticsServiceImpl implements StatisticsService {
param.setSize(10); param.setSize(10);
} }
//分页获取部门下所有员工主键 // 分页获取部门下所有员工主键
PageHelper.startPage(param.getCurrentPage(), param.getSize()); PageHelper.startPage(param.getCurrentPage(), param.getSize());
List<Integer> userIdList = workUserDeptMapper.listByDeptId(deptId); List<Integer> userIds = workUserDeptMapper.listByDeptId(deptId);
PageInfo pageInfo = new PageInfo(userIdList); PageInfo pageInfo = new PageInfo(userIds);
//获取员工一个月中已审核、自动审核的工单单日汇总汇总 // 获取员工一个月中已审核、自动审核的工单单日汇总汇总
List<StatisticsTableDto> statisticsTableDtos = new ArrayList<>(); List<StatisticsTableDto> statisticsTableDtos = workUserMapper.getStatisticsTableDtos(userIds);
for (Integer id : userIdList) { // 获取当月每天工时信息
WorkUser workUser = workUserService.getById(id); List<DayWorkTimeAndType> dayWorkTimeAndTypes = workTimeOrderMapper.listByDateAndUserId(year, month, userIds);
//获取当月每天工时信息
List<DayWorkTimeAndType> dayWorkTimeAndTypes = workTimeOrderMapper.listByDateAndUserId(year, month, id); // 将工单集合与对应用户信息封装在一起
//获取用户部门名称 Iterator<StatisticsTableDto> statisticsTableDtoIterator = statisticsTableDtos.iterator();
String deptName = workDeptService.getById(workUser.getDeptId()).getDeptName(); while (statisticsTableDtoIterator.hasNext()) {
//封装成对象 StatisticsTableDto statisticsTableDto = statisticsTableDtoIterator.next();
StatisticsTableDto statisticsTableDto = new StatisticsTableDto(); ArrayList<DayWorkTimeAndType> dayWorkTimeAndTypes1 = new ArrayList<>();
statisticsTableDto.setUserId(id); // 查询出的工时信息遍历
statisticsTableDto.setUserName(workUser.getName()); Iterator<DayWorkTimeAndType> workTimeAndTypeIterator = dayWorkTimeAndTypes.iterator();
statisticsTableDto.setDeptName(deptName); while (workTimeAndTypeIterator.hasNext()){
statisticsTableDto.setDayWorkTimeAndTypeList(dayWorkTimeAndTypes); DayWorkTimeAndType dayWorkTimeAndType = workTimeAndTypeIterator.next();
if (dayWorkTimeAndType.getUserId().equals(statisticsTableDto.getUserId())) {
dayWorkTimeAndTypes1.add(dayWorkTimeAndType);
}
}
statisticsTableDto.setDayWorkTimeAndTypeList(dayWorkTimeAndTypes1);
statisticsTableDtos.add(statisticsTableDto); statisticsTableDtos.add(statisticsTableDto);
} }
pageInfo.setList(statisticsTableDtos); pageInfo.setList(statisticsTableDtos);
return pageInfo; return pageInfo;
} }
...@@ -124,15 +144,14 @@ public class StatisticsServiceImpl implements StatisticsService { ...@@ -124,15 +144,14 @@ public class StatisticsServiceImpl implements StatisticsService {
user.getLevel().equals(ManagerEnum.IS_DEPARTMENT_DIRECTOR.getCode())) { user.getLevel().equals(ManagerEnum.IS_DEPARTMENT_DIRECTOR.getCode())) {
//以审批人、审核状态、开始时间查询本月审批数量 //以审批人、审核状态、开始时间查询本月审批数量
map.put("reviewerId", userId); map.put("reviewerId", userId);
map.put("status", WorkOrderStatus.ALREADY_AUDIT.getCode());
map.put("startDay", firstDayOfMonth); map.put("startDay", firstDayOfMonth);
Integer completedCount = workTimeOrderMapper.getCountByCondition(map); Integer completedCount = workTimeOrderMapper.getCountByCondition(map);
//本月驳回未重报数量 //本月驳回未重报数量
map.replace("status", WorkOrderStatus.TURN_DOWN.getCode()); map.put("status", WorkOrderStatus.TURN_DOWN.getCode());
Integer rejectAndNotResubmit = workTimeOrderMapper.getCountByCondition(map); Integer rejectAndNotResubmit = workTimeOrderMapper.getCountByCondition(map);
//项目级别,获取所管理项目, // 项目级别,获取所管理项目,
if (user.getLevel().equals(ManagerEnum.IS_PROJECT_DIRECTOR.getCode())) { if (user.getLevel().equals(ManagerEnum.IS_PROJECT_DIRECTOR.getCode())) {
List<WorkProject> projects = workProjectService.getUserManageProjects(userId); List<WorkProject> projects = workProjectService.getUserManageProjects(userId);
if (CollectionUtil.isNotEmpty(projects)) { if (CollectionUtil.isNotEmpty(projects)) {
...@@ -140,7 +159,7 @@ public class StatisticsServiceImpl implements StatisticsService { ...@@ -140,7 +159,7 @@ public class StatisticsServiceImpl implements StatisticsService {
map.put("projectIds", projectIds); map.put("projectIds", projectIds);
} }
} }
//部门级别 // 部门级别
if (user.getLevel().equals(ManagerEnum.IS_DEPARTMENT_DIRECTOR.getCode())) { if (user.getLevel().equals(ManagerEnum.IS_DEPARTMENT_DIRECTOR.getCode())) {
map.put("deptId", user.getDeptId()); map.put("deptId", user.getDeptId());
//填报项目类型编码 //填报项目类型编码
...@@ -213,6 +232,70 @@ public class StatisticsServiceImpl implements StatisticsService { ...@@ -213,6 +232,70 @@ public class StatisticsServiceImpl implements StatisticsService {
return collect; return collect;
} }
@Override
public HSSFWorkbook exportWorkTimeExcel(GetMonthlyCollectParam param) {
log.info("StatisticsServiceImpl[]exportWorkTimeExcel[]input.param{}" + param);
PageInfo dayWorkTimeOfMonth = getDayWorkTimeOfMonth(param);
Date date = DateUtil.convertStrToDate(param.getYear() + "-" + param.getMonth(), "yyyy-MM");
// 传参的时间当月总天数
int dayCount = DateUtil.getMonthDaysArray(date).length;
// 导出的信息
List<StatisticsTableDto> list = dayWorkTimeOfMonth.getList();
// 创建工作簿
HSSFWorkbook sheets = new HSSFWorkbook();
// 创建表
HSSFSheet sheet = sheets.createSheet("Sheet1");
// 样式对象
HSSFCellStyle cellStyle = sheets.createCellStyle();
// 垂直、水平中心对齐
cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
cellStyle.setAlignment(HorizontalAlignment.CENTER);
// 第一列列宽
sheet.setColumnWidth(1, EXCEL_WIDTH);
// 创建一行
HSSFRow row1 = sheet.createRow(0);
// 给第一行赋值
row1.createCell(0).setCellValue("序号");
row1.createCell(1).setCellValue("部门名称");
row1.createCell(2).setCellValue("姓名");
for (int i = 1; i <= dayCount; i++) {
HSSFCell cell = row1.createCell(2 + i);
cell.setCellValue(i + "日");
cell.setCellStyle(cellStyle);
}
Iterator<StatisticsTableDto> iterator = list.iterator();
int i = 1;
while (iterator.hasNext()) {
StatisticsTableDto next = iterator.next();
// 创建对应的行
HSSFRow row_i = sheet.createRow(i);
// 序号
row_i.createCell(0).setCellValue(i);
// 部门名称
row_i.createCell(1).setCellValue(next.getDeptName());
// 姓名
row_i.createCell(2).setCellValue(next.getUserName());
// 日期与工时集合
List<DayWorkTimeAndType> timeAndTypes = next.getDayWorkTimeAndTypeList();
// 在excel中对应日期单元格添加工时
for (DayWorkTimeAndType timeAndType : timeAndTypes) {
HSSFCell cell = row_i.getCell(timeAndType.getDay() + 2);
String cellValue = ExcelUtils.getCellValue(cell, true);
// 单元格为空则直接写入,否则追加
if (!StringUtils.isEmpty(cellValue)) {
cell.setCellValue(timeAndType.getType() + timeAndType.getWorkTime().toString() + "\n" + cellValue);
} else {
HSSFCell emptyCell = row_i.createCell(timeAndType.getDay() + 2);
emptyCell.setCellValue(timeAndType.getType() + timeAndType.getWorkTime().toString());
emptyCell.setCellStyle(cellStyle);
}
}
// 换行
i++;
}
return sheets;
}
//获取本月应上班日期 //获取本月应上班日期
public List<Date> currentMonthWorkDays() { public List<Date> currentMonthWorkDays() {
log.info("StatisticsServiceImpl[]currentMonthWorkDays[]input.method"); log.info("StatisticsServiceImpl[]currentMonthWorkDays[]input.method");
...@@ -241,7 +324,7 @@ public class StatisticsServiceImpl implements StatisticsService { ...@@ -241,7 +324,7 @@ public class StatisticsServiceImpl implements StatisticsService {
if (specialDate.getDayType().equals(HolidayTypeEnum.WORKING_DAY.getCode())) { if (specialDate.getDayType().equals(HolidayTypeEnum.WORKING_DAY.getCode())) {
//在本月应上班集合中加上这天,一般都是放假前后的周末调整上班 //在本月应上班集合中加上这天,一般都是放假前后的周末调整上班
workDays.addAll(isWorkDays); workDays.addAll(isWorkDays);
}else { } else {
//否则就除去假日 //否则就除去假日
workDays.removeAll(isWorkDays); workDays.removeAll(isWorkDays);
} }
...@@ -253,7 +336,7 @@ public class StatisticsServiceImpl implements StatisticsService { ...@@ -253,7 +336,7 @@ public class StatisticsServiceImpl implements StatisticsService {
public List<Date> currentMonthFilledDays(Integer userId) { public List<Date> currentMonthFilledDays(Integer userId) {
log.info("StatisticsServiceImpl[]currentMonthFilledDays[]input.method"); log.info("StatisticsServiceImpl[]currentMonthFilledDays[]input.method");
Date firstDayOfMonth = DateUtil.getCurrentFirstDayOfMonth(); Date firstDayOfMonth = DateUtil.getCurrentFirstDayOfMonth();
List<Date> currentMonthFilledDays = workTimeOrderMapper.getDaysByDateAndStatus(firstDayOfMonth,userId); List<Date> currentMonthFilledDays = workTimeOrderMapper.getDaysByDateAndStatus(firstDayOfMonth, userId);
return currentMonthFilledDays; return currentMonthFilledDays;
} }
......
...@@ -14,11 +14,15 @@ import io.swagger.annotations.Api; ...@@ -14,11 +14,15 @@ import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam; import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation; import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletResponse;
import java.net.URLEncoder;
import java.util.List; import java.util.List;
/** /**
...@@ -77,4 +81,33 @@ public class StatisticsController extends BaseController { ...@@ -77,4 +81,33 @@ public class StatisticsController extends BaseController {
return getResult(monthlyStatistics); return getResult(monthlyStatistics);
} }
@ApiOperation(value = "导出部门员工一个月每天的工时", notes = "导出部门员工一个月每天的工时")
@GetMapping("/exportWorkTime")
public void export(GetMonthlyCollectParam param, HttpServletResponse response) {
log.info("StatisticsController[]getDeptInfo[]input.param" + param);
// 获取excel
HSSFWorkbook sheets = statisticsService.exportWorkTimeExcel(param);
// 获取excel中的第2行第2列中的部门名称
String deptName = sheets.getSheet("Sheet1").getRow(1).getCell(1).getStringCellValue();
// 文件名
String fileName = deptName + "-" + param.getYear() + "-" + param.getMonth();
//响应类型为application/octet- stream情况下使用了这个头信息的话,那就意味着不想直接显示内容
response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE);
//attachment为以附件方式下载
try {
response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(
fileName + ".xls",
"utf-8"));
response.setHeader("Cache-Control", "No-cache");
response.flushBuffer();
sheets.write(response.getOutputStream());
sheets.close();
} catch (Exception e) {
throw new BaseCustomException(BASE_RESP_CODE_ENUM.SERVER_ERROR);
}
}
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment