157 lines
5.4 KiB
Java

package com.designhub.service;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.designhub.entity.*;
import com.designhub.mapper.*;
import com.designhub.util.ExcelExportUtil;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import java.io.IOException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.stream.Collectors;
/**
* 数据导出Service
*/
@Service
@RequiredArgsConstructor
public class ExportService {
private final TaskMapper taskMapper;
private final UserMapper userMapper;
private final DepartmentMapper departmentMapper;
private final DesignerService designerService;
private static final DateTimeFormatter DATE_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
/**
* 导出任务列表
*/
public void exportTasks(HttpServletResponse response, String status, Long publisherId) throws IOException {
// 查询任务数据
LambdaQueryWrapper<Task> queryWrapper = new LambdaQueryWrapper<>();
if (status != null && !status.isEmpty()) {
queryWrapper.eq(Task::getStatus, status);
}
if (publisherId != null) {
queryWrapper.eq(Task::getPublisherId, publisherId);
}
queryWrapper.orderByDesc(Task::getCreateTime);
List<Task> tasks = taskMapper.selectList(queryWrapper);
// 定义表头
String[] headers = {
"任务ID", "任务标题", "发布人", "接收人", "任务类型", "任务状态",
"预估积分", "任务时效(h)", "开始时间", "截止时间", "创建时间"
};
// 定义字段名
String[] fieldNames = {
"id", "title", "publisherId", "receiverId", "demandTypeId", "status",
"scoreReward", "estimatedDuration", "actualStartTime", "deadline", "createTime"
};
// 生成文件名
String fileName = "任务列表_" + LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMddHHmmss"));
// 导出Excel
ExcelExportUtil.exportExcel(response, fileName, "任务列表", headers, fieldNames, tasks);
}
/**
* 导出用户列表
*/
public void exportUsers(HttpServletResponse response, Long deptId, Integer status) throws IOException {
// 查询用户数据
LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
if (deptId != null) {
queryWrapper.eq(User::getDeptId, deptId);
}
if (status != null) {
queryWrapper.eq(User::getStatus, status);
}
queryWrapper.orderByDesc(User::getCreateTime);
List<User> users = userMapper.selectList(queryWrapper);
// 定义表头
String[] headers = {
"用户ID", "用户名", "真实姓名", "手机号", "邮箱", "部门ID",
"状态", "创建时间"
};
// 定义字段名
String[] fieldNames = {
"id", "username", "realName", "phone", "email", "deptId",
"status", "createTime"
};
// 生成文件名
String fileName = "用户列表_" + LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMddHHmmss"));
// 导出Excel
ExcelExportUtil.exportExcel(response, fileName, "用户列表", headers, fieldNames, users);
}
/**
* 导出部门列表
*/
public void exportDepartments(HttpServletResponse response) throws IOException {
// 查询部门数据
List<Department> departments = departmentMapper.selectList(
new LambdaQueryWrapper<Department>()
.orderByAsc(Department::getSortOrder)
);
// 定义表头
String[] headers = {
"部门ID", "部门名称", "部门编码", "部门类型", "父部门ID",
"描述", "排序", "状态", "创建时间"
};
// 定义字段名
String[] fieldNames = {
"id", "deptName", "deptCode", "deptType", "parentId",
"description", "sortOrder", "status", "createTime"
};
// 生成文件名
String fileName = "部门列表_" + LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMddHHmmss"));
// 导出Excel
ExcelExportUtil.exportExcel(response, fileName, "部门列表", headers, fieldNames, departments);
}
/**
* 导出设计师统计数据
*/
public void exportDesignerStats(HttpServletResponse response) throws IOException {
// 获取设计师统计数据
var stats = designerService.getAllDesignerStats();
// 定义表头
String[] headers = {
"设计师ID", "设计师姓名", "当前任务数", "排队任务数", "已完成任务数",
"累计积分", "延误数量", "好评数", "好评率(%)"
};
// 定义字段名
String[] fieldNames = {
"designerId", "designerName", "currentTasks", "queueTasks", "completedTasks",
"totalScore", "delayCount", "goodReviews", "goodReviewRate"
};
// 生成文件名
String fileName = "设计师统计_" + LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMddHHmmss"));
// 导出Excel
ExcelExportUtil.exportExcel(response, fileName, "设计师统计", headers, fieldNames, stats);
}
}