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

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import org.eclipse.swt.SWT;
import org.eclipse.swt.core.widgets.EDialog;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;

import com.gx.obe.business.enumeration.SupplierEnum;
import com.gx.obe.components.core.enumeration.CommonEnum;
import com.gx.obe.evaluation.step.composite.ShowPriceResultComposite;
import com.gx.obe.evaluation.step.composite.ShowPriceResultWithOutPriceComposite;
import com.gx.obe.evaluation.step.composite.ShowPriceResultWithOutScoreComposite;
import com.gx.obe.evaluation.step.inter.IPriceShowTable;
import com.gx.obe.util.utils.LogUtils;
import com.gx.obe.util.utils.ObjectUtils;
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.entity.expert.Expert;
import com.gx.obe.web.entity.tender.TenderProjectEntity;
import com.gx.obe.web.entity.tender.TenderProjectRuleEntity;
import com.gx.obe.web.service.SupplierOpeningResultService;
import com.gx.obe.web.service.SupplierService;
import com.gx.obe.web.service.evaluation.EvaluationFactorService;
import com.gx.obe.web.service.evaluation.EvaluationStepService;
import com.gx.obe.web.service.expert.ExpertService;

/**
 * @Description: 查看投标报价文件
 * @author guoyr
 */
public class ViewBidPriceResultDialog extends EDialog {

	private SupplierService supplierService = new SupplierService();
	private EvaluationStepService evaluationStepService = new EvaluationStepService();
	private SupplierOpeningResultService supplierOpeningResultService;
	private EvaluationFactorService evaluationFactorService = new EvaluationFactorService();        
	private ExpertService expertService;
	private TenderProjectEntity tenderProjectEntity;
	private TenderProjectRuleEntity tenderProjectRuleEntity;
	private Expert CURRENT_EXPERT;
	private Composite container;
	private EvaluationStep evaluationStep;
	private List<Supplier> supplierList;
	private Composite composite_2;
	private IPriceShowTable composite;
	private List<EvaluationFactor> priceEvaluationFactorList;
	
	private boolean hasFactorWeight = false;
	{
		LogUtils.logClass(ViewBidPriceResultDialog.class);
	}
	
	/**
	 * @param parentShell
	 * @param tenderProjectEntity
	 * @param tenderProjectRuleEntity
	 * @param supplier
	 */
	public ViewBidPriceResultDialog(Shell parentShell, TenderProjectEntity tenderProjectEntity,TenderProjectRuleEntity tenderProjectRuleEntity, EvaluationStep evaluationStep) {
		super(parentShell);
		this.tenderProjectEntity = tenderProjectEntity;
		this.tenderProjectRuleEntity = tenderProjectRuleEntity;
		this.evaluationStep = evaluationStep;
		supplierOpeningResultService = new SupplierOpeningResultService();
	}
	/** 
	 * @Description: 
	 * @param parent
	 * @return
	 */
	@Override
	protected Control createDialogArea(Composite parent) {
		container = (Composite) super.createDialogArea(parent);
		GridLayout gl_container = new GridLayout(1, false);
		gl_container.verticalSpacing = 0;
		gl_container.horizontalSpacing = 0;
		gl_container.marginHeight = 0;
		gl_container.marginWidth = 0;
		container.setLayout(gl_container);
		
		composite_2 = new Composite(container, SWT.NONE);
		composite_2.setLayout(new FillLayout(SWT.HORIZONTAL));
		composite_2.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
		init();
		return container;
	}

