package com.gx.obe.management.composite;

import java.math.BigDecimal;
import java.text.MessageFormat;
import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import org.eclipse.nebula.widgets.grid.Grid;
import org.eclipse.nebula.widgets.grid.GridColumn;
import org.eclipse.nebula.widgets.grid.GridItem;
import org.eclipse.nebula.widgets.grid.listener.GridSelectionListener;
import org.eclipse.swt.SWT;
import org.eclipse.swt.core.CallBack;
import org.eclipse.swt.core.ImageResolver;
import org.eclipse.swt.core.MessageDialog;
import org.eclipse.swt.core.listener.FloatInputListener;
import org.eclipse.swt.core.utils.WidgetUtils;
import org.eclipse.swt.core.widgets.BigButton;
import org.eclipse.swt.core.widgets.ConfirmDialog;
import org.eclipse.swt.core.widgets.ELabel;
import org.eclipse.swt.core.widgets.ELink;
import org.eclipse.swt.core.widgets.EMenu;
import org.eclipse.swt.core.widgets.ESepator;
import org.eclipse.swt.custom.ScrolledComposite;
import org.eclipse.swt.events.DisposeEvent;
import org.eclipse.swt.events.DisposeListener;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;

import com.gx.obe.ColorConstants;
import com.gx.obe.action.bean.Action;
import com.gx.obe.business.entity.MenuFunction;
import com.gx.obe.business.entity.ScoreItem;
import com.gx.obe.business.enumeration.ExpertEnum;
import com.gx.obe.common.icons.IconsActivator;
import com.gx.obe.common.widget.thread.SyncThread;
import com.gx.obe.common.widget.utils.MenuFunctionUtils;
import com.gx.obe.components.core.Constants;
import com.gx.obe.components.core.enumeration.CommonEnum;
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.management.dialog.ScoreItemDialog;
import com.gx.obe.message.Messages;
import com.gx.obe.util.utils.BigDecimalUtils;
import com.gx.obe.util.utils.CollectionUtils;
import com.gx.obe.util.utils.LogUtils;
import com.gx.obe.util.utils.ObjectUtils;
import com.gx.obe.util.utils.SerializeUtils;
import com.gx.obe.util.utils.StringUtils;
import com.gx.obe.web.entity.auth.Menu;
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.service.TenderProjectService;
import com.gx.obe.web.service.evaluation.EvaluationFactorService;
import com.gx.obe.web.service.evaluation.EvaluationStepService;
import com.gx.obe.web.service.expert.ExpertService;
import com.swtdesigner.ResourceManager;
import com.swtdesigner.SWTResourceManager;

/**
 * @Description: 评分指标管理
 * @author guoyr
 */
public class ScoreFactorsManagerComposite extends Composite {
	
	private Text factor_name_text;
	private Text factor_memo_text;
	private ELink add_btn;
	private ELink add_sub_btn;
	private ELink del_btn;
	private BigButton save_btn;
	private Grid factors_grid;
	private GridColumn gridColumn;
	private ImageResolver imageResolver = new ImageResolver();
	private Image menuOpenImage;
	private Image menuImage;
	private Text factor_score_text;
	private Text min_score_text;
	private Text max_score_text;
	private Text weight_text;
	private Composite kgxParentComposite;
	private Composite weightComposite;
	private EvaluationFactor curFactor;

	private ScrolledComposite scrolledComposite;
	private Composite scroContentComposite;
	private Composite composite_1;
	private ELink upFactorBtn;
	private Composite composite_4;
	private ELink downFactorBtn;
	private boolean isInEvaluationProgress = false;
	
	private boolean isExistEvaluationStep = false;
	private Composite objectiveScoreComposite;
	private ELabel label_18;
	private Composite composite_7;
	private GridColumn gridColumn_1;
	private GridColumn gridColumn_2;
	private GridColumn gridColumn_3;
	private Composite composite_8;
	private Composite composite_10;
	private ELink addObjectiveScoreBtn;
	private ELink updateObjectiveScoreBtn;
	private ELink delObjectiveScoreBtn;
	private Grid objectiveItemGrid;
	private List<ScoreItem> objectiveItemList;
	private ScoreItem selectObjectItem;
	private Composite scoreTypeComposite;
	private Button scoreTypeDirectBtn;
	private Button scoreTypeObjectiveBtn;
//	private Button scoreTypePassBtn;
	private Composite compositeContentComposite;
	private Button kgxNoBtn;
	private Button kgxYesBtn;
	private Label defaultValueLabel;
//	private EvaluationFactorService evaluationFactorService = new EvaluationFactorService();
	private Composite composite_11;
	private Composite score_composite;
//	private boolean hasFloat;
	private Composite composite_14;
	private Label weightTitle;
	private Label scoreTitle;
//	private boolean hasCreditFactor = false;
	private Label evalBidTypeTitle;
	private Combo evalBidTypeCombo;
	private Label scoreTypeLabel;
	private Label label_groupType;
	private List<String[]> groupList = new ArrayList<String[]>();
	private Combo groupCombo;
	private Button scoreTypeAIBtn;
	private Menu menu;
	private MenuFunctionUtils menuFunctionUtils = new MenuFunctionUtils();
	private EvaluationStepService evaluationStepService;
	private TenderProjectEntity tenderProject;
	private ExpertService expertService;
	private TenderProjectService tenderProjectService; 
	private EvaluationFactorService evaluationFactorService;
	private Action projectAction;
	private FloatInputListener factorPositiveMinScoreListener;
	private FloatInputListener factorPositiveMaxScoreListener;
	private FloatInputListener factorNegativeMinScoreListener;
	private FloatInputListener factorNegativeMaxScoreListener;
	
	{
		LogUtils.logClass(ScoreFactorsManagerComposite.class);
	}
	
	/**
	 * Create the composite.
	 * @param parent
	 * @param style
	 */
	public ScoreFactorsManagerComposite(Composite parent, Action projectAction, int style) {
		super(parent, style);
		GridLayout gridLayout = new GridLayout(2, false);
		gridLayout.horizontalSpacing = 10;
		gridLayout.verticalSpacing = 0;
		gridLayout.marginHeight = 0;
		gridLayout.marginWidth = 0;
		setLayout(gridLayout);
		addDisposeListener(new DisposeListener() {
			public void widgetDisposed(DisposeEvent e) {
				imageResolver.disposite();
			}
		});
		this.projectAction = projectAction;
		
		composite_1 = new Composite(this, SWT.NONE);
		GridData gd_composite_1 = new GridData(SWT.FILL, SWT.FILL, false, true, 1, 1);
		gd_composite_1.widthHint = 300;
		composite_1.setLayoutData(gd_composite_1);
		composite_1.setBackground(ColorConstants.BORDER_LINE_COLOR);
		GridLayout gl_composite_1 = new GridLayout(1, false);
		gl_composite_1.marginWidth = 1;
		gl_composite_1.marginHeight = 1;
		gl_composite_1.verticalSpacing = 1;
		composite_1.setLayout(gl_composite_1);
		composite_4 = new Composite(composite_1, SWT.NONE);
		composite_4.setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
		composite_4.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false, 1, 1));
		GridLayout gl_composite_4 = new GridLayout(5, false);
		composite_4.setLayout(gl_composite_4);
		
		upFactorBtn = new ELink(composite_4, SWT.NONE);
		upFactorBtn.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseUp(MouseEvent e) {
				moveEvaluationFactorAction(-1);
			}
		});
		upFactorBtn.setImage(ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, "icons/btn/blue/up.png"));
		upFactorBtn.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, true, false, 1, 1));
		upFactorBtn.setText(Messages.Button_moveUp);
		
		downFactorBtn = new ELink(composite_4, SWT.NONE);
		downFactorBtn.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		downFactorBtn.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseUp(MouseEvent e) {
				moveEvaluationFactorAction(1);
			}
		});
		downFactorBtn.setImage(ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, "icons/btn/blue/down.png"));
		downFactorBtn.setText(Messages.Button_moveDown);
		
		add_btn = new ELink(composite_4, SWT.NONE);
		add_btn.setImage(ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, "icons/btn/blue/add.png"));
		add_btn.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseUp(MouseEvent e) {
				addFctorAction();
			}
		});
		add_btn.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		add_btn.setText(Messages.Button_add);
		add_btn.setToolTipText(Messages.Button_addSibling);
		menuFunctionUtils.addFunction(add_btn, "ADD");
		
		add_sub_btn = new ELink(composite_4, SWT.NONE);
		add_sub_btn.setImage(ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, "icons/btn/blue/add_sub.png"));
		add_sub_btn.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseUp(MouseEvent e) {
				addSubFctorAction();
			}
		});
		add_sub_btn.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		add_sub_btn.setText(Messages.Button_add);
		add_sub_btn.setToolTipText(Messages.Button_addChildSibling);
		menuFunctionUtils.addFunction(add_sub_btn, "ADD_SUB");
		
		del_btn = new ELink(composite_4, SWT.NONE);
		del_btn.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		del_btn.setImage(ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, "icons/btn/blue/delete.png"));
		del_btn.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseUp(MouseEvent e) {
				delEvaluationFactorAction();
			}
		});
		del_btn.setText(Messages.Button_delete);
		menuFunctionUtils.addFunction(del_btn, "DELETE");
		
		composite_14 = new Composite(composite_1, SWT.NONE);
		composite_14.setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
		composite_14.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
		GridLayout gl_composite_14 = new GridLayout(1, false);
		gl_composite_14.marginHeight = 0;
		gl_composite_14.marginWidth = 0;
		composite_14.setLayout(gl_composite_14);
		
		factors_grid = new Grid(composite_14, SWT.V_SCROLL);
		factors_grid.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
		
		gridColumn = new GridColumn(factors_grid, SWT.NONE);
		gridColumn.setText(Messages.Factor_score);
		gridColumn.setWidth(200);
		
		Composite composite = new Composite(this, SWT.NONE);
		GridLayout gl_composite = new GridLayout(1, false);
		gl_composite.verticalSpacing = 0;
		gl_composite.horizontalSpacing = 0;
		gl_composite.marginWidth = 0;
		gl_composite.marginHeight = 0;
		composite.setLayout(gl_composite);
		composite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
		scrolledComposite = new ScrolledComposite(composite, SWT.V_SCROLL);
		scrolledComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
		scrolledComposite.setExpandHorizontal(true);
		scrolledComposite.setExpandVertical(true);
		
		scroContentComposite = new Composite(scrolledComposite, SWT.NONE);
		GridLayout gl_scroContentComposite = new GridLayout(1, false);
		gl_scroContentComposite.horizontalSpacing = 0;
		gl_scroContentComposite.verticalSpacing = 0;
		gl_scroContentComposite.marginHeight = 0;
		gl_scroContentComposite.marginWidth = 0;
		scroContentComposite.setLayout(gl_scroContentComposite);
		
		Composite composite_5 = new Composite(scroContentComposite, SWT.NONE);
		composite_5.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		GridLayout gl_composite_5 = new GridLayout(2, false);
		gl_composite_5.verticalSpacing = 10;
		gl_composite_5.marginWidth = 10;
		gl_composite_5.marginTop = 5;
		composite_5.setLayout(gl_composite_5);
		
		Label label = new Label(composite_5, SWT.RIGHT);
		label.setFont(SWTResourceManager.getFont("微软雅黑", 10, SWT.NORMAL));
