package com.gx.obe.management.dialog;

import java.io.File;
import java.math.BigDecimal;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.List;

import org.eclipse.core.runtime.Platform;
import org.eclipse.swt.SWT;
import org.eclipse.swt.core.ImageResolver;
import org.eclipse.swt.core.widgets.EDialog;
import org.eclipse.swt.core.widgets.ESepator;
import org.eclipse.swt.core.widgets.ImageButton;
import org.eclipse.swt.custom.ScrolledComposite;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.layout.RowData;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Shell;

import com.gx.obe.ColorConstants;
import com.gx.obe.business.entity.EvaluationStepFactor;
import com.gx.obe.business.vo.EvaluationStepGenerator;
import com.gx.obe.common.file.enumeration.FileConstants;
import com.gx.obe.common.icons.IconsActivator;
import com.gx.obe.common.widget.button.StepButton;
import com.gx.obe.common.widget.composite.TipsComposite;
import com.gx.obe.components.core.I18NEnum;
import com.gx.obe.components.core.enumeration.EvaluationFactorEnum;
import com.gx.obe.components.core.enumeration.EvaluationStepEnum;
import com.gx.obe.components.core.enumeration.TenderProjectEnum;
import com.gx.obe.config.ConfigConstants;
import com.gx.obe.config.utils.PropertiesUtils;
import com.gx.obe.message.Messages;
import com.gx.obe.util.utils.BigDecimalUtils;
import com.gx.obe.util.utils.LogUtils;
import com.gx.obe.util.utils.ObjectUtils;
import com.gx.obe.util.utils.StringUtils;
import com.gx.obe.util.utils.UuidUtils;
import com.gx.obe.web.entity.evaluation.EvaluationFactor;
import com.gx.obe.web.entity.evaluation.EvaluationStep;
import com.gx.obe.web.entity.tender.TenderProjectEntity;
import com.gx.obe.web.entity.tender.TenderProjectRuleEntity;
import com.gx.obe.web.service.ProjectRuleService;
import com.gx.obe.web.service.evaluation.EvaluationFactorService;
import com.gx.obe.web.service.evaluation.EvaluationStepService;
import com.swtdesigner.ResourceManager;
import com.swtdesigner.SWTResourceManager;

/**
 * @Description: 
 * @author guoyr
 */
public class CreateDefaultEvauationStepDialog extends EDialog {

	private TenderProjectEntity tenderProjectEntity;
	private TenderProjectRuleEntity tenderProjectRuleEntity;
	private ProjectRuleService projectRuleService;
	private EvaluationStepService evaluationStepService = new EvaluationStepService();
	private EvaluationFactorService evaluationFactorService = new EvaluationFactorService();
	private Composite nodeContentComposite;
	private ImageResolver imageResolver = new ImageResolver();
	private TipsComposite noteMessageLabel;
	private String detailName = Messages.Step_score;
	private String recommendName = Messages.Step_recommoned;
	private List<EvaluationFactor> complianceEvaluationFactorList = null;
	private List<EvaluationFactor> scoreFactorList = null;
	private List<EvaluationFactor> scoreFactorByPassList = null;
	private List<EvaluationFactor> priceFactorList = null;
	private List<EvaluationFactor> billFactorList = null;
	private List<EvaluationFactor> creditFactorList = null;// 信用得分指标
	private ScrolledComposite scrolledComposite;
	{
		LogUtils.logClass(CreateDefaultEvauationStepDialog.class);
	}
	
	/**
	 * Create the dialog.
	 * @param parentShell
	 */
	public CreateDefaultEvauationStepDialog(Shell parentShell, TenderProjectEntity tenderProjectEntity) {
		super(parentShell);
		this.setTitle(Messages.CreateDefaultEvaluationStep);
		this.tenderProjectEntity =tenderProjectEntity;
		projectRuleService = new ProjectRuleService();
	}