	/**
	 * @Description: 初始化界面数据
	 * @author guoyr
	 */
	private void init(){
		expertService = new ExpertService();
		if(null != evaluationStep){
			supplierList = supplierService.getEvaluationStepSupplierList(tenderProjectEntity.getId(),evaluationStep);
		}
		if(null != supplierList && supplierList.size() > 0){
			// 则获取开标成功的投标人
			List<SupplierOpeningResult> supplierOpeningResultList = supplierOpeningResultService.getSupplierOpeningResultListByTenderIdAndBidOpeningStatus(tenderProjectEntity.getId(),SupplierEnum.OPENBID_SUCCESS);
			
			for(Supplier supplier : supplierList){
				SupplierOpeningResult supplierOpeningResult = null;
				if(null != supplierOpeningResultList && supplierOpeningResultList.size() > 0){
					for(SupplierOpeningResult result : supplierOpeningResultList){
						if(ObjectUtils.equals(result.getSupplierId(), supplier.getId())){
							if(null == result.getEvaluationPrice()){
								result.setEvaluationPrice(result.getBidPrice());
							}
							supplierOpeningResult = result;
							break;
						}
					}
				}
				if(null == supplierOpeningResult){
					supplierOpeningResult = new SupplierOpeningResult();
					supplierOpeningResult.setTenderId(tenderProjectEntity.getId());
					supplierOpeningResult.setSupplierId(supplier.getId());
				}
				supplier.setSupplierOpeningResult(supplierOpeningResult);
			}
		}
		if(null == priceEvaluationFactorList){
			priceEvaluationFactorList = new ArrayList<EvaluationFactor>();
		}
		priceEvaluationFactorList.clear();
		EvaluationStep priceEvaluationStep = initEvaluationStepFactor();
		if(null != priceEvaluationStep){
			CURRENT_EXPERT = expertService.getEvaluationStepExpert(tenderProjectEntity.getId(), priceEvaluationStep);
		}
		if(null != CURRENT_EXPERT && CommonEnum.YES.equals(tenderProjectRuleEntity.getViewBidPrice()) && CommonEnum.YES.equals(tenderProjectRuleEntity.getViewPriceScore())){
			composite = new ShowPriceResultComposite(composite_2, SWT.NONE);
			this.setTitle("查看价格得分");
		}else if(null != CURRENT_EXPERT && CommonEnum.YES.equals(tenderProjectRuleEntity.getViewPriceScore())){
			composite = new ShowPriceResultWithOutPriceComposite(composite_2, SWT.NONE);
			this.setTitle("查看价格得分");
		}else if(null == tenderProjectRuleEntity.getViewBidPrice() || CommonEnum.YES.equals(tenderProjectRuleEntity.getViewBidPrice())){
			composite = new ShowPriceResultWithOutScoreComposite(composite_2, SWT.NONE);
			this.setTitle("查看报价");
		}
		if(null != composite){
			composite.refreshTable(tenderProjectEntity, tenderProjectRuleEntity, priceEvaluationStep, CURRENT_EXPERT, supplierList, priceEvaluationFactorList, hasFactorWeight);
			composite_2.layout();
			container.layout();
		}else{
			Label infoLabel = new Label(composite_2, SWT.NONE);
			infoLabel.setText("    评标规则设置不允许查看！");
		}
	}
	/**
	 * @Description: 初始化当前评标步骤的评审指标
	 * @author guoyr
	 * @return 
	 */
	private EvaluationStep initEvaluationStepFactor(){
		
		hasFactorWeight = false;
		//查询价格评审步骤
		List<EvaluationStep> priceEvaluationStepList = evaluationStepService.getEvaluationStepListByType(tenderProjectEntity.getId(), com.gx.obe.components.core.enumeration.EvaluationStepEnum.PRICE);
		if(null != priceEvaluationStepList && priceEvaluationStepList.size() > 0){
			EvaluationStep priceEvaluationStep = priceEvaluationStepList.get(0);

			List<EvaluationFactor> evaluationTopFactorList = evaluationFactorService.getEvaluationStepFactorTreeList(tenderProjectEntity.getId(), priceEvaluationStep);
			if(null != evaluationTopFactorList && evaluationTopFactorList.size() > 0){
				for(EvaluationFactor topFactor : evaluationTopFactorList){
					// 当前评标步骤是否存在权重
					if(null != topFactor.getFactorWeight() && (topFactor.getFactorWeight().compareTo(BigDecimal.ZERO) >0 && topFactor.getFactorWeight().compareTo(new BigDecimal(100)) < 0)){
						hasFactorWeight = true;
					}
					initAddChildEvaluationFactor(priceEvaluationFactorList, topFactor);
				}
			}
		
			return priceEvaluationStep;
		}
		return null;
	}
	/**
	 * @Description: 递归获取所有的评审叶子指标
	 * @author guoyr
	 * @param evalFactor
	 */
	private void initAddChildEvaluationFactor(List<EvaluationFactor> evaluationFactorList, EvaluationFactor evalFactor){
		if(null != evalFactor.getChildFactorList() && evalFactor.getChildFactorList().size() > 0){
			for(EvaluationFactor childFactor : evalFactor.getChildFactorList()){
				childFactor.setLevel(evalFactor.getLevel() + 1);
				initAddChildEvaluationFactor(evaluationFactorList, childFactor);
			}
		}else {
			evaluationFactorList.add(evalFactor);
		}
	}
	
	@Override
	protected Point getInitialSize() {
		return new Point(1121, 496);
	}
}