//		gd_label.widthHint = 80;
		label.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		label.setText(Messages.FactorName + "：");
		
		composite_11 = new Composite(composite_5, SWT.NONE);
		composite_11.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, false, 1, 1));
		GridLayout gl_composite_11 = new GridLayout(1, false);
		gl_composite_11.marginWidth = 0;
		gl_composite_11.marginHeight = 0;
		gl_composite_11.verticalSpacing = 0;
		composite_11.setLayout(gl_composite_11);
		
		factor_name_text = new Text(composite_11, SWT.WRAP | SWT.MULTI);
		factor_name_text.setTopIndex(5);
		GridData gd_factor_name_text = new GridData(SWT.FILL, SWT.FILL, true, false, 1, 1);
		gd_factor_name_text.widthHint = 400;
		factor_name_text.setLayoutData(gd_factor_name_text);
		factor_name_text.setFont(SWTResourceManager.getFont("微软雅黑", 9, SWT.NORMAL));
		factor_name_text.setTextLimit(500);
		
		ESepator label_2 = new ESepator(composite_11, SWT.HORIZONTAL);
		label_2.setBackground(ColorConstants.SEP_LIGHT_COLOR);
		label_2.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		
		scoreTitle = new Label(composite_5, SWT.RIGHT);
		scoreTitle.setFont(SWTResourceManager.getFont("微软雅黑", 10, SWT.NORMAL));
		scoreTitle.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		scoreTitle.setText(Messages.FactorScore + "：");
		
		score_composite = new Composite(composite_5, SWT.NONE);
		score_composite.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1));
		GridLayout gl_composite_12 = new GridLayout(6, false);
		gl_composite_12.verticalSpacing = 0;
		gl_composite_12.horizontalSpacing = 0;
		gl_composite_12.marginWidth = 0;
		gl_composite_12.marginHeight = 0;
		score_composite.setLayout(gl_composite_12);
		
		factor_score_text = new Text(score_composite, SWT.NONE | SWT.CENTER);
		factor_score_text.addModifyListener(new ModifyListener() {
			public void modifyText(ModifyEvent arg0) {
				modifyFactorScoreAction(this);
			}
		});
		GridData gd_score_text = new GridData(SWT.CENTER, SWT.CENTER, false, false, 1, 1);
		gd_score_text.widthHint = 50;
		factor_score_text.setLayoutData(gd_score_text);
		
		Label label_19 = new Label(score_composite, SWT.NONE);
		label_19.setFont(SWTResourceManager.getFont("微软雅黑", 10, SWT.BOLD));
		label_19.setForeground(ColorConstants.NOTE_TIPS_COLOR);
		label_19.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, false, false, 1, 2));
		label_19.setText(Messages.TextScore + "（");
		
		min_score_text = new Text(score_composite, SWT.NONE | SWT.CENTER);
		
		GridData gd_min_score_text = new GridData(SWT.CENTER, SWT.CENTER, false, false, 1, 1);
		gd_min_score_text.widthHint = 50;
		min_score_text.setLayoutData(gd_min_score_text);
		
		ESepator label_6 = new ESepator(score_composite, SWT.SEPARATOR | SWT.HORIZONTAL);
		GridData gd_label_6 = new GridData(SWT.LEFT, SWT.CENTER, false, false, 1, 2);
		gd_label_6.widthHint = 20;
		label_6.setLayoutData(gd_label_6);
		label_6.setBackground(ColorConstants.SEP_LIGHT_COLOR);
		
		max_score_text = new Text(score_composite, SWT.NONE | SWT.CENTER);
		GridData gd_max_score_text = new GridData(SWT.CENTER, SWT.CENTER, false, false, 1, 1);
		gd_max_score_text.widthHint = 50;
		max_score_text.setLayoutData(gd_max_score_text);
		
		Label label_21 = new Label(score_composite, SWT.NONE);
		label_21.setFont(SWTResourceManager.getFont("微软雅黑", 10, SWT.BOLD));
		label_21.setForeground(ColorConstants.NOTE_TIPS_COLOR);
		label_21.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, false, false, 1, 2));
		label_21.setText("）");
		
		ESepator label_3 = new ESepator(score_composite, SWT.HORIZONTAL);
		label_3.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1));
		label_3.setBackground(ColorConstants.SEP_LIGHT_COLOR);
		
		ESepator label_23 = new ESepator(score_composite, SWT.HORIZONTAL);
		label_23.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1));
		label_23.setBackground(ColorConstants.SEP_LIGHT_COLOR);
		
		ESepator label_22 = new ESepator(score_composite, SWT.HORIZONTAL);
		label_22.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1));
		label_22.setBackground(ColorConstants.SEP_LIGHT_COLOR);
		
		weightTitle = new Label(composite_5, SWT.NONE);
		weightTitle.setFont(SWTResourceManager.getFont("微软雅黑", 10, SWT.NORMAL));
		weightTitle.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		weightTitle.setText(Messages.FactorWeight + "：");
		
		{
			weightComposite = new Composite(composite_5, SWT.NONE);
			GridData gd_weightComposite = new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1);
			gd_weightComposite.widthHint = 300;
			weightComposite.setLayoutData(gd_weightComposite);
			GridLayout gl_weightComposite = new GridLayout(3, false);
			gl_weightComposite.verticalSpacing = 0;
			gl_weightComposite.marginHeight = 0;
			gl_weightComposite.marginWidth = 0;
			weightComposite.setLayout(gl_weightComposite);
			
			weight_text = new Text(weightComposite, SWT.CENTER);
			GridData gd_weightText = new GridData(SWT.FILL, SWT.BOTTOM, false, true, 1, 1);
			gd_weightText.widthHint = 50;
			weight_text.setLayoutData(gd_weightText);
			
			Label label_9 = new Label(weightComposite, SWT.NONE);
			label_9.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, false, false, 1, 2));
			label_9.setText("%");
			
			Label wieght_tips = new Label(weightComposite, SWT.WRAP);
			wieght_tips.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 2));
			wieght_tips.setForeground(SWTResourceManager.getColor(SWT.COLOR_DARK_GRAY));
			wieght_tips.setText(Messages.Tips_weight);
			
			ESepator label_5 = new ESepator(weightComposite, SWT.HORIZONTAL);
			label_5.setLayoutData(new GridData(SWT.FILL, SWT.TOP, false, true, 1, 1));
			label_5.setBackground(ColorConstants.SEP_LIGHT_COLOR);
		}
		
		evalBidTypeTitle = new Label(composite_5, SWT.NONE);
		evalBidTypeTitle.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		evalBidTypeTitle.setFont(SWTResourceManager.getFont("微软雅黑", 10, SWT.NORMAL));
		evalBidTypeTitle.setText(Messages.EvalType+"：");
		
		evalBidTypeCombo = new Combo(composite_5, SWT.READ_ONLY);
		GridData gd_combo = new GridData(SWT.LEFT, SWT.CENTER, false, false, 1, 1);
		gd_combo.widthHint = 100;
		evalBidTypeCombo.setLayoutData(gd_combo);
		
		label_groupType = new Label(composite_5, SWT.NONE);
		label_groupType.setAlignment(SWT.RIGHT);
		label_groupType.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		label_groupType.setFont(SWTResourceManager.getFont("微软雅黑", 10, SWT.NORMAL));
		label_groupType.setText(Messages.EvalGroup+"：");
		menuFunctionUtils.addFunction(label_groupType, "EVAL_GROUP");
		
		groupCombo = new Combo(composite_5, SWT.READ_ONLY);
		gd_combo.widthHint = 100;
		menuFunctionUtils.addFunction(groupCombo, "EVAL_GROUP");
		
		Label label_8 = new Label(composite_5, SWT.NONE);
		label_8.setAlignment(SWT.RIGHT);
		label_8.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		label_8.setFont(SWTResourceManager.getFont("微软雅黑", 10, SWT.NORMAL));
		label_8.setText(Messages.ConsistencyCheck+"：");
		{
			
			kgxParentComposite = new Composite(composite_5, SWT.NONE);
			kgxParentComposite.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1));
			GridLayout gl_kgxParentComposite = new GridLayout(3, false);
			gl_kgxParentComposite.marginWidth = 0;
			gl_kgxParentComposite.marginHeight = 0;
			kgxParentComposite.setLayout(gl_kgxParentComposite);
			
			kgxYesBtn = new Button(kgxParentComposite, SWT.RADIO);
			kgxYesBtn.addMouseListener(new MouseAdapter() {
				@Override
				public void mouseUp(MouseEvent e) {
					changeObjective(EvaluationFactorEnum.OBJECTIVE_YES);
				}
			});
			kgxYesBtn.setData(EvaluationFactorEnum.OBJECTIVE_YES);
			kgxYesBtn.setText(Messages.Yes);
			
			kgxNoBtn = new Button(kgxParentComposite, SWT.RADIO);
			kgxNoBtn.setData(EvaluationFactorEnum.OBJECTIVE_NO);
			kgxNoBtn.setSelection(true);
			kgxNoBtn.setText(Messages.No);
			kgxNoBtn.addMouseListener(new MouseAdapter() {
				@Override
				public void mouseUp(MouseEvent e) {
					changeObjective(EvaluationFactorEnum.OBJECTIVE_NO);
				}
			});
			
			defaultValueLabel = new Label(kgxParentComposite, SWT.WRAP);
			defaultValueLabel.setForeground(SWTResourceManager.getColor(SWT.COLOR_DARK_GRAY));
			defaultValueLabel.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
			defaultValueLabel.setText(Messages.Tips_consistencyCheck);
			
		}
		
		scoreTypeLabel = new Label(composite_5, SWT.RIGHT);
		scoreTypeLabel.setFont(SWTResourceManager.getFont("微软雅黑", 10, SWT.NORMAL));
		scoreTypeLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.TOP, false, false, 1, 1));
		scoreTypeLabel.setText(Messages.ScoreType+"：");
		
		compositeContentComposite = new Composite(composite_5, SWT.NONE);
		compositeContentComposite.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		GridLayout gl_composite_6 = new GridLayout(1, false);
		gl_composite_6.horizontalSpacing = 0;
		gl_composite_6.marginHeight = 0;
		gl_composite_6.marginWidth = 0;
		compositeContentComposite.setLayout(gl_composite_6);
		
		scoreTypeComposite = new Composite(compositeContentComposite, SWT.NONE);
		scoreTypeComposite.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		GridLayout gl_scoreTypeComposite = new GridLayout(3, false);
		gl_scoreTypeComposite.marginHeight = 0;
		gl_scoreTypeComposite.marginWidth = 0;
		scoreTypeComposite.setLayout(gl_scoreTypeComposite);
		
		scoreTypeDirectBtn = new Button(scoreTypeComposite, SWT.RADIO);
		scoreTypeDirectBtn.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseUp(MouseEvent e) {
				scoreTypeChangeAction(true);
			}
		});
		scoreTypeDirectBtn.setText(Messages.Tips_scoreTypeInput);
		scoreTypeDirectBtn.setData(EvaluationFactorEnum.SCORE_TYPE_DIRECT);
		scoreTypeDirectBtn.setSelection(true);
		scoreTypeDirectBtn.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1));
		
		scoreTypeObjectiveBtn = new Button(scoreTypeComposite, SWT.RADIO);
		scoreTypeObjectiveBtn.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseUp(MouseEvent e) {
				scoreTypeChangeAction(true);
			}
		});
		scoreTypeObjectiveBtn.setText(Messages.Tips_scoreTypeSelect);
		scoreTypeObjectiveBtn.setData(EvaluationFactorEnum.SCORE_TYPE_OBJECTIVE);
		scoreTypeObjectiveBtn.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1));
		
		scoreTypeAIBtn = new Button(scoreTypeComposite, SWT.RADIO);
		scoreTypeAIBtn.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseUp(MouseEvent e) {
				scoreTypeChangeAction(true);
			}
		});
		scoreTypeAIBtn.setText(Messages.Tips_scoreTypeAI);
		scoreTypeAIBtn.setData(EvaluationFactorEnum.SCORE_TYPE_AI);
		scoreTypeAIBtn.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1));
		menuFunctionUtils.addFunction(scoreTypeAIBtn, "SCORE_TYPE_AI");
		
		objectiveScoreComposite = new Composite(compositeContentComposite, SWT.NONE);
		GridLayout gl_objectiveScoreComposite = new GridLayout(1, false);
		gl_objectiveScoreComposite.marginHeight = 1;
		gl_objectiveScoreComposite.marginWidth = 1;
		objectiveScoreComposite.setLayout(gl_objectiveScoreComposite);
		objectiveScoreComposite.setBackground(ColorConstants.BORDER_LINE_COLOR);
		GridData gd_objectiveScoreComposite = new GridData(SWT.FILL, SWT.CENTER, true, false, 2, 1);
		gd_objectiveScoreComposite.heightHint = 180;
		objectiveScoreComposite.setLayoutData(gd_objectiveScoreComposite);
		
		
		composite_7 = new Composite(objectiveScoreComposite, SWT.NONE);
		composite_7.setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
		GridLayout gl_composite_7 = new GridLayout(1, false);
		gl_composite_7.verticalSpacing = 0;
		gl_composite_7.horizontalSpacing = 0;
		gl_composite_7.marginHeight = 0;
		gl_composite_7.marginWidth = 0;
		composite_7.setLayout(gl_composite_7);
		composite_7.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
		
		composite_8 = new Composite(composite_7, SWT.NONE);
		composite_8.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false, 1, 1));
		GridLayout gl_composite_8 = new GridLayout(2, false);
		gl_composite_8.marginWidth = 0;
		composite_8.setLayout(gl_composite_8);
		
		label_18 = new ELabel(composite_8, SWT.NONE);
		label_18.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		label_18.setText(Messages.Title_dropDownOptions);
		label_18.setForeground(ColorConstants.NOTE_TIPS_COLOR);
		label_18.setFont(SWTResourceManager.getFont("微软雅黑", 10, SWT.BOLD));
		
		composite_10 = new Composite(composite_8, SWT.NONE);
		GridLayout gl_composite_10 = new GridLayout(3, false);
		gl_composite_10.marginHeight = 0;
		gl_composite_10.marginWidth = 0;
		composite_10.setLayout(gl_composite_10);
		composite_10.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1));
		
		addObjectiveScoreBtn = new ELink(composite_10, SWT.NONE);
		addObjectiveScoreBtn.setImage(ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, "icons/btn/blue/add.png"));
		addObjectiveScoreBtn.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseUp(MouseEvent e) {
				addObjectiveScoreAction();
			}
		});
		addObjectiveScoreBtn.setText(Messages.Button_add);
		
		updateObjectiveScoreBtn = new ELink(composite_10, SWT.NONE);
		updateObjectiveScoreBtn.setImage(ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, "icons/btn/blue/edit.png"));
		updateObjectiveScoreBtn.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseUp(MouseEvent e) {
				updateObjectiveScoreAction();
			}
		});
		updateObjectiveScoreBtn.setText(Messages.Button_modify);
		
		delObjectiveScoreBtn = new ELink(composite_10, SWT.NONE);
		delObjectiveScoreBtn.setImage(ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, "icons/btn/blue/delete.png"));
		delObjectiveScoreBtn.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseUp(MouseEvent e) {
				delObjectiveScoreAction();
			}
		});
		delObjectiveScoreBtn.setText(Messages.Button_delete);
		
		
		objectiveItemGrid = new Grid(composite_7, SWT.H_SCROLL | SWT.V_SCROLL);
		GridData gd_objectiveItemGrid = new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1);
		gd_objectiveItemGrid.widthHint = 400;
		gd_objectiveItemGrid.heightHint = 100;
		objectiveItemGrid.setLayoutData(gd_objectiveItemGrid);
		objectiveItemGrid.setHeaderVisible(true);
		
		gridColumn_1 = new GridColumn(objectiveItemGrid, SWT.CENTER);
		gridColumn_1.setText(Messages.Num);
		gridColumn_1.setWidth(40);
		gridColumn_1.setMaxmumWidth(50);
		
		gridColumn_3 = new GridColumn(objectiveItemGrid, SWT.NONE);
		gridColumn_3.setText(Messages.ScoreDescription);
		gridColumn_3.setWordWrap(true);
		gridColumn_3.setWidth(300);
		
		gridColumn_2 = new GridColumn(objectiveItemGrid, SWT.RIGHT);
		gridColumn_2.setText(Messages.Score);
		gridColumn_2.setWidth(30);
