package com.lantone.qc.kernel.catalogue;

import com.google.common.collect.Maps;
import com.lantone.qc.kernel.util.CatalogueUtil;
import com.lantone.qc.pub.model.InputInfo;
import com.lantone.qc.pub.model.OutputInfo;
import com.lantone.qc.pub.util.StringUtil;
import lombok.extern.slf4j.Slf4j;

import java.text.ParseException;
import java.util.Map;

/**
 * @ClassName : QCCatalogue
 * @Description : 质控条目解析
 * @Author : 楼辉荣
 * @Date: 2020-03-04 11:24
 */
@Slf4j
public class QCCatalogue {

    protected ThreadLocal<String> status = new ThreadLocal<String>();
    protected ThreadLocal<String> info = new ThreadLocal<String>();
    protected ThreadLocal<Map<Object, Object>> extData = new ThreadLocal<Map<Object, Object>>();

    public void execute(InputInfo inputInfo, OutputInfo outputInfo) {
//        long t1 = System.currentTimeMillis();
        if (outputInfo.getResult().get(className) != null) {
            return;
        }
        if (!precondExecute(inputInfo, outputInfo)) {
            variablePreset("-2", "");
            //return;
        }
        try {
            variablePreset("-1", "");
            start(inputInfo, outputInfo);
        } catch (Exception e) {
            variablePreset("-1", "");
            log.error(e.getMessage() + "......类名：" + this.className);
        }
//        long t2 = System.currentTimeMillis();
//        log.error(inputInfo.getMedicalRecordInfoDoc().getStructureMap().get("behospitalCode") + "-----" + className + "（规则）  耗时：" + (t2 - t1));
        insertOpt(outputInfo);
    }

    private void insertOpt(OutputInfo outputInfo) {
        Map<String, Object> resultDetail = Maps.newHashMap();
        resultDetail.put("info", info.get());
        resultDetail.put("status", status.get());
        resultDetail.put("extData", extData.get());
        outputInfo.getResult().put(className, resultDetail);
        status.remove();
        info.remove();
        extData.remove();
    }

    private void variablePreset(String arg0, String arg1) {
        status.set(arg0);
        info.set(arg1);
    }

    //前置条件运行 true通过，false不通过
    private boolean precondExecute(InputInfo inputInfo, OutputInfo outputInfo) {
        Map<String, String> catalogueMap = inputInfo.getInputCatalogueMap().get(className);
        if (catalogueMap == null) {
            return true;
        }
        String precond = catalogueMap.get("precond");
        if (StringUtil.isBlank(precond)) {
            return true;
        }
        boolean flag = true;
        String[] codes = precond.split(",");
        for (String code : codes) {
            QCCatalogue qCCatalogue = CatalogueUtil.qcCatalogueMap.get(code);
            if (qCCatalogue != null) {
                qCCatalogue.execute(inputInfo, outputInfo);
            }
        }
        for (String code : codes) {
            if (outputInfo.getResult().get(code) == null) {
                continue;
            }
            if (!outputInfo.getResult().get(code).get("status").equals("0")) {
                flag = false;
                break;
            }
        }
        return flag;
    }

    private String className = this.getClass().getSimpleName();

    public QCCatalogue() {
        CatalogueUtil.qcCatalogueMap.put(className, this);
    }

    protected void start(InputInfo inputInfo, OutputInfo outputInfo) throws ParseException {
    }

}