小米训练营-第七次作业
1. 流程图展示

2. 代码展示
@Service public class PayrollTask {
@Autowired private EmployeeMapper employeeMapper;
@Autowired private PayrollProcessingService payrollProcessingService; @Scheduled(cron = "0 0 12 15 * ?") public void processPayrollIntern() { List<Employee> list = employeeMapper.listForIntern(Intern.class); for (Employee employee : list) { payrollProcessingService.processPayroll(employee); } }
@Scheduled(cron = "0 0 12 5 * ?") public void processPayroll() { List<Employee> list = employeeMapper.listForFullTimeEmployee(FullTimeEmployee.class); 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) { PaymentMethod method = employee.getPaymentMethod(); BigDecimal salary = employee.calculateSalary(); PaymentStrategy strategy = paymentStrategyFactory.getStrategy(method); 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); } }
|
代码结果图为:
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(); } }
|
