package cn.estsh.i3plus.ext.mes.pcn.apiservice.serviceimpl.busi.print.strategy;

import cn.estsh.i3plus.ext.mes.pcn.api.busi.ISxPrintTemplateStrategyService;
import cn.estsh.i3plus.ext.mes.pcn.pojo.model.SxPrintTemplateModel;
import cn.estsh.i3plus.platform.common.exception.ImppExceptionEnum;
import cn.estsh.i3plus.pojo.base.enumutil.CommonEnumUtil;
import cn.estsh.impp.framework.boot.exception.ImppExceptionBuilder;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @Description 打印模板策略
 * @Reference
 * @Author wangjie
 * @CreateDate 2020-06-22 10:28
 * @Modify
 */
@Service
public class SxPrintTemplateStrategyContext {

    @Autowired
    private final Map<String, ISxPrintTemplateStrategyService> strategyMap = new ConcurrentHashMap<>();

    public SxPrintTemplateStrategyContext(Map<String, ISxPrintTemplateStrategyService> strategyMap) {
        this.strategyMap.clear();
        strategyMap.forEach((k, v) -> this.strategyMap.put(k, v));
    }

    public List<Map<String, Object>> getPrintTemplateService(SxPrintTemplateModel printModel, Class<?> clazz) {
        if (null == printModel || clazz == null) {
            throw ImppExceptionBuilder.newInstance()
                    .setSystemID(CommonEnumUtil.SOFT_TYPE.MES_PCN.getCode())
                    .setErrorCode(ImppExceptionEnum.VARIFY_EXCEPTION.getCode())
                    .setErrorDetail("打印模板策略校验缺少打印必要入参")
                    .build();
        }
        return this.strategyMap.get(StringUtils.uncapitalize(clazz.getSimpleName())).getPrintTemplateData(printModel);
    }

}