//		gridColumn_2.setMaxmumWidth(50);
		
		Label label_1 = new Label(composite_5, SWT.RIGHT);
		label_1.setFont(SWTResourceManager.getFont("微软雅黑", 10, SWT.NORMAL));
		label_1.setLayoutData(new GridData(SWT.RIGHT, SWT.TOP, false, false, 1, 1));
		label_1.setText(Messages.EvalExplain + "：");
		
		factor_memo_text = new Text(composite_5, SWT.BORDER | SWT.WRAP | SWT.V_SCROLL);
		GridData gd_factorMemoText = new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1);
		gd_factorMemoText.widthHint = 200;
		gd_factorMemoText.heightHint = 50;
		factor_memo_text.setLayoutData(gd_factorMemoText);
		factor_memo_text.setTextLimit(1000);
		
		scrolledComposite.setContent(scroContentComposite);
		scrolledComposite.setMinSize(scroContentComposite.computeSize(SWT.DEFAULT, SWT.DEFAULT));
		
		Composite composite_2 = new Composite(composite, SWT.NONE);
		composite_2.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		composite_2.setLayout(new GridLayout(1, false));
		
		menuFunctionUtils.addFunction(composite_2, "SAVE");
		save_btn = new BigButton(composite_2, SWT.NONE);
		save_btn.addMouseListener(new MouseAdapter() {
			public void mouseUp(MouseEvent e) {
				saveEvaluationFactorAction();
			}
		});
		save_btn.setFont(SWTResourceManager.getFont("微软雅黑", 10, SWT.BOLD));
		save_btn.setLayoutData(new GridData(SWT.CENTER, SWT.FILL, true, true, 1, 1));
		save_btn.setText(Messages.Button_save);
		
		evaluationFactorService = new EvaluationFactorService();
		tenderProjectService = new TenderProjectService();
		expertService = new ExpertService();
		init();
	}
	
	/**
	 * @Description: 切换一致性检查
	 * @author wangxiang
	 * @param Objective
	 */
	private void changeObjective(String objective) {
		if (factors_grid.getSelectionCount() > 0 && factors_grid.getSelection()[0].getItemCount() > 0) {
			if (MessageDialog.openConfirm(getShell(), Messages.Dialog_warmTips, Messages.Tips_applyToAllChildSiblingFactor, new String[] {Messages.Button_confirm, Messages.Button_cancel})) {
				String objectValue = ObjectUtils.getObjString(WidgetUtils.getRadioVal(kgxParentComposite));
				List<EvaluationFactor> childFactorLis = new ArrayList<EvaluationFactor>();
				setSubChildFactorProperty(childFactorLis, objectValue, curFactor);

 				if (evaluationFactorService.batchUpdateProperty(childFactorLis, new String[] {"id", "objective" })) {
					MessageDialog.openShortMessage(Messages.Tips_operateSuccess);
					refreshFactorTree(curFactor.getId());
				} else {
					MessageDialog.openError(getShell(), Messages.Dialog_warmTips, Messages.Tips_operateFail);
				}

			}
		}
	
	}
	
	/**
	 * @Description: 递归循环添加
	 * @author wangxiang
	 * @param nowFactor
	 * @param childFactorLis
	 * @param value
	 */
	private void setSubChildFactorProperty(List<EvaluationFactor> childFactorLis, String value, EvaluationFactor evalFactor) {
		List<EvaluationFactor> curChildFactorLis = evalFactor.getChildFactorList();
		evalFactor.setObjective(value);
		childFactorLis.add(evalFactor);
		if (null != curChildFactorLis && curChildFactorLis.size() > 0) {
			for (EvaluationFactor childFactor : curChildFactorLis) {
				childFactor.setObjective(value);
				childFactorLis.add(childFactor);
				if (null != childFactor.getChildFactorList() && childFactor.getChildFactorList().size() > 0) {
					setSubChildFactorProperty(childFactorLis, value, childFactor);
				}
			}
		}
	}

	/**
	 * @Description: 初始化
	 * @author guoyr 
	 */
	private void init(){
		
		this.tenderProject = projectAction.getTenderProject();
		this.menu = projectAction.getMenu();
		
		factors_grid.setItemHeight(Constants.ROW_HIGHT);
		factors_grid.setLinesVisible(false);
//		factors_grid.setAutoHeight(true);
		gridColumn.setShowTreeTogglePic(true);
//		gridColumn.setWordWrap(true);
		gridColumn.setTreeTogglePic(ImageResolver.RESOLVER_INSTANCE.loadImage(IconsActivator.PLUGIN_ID,"icons/tree_expend.png"), ImageResolver.RESOLVER_INSTANCE.loadImage(IconsActivator.PLUGIN_ID,"icons/tree_close.png"));
		gridColumn.setTree(true);
		menuOpenImage = imageResolver.loadImage(IconsActivator.PLUGIN_ID,"icons/tree_parent.png");
		menuImage = imageResolver.loadImage(IconsActivator.PLUGIN_ID,"icons/tree_item.png");
		initInputListener();
		
		evalBidTypeCombo.add(EvaluationFactorEnum.getEvalBidTypeCN(EvaluationFactorEnum.EVAL_BID_DETAIL), 0);
		evalBidTypeCombo.setData("0", EvaluationFactorEnum.EVAL_BID_DETAIL);
		evalBidTypeCombo.add(EvaluationFactorEnum.getEvalBidTypeCN(EvaluationFactorEnum.EVAL_BID_BUS), 1);
		evalBidTypeCombo.setData("1", EvaluationFactorEnum.EVAL_BID_BUS);
		evalBidTypeCombo.add(EvaluationFactorEnum.getEvalBidTypeCN(EvaluationFactorEnum.EVAL_BID_TEC), 2);
		evalBidTypeCombo.setData("2", EvaluationFactorEnum.EVAL_BID_TEC);
//		evalBidTypeCombo.add(EvaluationFactorEnum.getEvalBidTypeCN(EvaluationFactorEnum.EVAL_BID_CREDIT), 3);
//		evalBidTypeCombo.setData("3", EvaluationFactorEnum.EVAL_BID_CREDIT);
		isInEvaluationProgress = tenderProjectService.isInProgress(tenderProject.getId(), EvaluationStepEnum.DETAIL);
		if(isInEvaluationProgress){
			evalBidTypeCombo.setEnabled(false);
		}
		
		 menuFunctionUtils.initFunctionAuthority(menu);
		
		if (TenderProjectEnum.REMOTE.equals(tenderProject.getRemoteStatus())) {
//			WidgetUtils.setControlVisible(add_btn, false);
//			WidgetUtils.setControlVisible(add_sub_btn, false);
//			WidgetUtils.setControlVisible(del_btn, false);
		}else if (TenderProjectEnum.LOCAL.equals(tenderProject.getRemoteStatus())) {
			WidgetUtils.setControlVisible(add_btn, true);
			WidgetUtils.setControlVisible(add_sub_btn, true);
			WidgetUtils.setControlVisible(del_btn, true);
		}
		
		if(groupCombo.getVisible()){
			listEvaluationGroup();
		}
		
		refreshFactorTree(null);
		// 添选中事件和右键菜单事件
		factors_grid.addListener(SWT.Selection, new GridSelectionListener(){
			@Override
			public void leftMouseButtonDown(GridItem item) {
				selectionFactorAction();
			}
			@Override
			public void rightMouseButtonDown(GridItem item) {
				mouseRightClickAction();
			}
		});
		// 客观范围打分
		objectiveItemGrid.setAutoWidth(true);
		objectiveItemGrid.setAutoHeight(true);
		objectiveItemGrid.setItemHeight(20);
		objectiveItemGrid.addListener(SWT.Selection, new GridSelectionListener(){
			@Override
			public void leftMouseButtonDown(GridItem item) {
				getSelectObjectiveItem();
			}
			@Override
			public void rightMouseButtonDown(GridItem item) {
			}
		});
		
	}
	
	/**
	 * @Description: 初始化输入框的输入监听
	 * @author guoyr 
	 */
	private void initInputListener(){
		// 权重检查
		FloatInputListener factorWeightListener = new FloatInputListener();
		factorWeightListener.addMinCheck(0, Messages.Tips_pleaseEnterValidWeight);
		factorWeightListener.addMaxCheck(100, Messages.Tips_pleaseEnterValidWeight);
		weight_text.addKeyListener(factorWeightListener);
		
		// 最小分值检查
		factorPositiveMinScoreListener = new FloatInputListener();
		factorPositiveMinScoreListener.addMinCheck(0, Messages.Tips_pleaseEnterValidRange);
		factorPositiveMinScoreListener.addMaxCheck(max_score_text, Messages.Tips_pleaseEnterValidRange);
		
		// 最大值检查
		factorPositiveMaxScoreListener = new FloatInputListener();
		factorPositiveMaxScoreListener.addMinCheck(min_score_text, Messages.Tips_pleaseEnterValidRange);
		factorPositiveMaxScoreListener.addMaxCheck(factor_score_text, Messages.Tips_pleaseEnterValidRange);
		
		// 最小分值检查
		factorNegativeMinScoreListener = new FloatInputListener();
		factorNegativeMinScoreListener.addMinCheck(factor_score_text, Messages.Tips_pleaseEnterValidRange);
		factorNegativeMinScoreListener.addMaxCheck(max_score_text, Messages.Tips_pleaseEnterValidRange);
		
		// 最大值检查
		factorNegativeMaxScoreListener = new FloatInputListener();
		factorNegativeMaxScoreListener.addMinCheck(min_score_text, Messages.Tips_pleaseEnterValidRange);
		factorNegativeMaxScoreListener.addMaxCheck(0, Messages.Tips_pleaseEnterValidRange);
	}
	
	/**
	 * @Description: 刷新指标树
	 * @author guoyr
	 * @param factorId 
	 */
	public void refreshFactorTree(final String factorId){
//		hasCreditFactor = false;
		factors_grid.removeAll();
		new SyncThread(new SyncThread.Runnable() {
			public void run(){
				final List<EvaluationFactor> evaluationFactorList = evaluationFactorService.getAllEvaluationFactorList(tenderProject.getId(), EvaluationFactorEnum.AUDIT_SCORE);
				final List<EvaluationFactor> creditFactorList = evaluationFactorService.getAllEvaluationFactorList(tenderProject.getId(), EvaluationFactorEnum.AUDIT_CREDIT);
				Display.getDefault().syncExec(new Runnable(){
					public void run(){
						if(factors_grid.isDisposed())return;
						if(null != evaluationFactorList && evaluationFactorList.size() > 0){
							for(EvaluationFactor factor : evaluationFactorList){
								if(null != factor.getFactorType() && factor.getFactorType().equals(EvaluationFactorEnum.FACTOR_FLOAT)){
									continue;
								}
								GridItem gridItem = new GridItem(factors_grid, SWT.NONE);
								initChildGridItem(factor, gridItem, factorId);
							}
						}
						if(null != creditFactorList && creditFactorList.size() > 0){
							for(EvaluationFactor factor : creditFactorList){
								if(null != factor.getFactorType() && factor.getFactorType().equals(EvaluationFactorEnum.FACTOR_FLOAT)){
									continue;
								}
								GridItem gridItem = new GridItem(factors_grid, SWT.NONE);
								initChildGridItem(factor, gridItem, factorId);
							}
						}
						if(factors_grid.getSelectionCount() == 0){
							if(factors_grid.getItemCount() > 0){
								factors_grid.select(0);
							}
						}
						selectionFactorAction();
						Rectangle rectangle = factors_grid.getBounds();
						factors_grid.scroll(0, 0, 0, 0, rectangle.width, rectangle.height, true);
						factors_grid.getVerticalBar().setSelection(factors_grid.getSelectionIndex() -5);
						factors_grid.getVerticalBar().notifyListeners(SWT.Selection, null);
					}
				});
			}
		}).start();
	}
	
	/**
	 * @Description: 递归实现指标树
	 * @author guoyr
	 * @param factor
	 * @param parentGridItem
	 * @param selectId 
	 */
	private void initChildGridItem(EvaluationFactor factor, GridItem parentGridItem, String selectId){
		if(null != factor.getChildFactorList() && factor.getChildFactorList().size() > 0){
			for(EvaluationFactor childFactor : factor.getChildFactorList()){
				if(null != childFactor.getFactorType() && childFactor.getFactorType().equals(EvaluationFactorEnum.FACTOR_FLOAT)){
					continue;
				}
				GridItem gridItem = new GridItem(parentGridItem, SWT.NONE);
				initChildGridItem(childFactor, gridItem, selectId);
			}
			parentGridItem.setExpanded(true);
			parentGridItem.setImage(0, menuOpenImage);
		}else {
			parentGridItem.setImage(0, menuImage);
		}
//		if(null != factor.getEvalBidType() && factor.getEvalBidType().equals(EvaluationFactorEnum.EVAL_BID_CREDIT)){
//			hasCreditFactor = true;
//		}
		setFactorItemContent(factor, parentGridItem, false);
		parentGridItem.setToolTipText(0, parentGridItem.getText());
		if(null != selectId && selectId.equals(factor.getId())){
			factors_grid.setSelection(new GridItem[]{parentGridItem});
		}
	}
	
	/**
	 * @Description: 选中指标事件
	 * @author guoyr
	 */
	private void selectionFactorAction(){
		EvaluationFactor factor = getSelectionEvaluationFactor();
		add_btn.setEnabled(true);
		add_sub_btn.setEnabled(false);
		kgxParentComposite.setEnabled(true);
		scoreTypeDirectBtn.setEnabled(true);
		scoreTypeObjectiveBtn.setEnabled(true);
		scoreTypeAIBtn.setEnabled(true);
		((GridData)scoreTypeLabel.getLayoutData()).exclude = false;
		scoreTypeLabel.setVisible(true);
		((GridData)compositeContentComposite.getLayoutData()).exclude = false;
		compositeContentComposite.setVisible(true);
		if(null != factor){
			add_sub_btn.setEnabled(true);
			if(null != factor.getAuditType() && factor.getAuditType().equals(EvaluationFactorEnum.AUDIT_CREDIT)){
				add_sub_btn.setEnabled(false);
			}
			del_btn.setEnabled(true);
			// 如果选中的是父级
			if(factors_grid.getSelectionCount() > 0 && factors_grid.getSelection()[0].getItemCount() > 0){
				del_btn.setEnabled(false);
//				kgxParentComposite.setEnabled(false);
				((GridData)compositeContentComposite.getLayoutData()).exclude = true;
				compositeContentComposite.setVisible(false);
				((GridData)scoreTypeLabel.getLayoutData()).exclude = true;
				scoreTypeLabel.setVisible(false);
				
				showGroupVisible(false);
			}else {
				showGroupVisible(MenuFunction.SHOW.equals(menuFunctionUtils.getAuthority("EVAL_GROUP")));
			}
			// 如果是基础指标或浮动指标，则不可以添加下级指标
			if(null != factor.getContainId() && !"".equals(factor.getContainId())){
				add_sub_btn.setEnabled(false);
			}
		}else {
			del_btn.setEnabled(false);
		}
		
		// 如果当前指标是通过性指标
		if(null != factor && null != factor.getScoreType() && factor.getScoreType().equals(EvaluationFactorEnum.SCORE_TYPE_PASS)){
			showScoreProperty(false);
			boolean parentFactorIsScorePass = false;
			GridItem parentItem = factors_grid.getSelection()[0].getParentItem();
			if(null != parentItem){
				EvaluationFactor parentFactor = (EvaluationFactor)parentItem.getData("evaluationFactor");
				if(null != parentFactor.getScoreType() && parentFactor.getScoreType().equals(EvaluationFactorEnum.SCORE_TYPE_PASS)){
					parentFactorIsScorePass = true;
				}else {
					showScoreProperty(true);
				}
			}else {
				showScoreProperty(true);
			}
			if(parentFactorIsScorePass || factors_grid.getSelection()[0].getItemCount() > 0){
				scoreTypeDirectBtn.setEnabled(false);
				scoreTypeObjectiveBtn.setEnabled(false);
				scoreTypeAIBtn.setEnabled(false);
			}
		}else {
			showScoreProperty(true);
//			scoreTypePassBtn.setEnabled(false);
		}
		
		refreshFactorTreeBtn();
		refreshFactorInfo(factor);
		
	}

	/**
	 * @Description: 刷新指标表单
	 * @author guoyr
	 * @param factor
	 */
	@SuppressWarnings("unchecked")
	private void refreshFactorInfo(EvaluationFactor factor){
		if(null == factor){
			factor = new EvaluationFactor();
			factor.setObjective(EvaluationFactorEnum.OBJECTIVE_NO);
		}
		curFactor = factor;
		factor_name_text.forceFocus();
		// 设置当前指标的信息
		factor_name_text.setText(ObjectUtils.getObjString(factor.getFactorName()));
		factor_score_text.setText(null != factor.getFactorScore()?NumberFormat.getInstance().format(factor.getFactorScore()):"");
		min_score_text.setText(null != factor.getFactorMinScore()?NumberFormat.getInstance().format(factor.getFactorMinScore()):"");
		max_score_text.setText(null != factor.getFactorMaxScore() ? NumberFormat.getInstance().format(factor.getFactorMaxScore()):"");
		factor_memo_text.setText(ObjectUtils.getObjString(factor.getMemo()));
		if(null == factor.getScoreType()){
			factor.setScoreType(EvaluationFactorEnum.SCORE_TYPE_DIRECT);
		}
		if(null != factor.getScoreType() && factor.getScoreType().equals(EvaluationFactorEnum.SCORE_TYPE_PASS_PARENT)){
			scoreTypeDirectBtn.setData(EvaluationFactorEnum.SCORE_TYPE_PASS_PARENT);
		}else {
			scoreTypeDirectBtn.setData(EvaluationFactorEnum.SCORE_TYPE_DIRECT);
		}
		WidgetUtils.setRadioChecked(scoreTypeComposite, factor.getScoreType());
		// 默认为非客观项
		if(null == factor.getObjective() || !factor.getObjective().equals(EvaluationFactorEnum.OBJECTIVE_YES)){
			curFactor.setObjective(EvaluationFactorEnum.OBJECTIVE_NO);
		}
		
		WidgetUtils.setRadioChecked(kgxParentComposite, factor.getObjective());
		
		// 设置有效的权重权重
		if(null != factor.getFactorWeight() && factor.getFactorWeight().doubleValue() > 0 && factor.getFactorWeight().doubleValue() < 100){
			// 如果有权重，则不隐藏
			weight_text.setText(NumberFormat.getInstance().format(factor.getFactorWeight()));
		}else {
			weight_text.setText("");
			curFactor.setFactorWeight(null);
		}
		
		if(MenuFunction.HIDE.equals(menuFunctionUtils.getAuthority("SUB_WEIGHT"))){
			WidgetUtils.setControlVisible(weightTitle, null == curFactor.getParentId());
			WidgetUtils.setControlVisible(weightComposite, null == curFactor.getParentId());
			if(null != curFactor.getParentId() && weight_text.getText().length() > 0){
				WidgetUtils.setControlVisible(weightTitle, true);
				WidgetUtils.setControlVisible(weightComposite, true);
			}
		}else if(null != curFactor.getParentId()){
			// 如果没有设置下级权重，则不显示下级权重
			if(weight_text.getText().length() == 0){
				WidgetUtils.setControlVisible(weightTitle, false);
				WidgetUtils.setControlVisible(weightComposite, false);
			}
		}

		if(null != curFactor.getParentId() && weight_text.getText().length() > 0){
			WidgetUtils.setControlVisible(weightTitle, true);
			WidgetUtils.setControlVisible(weightComposite, true);
		}
		
		WidgetUtils.setControlVisible(evalBidTypeTitle, null == curFactor.getParentId());
		WidgetUtils.setControlVisible(evalBidTypeCombo, null == curFactor.getParentId());
		evalBidTypeCombo.select(0);
		if(evalBidTypeCombo.getVisible()){
			WidgetUtils.setComboSelection(evalBidTypeCombo, curFactor.getEvalBidType());
		}
		
		if(groupCombo.getVisible()){
			groupCombo.removeAll();
			groupCombo.setData("0", null);
			groupCombo.add("", 0);
			int index = 1;
			for(String[] group : groupList){
				if(null != factor.getEvalBidType() && factor.getEvalBidType().length() > 0){
					if(!group[0].startsWith(factor.getEvalBidType())){
						continue;
					}
				}
				groupCombo.setData(index + "", group[0]);
				groupCombo.add(group[1], index);
				index ++;
			}
			WidgetUtils.setComboSelection(groupCombo, factor.getEvalGroup());
		}
		
		scoreTypeChangeAction(StringUtils.isEmpty(factor.getId()));
		// 刷新客观范围打分数据
		objectiveItemGrid.removeAll();
		objectiveItemList = (List<ScoreItem>) SerializeUtils.deserializeObject(factor.getObjectiveItem());
		if(null != objectiveItemList && objectiveItemList.size() > 0){
			int index = 1;
			for(ScoreItem item : objectiveItemList){
				GridItem gridItem = new GridItem(objectiveItemGrid, SWT.NONE);
				gridItem.setText(0, ObjectUtils.getObjString(index++));
				initObjectiveItemGridItem(gridItem, item);
			}
		}
		getSelectObjectiveItem();
		scroContentComposite.layout();
		scrolledComposite.setMinSize(scroContentComposite.computeSize(SWT.DEFAULT, SWT.DEFAULT));
	}
	
	private void showScoreProperty(boolean isVisible){
		((GridData)scoreTitle.getLayoutData()).exclude = !isVisible;
		scoreTitle.setVisible(isVisible);
		((GridData)score_composite.getLayoutData()).exclude = !isVisible;
		score_composite.setVisible(isVisible);
		((GridData)weightTitle.getLayoutData()).exclude = !isVisible;
		weightTitle.setVisible(isVisible);
		((GridData)weightComposite.getLayoutData()).exclude = !isVisible;
		weightComposite.setVisible(isVisible);
	}
	
	/**
	 * @Description: 列出评审要素分组
	 * @author guoyr 
	 */
	private void listEvaluationGroup(){
	 	List<Expert> expertList = expertService.findEvalBidTypeByTenderIdList(tenderProject.getId());
		groupList.clear();
		if(null != expertList && expertList.size() > 0){
			int defaultCount = 0;
			int busCount = 0;
			int tecCount = 0;
			for(Expert expert : expertList){
				int evalBidType = expert.getEvalBidType();
				boolean isDefault = true;
				if ((evalBidType & ExpertEnum.EXPERT_BUS) != 0) {
					busCount ++;
					groupList.add(EvaluationFactorEnum.getEvalGroup(EvaluationFactorEnum.EVAL_BID_BUS, busCount));
					isDefault = false;
				}
				if ((evalBidType & ExpertEnum.EXPERT_TEC) != 0) {
					tecCount ++;
					groupList.add(EvaluationFactorEnum.getEvalGroup(EvaluationFactorEnum.EVAL_BID_TEC, tecCount));
					isDefault = false;
				}
				if(isDefault){
					defaultCount ++;
					groupList.add(EvaluationFactorEnum.getEvalGroup(EvaluationFactorEnum.EVAL_BID_DETAIL, defaultCount));
				}
			}
		}else {
			for(int defaultCount = 1; defaultCount < 10; defaultCount ++){
				groupList.add(EvaluationFactorEnum.getEvalGroup(EvaluationFactorEnum.EVAL_BID_BUS, defaultCount));
				groupList.add(EvaluationFactorEnum.getEvalGroup(EvaluationFactorEnum.EVAL_BID_TEC, defaultCount));
			}
		}
	}
	
	/**
	 * @Description: 设置评审要素分组显示
	 * @author guoyr
	 * @param isVisible 
	 */
	private void showGroupVisible(boolean isVisible){
		((GridData)label_groupType.getLayoutData()).exclude = !isVisible;
		label_groupType.setVisible(isVisible);
		((GridData)groupCombo.getLayoutData()).exclude = !isVisible;
		groupCombo.setVisible(isVisible);
	}
	
	/**
	 * @Description: 选中下拉列表，并切换容器
	 * @author guoyr
	 */
	private void scoreTypeChangeAction(boolean layout){
		String scoreType = ObjectUtils.getObjString(WidgetUtils.getRadioVal(scoreTypeComposite));
		if(StringUtils.equals(EvaluationFactorEnum.SCORE_TYPE_OBJECTIVE, scoreType)){
			((GridData)objectiveScoreComposite.getLayoutData()).heightHint = 180;
			((GridData)objectiveScoreComposite.getLayoutData()).exclude = false;
			objectiveScoreComposite.setVisible(true);
		}else {
			((GridData)objectiveScoreComposite.getLayoutData()).heightHint = 0;
			((GridData)objectiveScoreComposite.getLayoutData()).exclude = true;
			objectiveScoreComposite.setVisible(false);
		}
		compositeContentComposite.layout();
		if(layout){
			scroContentComposite.layout();
			scrolledComposite.setMinHeight(scroContentComposite.computeSize(SWT.DEFAULT, SWT.DEFAULT).y+10);
		}
	}
	
	/**
	 * @Description: 初始化客观范围打分GridItem
	 * @author guoyr
	 * @param gridItem
	 * @param item
	 */
	private void initObjectiveItemGridItem(GridItem gridItem, ScoreItem item){
		gridItem.setText(1, ObjectUtils.getObjString(item.getName()));
		gridItem.setText(2, ObjectUtils.getObjString(item.getScore()));
		gridItem.setData("scoreItem", item);
	}
	
	/**
	 * @Description: 获得选中客观范围打分
	 * @author guoyr
	 */
	private void getSelectObjectiveItem(){
		if(null != objectiveItemGrid && !objectiveItemGrid.isDisposed() && objectiveItemGrid.getSelectionCount() > 0){
			selectObjectItem = (ScoreItem)objectiveItemGrid.getSelection()[0].getData("scoreItem");
		}else {
			selectObjectItem = null;
		}
		
		updateObjectiveScoreBtn.setEnabled(false);
		delObjectiveScoreBtn.setEnabled(false);
		if(null != selectObjectItem){
			updateObjectiveScoreBtn.setEnabled(true);
			delObjectiveScoreBtn.setEnabled(true);
		}
		objectiveItemGrid.resizeColumnWidth();
	}
	
	/**
	 * @Description: 添加新指标
	 * @author guoyr
	 */
	private void addFctorAction(){
		if(isInEvaluationProgress()){return;}
		if(add_btn.getEnabled()){
			scoreTypeDirectBtn.setEnabled(true);
			scoreTypeObjectiveBtn.setEnabled(true);
			EvaluationFactor newFactor = new EvaluationFactor();
			newFactor.setObjective(EvaluationFactorEnum.OBJECTIVE_NO);
			EvaluationFactor selectFactor = getSelectionEvaluationFactor();
			if(null != selectFactor){
				newFactor.setParentId(selectFactor.getParentId());
				newFactor.setScoreType(selectFactor.getScoreType());
//				if(null != selectFactor.getScoreType() && selectFactor.getScoreType().equals(EvaluationFactorEnum.SCORE_TYPE_PASS)){
//					scoreTypePassBtn.setEnabled(false);
//				}
				GridItem parentItem = factors_grid.getSelection()[0].getParentItem();
				if(null != parentItem){
					EvaluationFactor parentFactor = (EvaluationFactor)parentItem.getData("evaluationFactor");
					newFactor.setEvalBidType(parentFactor.getEvalBidType());
					if(null != parentFactor.getScoreType() && parentFactor.getScoreType().equals(EvaluationFactorEnum.SCORE_TYPE_PASS)){
//						showScoreProperty(false);
						scoreTypeDirectBtn.setEnabled(false);
						scoreTypeObjectiveBtn.setEnabled(false);
					}else {
						if(null != selectFactor.getScoreType() && selectFactor.getScoreType().equals(EvaluationFactorEnum.SCORE_TYPE_PASS)){
							newFactor.setScoreType(parentFactor.getScoreType());
							showScoreProperty(true);
							scoreTypeDirectBtn.setEnabled(true);
							scoreTypeObjectiveBtn.setEnabled(true);
						}
					}
				}
			}
			
			refreshFactorInfo(newFactor);
		}
	}
	
	/**
	 * @Description: 新增下级指标
	 * @author guoyr 
	 */
	private void addSubFctorAction(){
		if(isInEvaluationProgress()){return;}
		EvaluationFactor selectFactor = getSelectionEvaluationFactor();
		if(add_sub_btn.getEnabled() && null != selectFactor){
			EvaluationFactor newFactor = new EvaluationFactor();
			newFactor.setObjective(EvaluationFactorEnum.OBJECTIVE_NO);
			newFactor.setScoreType(selectFactor.getScoreType());
			newFactor.setEvalBidType(selectFactor.getEvalBidType());
			newFactor.setParentId(selectFactor.getId());
			if(null != selectFactor.getScoreType() && selectFactor.getScoreType().equals(EvaluationFactorEnum.SCORE_TYPE_PASS)){
				showScoreProperty(false);
				scoreTypeDirectBtn.setEnabled(false);
				scoreTypeObjectiveBtn.setEnabled(false);
			}else {
				scoreTypeDirectBtn.setEnabled(true);
				scoreTypeObjectiveBtn.setEnabled(true);
			}
			refreshFactorInfo(newFactor);
		}
	}
	
	/**
	 * @Description: 右键菜单
	 * @author guoyr
	 */
	private void mouseRightClickAction(){

		final EMenu gmenu = new EMenu(getShell());
		gmenu.addMenuItem(upFactorBtn);
		gmenu.addMenuItem(downFactorBtn);
		gmenu.addSeparator();
		gmenu.addMenuItem(add_btn);
		gmenu.addMenuItem(add_sub_btn);
		gmenu.addMenuItem(del_btn);
		if(MenuFunction.SHOW.equals(menuFunctionUtils.getAuthority("DELETE_ALL"))) {
			gmenu.addMenuItem(Messages.Button_deleteAll, del_btn.getImage(), factors_grid.getItemCount() > 1,  new EMenu.ClickListener() {
				public void click() { 
					delAllEvaluationFactorAction();
				}
			});
		}
		gmenu.open();
	
	}
	
	/**
	 * @Description: 获得选中的指标
	 * @author guoyr
	 * @return
	 */
	private EvaluationFactor getSelectionEvaluationFactor(){
		EvaluationFactor factor = null;
		if(factors_grid.getSelectionCount() > 0){
			factor = (EvaluationFactor)factors_grid.getSelection()[0].getData("evaluationFactor");
		}
		return factor;
	}
		
	/**
	 * @Description: 保存或修改指标
	 * @author guoyr
	 */
	private void saveEvaluationFactorAction(){
		if(!save_btn.getEnabled())return;
		if(factor_name_text.getText().trim().length() == 0){
			MessageDialog.openInformation(getShell(), Messages.Dialog_warmTips, MessageFormat.format(Messages.Tips_required, Messages.FactorName));
			return;
		}
		String scoreType = ObjectUtils.getObjString(WidgetUtils.getRadioVal(scoreTypeComposite));
		boolean valTotalScore = true;
		if(valTotalScore) {
			if(factor_score_text.getText().trim().length() == 0){
				MessageDialog.openInformation(getShell(), Messages.Dialog_warmTips, MessageFormat.format(Messages.Tips_required, Messages.FactorScore));
				return;
			}
		}
		if(evalBidTypeCombo.getVisible()){
			curFactor.setEvalBidType((String)WidgetUtils.getComboSelection(evalBidTypeCombo));
		}
		curFactor.setEvalGroup(null);
		if(groupCombo.getVisible()){
			curFactor.setEvalGroup((String)WidgetUtils.getComboSelection(groupCombo));
		}
		boolean isNew = StringUtils.isEmpty(curFactor.getId());
		if(isNew){
			if(isInEvaluationProgress()){return;}
		}else {
			if(isInEvaluationStepProgress(curFactor.getId())){return;}
		}
		
		boolean changeWeight = false;
		BigDecimal factorWeight = ObjectUtils.getObjBigDecimal(weight_text.getText().trim(), Constants.SCORE_SCALE);
		if(null != factorWeight){
			changeWeight = true;
			if(factorWeight.compareTo(BigDecimal.ZERO) == 0){
				factorWeight = null;
			}
			curFactor.setFactorWeight(factorWeight);
		}else {
			if(null != curFactor.getFactorWeight()){
				changeWeight = true;
			}
			curFactor.setFactorWeight(null);
		}
		curFactor.setContainFactor(null);
		
		curFactor.setTenderId(tenderProject.getId());
		curFactor.setFactorName(factor_name_text.getText().trim());
		curFactor.setMemo(factor_memo_text.getText().trim());
		curFactor.setAuditType(EvaluationFactorEnum.AUDIT_SCORE);
		if(null != curFactor.getEvalBidType() && curFactor.getEvalBidType().equals(EvaluationFactorEnum.EVAL_BID_CREDIT)){
			curFactor.setAuditType(EvaluationFactorEnum.AUDIT_CREDIT);
		}
		curFactor.setObjective(ObjectUtils.getObjString(WidgetUtils.getRadioVal(kgxParentComposite)));
		curFactor.setFactorScore(ObjectUtils.getObjBigDecimal(factor_score_text.getText().trim(), Constants.SCORE_SCALE));
		curFactor.setFactorMinScore(ObjectUtils.getObjBigDecimal(min_score_text.getText().trim(), Constants.SCORE_SCALE));
		if(null == curFactor.getFactorMinScore()){
			curFactor.setFactorMinScore(BigDecimal.ZERO);
		}
		curFactor.setFactorMaxScore(ObjectUtils.getObjBigDecimal(max_score_text.getText().trim(), Constants.SCORE_SCALE));
		if(null == curFactor.getFactorMaxScore()){
			curFactor.setFactorMaxScore(curFactor.getFactorScore());
		}
		
		if(null != curFactor.getId() && null != scoreType && scoreType.length() > 0){
			curFactor.setScoreType(scoreType);
		}
		if(ObjectUtils.equals(EvaluationFactorEnum.SCORE_TYPE_OBJECTIVE, scoreType)){
			if(null != objectiveItemList && objectiveItemList.size() > 0){
				// 序列化之前对集合排序操作
				Collections.sort(objectiveItemList, new SortComparator(false));
				curFactor.setObjectiveItem(SerializeUtils.serializeObject(objectiveItemList));
			}else {
				MessageDialog.openInformation(getShell(), Messages.Dialog_warmTips, Messages.Tips_noObjectiveScoreSelection);
				return ;
			}
		}
		
		if(valTotalScore && !valuateScore(curFactor)){
			return;
		}
		
		// 没有点击新增，直接在顶级节点下进行保存时
		if(curFactor.getSortNo() == 0){
 			int sortNo = evaluationFactorService.getMaxEvaluationFactorSortNo(tenderProject.getId(), EvaluationFactorEnum.AUDIT_SCORE, curFactor.getParentId());
			curFactor.setSortNo(sortNo + 1);
		}
 		curFactor = evaluationFactorService.saveEvaluationFactor(curFactor, changeWeight);
		
		if(null != curFactor){
			MessageDialog.openShortMessage(Messages.Tips_saveSuccess);
			refreshFactorTree(curFactor.getId());
			if(isNew){
				checkEvaluationStepAfterChangeFactor();
			}
		}else{
			MessageDialog.openError(getShell(), Messages.Dialog_warmTips, Messages.Tips_saveFail);
		}
	}
	
	/**
	 * @Description: 设置指标树的内容
	 * @author guoyr
	 * @param factor
	 * @param gridItem
	 * @param refresh
	 */
	private void setFactorItemContent(EvaluationFactor factor, GridItem gridItem, boolean refresh){
		gridItem.setData("evaluationFactor", factor);
		if(null != factor.getEvalGroup()){
			gridItem.setText(0, "["+EvaluationFactorEnum.getEvalGroupName(factor.getEvalGroup())+"]"+factor.getFactorName());
		}else {
			
			gridItem.setText(0, factor.getFactorName());
		}
		// 如果是客观项指标
		if(ObjectUtils.equals(factor.getObjective(), EvaluationFactorEnum.OBJECTIVE_YES)){
			// 如果即是客观项指标，又是浮动指标
			if(null != factor.getContainFactor()){
				gridItem.setForeground(0,SWTResourceManager.getColor(SWT.COLOR_DARK_RED));
			}else {
				gridItem.setForeground(0,SWTResourceManager.getColor(SWT.COLOR_DARK_GREEN));
			}
		}else if(null != factor.getContainFactor()){
			// 如果是浮动指标
			gridItem.setForeground(0,SWTResourceManager.getColor(SWT.COLOR_DARK_MAGENTA));
		}else if(refresh){
			gridItem.setForeground(0,null);
		}
		// 如果顶级指标是通过性指标
		if(null != factor.getScoreType() && factor.getScoreType().equals(EvaluationFactorEnum.SCORE_TYPE_PASS)){
			gridItem.setForeground(0,SWTResourceManager.getColor(SWT.COLOR_BLUE));
//			if(gridItem.getItemCount() > 0){
//				gridItem.setImage(ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, "icons/btn/blue/ok.png"));
//			}
		}
	}

	/**
	 * @Description: 删除指标
	 * @author guoyr
	 */
	private void delEvaluationFactorAction(){
		if(!del_btn.getEnabled())return;
		if(isInEvaluationProgress()){return;}
		EvaluationFactor factor = getSelectionEvaluationFactor();
		if(null != factor){
			if(MessageDialog.openConfirm(getShell(), Messages.Dialog_warmTips, MessageFormat.format(Messages.Tips_deleteObj, ObjectUtils.getObjString(factor.getFactorName())))){
				int selectIndex = factors_grid.getSelectionIndex();
				String preFactorId = null;
				if(selectIndex > 1){
					EvaluationFactor preFactor = (EvaluationFactor)factors_grid.getItem(selectIndex-1).getData("evaluationFactor");
					if(null != preFactor){
						preFactorId = preFactor.getId();
					}
				}
				int delCount = evaluationFactorService.deleteEvaluationFactor(factor);
				if(delCount > 0){
					refreshFactorTree(preFactorId);
					checkEvaluationStepAfterChangeFactor();
				}
			}
		}
	}
	
	/**
	 * @Description: 全部删除
	 * @author guoyr
	 */
	private void delAllEvaluationFactorAction(){
		if(isInEvaluationProgress()){return;}
		if(MessageDialog.openConfirm(getShell(), Messages.Dialog_warmTips, MessageFormat.format(Messages.Tips_deleteAllObject, Messages.Factor_score))){
			new SyncThread(new SyncThread.Runnable() {
				public void run() {
					evaluationFactorService.delEvaluationFactorByType(tenderProject.getId(), EvaluationFactorEnum.AUDIT_SCORE);
					Display.getDefault().syncExec(new Runnable(){
						public void run(){
							if (!factors_grid.isDisposed()) {
								refreshFactorTree(null);
								checkEvaluationStepAfterChangeFactor();
							} 
						} 
					});
				}
			}).start();;
		}
	}
	
	/**
	 * @Description: 提示如果已经设置评标步骤，且又新增或删除过指标
	 * @author guoyr
	 * @param opName
	 */
	private void checkEvaluationStepAfterChangeFactor(){
		if(null == evaluationStepService){
			evaluationStepService = new EvaluationStepService();
			List<String>nodeList =evaluationStepService.getIdByTenderIdCount(tenderProject.getId());
			if(null != nodeList && nodeList.size() > 0){
				isExistEvaluationStep = true;
			}
		}
		if(isExistEvaluationStep){
			MessageDialog.openInformation(getShell(), Messages.Dialog_warmTips, Messages.Tips_pleaseResetEvalStep);
		}
	}
	
	/**
	 * @Description: 修改分值
	 * @author guoyr
	 */
	private void modifyFactorScoreAction(ModifyListener listener){
		String factor_score = factor_score_text.getText().trim();
		if ("-".equals(factor_score) || "0".equals(factor_score)) {
			return;
		}
		BigDecimal factorScore = ObjectUtils.getObjBigDecimal(factor_score);
		boolean valFlag = true;
		if(null != factorScore){
			if (factorScore.doubleValue() < -100 || factorScore.doubleValue() > 100 || factorScore.doubleValue() == 0) {
				MessageDialog.openInformation(getShell(), Messages.Dialog_warmTips, Messages.Tips_pleaseEnterValidScore);
				valFlag = false;
			}
		}else {
			valFlag = false;
		}
		if(valFlag){
			min_score_text.removeKeyListener(factorPositiveMinScoreListener);
			min_score_text.removeKeyListener(factorNegativeMinScoreListener);
			max_score_text.removeKeyListener(factorPositiveMaxScoreListener);
			max_score_text.removeKeyListener(factorNegativeMaxScoreListener);
			if (factorScore.compareTo(BigDecimal.ZERO) > 0) {
				min_score_text.setText("0");
				max_score_text.setText(factor_score_text.getText());
				factorPositiveMinScoreListener.addMinCheck(0-factorScore.doubleValue(), Messages.Tips_pleaseEnterValidRange);
				min_score_text.addKeyListener(factorPositiveMinScoreListener);
				max_score_text.addKeyListener(factorPositiveMaxScoreListener);
			} else {
				min_score_text.setText(factor_score_text.getText());
				max_score_text.setText("0");
				min_score_text.addKeyListener(factorNegativeMinScoreListener);
				max_score_text.addKeyListener(factorNegativeMaxScoreListener);
			}
			max_score_text.setEditable(true);
			min_score_text.setEditable(true);
		}else {
			factor_score_text.removeModifyListener(listener);
			factor_score_text.setText("");
			min_score_text.setText("");
			max_score_text.setText("");
			max_score_text.setEditable(false);
			min_score_text.setEditable(false);
			factor_score_text.addModifyListener(listener);
		}
	}
	
	/**
	 * @Description: 移动指标
	 * @author guoyr
	 * @param upOrDown upOrDown -1：上移；1：下移
	 */
	private void moveEvaluationFactorAction(int upOrDown){
		if(upOrDown == 1 && !downFactorBtn.getEnabled()){
			return;
		}else if(upOrDown == -1 && !upFactorBtn.getEnabled()){
			return;
		}
		if(factors_grid.getSelectionCount() > 0){
			GridItem selectGridItem = factors_grid.getSelection()[0];
			if(null != selectGridItem.getData("evaluationFactor")){
				EvaluationFactor selectFactor = (EvaluationFactor)selectGridItem.getData("evaluationFactor");
				GridItem selectParentGridItem = selectGridItem.getParentItem();
				GridItem targetItem =  null;
//				int index = selectParentGridItem.indexOf(selectGridItem);
				
				if(null == selectParentGridItem){
					int index = factors_grid.indexOf(selectGridItem);
					if(upOrDown == 1){
						targetItem = factors_grid.getItem(index + findNextItemIndex(selectGridItem));
					}else{
						targetItem = findRootItem(factors_grid.getItem(index + upOrDown));
					}
				}else{
					int index = selectParentGridItem.indexOf(selectGridItem);
					// 获取目标资源
					targetItem = selectParentGridItem.getItem(index + upOrDown);
				}
				
				
				
				// 将当前要进行上移的资源信息记录
				GridItem[] tempGridItems = selectGridItem.getItems();
				boolean tempExpanded = selectGridItem.isExpanded();
				// 获取目标资源
//				GridItem targetItem = selectParentGridItem.getItem(index + upOrDown);
				EvaluationFactor targetFactor = (EvaluationFactor)targetItem.getData("evaluationFactor");
				
				// 将目录资源换到当前资源
				selectGridItem.setText(targetItem.getText());
				selectGridItem.setData("evaluationFactor", targetItem.getData("evaluationFactor"));
				// 递归将目录的下级资源移动到当前资源
				moveSubResourceItem(targetItem.getItems(), targetItem.isExpanded(), selectGridItem);
				
				// 将当前资源换到目录资源
				targetItem.setText(selectFactor.getFactorName());
				targetItem.setData("evaluationFactor", selectFactor);
				// 递归将当前的下级资源移动目标资源
				moveSubResourceItem(tempGridItems, tempExpanded, targetItem);
				factors_grid.setSelection(new GridItem[]{targetItem});
				refreshFactorTreeBtn();
	 			evaluationFactorService.changeEvaluationFactorSortNo(selectFactor, targetFactor);
	 			int targetSortNO = targetFactor.getSortNo();
	 			targetFactor.setSortNo(selectFactor.getSortNo());
	 			selectFactor.setSortNo(targetSortNO);
	 			if(null != curFactor) {
		  	 		curFactor.setSortNo(selectFactor.getSortNo());
		  	 	}
			}
		
		}
	}
	
	/**
	 * @Description: 递归移动下级资源
	 * @author guoyr
	 * @param items
	 * @param parentItem
	 */
	private void moveSubResourceItem(GridItem[] items, boolean isExpanded, GridItem parentItem){
		if(items.length > 0){
			for(GridItem item : items){
				GridItem newItem = new GridItem(parentItem, SWT.NONE);
				newItem.setText(0, item.getText());
				newItem.setData("evaluationFactor", item.getData("evaluationFactor"));
				moveSubResourceItem(item.getItems(),item.isExpanded(), newItem);
				item.dispose();
			}
			parentItem.setImage(0, menuOpenImage);
			parentItem.setExpanded(isExpanded);
		}else {
			parentItem.setImage(0, menuImage);
		}
	}

	/**
	 * @Description: 刷新指标树的移动按钮
	 * @author guoyr
	 */
	private void refreshFactorTreeBtn(){
		
		if (factors_grid.getSelectionCount() > 0) {
			GridItem selectGridItem = factors_grid.getSelection()[0];
			GridItem selectParentGridItem = selectGridItem.getParentItem();
			if (null != selectParentGridItem) {
				if(selectParentGridItem.getItemCount() > 0 ){
					int index =selectParentGridItem.indexOf(selectGridItem);
					if(index > 0){
						upFactorBtn.setEnabled(true);
					}else {
						upFactorBtn.setEnabled(false);
					}
					if(index < selectParentGridItem.getItemCount()-1){
						downFactorBtn.setEnabled(true);
					}else {
						downFactorBtn.setEnabled(false);
					}
				}else {
					upFactorBtn.setEnabled(false);
					downFactorBtn.setEnabled(false);
				}
			}else{
				int index = factors_grid.indexOf(selectGridItem);
				if((index + findNextItemIndex(selectGridItem) >= factors_grid.getItemCount())){
					downFactorBtn.setEnabled(false);
				}else{
					downFactorBtn.setEnabled(true);
				}
				if(index -1 < 0){
					upFactorBtn.setEnabled(false);
				}else{
					upFactorBtn.setEnabled(true);
				}
			}
		}else {
			upFactorBtn.setEnabled(false);
			downFactorBtn.setEnabled(false);
		}
	}
	/**
	 * @Description: 获取目标节点
	 * @author guoyr
	 * @param item
	 * @return 
	 */
	private GridItem findRootItem(GridItem item){
		if(null != item.getParentItem()){
			return findRootItem(item.getParentItem());
		}else{
			return item;
		}
	}
	
	/**
	 * @Description: 下移：统计当前节点的子节点个数
	 * @author guoyr
	 * @param gridItem
	 * @param num
	 * @return 
	 */
	private int findNextItemIndex(GridItem gridItem){
		int num = 1;
		GridItem[] gridItems = gridItem.getItems();
		for(GridItem  item : gridItems){
			num += findNextItemIndex(item);
		}
		return num;
	}
	/**
	 * @Description: 验证分值
	 * @author guoyr
	 * @return 
	 */
	private boolean valuateScore(EvaluationFactor evaluationFactor){
		// 验证同级
//		QueryWrapper queryWrapper = new QueryWrapper(EvaluationFactor.class);
//		queryWrapper.setQueryParm("id");
//		queryWrapper.setQueryParm("factorName");
//		queryWrapper.setQueryParm("factorScore");
//		queryWrapper.setQueryParm("factorWeight");
//		queryWrapper.setWhereParam("tenderId", tenderProjectEntity.getId());
//		queryWrapper.setWhereParam("parentId", evaluationFactor.getParentId());
//		if(null == evaluationFactor.getParentId()){
//			queryWrapper.setWhereParam("auditType", new String[]{EvaluationFactorEnum.AUDIT_SCORE, EvaluationFactorEnum.AUDIT_PRICE}, WhereParam.OP_IN);
//		}
//		List<EvaluationFactor> evaluationFactorList = evaluationFactorService.queryList(queryWrapper, EvaluationFactor.class);
		String [] auditType = null;
		if(null == evaluationFactor.getParentId()){
			auditType = new String[]{EvaluationFactorEnum.AUDIT_SCORE, EvaluationFactorEnum.AUDIT_PRICE};
		}
		List<EvaluationFactor> evaluationFactorList = evaluationFactorService.findTenderAndParentId(tenderProject.getId(),evaluationFactor.getParentId(),auditType);
		BigDecimal totalScore = BigDecimal.ZERO;
		BigDecimal topFullScore = new BigDecimal(100);
		if(null != evaluationFactor.getParentId()){
			topFullScore = new BigDecimal(100);
//		EvaluationFactor parentEvaluationFactor = evaluationFactorService.getById(evaluationFactor.getParentId(), EvaluationFactor.class);
			EvaluationFactor parentEvaluationFactor = evaluationFactorService.findById(evaluationFactor.getParentId());
			
			topFullScore = parentEvaluationFactor.getFactorScore();
			if(null == topFullScore){
				topFullScore = BigDecimal.ZERO;
			}
			
		}
		CollectionUtils.filter(evaluationFactorList, new CollectionUtils.Filter<EvaluationFactor>() {
			public boolean select(EvaluationFactor t) {
				return null == evaluationFactor.getId() || !evaluationFactor.getId().equals(t.getId());
			}
		});
		if(CollectionUtils.isNotNull(evaluationFactorList)){
			for(EvaluationFactor queryEvaluationFactor : evaluationFactorList){
				if(null != queryEvaluationFactor.getFactorWeight()){
					totalScore = BigDecimalUtils.add(totalScore, BigDecimalUtils.div(BigDecimalUtils.mul(queryEvaluationFactor.getFactorScore(), queryEvaluationFactor.getFactorWeight()), new BigDecimal(100)));
				}else {
					totalScore = BigDecimalUtils.add(totalScore, queryEvaluationFactor.getFactorScore());
				}
			}
			// 如果不含该指标的分值时已经超过上级分值，则认为不能设置有效的分值
			if(totalScore.compareTo(topFullScore) > 0){
//			MessageDialog.openInformation(getShell(), "不能为["+curFactor.getFactorName()+"]设置有效的分值！");
				ConfirmDialog confirmDialog = new ConfirmDialog(getShell(), Messages.Dialog_warmTips, Messages.Tips_overParentLevalScore, null);
				confirmDialog.addButton(Messages.Button_reInput, "01", SWTResourceManager.getColor(77, 163, 19));
//				confirmDialog.addButton(Messages.Button_forceSave, "force", SWTResourceManager.getColor(255, 0, 0));
				confirmDialog.open();
//				if(!"force".equals(confirmDialog.getClickStatus())){
//				}
				factor_score_text.setText("");
				return false;
			}
		}
		if(null != evaluationFactor.getFactorWeight()){
			totalScore = totalScore.add(BigDecimalUtils.div(BigDecimalUtils.mul(evaluationFactor.getFactorScore(), evaluationFactor.getFactorWeight()), new BigDecimal(100)));
		}else {
			totalScore = totalScore.add(evaluationFactor.getFactorScore());
		}
		BigDecimal dValue = totalScore.subtract(topFullScore);
		if(dValue.compareTo(BigDecimal.ZERO) > 0){
			String score_value = "";
			if(null != evaluationFactor.getFactorWeight()){
//				dValue = dValue.multiply(new BigDecimal(100)).divide(evaluationFactor.getFactorWeight(), 2, RoundingMode.UP);
//				score_value = NumberFormat.getInstance().format(evaluationFactor.getFactorScore().subtract(dValue));
//				MessageDialog.openInformation(getShell(), "["+curFactor.getFactorName()+"]分值为"+score_value+"！");
				
//				MessageDialog.openInformation(getShell(), "请为["+curFactor.getFactorName()+"]设置有效的权重！");
				ConfirmDialog confirmDialog = new ConfirmDialog(getShell(), Messages.Dialog_warmTips, Messages.Tips_cantSetValidWeight, null);
				confirmDialog.addButton(Messages.Button_reInput, "01", SWTResourceManager.getColor(77, 163, 19));
//				confirmDialog.addButton(Messages.Button_forceSave, "force", SWTResourceManager.getColor(255, 0, 0));
				if(null == evaluationFactor.getParentId()) {
					confirmDialog.setTips("提示：评标总分超过100分！" );
					confirmDialog.setTipsColor(SWTResourceManager.getColor(255, 0, 0));
				}
				confirmDialog.open();
//				if(!"force".equals(confirmDialog.getClickStatus())){
					weight_text.setText("");
					weight_text.forceFocus();
					return false;
//				}
			}else {
				score_value = NumberFormat.getInstance().format(evaluationFactor.getFactorScore().subtract(dValue));
//				MessageDialog.openInformation(getShell(), "["+curFactor.getFactorName()+"]最大分值只能为"+score_value+"！");
				ConfirmDialog confirmDialog = new ConfirmDialog(getShell(), Messages.Dialog_warmTips, MessageFormat.format(Messages.Tips_canOnlySetScore, score_value), null);
				confirmDialog.addButton(Messages.Button_reInput, "01", SWTResourceManager.getColor(77, 163, 19));
//				confirmDialog.addButton(Messages.Button_forceSave, "force", SWTResourceManager.getColor(255, 0, 0));
				if(null == evaluationFactor.getParentId()) {
					confirmDialog.setTips("提示：评标总分超过100分！" );
					confirmDialog.setTipsColor(SWTResourceManager.getColor(255, 0, 0));
				}
				confirmDialog.open();
//				if(!"force".equals(confirmDialog.getClickStatus())){
					factor_score_text.setText(score_value);
					factor_score_text.forceFocus();
					return false;
//				}
			}
		}
		
		// 验证当前指标的分值设置是否比下级的小
		if(null != evaluationFactor.getId()){
//			queryWrapper.clear();
//			queryWrapper.setQueryParm("id");
//			queryWrapper.setQueryParm("factorScore");
//			queryWrapper.setQueryParm("factorWeight");
//			queryWrapper.setWhereParam("tenderId", tenderProjectEntity.getId());
//			queryWrapper.setWhereParam("parentId", evaluationFactor.getId());
//	a		evaluationFactorList = evaluationFactorService.queryList(queryWrapper, EvaluationFactor.class);
			evaluationFactorList = evaluationFactorService.findTenderAndParentId(tenderProject.getId(),evaluationFactor.getId(),null);
			totalScore = BigDecimal.ZERO;
			if(CollectionUtils.isNotNull(evaluationFactorList)){
				for(EvaluationFactor queryEvaluationFactor : evaluationFactorList){
					if(null != queryEvaluationFactor.getFactorWeight()){
						totalScore = BigDecimalUtils.add(totalScore, BigDecimalUtils.div(BigDecimalUtils.mul(queryEvaluationFactor.getFactorScore(), queryEvaluationFactor.getFactorWeight()), new BigDecimal(100)));
					}else {
						totalScore = BigDecimalUtils.add(totalScore, queryEvaluationFactor.getFactorScore());
					}
				}
				dValue = totalScore.subtract(curFactor.getFactorScore());
				// 如果下级指标的分值超过当前指标的分值
				if(dValue.compareTo(BigDecimal.ZERO) > 0){
					String score_value = NumberFormat.getInstance().format(totalScore);
//				MessageDialog.openWarning(getShell(), "["+curFactor.getFactorName()+"]的分值小于其下级指标的分值之和，最小分值应为"+score_value +" ！");
					ConfirmDialog confirmDialog = new ConfirmDialog(getShell(), Messages.Dialog_warmTips, Messages.Tips_lessThanChildLevalScore, null);
					confirmDialog.addButton(Messages.Button_reInput, "01", SWTResourceManager.getColor(77, 163, 19));
//					confirmDialog.addButton(Messages.Button_forceSave, "force", SWTResourceManager.getColor(255, 0, 0));
					confirmDialog.open();
//					if(!"force".equals(confirmDialog.getClickStatus())){
						factor_score_text.setText(score_value);
						factor_score_text.forceFocus();
						return false;
//					}
				}
			}
		}
		return true;
	}
	
	/**
	 * @Description: 添加客观范围打分
	 * @author guoyr
	 */
	private void addObjectiveScoreAction(){
		if(!addObjectiveScoreBtn.getEnabled()){
			return ;
		}
		String factorScore = factor_score_text.getText().trim();
		if(StringUtils.isEmpty(factorScore)){
			factor_score_text.setFocus();
			MessageDialog.openInformation(getShell(), Messages.Dialog_warmTips, Messages.Tips_pleaseEnterValidScore);
			return ;
		}
		
		if(null == objectiveItemList){
			objectiveItemList = new ArrayList<ScoreItem>();
		}
		ScoreItemDialog dialog = new ScoreItemDialog(getShell(), factorScore, null, objectiveItemList, new CallBack(){
			public void callBackObject(Object obj) {
				ScoreItem item = (ScoreItem) obj;
				if(null != item){
					objectiveItemList.add(item);
					GridItem gridItem = new GridItem(objectiveItemGrid, SWT.NONE);
					gridItem.setText(0, ObjectUtils.getObjString(objectiveItemList.size()));
					initObjectiveItemGridItem(gridItem, item);
					objectiveItemGrid.setSelection(new GridItem[]{gridItem});
					getSelectObjectiveItem();
				}
			}
		});
		dialog.setTitle(Messages.Title_dropDownOptions);
		dialog.open();
	}
	
	/**
	 * @Description: 删除客观范围打分
	 * @author guoyr
	 */
	private void delObjectiveScoreAction(){
		if(!delObjectiveScoreBtn.getEnabled()){
			return ;
		}
		
		if(MessageDialog.openConfirm(getShell(), Messages.Dialog_warmTips, MessageFormat.format(Messages.Tips_deleteObj, selectObjectItem.getScore()))){
			objectiveItemList.remove(selectObjectItem);
			GridItem gridItem = objectiveItemGrid.getSelection()[0];
			gridItem.dispose();
			int selectIndex = objectiveItemGrid.getSelectionIndex();
			if(objectiveItemGrid.getItemCount() > 0){
				if(selectIndex <= objectiveItemGrid.getItemCount()){
					objectiveItemGrid.setSelection(objectiveItemGrid.getItemCount() - 1);
				}else {
					objectiveItemGrid.setSelection(selectIndex);
				}
				selectIndex = 1;
				for(GridItem item : objectiveItemGrid.getItems()){
					item.setText(0, ObjectUtils.getObjString(selectIndex++));
				}
			}
			getSelectObjectiveItem();
		}
	}
	
	/**
	 * @Description: 修改客观范围打分
	 * @author guoyr
	 */
	private void updateObjectiveScoreAction(){
		if(!updateObjectiveScoreBtn.getEnabled()){
			return ;
		}
		ScoreItemDialog dialog = new ScoreItemDialog(getShell(), factor_score_text.getText().trim(), selectObjectItem, objectiveItemList, new CallBack(){
			public void callBackObject(Object obj) {
				ScoreItem item = (ScoreItem) obj;
				if(null != item){
					GridItem gridItem = objectiveItemGrid.getSelection()[0];
					initObjectiveItemGridItem(gridItem, item);
					getSelectObjectiveItem();
				}
			}
		});
		dialog.setTitle(Messages.Title_dropDownOptions);
		dialog.setOperator(SWT.MAX | SWT.CLOSE);
		dialog.open();
	}
	
	/**
	 * @Description: 
	 * @author guoyr
	 */
	private class SortComparator implements Comparator<ScoreItem> {
		
		private boolean desc = true;
		
		public SortComparator(boolean desc){
			this.desc = desc;
		}
		
		public int compare(ScoreItem o1, ScoreItem o2) {
			BigDecimal sortValue1 = o1.getScore();
			BigDecimal sortValue2 = o2.getScore();
			if(sortValue1.compareTo(sortValue2) > 0){
				return (desc?-11:1)*1;
			}else if(sortValue1.compareTo(sortValue2) < 0){
				return (desc?-1:1)*(-1);
			}else {
				return 0;
			}
		}
	}
	
	/**
	 * @Description: 验证是否已经进入评分环节
	 * @author guoyr
	 * @return
	 */
	private boolean isInEvaluationProgress(){
		isInEvaluationProgress = tenderProjectService.isInProgress(tenderProject.getId(), EvaluationStepEnum.DETAIL);
		if(isInEvaluationProgress){
			MessageDialog.openInformation(getShell(), Messages.Dialog_warmTips, Messages.Tips_enteredScoreEvaluation);
		}
		return isInEvaluationProgress;
	}
	
	/**
	 * @Description: 验证当前评审指标的是不是已经在评审中的评标步骤中
	 * @author guoyr
	 * @param factorId
	 * @return
	 */
	private boolean isInEvaluationStepProgress(String factorId){
		if(null == evaluationStepService){
			evaluationStepService = new EvaluationStepService();
		}
 		EvaluationStep evaluatoinNode = evaluationStepService.getEvaluationStepByFactor(tenderProject.getId(), factorId);

		if(null != evaluatoinNode && (ObjectUtils.equals(evaluatoinNode.getStepStatus(), CommonEnum.IN_PROGRESS) || ObjectUtils.equals(evaluatoinNode.getStepStatus(), CommonEnum.COMPLETE))){
			MessageDialog.openInformation(getShell(), Messages.Dialog_warmTips, Messages.Tips_enteredScoreEvaluation);
			return true;
		}else {
			return false;
		}
	}
	
	@Override
	protected void checkSubclass() {
	}
	
}
