package com.czg.service.order.print;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.List;


/**
 * 打印机责任链初始化
 * @author Administrator
 */
@Configuration
public class PrintConfig {

    @Resource
    private List<PrinterHandler> printers;

    // 初始化责任链
    @PostConstruct
    public void initChain() {
        // 检查打印处理器列表是否为空
        if (printers != null && !printers.isEmpty()) {
            for (int i = 0; i < printers.size() - 1; i++) {
                // 设置当前处理器的下一个处理器
                printers.get(i).setNextPrinter(printers.get(i + 1));
            }
        }
    }

    @Bean
    @Primary
    public PrinterHandler printerHandler() {
        // 返回责任链的起始处理器
        return printers.getFirst();
    }

}

