package com.gx.obe.evaluation.step.dataset;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.gx.obe.business.entity.SupplierEvaluationFactor;
import com.gx.obe.components.core.enumeration.EvaluationFactorEnum;
import com.gx.obe.components.core.enumeration.EvaluationStepEnum;
import com.gx.obe.report.dataset.excel.BaseDateSet;
import com.gx.obe.web.entity.Supplier;
import com.gx.obe.web.entity.SupplierOpeningResult;
import com.gx.obe.web.entity.evaluation.EvaluationFactor;
import com.gx.obe.web.entity.evaluation.EvaluationStep;
import com.gx.obe.web.service.SupplierEvaluationFactorService;
import com.gx.obe.web.service.SupplierOpeningResultService;
import com.gx.obe.util.utils.CollectionUtils;
import com.report.excel.ReportDataSet;

/**
 * 清标评议内容
 * 
 * @author Administrator
 * 
 */
public class ClearCommentsDataSet extends BaseDateSet {
	private SupplierEvaluationFactorService supplierEvaluationFactorService = new SupplierEvaluationFactorService();
	private SupplierOpeningResultService supplierOpeningResultService = new SupplierOpeningResultService();
	
	private List<Supplier> supplierList;
	private List<EvaluationFactor> factorList;
	
	public List<ReportDataSet> getDateSetList(Map<String, String> paramsMap, Map<String, String> enumsMap) {
		List<ReportDataSet> reportDataList = new ArrayList<ReportDataSet>();
		List<SupplierEvaluationFactor> supplierEvaluationFactorList = supplierEvaluationFactorService.getEntityListByTenderId(tenderProjectEntity.getId());
		Map<String, Object> crossData = new HashMap<String, Object>();
		Map<String,SupplierOpeningResult> supplierOpeningResultMap = new HashMap<String,SupplierOpeningResult>();
		List<EvaluationStep> cleanTenderStepList = evaluationStepService.getEvaluationStepListByType(tenderProjectEntity.getId(), EvaluationStepEnum.PRE_EVALUATION);
		if(null == cleanTenderStepList || cleanTenderStepList.size() == 0){
			return reportDataList;
		}else {
			// 获得清标步骤的供应商
			List<Supplier> supplierList = supplierService.getEvaluationStepSupplierList(tenderProjectEntity.getId(),cleanTenderStepList.get(0));
			if (null == supplierList || supplierList.size() == 0) {
				return reportDataList;
			}else {
				for(Supplier supplier : supplierList) {
					SupplierOpeningResult supplierOpeningResult = supplierOpeningResultService.getSupplierOpeningResult(tenderProjectEntity.getId(), supplier.getId());
					supplierOpeningResultMap.put(supplier.getId(), supplierOpeningResult);
				}
			}
			for(EvaluationStep clearEvaluationStep : cleanTenderStepList) {
				String evalBidType = clearEvaluationStep.getEvalBidType();
				factorList = new ArrayList<EvaluationFactor>();;
				// 清标步骤
				EvaluationStep clearTenderStep = clearEvaluationStep;
				//获得指标
				if(!EvaluationFactorEnum.EVAL_BID_PRICE.equals(evalBidType)) {
					Integer factorType = clearEvaluationStep.getEvaluationParamByKey(EvaluationStepEnum.FACTOR_TYPE);
					if (null == factorType) {
						factorType = com.gx.obe.components.core.enumeration.EvaluationStepEnum.FACTOR_COMPLIANCE | com.gx.obe.components.core.enumeration.EvaluationStepEnum.FACTOR_SCORE;
					}
					if ((factorType & com.gx.obe.components.core.enumeration.EvaluationStepEnum.FACTOR_COMPLIANCE) != 0) {
						List<EvaluationStep> complianceEvaluationStepList = evaluationStepService.getEvaluationStepListByType(tenderProjectEntity.getId(), com.gx.obe.components.core.enumeration.EvaluationStepEnum.COMPLIANCE);
						for (EvaluationStep evaluationStep : complianceEvaluationStepList) {
							List<EvaluationFactor> evaluationStepFactorTreeList = evaluationFactorService.getEvaluationStepFactorTreeList(tenderProjectEntity.getId(), evaluationStep);
							Map<String, EvaluationFactor> childFactorCodeMap = new HashMap<String, EvaluationFactor>();
							for (EvaluationFactor evaluationFactor : evaluationStepFactorTreeList) {
								List<EvaluationFactor> busFctorList = recursionChildFactorList(evaluationFactor, childFactorCodeMap);
								factorList.addAll(busFctorList);
							}
						}
					}
					if ((factorType & com.gx.obe.components.core.enumeration.EvaluationStepEnum.FACTOR_SCORE) != 0) {
						List<EvaluationStep> detailEvaluationStepList = evaluationStepService.getEvaluationStepListByType(tenderProjectEntity.getId(), com.gx.obe.components.core.enumeration.EvaluationStepEnum.DETAIL);
						for (EvaluationStep evaluationStep : detailEvaluationStepList) {
							List<EvaluationFactor> evaluationStepFactorTreeList = evaluationFactorService.getEvaluationStepFactorTreeList(tenderProjectEntity.getId(), evaluationStep);
							Map<String, EvaluationFactor> childFactorCodeMap = new HashMap<String, EvaluationFactor>();
							for (EvaluationFactor evaluationFactor : evaluationStepFactorTreeList) {
								List<EvaluationFactor> scoreFctorList = recursionChildFactorList(evaluationFactor, childFactorCodeMap);
								factorList.addAll(scoreFctorList);
							}
						}
					}
				}else {
					continue;
				}
				
				if (null == factorList) {
					continue;
				}else {
					if(!EvaluationFactorEnum.EVAL_BID_PRICE.equals(evalBidType)) {
						if(!EvaluationFactorEnum.EVAL_BID_DETAIL.equals(evalBidType)) {
							CollectionUtils.filter(factorList, t -> evalBidType.equals(t.getEvalBidType()));
						}
						for (EvaluationFactor evaluationFactor : factorList) {
							for (Supplier supplier : supplierList) {
								String factorId = evaluationFactor.getId();
								final String factorCode = evaluationFactor.getFactorCode();
								final String supplierId = supplier.getId();
								
								SupplierEvaluationFactor supplierEvaluationFactor = CollectionUtils.get(supplierEvaluationFactorList, t -> {
									return factorCode.equals(t.getFactorCode()) && supplierId.equals(t.getSupplierId());
								});
								String opinion = null;
								if (null != supplierEvaluationFactor) {
									opinion = supplierEvaluationFactor.getOpinion();
								}
								if (null == opinion) opinion = "";
								crossData.put(factorId.concat(supplierId), opinion);
							}
						}
					}
					ReportDataSet reportDataSet = getBaseDataSet(paramsMap);
					reportDataSet.setFileName(clearEvaluationStep.getStepName()+"对比表");
					reportDataSet.setReportName(clearEvaluationStep.getStepName());
					reportDataSet.putDimension("supplierList", supplierList);
					reportDataSet.putDimension("factorList", factorList);
					reportDataSet.putCrossData(crossData);
					reportDataList.add(reportDataSet);
				}
			}
		}
		
		return reportDataList;
	}
	