	/**
	 * Create contents of the dialog.
	 * @param parent
	 */
	@Override
	protected Control createDialogArea(Composite parent) {
		Composite container = (Composite) super.createDialogArea(parent);
		GridLayout gl_container = new GridLayout(1, false);
		gl_container.verticalSpacing = 0;
		gl_container.horizontalSpacing = 0;
		gl_container.marginHeight = 10;
		gl_container.marginWidth = 10;
		container.setLayout(gl_container);
		
//		noteMessageLabel = new Tips(container, SWT.WRAP);
//		noteMessageLabel.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
//		noteMessageLabel.setText("提示");
		noteMessageLabel = new TipsComposite(container, SWT.NONE);
		noteMessageLabel.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false, 1, 1));
		noteMessageLabel.setBackground(SWTResourceManager.getColor(252, 252, 238));
		noteMessageLabel.setImage(ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, "icons/tips.png"));
		scrolledComposite = new ScrolledComposite(container, SWT.V_SCROLL);
		scrolledComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
		scrolledComposite.setExpandHorizontal(true);
		scrolledComposite.setExpandVertical(true);
		
		nodeContentComposite = new Composite(scrolledComposite, SWT.NONE);
		nodeContentComposite.setLayout(new GridLayout(1, false));
		
		scrolledComposite.setContent(nodeContentComposite);
		scrolledComposite.setMinSize(nodeContentComposite.computeSize(SWT.DEFAULT, SWT.DEFAULT));
		init();
		return container;
	}
	
	/**
	 * @Description:  初始化
	 * @author guoyr
	 */
	private void init(){
		tenderProjectRuleEntity = projectRuleService.getProjectRule(tenderProjectEntity.getId());
		if(null == tenderProjectRuleEntity)tenderProjectRuleEntity = new TenderProjectRuleEntity();
		
		if(StringUtils.isEmpty(tenderProjectRuleEntity.getEvalMethodName())){
			noteMessageLabel.addTips(MessageFormat.format(Messages.Tips_notSetFormat, Messages.EvalMethod), ColorConstants.FONT_RED_COLOR, null);
		}
		// 如果存在符合性评审指标才添加添加符合性评审节点
		complianceEvaluationFactorList = evaluationFactorService.getAllEvaluationFactorList(tenderProjectEntity.getId(), EvaluationFactorEnum.AUDIT_COMPLIANCE);
		if(null == complianceEvaluationFactorList || complianceEvaluationFactorList.size() == 0){
			noteMessageLabel.addTips(MessageFormat.format(Messages.Tips_notSetFormat, Messages.Factor_compliance), ColorConstants.FONT_RED_COLOR, null);
		}

		List<EvaluationFactor> queryScoreFactorList = evaluationFactorService.getAllEvaluationFactorList(tenderProjectEntity.getId(), EvaluationFactorEnum.AUDIT_SCORE);
		if(null != queryScoreFactorList && queryScoreFactorList.size() > 0){
			for(EvaluationFactor evaluationFactor : queryScoreFactorList){
				// 信用得分单独创建一个步骤
				if(null != evaluationFactor.getEvalBidType() && evaluationFactor.getEvalBidType().equals(EvaluationFactorEnum.EVAL_BID_CREDIT)){
					if(null == creditFactorList){
						creditFactorList = new ArrayList<EvaluationFactor>();
					}
					creditFactorList.add(evaluationFactor);
					continue;
				}
				if(null != evaluationFactor.getScoreType() && evaluationFactor.getScoreType().equals(EvaluationFactorEnum.SCORE_TYPE_PASS)){
					if(null == scoreFactorByPassList){
						scoreFactorByPassList = new ArrayList<EvaluationFactor>();
					}
					scoreFactorByPassList.add(evaluationFactor);
				}else {
					if(null == scoreFactorList){
						scoreFactorList = new ArrayList<EvaluationFactor>();
					}
					// 先判断是否存在通过性指标，如果有通过性指标，则
					boolean hasScoreTypePassFactor = false;
					if(null != evaluationFactor.getChildFactorList() && evaluationFactor.getChildFactorList().size() > 0){
						for(EvaluationFactor childFactor : evaluationFactor.getChildFactorList()){
							if(null != childFactor.getScoreType() && childFactor.getScoreType().equals(EvaluationFactorEnum.SCORE_TYPE_PASS)){
								hasScoreTypePassFactor = true;
								break;
							}
						}
					}
					if(hasScoreTypePassFactor){
						if(null == evaluationFactor.getScoreType() || !evaluationFactor.getScoreType().equals(EvaluationFactorEnum.SCORE_TYPE_PASS_PARENT)){
							// 当出现这种情况时将父节点标识为在分配步指标时不显示，以免因为最在分配完最后一个二级指标时，因为把父级指标分配进去，导致其他步骤显示不出指标
							evaluationFactor.setScoreType(EvaluationFactorEnum.SCORE_TYPE_PASS_PARENT);
							evaluationFactorService.updateAssignProperty(evaluationFactor, new String[]{"scoreType"});
						}
						for(EvaluationFactor childFactor : evaluationFactor.getChildFactorList()){
							if(null != childFactor.getScoreType() && childFactor.getScoreType().equals(EvaluationFactorEnum.SCORE_TYPE_PASS)){
								if(null == scoreFactorByPassList){
									scoreFactorByPassList = new ArrayList<EvaluationFactor>();
								}
								scoreFactorByPassList.add(childFactor);
							}else {
								scoreFactorList.add(childFactor);
							}
						}
					}else {
						scoreFactorList.add(evaluationFactor);
					}
				}
			}
		}else {
			noteMessageLabel.addTips(MessageFormat.format(Messages.Tips_notSetFormat, Messages.Factor_score), ColorConstants.FONT_RED_COLOR, null);
		}
		
		creditFactorList = evaluationFactorService.getAllEvaluationFactorList(tenderProjectEntity.getId(), EvaluationFactorEnum.AUDIT_CREDIT);
		
		// 如果不是二阶段评标法和非资格预审项目才能生成价格评审
		if(!ObjectUtils.equals(tenderProjectEntity.getPhaseType(), TenderProjectEnum.PHASE_TYPE_PRE)){
			
			priceFactorList = evaluationFactorService.getAllEvaluationFactorList(tenderProjectEntity.getId(), EvaluationFactorEnum.AUDIT_PRICE);
			if(null == priceFactorList || priceFactorList.size() == 0){
				noteMessageLabel.addTips(MessageFormat.format(Messages.Tips_notSetFormat, Messages.Factor_price), ColorConstants.FONT_RED_COLOR, null);
			}
		}
		
		if(StringUtils.equals(tenderProjectEntity.getTenderType(), TenderProjectEnum.TENDER_YTPE_JZGC)){
			billFactorList = evaluationFactorService.getAllEvaluationFactorList(tenderProjectEntity.getId(), EvaluationFactorEnum.AUDIT_BILL);
		}
		
		for(Control control : nodeContentComposite.getChildren()){
			control.dispose();
		}
		noteMessageLabel.addTips(Messages.Tips_generatStep);
		
		EvaluationStepAndFactor templateStep = getTemplateStepNode();
		if(null != templateStep && null != templateStep.getNodeList() && templateStep.getNodeList().size() > 0){
			createNodeContent(templateStep);
		}
		createNodeContent(createSingleDefaultNode());
		createNodeContent(createMultyDefaultNode());
		
		nodeContentComposite.layout();
		scrolledComposite.setContent(nodeContentComposite);
		scrolledComposite.setMinSize(nodeContentComposite.computeSize(SWT.DEFAULT, SWT.DEFAULT));
	}
	
	/**
	 * @Description: 创建评标步骤内容
	 * @author guoyr
	 * @param evaluationStepAndFactor
	 */
	private void createNodeContent(final EvaluationStepAndFactor evaluationStepAndFactor){
		Group group = new Group(nodeContentComposite, SWT.NONE);
		group.setLayout(new GridLayout(3, false));
		group.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1));
		
		Composite evaluationStepComposite = new Composite(group, SWT.NONE);
		evaluationStepComposite.setLayout(new RowLayout(SWT.HORIZONTAL));
		GridData gd_evaluationStepComposite = new GridData(SWT.FILL, SWT.CENTER, true, true, 1, 1);
		gd_evaluationStepComposite.widthHint = 652;
		evaluationStepComposite.setLayoutData(gd_evaluationStepComposite);
		
		int nodeCount = 0;
		for(final EvaluationStep node : evaluationStepAndFactor.nodeList){
			if(nodeCount > 0){
				ImageButton labelSep = new ImageButton(evaluationStepComposite, SWT.NONE);
				labelSep.setImage(ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, "icons/evaluaton_step/next.png"));
				labelSep.setLayoutData(new RowData(SWT.DEFAULT, 70));
				labelSep.setText("");
				labelSep.setSurroundColor(SWTResourceManager.getColor(60, 138, 226));
				labelSep.setSurroundMargin(5);
			}
			nodeCount ++;
			final StepButton nodeLabel = new StepButton(evaluationStepComposite, SWT.NONE);
			nodeLabel.setSurroundMargin(1);
			setNodeLableInfo(nodeLabel, node);
		}
		
		ESepator label_2 = new ESepator(group, SWT.SEPARATOR | SWT.VERTICAL);
		label_2.setLayoutData(new GridData(SWT.LEFT, SWT.FILL, false, true, 1, 2));
		label_2.setBackground(ColorConstants.SEP_LIGHT_COLOR);
		
		ImageButton checkLabel = new ImageButton(group, SWT.NONE | SWT.BOTTOM);
		checkLabel.setLayoutData(new GridData(SWT.LEFT, SWT.FILL, false, false, 1, 1));
		checkLabel.setImage(ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, "icons/add_select.png"));
		checkLabel.setSurroundColor(SWTResourceManager.getColor(60, 138, 226));
		checkLabel.setActiveSurroundColor(SWTResourceManager.getColor(77, 163, 19));
		checkLabel.setSurroundMargin(2);
		checkLabel.setText(Messages.Button_select);
		checkLabel.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseUp(MouseEvent e) {
				createDefaultEvalNodeAction(evaluationStepAndFactor);
			}
		});
	}
	
	/**
	 * @Description: 设置评标步骤的信息
	 * @author guoyr
	 * @param nodeLabel
	 * @param evaluationStep
	 */
	private void setNodeLableInfo(StepButton nodeLabel, EvaluationStep evaluationStep){
		nodeLabel.setImage(imageResolver.loadImage(IconsActivator.PLUGIN_ID, EvaluationStepEnum.getEvaluationStepIco(evaluationStep.getStepType())));
		nodeLabel.setText(evaluationStep.getStepName());
		nodeLabel.setData("evaluationStep", evaluationStep);
	}
	
	private EvaluationStep getEvaluationStep(String stepType, String defaultStepName, String _evalBidType, List<EvaluationFactor> topFactorList, int sortNo, boolean isScoreFactor){
		String detailStepId = UuidUtils.getUUID();
		String stepName = defaultStepName;
		String evalBidType = _evalBidType;
		BigDecimal score = null;
		BigDecimal weight = null;

		if(topFactorList.size() == 1){
			EvaluationFactor topFactor = topFactorList.get(0);
			if(isScoreFactor){
				score = topFactor.getFactorScore();
				if(null != topFactor.getParentFactor()){
					weight = topFactor.getParentFactor().getFactorWeight();
				}else {
					weight = topFactor.getFactorFinalWeight();
				}
			}
			if(null == evalBidType){
				evalBidType = topFactor.getEvalBidType();
			}
		}else {
			boolean isBusBid = false;
			boolean isTecBid = false;
			List<String> parentIds = new ArrayList<String>();
			for(EvaluationFactor topFactor : topFactorList){
				if(isScoreFactor){
					BigDecimal factorScore = topFactor.getFactorScore();
					BigDecimal factorWeight = topFactor.getFactorWeight();
					if(null != topFactor.getParentFactor()){
//						factorScore = topFactor.getParentFactor().getFactorScore();
						if(!parentIds.contains(topFactor.getParentFactor().getId())){
							factorWeight = topFactor.getParentFactor().getFactorWeight();
						}
					}
					if(null != factorScore){
						score = null != score ? BigDecimalUtils.add(score, factorScore) : factorScore;
						if(null != factorWeight){
							BigDecimal thisWeight = BigDecimalUtils.div(BigDecimalUtils.mul(factorScore, factorWeight), new BigDecimal(100));
							weight = null != weight ? BigDecimalUtils.add(thisWeight, weight) : thisWeight;
						}
					}
				}
				if(null == evalBidType){
					if(null != topFactor.getEvalBidType() && topFactor.getEvalBidType().length() > 0){
						if(!isBusBid && topFactor.getEvalBidType().equals(EvaluationFactorEnum.EVAL_BID_BUS)){
							isBusBid = true;
						}else if(!isTecBid && topFactor.getEvalBidType().equals(EvaluationFactorEnum.EVAL_BID_TEC)){
							isTecBid = true;
						}
					}
				}
			}
			if(null == evalBidType){
				if(isBusBid && isTecBid){
					evalBidType = EvaluationFactorEnum.EVAL_BID_DETAIL;
				}else if(isBusBid){
					evalBidType = EvaluationFactorEnum.EVAL_BID_BUS;
				}else if(isTecBid){
					evalBidType = EvaluationFactorEnum.EVAL_BID_TEC;
				}
			}
		}
		
		if(null == stepName){
			String factorName = topFactorList.get(0).getFactorName();
			stepName = factorName.length() > 9 ? factorName.substring(0,9):factorName;
		}
		EvaluationStep evaluationStep = EvaluationStep.initEvaluationStep(detailStepId, stepName, tenderProjectEntity.getId(), stepType, sortNo);
		evaluationStep.setEvalBidType(evalBidType);
		if(null != evalBidType && evalBidType.length() > 0){
			if(null == evaluationStep.getRoleType() || !(evaluationStep.getRoleType().equals(EvaluationStepEnum.EXPERT_LEADER) || evaluationStep.getRoleType().equals(EvaluationStepEnum.OPERATOR))){
				evaluationStep.setRoleType(EvaluationStepEnum.EVAL_BID_TYPE);
			}
		}
		evaluationStep.setScore(score);
		evaluationStep.setWeight(weight);
		return evaluationStep;
	}
	
	private EvaluationStepAndFactor getTemplateStepNode(){
		File templateStepFile = new File(Platform.getInstallLocation().getURL().getPath()+File.separator + FileConstants.DEFAULT_EVALUATION_STEP_FOLDER+File.separator + "evaluationStep.xml");
		if(null != templateStepFile && templateStepFile.exists()){
			EvaluationStepGenerator evaluationStepGenerator = new EvaluationStepGenerator(tenderProjectEntity, tenderProjectRuleEntity, templateStepFile.getAbsolutePath());
			evaluationStepGenerator.setComplianceEvaluationFactorList(null != complianceEvaluationFactorList ? new ArrayList<EvaluationFactor>(complianceEvaluationFactorList) : null);
			evaluationStepGenerator.setScoreFactorList(null != scoreFactorList ? new ArrayList<EvaluationFactor>(scoreFactorList) : null);
			evaluationStepGenerator.setScoreFactorByPassList(null != scoreFactorByPassList ? new ArrayList<EvaluationFactor>(scoreFactorByPassList) : null);
			evaluationStepGenerator.setCreditFactorList(null != creditFactorList ? new ArrayList<EvaluationFactor>(creditFactorList) : null);
			evaluationStepGenerator.setBillFactorList(null != billFactorList ? new ArrayList<EvaluationFactor>(billFactorList) : null);
			evaluationStepGenerator.setPriceFactorList(null != priceFactorList ? new ArrayList<EvaluationFactor>(priceFactorList) : null);
			
			List<EvaluationStep> evaluationStepList = new ArrayList<EvaluationStep>();
			List<EvaluationStepFactor> evaluationStepFactorList = new ArrayList<EvaluationStepFactor>();
			String message = evaluationStepGenerator.generateEvaluationStep();
			if(null == message){
				evaluationStepList = evaluationStepGenerator.getEvaluationStepList();
				evaluationStepFactorList = evaluationStepGenerator.getEvaluationStepFactorList();
			}
			
			if(null != evaluationStepList && evaluationStepList.size() > 0){
			}
			return new EvaluationStepAndFactor(evaluationStepList, evaluationStepFactorList); 
		}
		return null;
	}
	
	/**
	 * @Description: 创建单一的评标步骤
	 * @author guoyr
	 * @param complianceEvaluationFactorList
	 */
	private EvaluationStepAndFactor createSingleDefaultNode(){
		
		int sortNo = 1;
		List<EvaluationStep> nodeList = new ArrayList<EvaluationStep>();
		List<EvaluationStepFactor> evaluationStepFactorList = new ArrayList<EvaluationStepFactor>();
		// 添加签到节点
		nodeList.add(EvaluationStep.initEvaluationStep(UuidUtils.getUUID(), Messages.Step_signIn, tenderProjectEntity.getId(), EvaluationStepEnum.SIGIN, sortNo++));
		
		if(null != complianceEvaluationFactorList && complianceEvaluationFactorList.size() > 0){
			EvaluationStep evaluationStep = getEvaluationStep(EvaluationStepEnum.COMPLIANCE, Messages.Step_compliance, null, complianceEvaluationFactorList, sortNo++, false);
			nodeList.add(evaluationStep);
			evaluationStepFactorList.addAll(batchSaveEvaluationStepFactor(evaluationStep, tenderProjectEntity.getId(), complianceEvaluationFactorList));
		}
		if(null != scoreFactorByPassList && scoreFactorByPassList.size() > 0){
			EvaluationStep evaluationStep = getEvaluationStep(EvaluationStepEnum.DETAIL, null, null, scoreFactorByPassList, sortNo++, true);
			evaluationStep.setSummaryType(EvaluationStepEnum.SUMMARY_SCORE_BY_QUALIFIED);
			evaluationStep.setFilterType(EvaluationStepEnum.FILTER_YES);
			evaluationStep.setPassType(EvaluationStepEnum.PASS_MORE);
			nodeList.add(evaluationStep);
			evaluationStepFactorList.addAll(batchSaveEvaluationStepFactor(evaluationStep, tenderProjectEntity.getId(), scoreFactorByPassList));
		}
		if(null != scoreFactorList && scoreFactorList.size() > 0){
			EvaluationStep evaluationStep = getEvaluationStep(EvaluationStepEnum.DETAIL, detailName, null, scoreFactorList, sortNo++, true);
			nodeList.add(evaluationStep);
			evaluationStepFactorList.addAll(batchSaveEvaluationStepFactor(evaluationStep, tenderProjectEntity.getId(), scoreFactorList));
		}
		
		if(null != billFactorList && billFactorList.size() > 0){
			EvaluationStep evaluationStep = getEvaluationStep(EvaluationStepEnum.BILL, Messages.Step_billCheck, EvaluationFactorEnum.EVAL_BID_BUS, billFactorList, sortNo++, true);
			nodeList.add(evaluationStep);
			evaluationStepFactorList.addAll(batchSaveEvaluationStepFactor(evaluationStep, tenderProjectEntity.getId(), billFactorList));
		}
		if(null != creditFactorList && creditFactorList.size() > 0){
			EvaluationStep evaluationStep = getEvaluationStep(EvaluationStepEnum.CREDIT, Messages.Step_credit, null, creditFactorList, sortNo++, true);
			nodeList.add(evaluationStep);
			evaluationStepFactorList.addAll(batchSaveEvaluationStepFactor(evaluationStep, tenderProjectEntity.getId(), creditFactorList));
		}

		if(null != priceFactorList && priceFactorList.size() > 0){
			EvaluationStep evaluationStep = getEvaluationStep(EvaluationStepEnum.PRICE, Messages.Step_price, null, priceFactorList, sortNo++, true);
			evaluationStep.setEvalBidType(EvaluationFactorEnum.EVAL_BID_PRICE);
			nodeList.add(evaluationStep);
			evaluationStepFactorList.addAll(batchSaveEvaluationStepFactor(evaluationStep, tenderProjectEntity.getId(), priceFactorList));
		}
		
		// 添加推荐节点
		nodeList.add(EvaluationStep.initEvaluationStep(UuidUtils.getUUID(), recommendName, tenderProjectEntity.getId(), EvaluationStepEnum.RECOMMONED, sortNo++));
		return new EvaluationStepAndFactor(nodeList, evaluationStepFactorList); 
	}

	/**
	 * @Description: 创建单一的评标步骤
	 * @author guoyr
	 * @param complianceEvaluationFactorList
	 * @return 
	 */
	private EvaluationStepAndFactor createMultyDefaultNode(){
		int sortNo = 1;
		List<EvaluationStep> nodeList = new ArrayList<EvaluationStep>();
		List<EvaluationStepFactor> evaluationStepFactorList = new ArrayList<EvaluationStepFactor>();
		// 添加签到节点
		nodeList.add(EvaluationStep.initEvaluationStep(UuidUtils.getUUID(), Messages.Step_signIn, tenderProjectEntity.getId(), EvaluationStepEnum.SIGIN, sortNo++));
		if(null != complianceEvaluationFactorList && complianceEvaluationFactorList.size() > 0){
			
			if(complianceEvaluationFactorList.size() > 1 && complianceEvaluationFactorList.size() <= 5){
				for(EvaluationFactor evaluationFactor : complianceEvaluationFactorList){
					List<EvaluationFactor> evaluationFactorList = new ArrayList<EvaluationFactor>();
					evaluationFactorList.add(evaluationFactor);
					EvaluationStep evaluationStep = getEvaluationStep(EvaluationStepEnum.COMPLIANCE, null, null, evaluationFactorList, sortNo++, false);
					nodeList.add(evaluationStep);
					evaluationStepFactorList.addAll(batchSaveEvaluationStepFactor(evaluationStep, tenderProjectEntity.getId(), evaluationFactorList));
				}
				
			}else {
				// 如果只存在一个父级指标或存多余三个父级指标则只创建一个评标步骤
				EvaluationStep evaluationStep = getEvaluationStep(EvaluationStepEnum.COMPLIANCE, Messages.Step_compliance, null, complianceEvaluationFactorList, sortNo++, false);
				nodeList.add(evaluationStep);
				evaluationStepFactorList.addAll(batchSaveEvaluationStepFactor(evaluationStep, tenderProjectEntity.getId(), complianceEvaluationFactorList));
			
			}
		}
		if(null != scoreFactorByPassList && scoreFactorByPassList.size() > 0){
			EvaluationStep evaluationStep = getEvaluationStep(EvaluationStepEnum.DETAIL, null, null, scoreFactorByPassList, sortNo++, true);
			evaluationStep.setSummaryType(EvaluationStepEnum.SUMMARY_SCORE_BY_QUALIFIED);
			evaluationStep.setFilterType(EvaluationStepEnum.FILTER_YES);
			evaluationStep.setPassType(EvaluationStepEnum.PASS_MORE);
			nodeList.add(evaluationStep);
			evaluationStepFactorList.addAll(batchSaveEvaluationStepFactor(evaluationStep, tenderProjectEntity.getId(), scoreFactorByPassList));
		}
		if(null != scoreFactorList && scoreFactorList.size() > 0){
			
			if(scoreFactorList.size() > 1 && scoreFactorList.size() <= 5){
				for(EvaluationFactor evaluationFactor : scoreFactorList){
					List<EvaluationFactor> evaluationFactorList = new ArrayList<EvaluationFactor>();
					evaluationFactorList.add(evaluationFactor);
					EvaluationStep evaluationStep = getEvaluationStep(EvaluationStepEnum.DETAIL, null, null, evaluationFactorList, sortNo++, true);
					nodeList.add(evaluationStep);
					evaluationStepFactorList.addAll(batchSaveEvaluationStepFactor(evaluationStep, tenderProjectEntity.getId(), evaluationFactorList));
				}
			}else {

				// 如果只存在一个父级指标或存多余三个父级指标则只创建一个评标步骤
				EvaluationStep evaluationStep = getEvaluationStep(EvaluationStepEnum.DETAIL, detailName, null, scoreFactorList, sortNo++, true);
				nodeList.add(evaluationStep);
				evaluationStepFactorList.addAll(batchSaveEvaluationStepFactor(evaluationStep, tenderProjectEntity.getId(), scoreFactorList));
			}
		}
		if(null != billFactorList && billFactorList.size() > 0){
			EvaluationStep evaluationStep = getEvaluationStep(EvaluationStepEnum.BILL, Messages.Step_billCheck, EvaluationFactorEnum.EVAL_BID_BUS, billFactorList, sortNo++, true);
			nodeList.add(evaluationStep);
			evaluationStepFactorList.addAll(batchSaveEvaluationStepFactor(evaluationStep, tenderProjectEntity.getId(), billFactorList));
		}
		if(null != creditFactorList && creditFactorList.size() > 0){
			EvaluationStep evaluationStep = getEvaluationStep(EvaluationStepEnum.CREDIT, PropertiesUtils.getEnum(I18NEnum.CREDIT_FACTOR, "综合信用")+"评审", null, creditFactorList, sortNo++, true);
			nodeList.add(evaluationStep);
			evaluationStepFactorList.addAll(batchSaveEvaluationStepFactor(evaluationStep, tenderProjectEntity.getId(), creditFactorList));
		}

		if(null != priceFactorList && priceFactorList.size() > 0){
			EvaluationStep evaluationStep = getEvaluationStep(EvaluationStepEnum.PRICE, PropertiesUtils.getEnum(I18NEnum.PRICE_FACTOR, "价格")+"评审", null, priceFactorList, sortNo++, true);
			nodeList.add(evaluationStep);
			evaluationStepFactorList.addAll(batchSaveEvaluationStepFactor(evaluationStep, tenderProjectEntity.getId(), priceFactorList));
		}
		
		// 添加推荐节点
		nodeList.add(EvaluationStep.initEvaluationStep(UuidUtils.getUUID(), recommendName, tenderProjectEntity.getId(), EvaluationStepEnum.RECOMMONED, sortNo++));
		return new EvaluationStepAndFactor(nodeList, evaluationStepFactorList); 
	}
	
	/**
	 * @Description: 批量保存评标步骤的指标
	 * @author guoyr
	 * @param stepId
	 * @param factorIdsList
	 */
	private List<EvaluationStepFactor> batchSaveEvaluationStepFactor(EvaluationStep evaluationStep, String tenderId, List<EvaluationFactor> topFactorList){
		List<EvaluationStepFactor> evaluationStepFactorList = new ArrayList<EvaluationStepFactor>();
		List<EvaluationFactor> evaluationFactorList = new ArrayList<EvaluationFactor>();
		for(EvaluationFactor topFactor : topFactorList){
			if(null != topFactor.getParentFactor()){// && null != topFactor.getParentFactor().getScoreType() && topFactor.getParentFactor().getScoreType().equals(EvaluationFactorEnum.SCORE_TYPE_PASS_PARENT)){
				if(!evaluationFactorList.contains(topFactor.getParentFactor())){
					if(null == topFactor.getParentFactor().getScoreType() || !topFactor.getParentFactor().getScoreType().equals(EvaluationFactorEnum.SCORE_TYPE_PASS_PARENT)){
						topFactor.getParentFactor().setScoreType(EvaluationFactorEnum.SCORE_TYPE_PASS_PARENT);
						evaluationFactorService.updateAssignProperty(topFactor.getParentFactor(), new String[]{"scoreType"});
					}
					evaluationFactorList.add(topFactor.getParentFactor());
				}
			}
			evaluationFactorList.addAll(initAddChildEvaluationFactor(topFactor));
		}
		boolean hasAIFactor = false;
		for(EvaluationFactor factor : evaluationFactorList){
			if(null != factor){
				EvaluationStepFactor nodeFactor = new EvaluationStepFactor();
				nodeFactor.setId(UuidUtils.getUUID());
				nodeFactor.setStepId(evaluationStep.getId());
				nodeFactor.setFactorId(factor.getId());
				nodeFactor.setTenderId(tenderId);
				evaluationStepFactorList.add(nodeFactor);
				if(!hasAIFactor && EvaluationFactorEnum.SCORE_TYPE_AI.equals(factor.getScoreType())){
					hasAIFactor = true;
				}
			}
		}
		if(hasAIFactor){
			evaluationStep.setEvaluationParam(EvaluationStepEnum.EVALUATION_TYPE, EvaluationStepEnum.EVALUATION_TYPE_AI);
		}else {
			evaluationStep.setEvaluationParam(EvaluationStepEnum.EVALUATION_TYPE, PropertiesUtils.getProperty(ConfigConstants.DEFAULT_EVALUATION_TYPE, EvaluationStepEnum.EVALUATION_TYPE_COLLECT));
		}
		return evaluationStepFactorList;
	}
	
	/**
	 * @Description: 递归获取所有的评审叶子指标
	 * @author guoyr
	 * @param evalFactor
	 */
	private List<EvaluationFactor> initAddChildEvaluationFactor(EvaluationFactor evalFactor){
		List<EvaluationFactor> evaluationFactorList = new ArrayList<EvaluationFactor>();
		if(null != evalFactor.getChildFactorList() && evalFactor.getChildFactorList().size() > 0){
			evaluationFactorList.add(evalFactor);
			for(EvaluationFactor childFactor : evalFactor.getChildFactorList()){
				// 排除浮动指标
				if(ObjectUtils.equals(childFactor.getFactorType(), EvaluationFactorEnum.FACTOR_FLOAT)){
					continue;
				}
				childFactor.setLevel(evalFactor.getLevel() + 1);
				evaluationFactorList.addAll(initAddChildEvaluationFactor(childFactor));
			}
		}else {
			evaluationFactorList.add(evalFactor);
			// 如果存在浮动指标，则直接获取浮动指标，为了在打分时直接将浮动指标紧跟着基础指标
			if(ObjectUtils.equals(evalFactor.getFactorType(), EvaluationFactorEnum.FACTOR_BASE) && null != evalFactor.getContainFactor()){
				evaluationFactorList.add(evalFactor.getContainFactor());
			}
		}
		return evaluationFactorList;
	}
	private class EvaluationStepAndFactor{
		private List<EvaluationStepFactor> evaluationStepFactorList = null;
		private List<EvaluationStep> nodeList = null;
		
		public EvaluationStepAndFactor( List<EvaluationStep> nodeList, List<EvaluationStepFactor> evaluationStepFactorList){
			this.nodeList = nodeList;
			this.evaluationStepFactorList = evaluationStepFactorList;
		}

		public List<EvaluationStepFactor> getEvaluationStepFactorList() {
			return evaluationStepFactorList;
		}

		public List<EvaluationStep> getNodeList() {
			return nodeList;
		}
	}
	
	/**
	 * @Description: 创建默认评标步骤
	 * @author guoyr
	 * @param evaluationStepAndFactor
	 */
	public void createDefaultEvalNodeAction(EvaluationStepAndFactor evaluationStepAndFactor){
		evaluationStepService.createEvaluationSteps(tenderProjectEntity.getId(), evaluationStepAndFactor.getNodeList(), evaluationStepAndFactor.getEvaluationStepFactorList());
		getShell().close();
	}
	@Override
	public boolean close() {
		imageResolver.disposite();
		return super.close();
	}

	/**
	 * Return the initial size of the dialog.
	 */
	@Override
	protected Point getInitialSize() {
		return new Point(765, 507);
	}
}
