补齐店务端改密导出员工订单

This commit is contained in:
2026-07-16 12:00:06 +08:00
parent 08d24853a6
commit a9a23bfa6d
28 changed files with 768 additions and 33 deletions
@@ -0,0 +1,47 @@
package com.xiaowang.jayucloud.core.modules.manager.controller.admin;
import com.xiaowang.jayucloud.core.annotation.RequirePermission;
import com.xiaowang.jayucloud.core.modules.manager.service.ManagerExportService;
import com.xiaowang.jayucloud.core.modules.security.support.AdminScopeHelper;
import com.xiaowang.jayucloud.core.modules.user.entity.UserDO;
import lombok.RequiredArgsConstructor;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.http.ContentDisposition;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest;
import java.nio.charset.StandardCharsets;
import java.time.LocalDate;
@RestController
@RequestMapping("/admin")
@RequiredArgsConstructor
public class AdminExportController {
private final ManagerExportService managerExportService;
private final AdminScopeHelper adminScopeHelper;
@GetMapping("/export")
@RequirePermission("order:view")
public ResponseEntity<byte[]> export(HttpServletRequest request,
@RequestParam String type,
@RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate startDate,
@RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate endDate) {
UserDO currentUser = adminScopeHelper.getCurrentUser(request);
byte[] content = managerExportService.export(currentUser, type, startDate, endDate);
String fileName = type + "_" + startDate + "_" + endDate + ".xlsx";
return ResponseEntity.ok()
.contentType(MediaType.parseMediaType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"))
.header(HttpHeaders.CONTENT_DISPOSITION, ContentDisposition.attachment()
.filename(fileName, StandardCharsets.UTF_8)
.build()
.toString())
.body(content);
}
}
@@ -0,0 +1,41 @@
package com.xiaowang.jayucloud.core.modules.manager.controller.admin;
import com.xiaowang.jayucloud.core.annotation.RequirePermission;
import com.xiaowang.jayucloud.core.common.Result;
import com.xiaowang.jayucloud.core.modules.planning.service.PlanningService;
import com.xiaowang.jayucloud.core.modules.planning.vo.PlanningAppointmentVO;
import com.xiaowang.jayucloud.core.modules.security.support.AdminScopeHelper;
import com.xiaowang.jayucloud.core.modules.user.entity.UserDO;
import lombok.RequiredArgsConstructor;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest;
import java.time.LocalDate;
import java.util.List;
@RestController
@RequestMapping("/admin/manager/employees")
@RequiredArgsConstructor
public class AdminManagerEmployeeOrderController {
private final PlanningService planningService;
private final AdminScopeHelper adminScopeHelper;
@GetMapping("/{userId}/orders")
@RequirePermission("order:view")
public Result<List<PlanningAppointmentVO>> orders(HttpServletRequest request,
@PathVariable Long userId,
@RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate startDate,
@RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate endDate) {
UserDO currentUser = adminScopeHelper.getCurrentUser(request);
if (!adminScopeHelper.isPlatformAdmin(currentUser)) {
adminScopeHelper.ensureUserAccessibleInCurrentShop(currentUser, userId, "无权查看其他店铺员工订单");
}
return Result.success(planningService.getCalendar(currentUser, startDate, endDate, userId).getAppointments());
}
}
@@ -0,0 +1,194 @@
package com.xiaowang.jayucloud.core.modules.manager.service;
import com.xiaowang.jayucloud.core.common.vo.PageResultVO;
import com.xiaowang.jayucloud.core.modules.artist.dto.NailArtistInfoDTO;
import com.xiaowang.jayucloud.core.modules.artist.service.NailArtistInfoService;
import com.xiaowang.jayucloud.core.modules.planning.service.PlanningService;
import com.xiaowang.jayucloud.core.modules.planning.vo.PlanningAppointmentVO;
import com.xiaowang.jayucloud.core.modules.planning.vo.PlanningCalendarVO;
import com.xiaowang.jayucloud.core.modules.security.support.AdminScopeHelper;
import com.xiaowang.jayucloud.core.modules.user.entity.UserDO;
import com.xiaowang.jayucloud.core.modules.user.service.UserService;
import com.xiaowang.jayucloud.core.modules.user.vo.UserVO;
import lombok.RequiredArgsConstructor;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
@Service
@RequiredArgsConstructor
public class ManagerExportService {
private static final String EXPORT_TYPE_ORDER = "订单数据";
private static final String EXPORT_TYPE_CUSTOMER = "客户数据";
private static final String EXPORT_TYPE_EMPLOYEE = "员工数据";
private static final int EXPORT_LIMIT = 10000;
private static final DateTimeFormatter DATE_TIME_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
private final PlanningService planningService;
private final UserService userService;
private final NailArtistInfoService nailArtistInfoService;
private final AdminScopeHelper adminScopeHelper;
public byte[] export(UserDO currentUser, String type, LocalDate startDate, LocalDate endDate) {
validateParams(type, startDate, endDate);
try (Workbook workbook = new XSSFWorkbook(); ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) {
if (EXPORT_TYPE_ORDER.equals(type)) {
writeOrders(workbook, currentUser, startDate, endDate);
} else if (EXPORT_TYPE_CUSTOMER.equals(type)) {
writeCustomers(workbook, currentUser);
} else if (EXPORT_TYPE_EMPLOYEE.equals(type)) {
writeEmployees(workbook, currentUser);
} else {
throw new RuntimeException("不支持的导出类型:" + type);
}
workbook.write(outputStream);
return outputStream.toByteArray();
} catch (IOException error) {
throw new RuntimeException("生成 Excel 文件失败:" + error.getMessage(), error);
}
}
private void validateParams(String type, LocalDate startDate, LocalDate endDate) {
if (!StringUtils.hasText(type)) {
throw new RuntimeException("导出类型不能为空");
}
if (startDate == null || endDate == null) {
throw new RuntimeException("导出开始日期和结束日期不能为空");
}
if (endDate.isBefore(startDate)) {
throw new RuntimeException("结束日期不能早于开始日期");
}
}
private void writeOrders(Workbook workbook, UserDO currentUser, LocalDate startDate, LocalDate endDate) {
PlanningCalendarVO calendar = planningService.getCalendar(currentUser, startDate, endDate, null);
Sheet sheet = createSheet(workbook, EXPORT_TYPE_ORDER, List.of("日期", "订单号", "客户", "手机号", "项目", "美甲师", "开始时间", "结束时间", "金额", "状态", "备注"));
List<PlanningAppointmentVO> appointments = calendar.getAppointments() == null ? List.of() : calendar.getAppointments();
int rowIndex = 1;
for (PlanningAppointmentVO appointment : appointments) {
Row row = sheet.createRow(rowIndex);
rowIndex++;
writeRow(row, List.of(
text(appointment.getDate()),
text(appointment.getOrderNo()),
text(appointment.getCustomerName()),
text(appointment.getCustomerPhone()),
text(appointment.getProductTitle()),
text(appointment.getArtistName()),
text(appointment.getStartTime()),
text(appointment.getEndTime()),
money(appointment.getAmount()),
text(appointment.getStatusText()),
text(appointment.getRemark())
));
}
sizeColumns(sheet, 11);
}
private void writeCustomers(Workbook workbook, UserDO currentUser) {
Long shopId = adminScopeHelper.resolveShopScope(currentUser, null);
PageResultVO<UserVO> page = userService.pageUsers(1, EXPORT_LIMIT, null, shopId, 0);
Sheet sheet = createSheet(workbook, EXPORT_TYPE_CUSTOMER, List.of("客户", "账号", "手机号", "联系电话", "会员等级", "状态", "创建时间"));
List<UserVO> customers = page.getList() == null ? List.of() : page.getList();
int rowIndex = 1;
for (UserVO customer : customers) {
Row row = sheet.createRow(rowIndex);
rowIndex++;
writeRow(row, List.of(
text(firstText(customer.getNickname(), customer.getAccount(), "客户")),
text(customer.getAccount()),
text(customer.getPhone()),
text(customer.getContactPhone()),
text(customer.getVipLevel()),
text(statusText(customer.getStatus())),
dateTime(customer.getCreateTime())
));
}
sizeColumns(sheet, 7);
}
private void writeEmployees(Workbook workbook, UserDO currentUser) {
List<NailArtistInfoDTO> employees = adminScopeHelper.isPlatformAdmin(currentUser)
? nailArtistInfoService.getAllNailArtists()
: nailArtistInfoService.getByShopId(adminScopeHelper.resolveShopScope(currentUser, null));
Sheet sheet = createSheet(workbook, EXPORT_TYPE_EMPLOYEE, List.of("员工", "手机号", "联系电话", "店铺", "身份", "排班模式", "排班时间", "状态"));
List<NailArtistInfoDTO> employeeList = employees == null ? List.of() : employees;
int rowIndex = 1;
for (NailArtistInfoDTO employee : employeeList) {
Row row = sheet.createRow(rowIndex);
rowIndex++;
writeRow(row, List.of(
text(firstText(employee.getNickname(), "美甲师")),
text(employee.getPhone()),
text(employee.getContactPhone()),
text(employee.getShopName()),
text(employee.getIdentity()),
text(employee.getScheduleModeSummary()),
text(employee.getScheduleTimeSummary()),
text(statusText(employee.getStatus()))
));
}
sizeColumns(sheet, 8);
}
private Sheet createSheet(Workbook workbook, String sheetName, List<String> headers) {
Sheet sheet = workbook.createSheet(sheetName);
Row header = sheet.createRow(0);
writeRow(header, headers);
return sheet;
}
private void writeRow(Row row, List<String> values) {
for (int index = 0; index < values.size(); index++) {
Cell cell = row.createCell(index);
cell.setCellValue(values.get(index));
}
}
private void sizeColumns(Sheet sheet, int columnCount) {
for (int index = 0; index < columnCount; index++) {
sheet.autoSizeColumn(index);
sheet.setColumnWidth(index, Math.min(Math.max(sheet.getColumnWidth(index), 2800), 12000));
}
}
private String firstText(String primary, String fallback) {
return StringUtils.hasText(primary) ? primary : fallback;
}
private String firstText(String primary, String secondary, String fallback) {
if (StringUtils.hasText(primary)) {
return primary;
}
return StringUtils.hasText(secondary) ? secondary : fallback;
}
private String text(Object value) {
return value == null ? "" : String.valueOf(value);
}
private String money(BigDecimal value) {
return value == null ? "" : value.stripTrailingZeros().toPlainString();
}
private String dateTime(LocalDateTime value) {
return value == null ? "" : DATE_TIME_FORMATTER.format(value);
}
private String statusText(Integer status) {
return Integer.valueOf(0).equals(status) ? "停用" : "启用";
}
}
@@ -49,6 +49,7 @@ import com.xiaowang.jayucloud.core.utils.AccountValidationUtils;
import com.xiaowang.jayucloud.core.support.security.OperationGuardStrategy;
import lombok.Data;
import lombok.RequiredArgsConstructor;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
@@ -954,6 +955,10 @@ public class UserController {
@OperationGuard(action = "user-update-password", ttlSeconds = 10, message = "保存处理中,请勿重复提交")
public Result<Boolean> updatePassword(HttpServletRequest request, @RequestBody PasswordDTO passwordDTO) {
Long userId = (Long) request.getAttribute("userId");
if (StringUtils.hasText(passwordDTO.getOldPassword())) {
boolean success = userService.updatePasswordWithOldPassword(userId, passwordDTO.getOldPassword(), passwordDTO.getPassword());
return Result.success(success);
}
boolean success = userService.updatePassword(userId, passwordDTO.getPassword(), passwordDTO.getVerificationCode());
return Result.success(success);
}
@@ -1059,6 +1064,7 @@ public class UserController {
* Usage: reference this field from the owning layer and pass validated inputs before reading or updating related state.
*/
private String password;
private String oldPassword;
/**
* Purpose: verificationCode supports UserController.java by holding or executing the verification code business role.
* Usage: reference this field from the owning layer and pass validated inputs before reading or updating related state.
@@ -402,6 +402,8 @@ public interface UserService extends IService<UserDO> {
* @return boolean result consumed by the caller or returned to the frontend
*/
boolean updatePassword(Long userId, String password, String verificationCode);
boolean updatePasswordWithOldPassword(Long userId, String oldPassword, String password);
/**
* Update an existing record.
@@ -2272,6 +2272,29 @@ public class UserServiceImpl extends ServiceImpl<UserMapper, UserDO> implements
return updated;
}
@Override
public boolean updatePasswordWithOldPassword(Long userId, String oldPassword, String password) {
UserDO userDO = userMapper.selectById(userId);
assertUserAvailable(userDO);
if (!StringUtils.hasText(oldPassword)) {
throw new RuntimeException("原密码不能为空");
}
if (!StringUtils.hasText(userDO.getPassword())) {
throw new RuntimeException("当前账号未设置登录密码");
}
if (!passwordEncoder.matches(oldPassword, userDO.getPassword())) {
throw new RuntimeException("原密码不正确");
}
validatePasswordStrength(password);
userDO.setPassword(passwordEncoder.encode(password));
userDO.setUpdateTime(LocalDateTime.now());
boolean updated = userMapper.updateById(userDO) > 0;
if (updated) {
userSessionService.revokeAllUserSessions(userId);
}
return updated;
}
@Override
/**
* Update an existing record.