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

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

import com.gx.obe.evaluation.step.listener.ImportEvaluationResultListener;
import com.gx.obe.util.utils.ObjectUtils;
import com.gx.obe.web.entity.EvaluationFactorResult;
import com.gx.obe.web.entity.Supplier;
import com.gx.obe.web.entity.auth.AuthUser;
import com.gx.obe.web.entity.evaluation.EvaluationFactor;
import com.gx.obe.web.entity.tender.TenderProjectEntity;
import com.gx.obe.web.service.SupplierService;
import com.gx.obe.web.service.evaluation.EvaluationFactorResultService;
import com.gx.obe.web.service.evaluation.EvaluationStepService;

public class ImportEvaluationBySupplierScoreThread implements Runnable{
	

	private Thread thread;
	private TenderProjectEntity tenderProjectEntity;
	private List<Supplier> supplierList;
	private List<EvaluationFactor> evaluationFactorList;
	private AuthUser curUser;
	private List<TenderProjectEntity> tenderPackList;
	private ImportEvaluationResultListener importEvaluationResultListener;
	private EvaluationFactorResultService evaluationFactorResultService = new EvaluationFactorResultService();
	public EvaluationStepService evaluationStepService = new EvaluationStepService();
	public SupplierService supplierService = new SupplierService();
	
	public ImportEvaluationBySupplierScoreThread(TenderProjectEntity tenderProjectEntity, List<Supplier> supplierList, List<EvaluationFactor> evaluationFactorList, AuthUser curUser, ImportEvaluationResultListener importEvaluationResultListener){
		this.tenderProjectEntity = tenderProjectEntity;
		this.supplierList = supplierList;
		this.evaluationFactorList = evaluationFactorList;
		this.curUser = curUser;
		this.importEvaluationResultListener = importEvaluationResultListener;
		
	}

	public void start(){
		thread = new Thread(this);
		thread.start();
	}
	
	public void stop(){
		thread = null;
		
	}
	public void run() {
		Map<String, Map<String, EvaluationFactorResult>> importResultMap = new HashMap<String, Map<String, EvaluationFactorResult>>();
		for(Supplier supplier : supplierList){
			List<Map<String, Object>> supplierEvaluationResultList = evaluationFactorResultService.getSupplierEvaluationFactorResult(tenderProjectEntity.getParentId(), tenderProjectEntity.getPurcategoryNames(), curUser.getId(), supplier.getSupplierName());
			if(null != supplierEvaluationResultList && supplierEvaluationResultList.size() > 0){
				Map<String, EvaluationFactorResult> supplierEvalResultMap = new HashMap<String, EvaluationFactorResult>();
				for(EvaluationFactor evaluationFactor : evaluationFactorList){
					EvaluationFactorResult evaluationFactorResult = new EvaluationFactorResult();
					for(Map<String, Object> evaluationFactorResultMap : supplierEvaluationResultList){
						String factorName = (String)evaluationFactorResultMap.get("FACTOR_NAME");
						if(ObjectUtils.equals(evaluationFactor.getFactorName().trim(), factorName.trim())){
							evaluationFactorResult.setEvaluationScore(ObjectUtils.getObjBigDecimal(evaluationFactorResultMap.get("EVALUATION_SCORE")));
							supplierEvalResultMap.put(evaluationFactor.getId(), evaluationFactorResult);
							break;
						}
					}
				}
				importResultMap.put(supplier.getId(), supplierEvalResultMap);
			}
		}
		if(null != thread && null != importEvaluationResultListener){
			importEvaluationResultListener.importEvalResult(importResultMap);
		}
	
	}
	
	private List<Supplier> getImportSupplierList(String tenderPackId){
		// 查找当前分包下的供应商
//		QueryWrapper supplierQueryWrapper = new QueryWrapper(Supplier.class);
//		supplierQueryWrapper.setQueryParm("orgCode");
//		supplierQueryWrapper.setQueryParm("id");
//		supplierQueryWrapper.setQueryParm("supplierName");
//		supplierQueryWrapper.setWhereParam("tenderId", tenderPackId);
		return supplierService.getAllSupplierList(tenderPackId);
	}
}
