小米训练营-第七次作业

1. 流程图展示

下载

2. 代码展示

/**
* @ClassName: PayrollTask
* @Package: cn.yomigaeri.xiaomi.task
* @Description:
* @Author Yomigaeri
* @Create 2025/6/3 18:04
* @Version 1.0
*/
@Service
public class PayrollTask {

@Autowired
private EmployeeMapper employeeMapper;

@Autowired
private PayrollProcessingService payrollProcessingService;
//每个月15号
@Scheduled(cron = "0 0 12 15 * ?")
public void processPayrollIntern() {
//1. 在员工表中查询为实习生的员工
List<Employee> list = employeeMapper.listForIntern(Intern.class);
//2. 遍历
for (Employee employee : list) {
payrollProcessingService.processPayroll(employee);
}
}

//每个月5号
@Scheduled(cron = "0 0 12 5 * ?")
public void processPayroll() {
//1. 在员工表中查询为实习生的员工
List<Employee> list = employeeMapper.listForFullTimeEmployee(FullTimeEmployee.class);
//2. 遍历
for (Employee employee : list) {
payrollProcessingService.processPayroll(employee);
}
}
}

其中payrollProcessingService的实现类PayrollProcessingServiceImpl代码如下:

@Service
public class PayrollProcessingServiceImpl implements PayrollProcessingService {

private final PaymentStrategyFactory paymentStrategyFactory;

@Autowired
public PayrollProcessingServiceImpl(PayrollService payrollService, CourierService courierService) {
// 创建支付策略工厂
this.paymentStrategyFactory = new PaymentStrategyFactory(payrollService, courierService);
}

@Override
public void processPayroll(Employee employee) {
//1. 获取员工支付方式
PaymentMethod method = employee.getPaymentMethod();
//2. 获取薪资
BigDecimal salary = employee.calculateSalary();
//3. 从支付工厂中获取对应策略
PaymentStrategy strategy = paymentStrategyFactory.getStrategy(method);
//4. 开始发薪
strategy.process(employee, salary);
}
}

其中使用工厂模式 + 策略模式实现,PaymentStrategyFactory工厂类代码如下:

public class PaymentStrategyFactory {

private final PayrollService payrollService;
private final CourierService courierService;

public PaymentStrategyFactory(PayrollService payrollService, CourierService courierService) {
this.payrollService = payrollService;
this.courierService = courierService;
}

public PaymentStrategy getStrategy(PaymentMethod method) {
return switch (method) {
case SALARY_CARD -> new SalaryCardPaymentStrategy(payrollService);
case CASH -> new CashPaymentStrategy();
case MAIL -> new MailPaymentStrategy(courierService);
default -> throw new IllegalArgumentException("不支持的支付方式: " + method);
};
}
}

策略模式,有一个策略接口PaymentStrategy,由三个实现类实现:

public class CashPaymentStrategy implements PaymentStrategy {
@Override
public void process(Employee employee, BigDecimal salary) {
System.out.println("员工 " + employee.getId() + " 选择现金领取");
}
}

public class MailPaymentStrategy implements PaymentStrategy {

private final CourierService courierService;

public MailPaymentStrategy(CourierService courierService) {
this.courierService = courierService;
}

@Override
public void process(Employee employee, BigDecimal salary) {
String trackingNumber = courierService.placeOrder(employee.getId());
System.out.println("快递单号:" + trackingNumber);
}
}

public class SalaryCardPaymentStrategy implements PaymentStrategy {

private final PayrollService payrollService;

public SalaryCardPaymentStrategy(PayrollService payrollService) {
this.payrollService = payrollService;
}

@Override
public void process(Employee employee, BigDecimal salary) {
payrollService.transferToCard(employee.getId(), salary);
}
}

代码结果图为:

image-20250603181739226

3. 效果展示

使用Apifox调用一个controller层代码并测试的截图:

@RestController
public class PayrollController {
@Autowired
private PayrollProcessingService payrollProcessingService;

@GetMapping("/payroll")
public Result payroll() {
Intern intern = new Intern();
intern.setId("1");
intern.setName("小王");
intern.setWorkDays(20);
intern.setPaymentMethod(PaymentMethod.CASH);
payrollProcessingService.processPayroll(intern);

FullTimeEmployee fullTimeEmployee = new FullTimeEmployee();
fullTimeEmployee.setId("2");
fullTimeEmployee.setName("小程");
fullTimeEmployee.setPaymentMethod(PaymentMethod.MAIL);
fullTimeEmployee.setBaseSalary(new BigDecimal(10000));
fullTimeEmployee.submitPerformanceFactor(new BigDecimal(1.1));
payrollProcessingService.processPayroll(fullTimeEmployee);

return Result.success();
}
}

image-20250603182031103