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

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CountDownLatch;

import org.dom4j.Document;
import org.dom4j.Element;
import org.eclipse.swt.core.CallBack;
import org.eclipse.swt.core.MessageDialog;

import com.gx.obe.business.entity.SupplierEvaluationFactor;
import com.gx.obe.common.file.enumeration.FileConstants;
import com.gx.obe.common.widget.thread.MyRunnable;
import com.gx.obe.components.core.Constants;
import com.gx.obe.util.utils.XmlUtils;
import com.gx.obe.web.entity.Supplier;
import com.gx.obe.web.entity.evaluation.EvaluationFactor;
import com.gx.obe.web.entity.tender.TenderProjectEntity;
import com.gx.obe.web.service.SupplierEvaluationFactorService;
import com.gx.obe.web.service.evaluation.EvaluationFactorService;

/**
 * @Description: 下载供应商自评意见
 * @author wangxaing
 */
public class ParsingSupplierSelfOpinionThread extends MyRunnable {

	private CallBack callBack;
	private EvaluationFactorService evaluationFactorService = new EvaluationFactorService();
	private Supplier supplier;
	private CountDownLatch countDownLatch;
	private Map<String, SupplierEvaluationFactor> supplierEvaluationFactorMap;
	private TenderProjectEntity curTender;
	private HashMap<String, Map<String, String>> allSupplierOpinionMap;
	private List<EvaluationFactor> factorList;
	private SupplierEvaluationFactorService supplierEvaluationFactorService = new SupplierEvaluationFactorService();

	public ParsingSupplierSelfOpinionThread(TenderProjectEntity tenderproject,Map<String, SupplierEvaluationFactor> supplierEvaluationFactorMap, HashMap<String, Map<String, String>> allSupplierOpinionMap, Supplier supplier, List<EvaluationFactor> factorList, CountDownLatch countDownLatch, CallBack callBack) {
		this.curTender = tenderproject;
		this.supplierEvaluationFactorMap = supplierEvaluationFactorMap;
		this.allSupplierOpinionMap = allSupplierOpinionMap;
		this.supplier = supplier;
		this.factorList = factorList;
		this.countDownLatch = countDownLatch;
		this.callBack = callBack;
	}

	public void run() {
		callBack.callBackString("正在获取"+supplier.getSupplierName()+"自评意见。。。");
		parsingSupplierSelfOpinion();
		countDownLatch.countDown();
	}

	/**
	 * @Description: 解析自评文件
	 * @author wangxiang 
	 */
	private void parsingSupplierSelfOpinion() {
		File selfEvaluatesXml = new File(FileConstants.getBidFilePath(curTender.getId(), supplier.getId()).concat(Constants.SELFEVALUATES_XML));
		if(selfEvaluatesXml.exists()){
			parsingXml(supplier.getId(),selfEvaluatesXml);
			List<SupplierEvaluationFactor> updateSupplierEvaluationFactorList = new ArrayList<SupplierEvaluationFactor>();
			for(EvaluationFactor evaluationFactor : factorList){
				SupplierEvaluationFactor supplierEvaluationFactor = supplierEvaluationFactorMap.get(supplier.getId().concat(evaluationFactor.getFactorCode()));
				if(null == supplierEvaluationFactor){
					supplierEvaluationFactor = new SupplierEvaluationFactor();
					supplierEvaluationFactor.setTenderId(curTender.getId());
					supplierEvaluationFactor.setSupplierId(supplier.getId());
					supplierEvaluationFactor.setFactorCode(evaluationFactor.getFactorCode());
				}
				Map<String,String> supplierFactorOpinionMap = allSupplierOpinionMap.get(supplier.getId());
				if(null != supplierFactorOpinionMap){
					if(null == supplierEvaluationFactor.getOpinion()){
						supplierEvaluationFactor.setOpinion(supplierFactorOpinionMap.get(evaluationFactor.getFactorCode()));
					}
					supplierEvaluationFactor.setFactorResponse(supplierFactorOpinionMap.get(evaluationFactor.getFactorCode()));
				}
				updateSupplierEvaluationFactorList.add(supplierEvaluationFactor);
			}
			if(supplierEvaluationFactorService.batchSaveOrUpdate(updateSupplierEvaluationFactorList, new String[]{"opinion","factorResponse"}) == updateSupplierEvaluationFactorList.size()){
				callBack.callBackString(supplier.getSupplierName()+"自评意见获取成功！");
			}
		}else{
			callBack.callBackString("未查询到"+supplier.getSupplierName()+"自评意见，请检查标书文件！");
		}
		
	
	}
	
	
	/**
	 * @Description: 解析供应商自评意见XML
	 * @author wangx
	 */
	private void parsingXml(String supplierId, File xmlFile){
		Map<String,String> supplierFactorOpinionMap = new HashMap<String,String>();
		if(null != xmlFile && xmlFile.exists()){
			Document document = XmlUtils.getXmlDocument(xmlFile.getAbsolutePath());
			Element rootEle = document.getRootElement();
			List<Element> items = rootEle.elements("selfEvaluateItem");
			if(null != items && items.size() > 0){
				for(Element element : items){
					String factorCode = element.elementText("evalFactorCode");
					String opinion = element.elementText("value");
					EvaluationFactor evaluationFactor = evaluationFactorService.getFactorByFactorCode(curTender.getId(), factorCode);
					if(null != evaluationFactor){
//						String factorCode = evaluationFactor.getFactorCode();
						supplierFactorOpinionMap.put(factorCode, opinion);
					}
				}
				allSupplierOpinionMap.put(supplierId, supplierFactorOpinionMap);
			}
		}else{
			MessageDialog.openShortMessage("未获取到自评意见，请检查是否下载标书！");
		}
	}

}