	public void setSupplierList(List<Supplier> supplierList) {
		this.supplierList = supplierList;
	}
	
	public void setEvaluationFactorList(List<EvaluationFactor> factorList) {
		this.factorList = factorList;
	}
	
	/**
     * @Description: 递归叶子指标节点
     * @author chenxw
     * @param evaluationFactor
     * @param childFactorList
     */
    private List<EvaluationFactor> recursionChildFactorList(EvaluationFactor evaluationFactor, Map<String, EvaluationFactor> childFactorCodeMap) {
    	List<EvaluationFactor> evaluationFactorList = new ArrayList<EvaluationFactor>();
        if (null == evaluationFactor.getFactorCode()) evaluationFactor.setFactorCode(evaluationFactor.getId());
        if (CollectionUtils.isNull(evaluationFactor.getChildFactorList())) {
            childFactorCodeMap.put(evaluationFactor.getFactorCode(), evaluationFactor);
            evaluationFactorList.add(evaluationFactor);
        } else {
            for (EvaluationFactor childFactor : evaluationFactor.getChildFactorList()) {
            	List<EvaluationFactor> factorList =  recursionChildFactorList(childFactor, childFactorCodeMap);
            	evaluationFactorList.addAll(factorList);
            }
        }
        return evaluationFactorList;
    }
	
}
