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

import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.eclipse.nebula.widgets.grid.Grid;
import org.eclipse.nebula.widgets.grid.GridColumn;
import org.eclipse.nebula.widgets.grid.GridItem;
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.VerifyRequiredListener;
import org.eclipse.swt.core.utils.ValidateUtils;
import org.eclipse.swt.core.utils.WidgetUtils;
import org.eclipse.swt.core.widgets.EButton;
import org.eclipse.swt.core.widgets.EDialog;
import org.eclipse.swt.core.widgets.ELink;
import org.eclipse.swt.core.widgets.ESepator;
import org.eclipse.swt.custom.CLabel;
import org.eclipse.swt.events.DisposeEvent;
import org.eclipse.swt.events.DisposeListener;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

import com.gx.obe.ColorConstants;
import com.gx.obe.business.entity.Worker;
import com.gx.obe.business.enumeration.ExpertEnum;
import com.gx.obe.common.icons.IconsActivator;
import com.gx.obe.common.widget.button.ImageCheckBox;
import com.gx.obe.common.widget.button.Tips;
import com.gx.obe.components.core.I18NEnum;
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.evaluation.step.dialog.EvaluationGroupDialog;
import com.gx.obe.evaluation.step.inter.IEvaluationGroup;
import com.gx.obe.message.Messages;
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.StringUtils;
import com.gx.obe.util.utils.UuidUtils;
import com.gx.obe.web.entity.Supplier;
import com.gx.obe.web.entity.evaluation.EvaluationGroup;
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.SupplierService;
import com.gx.obe.web.service.WorkerService;
import com.gx.obe.web.service.evaluation.EvaluationGroupService;
import com.gx.obe.web.service.evaluation.EvaluationStepService;
import com.gx.obe.web.service.expert.ExpertEvaluationStepService;
import com.gx.obe.web.service.expert.ExpertService;
import com.swtdesigner.ResourceManager;
import com.swtdesigner.SWTResourceManager;

/**
 * @Description:清标评议步骤
 * @author chenxw
 */
public class PreEvaluationStepDialog extends EDialog implements IEvaluationGroup {
    
    private Text stepNameText;
    private Text stepMemoText;
    private EButton save_btn;
    private ImageResolver imageResolver = new ImageResolver();
    private ExpertService expertService = new ExpertService();
    private WorkerService workerService = new WorkerService();
    private SupplierService supplierService = new SupplierService();
    private EvaluationStepService evaluationStepService = new EvaluationStepService();
    private EvaluationGroupService evaluationGroupService = new EvaluationGroupService();
    private ExpertEvaluationStepService expertEvaluationStepService = new ExpertEvaluationStepService();
    private TenderProjectEntity tenderProject;
    private EvaluationStep evaluationStep;
    private CallBack callBack;
    private String supplierName;
    private Grid grid;
    private Map<String, Map<String, EvaluationGroup>> groupTypeMapSupplierMap;;
    private Map<String, String> userNameMap = new HashMap<String, String>();
    private List<Supplier> supplierList;
    private ELink setWorkerButton;
    private List<Expert> expertList;
    private List<Worker> workerList;
    private Tips tips;
    private ImageCheckBox evalBidBusBtn, evalBidTecBtn, evalPriceBtn;
    private Button factorComplianceBtn/*, factorScoreBtn*/;
    private Composite factorTypeComposite;
	private Button allowAuditButton;
    
    {
        LogUtils.logClass(PreEvaluationStepDialog.class);
    }
    
    /** 
     * @author chenxw
     * @param parentShell
     * @param tenderProject
     * @param evaluationStep 
     */
    public PreEvaluationStepDialog(Shell parentShell, TenderProjectEntity tenderProject, EvaluationStep evaluationStep) {
        super(parentShell);
        setTitle(Messages.Step_step);
        this.tenderProject = tenderProject;
        this.evaluationStep = evaluationStep;
        
        supplierName = com.gx.obe.components.core.I18N.getString(I18NEnum.SUPPLIERNAME, tenderProject.getTenderType());
    }
    
    /**
     * Create contents of the dialog.
     * 
     * @param parent
     */
    @Override
    protected Control createDialogArea(Composite parent) {
        Composite topContainer = (Composite) super.createDialogArea(parent);
        topContainer.addDisposeListener(new DisposeListener() {
            public void widgetDisposed(DisposeEvent e) {
                imageResolver.disposite();
            }
        });
        GridLayout gl_topContainer = new GridLayout(1, false);
        gl_topContainer.verticalSpacing = 0;
        gl_topContainer.horizontalSpacing = 0;
        gl_topContainer.marginHeight = 0;
        gl_topContainer.marginWidth = 0;
        topContainer.setLayout(gl_topContainer);
        
        Composite container = new Composite(topContainer, SWT.NONE);
        container.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
        GridLayout gl_container = new GridLayout(1, false);
        gl_container.marginHeight = 10;
        gl_container.marginWidth = 10;
        container.setLayout(gl_container);
        this.setOperator(SWT.MAX | SWT.CLOSE);
        
        tips = new Tips(container, SWT.WRAP);
        tips.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 2, 1));
        tips.setImage(ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, EvaluationStepEnum.getEvaluationStepIco(EvaluationStepEnum.CLEAN_TENDER)));
        tips.setSurroundColor(SWTResourceManager.getColor(60, 138, 226));
        
        Composite composite = new Composite(container, SWT.NONE);
        GridLayout gl_composite = new GridLayout(2, false);
        gl_composite.marginWidth = 0;
        composite.setLayout(gl_composite);
        composite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
        
        CLabel label = new CLabel(composite, SWT.NONE);
        label.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
        label.setText(Messages.StepName + "：");
        
        stepNameText = new Text(composite, SWT.BORDER);
        GridData gd_stepNameText = new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1);
        gd_stepNameText.widthHint = 150;
        stepNameText.setLayoutData(gd_stepNameText);
        stepNameText.addVerifyListener(new VerifyRequiredListener(stepNameText,  MessageFormat.format(Messages.Tips_required, Messages.StepName)));
        stepNameText.setTextLimit(20);
        stepNameText.setText("");
        
        CLabel label_11 = new CLabel(composite, SWT.NONE);
		label_11.setFont(SWTResourceManager.getFont("微软雅黑", 9, SWT.NORMAL));
		label_11.setLayoutData(new GridData(SWT.RIGHT, SWT.TOP, false, false, 1, 1));
		label_11.setText(Messages.EvaluationSet + "：");
		
		Composite composite_import = new Composite(composite, SWT.NONE);
		composite_import.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		RowLayout rl_composite_import = new RowLayout(SWT.HORIZONTAL);
		rl_composite_import.marginWidth = 5;
		rl_composite_import.marginRight = 0;
		rl_composite_import.marginBottom = 0;
		rl_composite_import.marginTop = 0;
		rl_composite_import.marginLeft = 0;
		composite_import.setLayout(rl_composite_import);
		
		allowAuditButton = new Button(composite_import, SWT.CHECK | SWT.WRAP);
		allowAuditButton.setText(Messages.Button_needLeaderAndManagerReview);
        
        CLabel evalBidTypeLabel = new CLabel(composite, SWT.NONE);
        evalBidTypeLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
        evalBidTypeLabel.setText(Messages.EvalType + "：");
        evalBidTypeLabel.setFont(SWTResourceManager.getFont("微软雅黑", 9, SWT.NORMAL));
        
        Composite evalBidTypeComposite = new Composite(composite, SWT.NONE);
        RowLayout rl_evalBidTypeComposite = new RowLayout(SWT.HORIZONTAL);
        rl_evalBidTypeComposite.fill = true;
        evalBidTypeComposite.setLayout(rl_evalBidTypeComposite);
        evalBidTypeComposite.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1));
        
        evalBidBusBtn = new ImageCheckBox(evalBidTypeComposite, SWT.CHECK);
        evalBidBusBtn.addSelectionListener(new SelectionAdapter() {
            public void widgetSelected(SelectionEvent e) {
                checkedEvalBidAction(EvaluationFactorEnum.EVAL_BID_BUS);
            }
        });
        evalBidBusBtn.setText(Messages.Business);
        evalBidBusBtn.setImageSize(20);
        
        evalBidTecBtn = new ImageCheckBox(evalBidTypeComposite, SWT.CHECK);
        evalBidTecBtn.setText(Messages.Technology);
        evalBidTecBtn.setImageSize(20);
        evalBidTecBtn.addSelectionListener(new SelectionAdapter() {
            public void widgetSelected(SelectionEvent e) {
                checkedEvalBidAction(EvaluationFactorEnum.EVAL_BID_TEC);
            }
        });
        
        evalPriceBtn = new ImageCheckBox(evalBidTypeComposite, SWT.CHECK);
        evalPriceBtn.setText(Messages.Economic);
        evalPriceBtn.setImageSize(20);
        evalPriceBtn.addSelectionListener(new SelectionAdapter() {
            public void widgetSelected(SelectionEvent e) {
                checkedEvalBidAction(EvaluationFactorEnum.EVAL_BID_PRICE);
            }
        });
        
        factorTypeComposite = new Composite(evalBidTypeComposite, SWT.NONE);
        GridLayout gl_factorTypeComposite = new GridLayout(1, false);
        gl_factorTypeComposite.marginHeight = 0;
        gl_factorTypeComposite.marginWidth = 0;
        factorTypeComposite.setLayout(gl_factorTypeComposite);
        
        factorComplianceBtn = new Button(factorTypeComposite, SWT.CHECK);
        factorComplianceBtn.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, true, 1, 1));

//      factorScoreBtn = new Button(factorTypeComposite, SWT.CHECK);
//      factorScoreBtn.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, true, 1, 1));
//      factorScoreBtn.setText("打分指标");
        
        setWorkerButton = new ELink(evalBidTypeComposite, SWT.NONE);
        setWorkerButton.addMouseListener(new MouseAdapter() {
            public void mouseUp(MouseEvent e) {
                setWorkerAction();
            }
        });
        setWorkerButton.setText(Messages.Button_assignWorker);
        setWorkerButton.setImage(ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, "icons/btn/blue/link.png"));
        
        CLabel label_1 = new CLabel(composite, SWT.NONE);
        label_1.setLayoutData(new GridData(SWT.RIGHT, SWT.TOP, false, false, 1, 1));
        label_1.setText(Messages.Worker +"：");
        
        Composite composite_2 = new Composite(composite, SWT.NONE);
        composite_2.setBackground(ColorConstants.BORDER_LINE_COLOR);
        GridLayout gl_composite_2 = new GridLayout(1, false);
        gl_composite_2.verticalSpacing = 0;
        gl_composite_2.marginHeight = 1;
        gl_composite_2.marginWidth = 1;
        composite_2.setLayout(gl_composite_2);
        GridData gd_composite_2 = new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1);
        gd_composite_2.heightHint = 200;
        composite_2.setLayoutData(gd_composite_2);
        
        Composite composite_3 = new Composite(composite_2, SWT.NONE);
        composite_3.setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
        GridLayout gl_composite_3 = new GridLayout(1, false);
        gl_composite_3.marginHeight = 0;
        gl_composite_3.marginWidth = 0;
        composite_3.setLayout(gl_composite_3);
        composite_3.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
        
        grid = new Grid(composite_3, SWT.NONE | SWT.V_SCROLL);
        grid.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
        grid.setHeaderVisible(true);
        grid.setAutoHeight(true);
        grid.setAutoWidth(true);
        
        GridColumn indexColumn = new GridColumn(grid, SWT.CENTER);
        indexColumn.setText(Messages.Num);
        indexColumn.setWidth(50);
        indexColumn.setMaxmumWidth(50);
        
        GridColumn gridColumn = new GridColumn(grid, SWT.NONE);
        gridColumn.setText(MessageFormat.format(Messages.NameFormat, supplierName));
        gridColumn.setWidth(250);
        gridColumn.setWordWrap(true);
        
        GridColumn gridColumn_1 = new GridColumn(grid, SWT.CENTER);
        gridColumn_1.setText(Messages.Business);
        gridColumn_1.setWidth(50);
        gridColumn_1.setMaxmumWidth(80);
        
        GridColumn gridColumn_2 = new GridColumn(grid, SWT.CENTER);
        gridColumn_2.setText(Messages.Technology);
        gridColumn_2.setWidth(50);
        gridColumn_2.setMaxmumWidth(80);
        
        GridColumn gridColumn_3 = new GridColumn(grid, SWT.CENTER);
        gridColumn_3.setText(Messages.Price);
        gridColumn_3.setWidth(50);
        gridColumn_3.setMaxmumWidth(80);
        
        CLabel label_5 = new CLabel(composite, SWT.NONE);
        label_5.setLayoutData(new GridData(SWT.RIGHT, SWT.TOP, false, true, 1, 1));
        label_5.setText(Messages.Note + ": ");
        
        stepMemoText = new Text(composite, SWT.BORDER | SWT.WRAP | SWT.V_SCROLL);
        GridData gd_stepMemoText = new GridData(SWT.FILL, SWT.FILL, true, false, 1, 1);
        gd_stepMemoText.heightHint = 50;
        stepMemoText.setLayoutData(gd_stepMemoText);
        stepMemoText.setTextLimit(100);
        
        ESepator but_sep = new ESepator(topContainer, SWT.SEPARATOR | SWT.HORIZONTAL);
        but_sep.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
        but_sep.setBackground(ColorConstants.SEP_LIGHT_COLOR);
        
        Composite composite_1 = new Composite(topContainer, SWT.NONE);
        composite_1.setBackgroundMode(SWT.INHERIT_FORCE);
        composite_1.setBackgroundImage(ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, "icons/fill_bg.png"));
        composite_1.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 3, 1));
        GridLayout gl_composite_1 = new GridLayout(2, false);
        composite_1.setLayout(gl_composite_1);
        
        save_btn = new EButton(composite_1, SWT.NONE);
        save_btn.setImage(ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, "icons/btn/blue/save.png"));
        GridData gd_confirmBtn = new GridData(SWT.RIGHT, SWT.CENTER, true, false, 1, 1);
        gd_confirmBtn.widthHint = 90;
        save_btn.setLayoutData(gd_confirmBtn);
        save_btn.setText(Messages.Button_save);
        save_btn.addMouseListener(new MouseAdapter() {
            public void mouseUp(MouseEvent e) {
                saveNodeAction();
            }
        });
        
        EButton cancel_btn = new EButton(composite_1, SWT.NONE);
        cancel_btn.setImage(ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, "icons/btn/blue/close.png"));
        cancel_btn.addMouseListener(new MouseAdapter() {
            public void mouseUp(MouseEvent e) {
                cancelAction();
            }
        });
        GridData gd_button = new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1);
        gd_button.widthHint = 90;
        cancel_btn.setLayoutData(gd_button);
        cancel_btn.setText(Messages.Button_close);
        
        init();
        return container;
    }
    
    /**
     * @Description: 初始化
     * @author chenxw
     */
    private void init() {
        if (null == evaluationStep) {
            evaluationStep = new EvaluationStep();
//          evaluationStep.setStepName(PropertiesUtils.getEnum(I18NEnum.PRE_EVAL_OPINION, "评议"));
            evaluationStep.setStepType(EvaluationStepEnum.PRE_EVALUATION);
            evaluationStep.setStepStatus(CommonEnum.NOT_START);
            this.setTitle(MessageFormat.format(Messages.Title_create, Messages.Step_step));
        } else {
            this.setTitle(MessageFormat.format(Messages.Title_edit, Messages.Step_step));
            if(CommonEnum.IN_PROGRESS.equals(evaluationStep.getStepStatus()) || CommonEnum.COMPLETE.equals(evaluationStep.getStepStatus())){
				evalBidBusBtn.setEnabled(false);
				evalBidTecBtn.setEnabled(false);
				evalPriceBtn.setEnabled(false);
				setWorkerButton.setEnabled(false);
			}
            if (ObjectUtils.equals(evaluationStep.getStepStatus(), CommonEnum.COMPLETE)) {
                save_btn.setEnabled(false);
            }
        }
        factorComplianceBtn.setText(MessageFormat.format(Messages.IncludeFormat, Messages.Factor_compliance));
        
        tips.setText(MessageFormat.format(Messages.Tips_bidCommentStep, Messages.Step_bidComment));
        groupTypeMapSupplierMap = evaluationGroupService.getGroupTypeMapSupplierMap(tenderProject.getId());
        
        refreshEvaltionNodeForm();
        grid.setItemHeight(25);
        initEvaluationGroupGrid(grid);
        refreshEvaluationGroupGrid(grid);
    }
    
    /**
     * @Description: 选择评标类型
     * @author guoyr
     * @param evalBidType
     */
    private void checkedEvalBidAction(String evalBidType) {
        if (EvaluationFactorEnum.EVAL_BID_PRICE.equals(evalBidType)) {
            if (!evalPriceBtn.getEnabled()) {
                return;
            }
            evalBidBusBtn.setSelection(false);
            evalBidTecBtn.setSelection(false);
        } else {
            if (EvaluationFactorEnum.EVAL_BID_BUS.equals(evalBidType)) {
                if (!evalBidBusBtn.getEnabled()) {
                    return;
                }
                if (evalBidBusBtn.getSelection()) {
                    evalPriceBtn.setSelection(false);
                }
            } else if (EvaluationFactorEnum.EVAL_BID_TEC.equals(evalBidType)) {
                if (!evalBidTecBtn.getEnabled()) {
                    return;
                }
                if (evalBidTecBtn.getSelection()) {
                    evalPriceBtn.setSelection(false);
                }
            }
        }
        
        if (null == evaluationStep.getId()) {
            if (evalBidBusBtn.getSelection() && evalBidTecBtn.getSelection()) {
                stepNameText.setText(Messages.Step_bidComment);
            } else if (evalBidBusBtn.getSelection()) {
                stepNameText.setText(Messages.Step_busComment);
            } else if (evalBidTecBtn.getSelection()) {
                stepNameText.setText(Messages.Step_tecComment);
            } else if (evalPriceBtn.getSelection()) {
                stepNameText.setText(Messages.Step_priceCcompar);
            }
        }
        refreshFactorTypeComposite();
    }
    
    /**
     * @Description: 刷新评标步骤表单
     * @author chenxw
     */
    private void refreshEvaltionNodeForm() {
        stepNameText.setText(ObjectUtils.getObjString(evaluationStep.getStepName()));
        stepMemoText.setText(ObjectUtils.getObjString(evaluationStep.getStepMemo()));
        
        allowAuditButton.setSelection(CommonEnum.YES.equals(evaluationStep.getEvaluationParamByKey(EvaluationStepEnum.USE_AUDIT_PRE_EVAL)));
        
        Integer factorType = evaluationStep.getEvaluationParamByKey(EvaluationStepEnum.FACTOR_TYPE, Integer.class);
        
        if (null == factorType) {
            factorComplianceBtn.setSelection(true);
//          factorScoreBtn.setSelection(true);
        } else {
            factorComplianceBtn.setSelection((factorType & EvaluationStepEnum.FACTOR_COMPLIANCE) != 0);
//          factorScoreBtn.setSelection((factorType & EvaluationStepEnum.FACTOR_SCORE) != 0);
        }
        List<EvaluationStep> preEvaluationStepList = evaluationStepService.getEvaluationStepListByType(tenderProject.getId(), EvaluationStepEnum.PRE_EVALUATION);
        if (null != preEvaluationStepList && preEvaluationStepList.size() == 1) {
            EvaluationStep preEvaluationStep = preEvaluationStepList.get(0);
            if (!preEvaluationStep.getId().equals(evaluationStep.getId())) {
                String evalBidType = preEvaluationStep.getEvalBidType();
                if (EvaluationFactorEnum.EVAL_BID_DETAIL.equals(evalBidType )) {
                    evalBidBusBtn.setEnabled(false);
                    evalBidTecBtn.setEnabled(false);
                } else if (EvaluationFactorEnum.EVAL_BID_BUS.equals(evalBidType)) {
                    evalBidBusBtn.setEnabled(false);
                } else if (EvaluationFactorEnum.EVAL_BID_TEC.equals(evalBidType)) {
                    evalBidTecBtn.setEnabled(false);
                } else if (EvaluationFactorEnum.EVAL_BID_PRICE.equals(evalBidType)) {
                    evalPriceBtn.setEnabled(false);
                }
            }
        }
        evalBidBusBtn.setSelection(false);
        evalBidTecBtn.setSelection(false);
        evalPriceBtn.setSelection(false);
        
        if (EvaluationFactorEnum.EVAL_BID_DETAIL.equals(evaluationStep.getEvalBidType())) {
            evalBidBusBtn.setSelection(true);
            evalBidTecBtn.setSelection(true);
        } else if (EvaluationFactorEnum.EVAL_BID_BUS.equals(evaluationStep.getEvalBidType())) {
            evalBidBusBtn.setSelection(true);
        } else if (EvaluationFactorEnum.EVAL_BID_TEC.equals(evaluationStep.getEvalBidType())) {
            evalBidTecBtn.setSelection(true);
        } else if (EvaluationFactorEnum.EVAL_BID_PRICE.equals(evaluationStep.getEvalBidType())) {
            evalPriceBtn.setSelection(true);
        }
        
        refreshFactorTypeComposite();
    }
    
    /**
     * @Description: 刷新指标类型
     * @author chenxw 
     */
    private void refreshFactorTypeComposite() {
        boolean isSelection = evalBidBusBtn.getSelection() || evalBidTecBtn.getSelection();
        WidgetUtils.setControlVisible(factorTypeComposite, isSelection);
        factorTypeComposite.getParent().layout();
    }

    /**
     * @Description: 初始化评审分组表格
     * @author chenxw
     */
    public void initEvaluationGroupGrid(Grid grid) {
        grid.removeAll();
        initUserNameMap();
        supplierList = supplierService.getOpenBidSuccessSupplier(tenderProject.getId());
        if (CollectionUtils.isNull(supplierList)) {
            return;
        }
        int index = 1;
        for (Supplier supplier : supplierList) {
            GridItem gridItem = new GridItem(grid, SWT.NONE);
            gridItem.setText(0, index + "");
            gridItem.setText(1, supplier.getSupplierName());
            gridItem.setData("supplierId", supplier.getId());
            index++;
        }
    }
    
    /**
     * @Description: 初始化用户名称
     * @author chenxw
     */
    private void initUserNameMap() {
        userNameMap.clear();
        expertList = expertService.getExpertList(tenderProject.getId(), ExpertEnum.ALL);
        if (CollectionUtils.isNotNull(expertList)) {
            for (Expert expert : expertList) {
                userNameMap.put(expert.getUserId(), expert.getUser().getUserName());
            }
        }
        workerList = workerService.getWorkerList(false,tenderProject.getId(), ExpertEnum.WORKER);
        if (CollectionUtils.isNotNull(workerList)) {
            for (Worker worker : workerList) {
                userNameMap.put(worker.getUserId(), worker.getUser().getUserName());
            }
        }
    }
    
    /**
     * @Description: 刷新
     * @author chenxw
     */
    public void refreshEvaluationGroupGrid(Grid grid) {
        for (GridItem gridItem : grid.getItems()) {
            String supplierId = (String) gridItem.getData("supplierId");
            if (StringUtils.isEmpty(supplierId)) {
                continue;
            }
            Map<String, EvaluationGroup> groupTypeMap = groupTypeMapSupplierMap.get(supplierId);
            if (null == groupTypeMap || groupTypeMap.isEmpty()) {
                continue;
            }
            
            EvaluationGroup busGroup = groupTypeMap.get(EvaluationFactorEnum.EVAL_BID_BUS);
            if (null != busGroup) {
                gridItem.setText(2, ObjectUtils.getObjString(userNameMap.get(busGroup.getUserId())));
            }
            EvaluationGroup tecGroup = groupTypeMap.get(EvaluationFactorEnum.EVAL_BID_TEC);
            if (null != tecGroup) {
                gridItem.setText(3, ObjectUtils.getObjString(userNameMap.get(tecGroup.getUserId())));
            }
            EvaluationGroup priceGroup = groupTypeMap.get(EvaluationFactorEnum.EVAL_BID_PRICE);
            if (null != priceGroup) {
                gridItem.setText(4, ObjectUtils.getObjString(userNameMap.get(priceGroup.getUserId())));
            }
        }
    }
    
    /**
     * @Description: 添加评审分组
     * @author chenxw
     * @param supplierIdList
     * @param evalBidType
     * @param userId
     */
    public void putAllGroupTypeMapSupplierMap(List<String> supplierIdList, String evalBidType, String userId) {
        if (CollectionUtils.isNull(supplierIdList) || StringUtils.isEmpty(evalBidType)) {
            return;
        }
        for (String supplierId : supplierIdList) {
            if (groupTypeMapSupplierMap.containsKey(supplierId)) {
                Map<String, EvaluationGroup> groupTypeMap = groupTypeMapSupplierMap.get(supplierId);
                if (groupTypeMap.containsKey(evalBidType)) {
                    groupTypeMap.get(evalBidType).setUserId(userId);
                } else {
                    groupTypeMap.put(evalBidType, getEvaluationGroup(supplierId, evalBidType, userId));
                }
            } else {
                Map<String, EvaluationGroup> groupTypeMap = new HashMap<String, EvaluationGroup>();
                groupTypeMap.put(evalBidType, getEvaluationGroup(supplierId, evalBidType, userId));
                groupTypeMapSupplierMap.put(supplierId, groupTypeMap);
            }
        }
    }
    
    /**
     * @Description: 获取评审分组
     * @author chenxw
     * @param supplierId
     * @param evalBidType
     * @param userId
     * @return
     */
    private EvaluationGroup getEvaluationGroup(String supplierId, String evalBidType, String userId) {
        EvaluationGroup evaluationGroup = new EvaluationGroup();
        evaluationGroup.setTenderId(tenderProject.getId());
        evaluationGroup.setSupplierId(supplierId);
        evaluationGroup.setEvalBidType(evalBidType);
        evaluationGroup.setUserId(userId);
        return evaluationGroup;
    }
    
    /**
     * @Description: 获取投标人名称
     * @author chenxw
     * @return
     */
    public String getSupplierName() {
        return supplierName;
    }
    
    /**
     * @Description: 获取专家列表
     * @author chenxw
     * @return
     */
    public List<Expert> getExpertList() {
        return expertList;
    }
    
    /**
     * @Description: 获取专家列表
     * @author chenxw
     * @return
     */
    public List<Worker> getWorkerList() {
        return workerList;
    }
    
    /**
     * @Description: 设置工作人员
     * @author chenxw
     */
    private void setWorkerAction() {
        if (setWorkerButton.getEnabled()) {
            String evalBidType = null;
            if (evalPriceBtn.getSelection()) {
                evalBidType = EvaluationFactorEnum.EVAL_BID_PRICE;
            } else {
                if (evalBidBusBtn.getSelection() && evalBidTecBtn.getSelection()) {
                    evalBidType = EvaluationFactorEnum.EVAL_BID_DETAIL;
                } else if (evalBidBusBtn.getSelection()) {
                    evalBidType = EvaluationFactorEnum.EVAL_BID_BUS;
                } else if (evalBidTecBtn.getSelection()) {
                    evalBidType = EvaluationFactorEnum.EVAL_BID_TEC;
                }
            }
            if (null == evalBidType) {
                MessageDialog.openShortMessage(Messages.Tips_SelectEvalType);
                return;
            }
            EvaluationGroupDialog dialog = new EvaluationGroupDialog(getShell(), tenderProject, evalBidType, this);
            dialog.setTitle(setWorkerButton.getText());
            dialog.open();
            refreshEvaluationGroupGrid(grid);
        }
    }
    
    /**
     * @Description: 确定
     * @author chenxw
     */
    private void saveNodeAction() {
        if (save_btn.getEnabled()) {
            if (!ValidateUtils.checkInput(stepNameText.getParent())) {
                return;
            }
            String evalBidType = null;
            if (evalPriceBtn.getSelection()) {
                evalBidType = EvaluationFactorEnum.EVAL_BID_PRICE;
            } else {
                if (evalBidBusBtn.getSelection() && evalBidTecBtn.getSelection()) {
                    evalBidType = EvaluationFactorEnum.EVAL_BID_DETAIL;
                } else if (evalBidBusBtn.getSelection()) {
                    evalBidType = EvaluationFactorEnum.EVAL_BID_BUS;
                } else if (evalBidTecBtn.getSelection()) {
                    evalBidType = EvaluationFactorEnum.EVAL_BID_TEC;
                }
            }
            if (null == evalBidType) {
                MessageDialog.openShortMessage(Messages.Tips_SelectEvalType);
                return;
            }
            // 验证评标步骤名称是否已经存在
            if (evaluationStepService.valEvaluationStepNameExist(tenderProject.getId(), evaluationStep.getId(), stepNameText.getText().trim())) {
                MessageDialog.openWarning(getShell(), Messages.Step_step + "[" + stepNameText.getText().trim() + "]" + Messages.Tips_AlreadyExistsPleaseRetype);
                stepNameText.setFocus();
                stepNameText.setSelection(stepNameText.getText().length());
                return;
            }
            
            evaluationStep.setStepName(stepNameText.getText().trim());
            evaluationStep.setStepMemo(stepMemoText.getText().trim());
            evaluationStep.setTenderId(tenderProject.getId());
            
            evaluationStep.setEvalBidType(evalBidType);
            
            evaluationStep.setEvaluationParam(EvaluationStepEnum.USE_AUDIT_PRE_EVAL, allowAuditButton.getSelection() ? CommonEnum.YES : CommonEnum.NO);
            
            int factorCompliance = 0;
            int factorScore = 0;
            if (factorComplianceBtn.getSelection()) {
                factorCompliance = EvaluationStepEnum.FACTOR_COMPLIANCE;
            }
//          if (factorScoreBtn.getSelection()) {
                factorScore = EvaluationStepEnum.FACTOR_SCORE;
//          }
            evaluationStep.setEvaluationParam(EvaluationStepEnum.FACTOR_TYPE, factorCompliance | factorScore);
            
            evaluationStep = evaluationStepService.saveEvaluationStep(evaluationStep);
            
            if (null != evaluationStep) {
            	expertEvaluationStepService.deleteExpertEvaluationStepByStepType(tenderProject.getId(), EvaluationStepEnum.PRE_EVALUATION);
                evaluationGroupService.deleteByTenderIdAndStepId(tenderProject.getId(), evaluationStep.getId());
                evaluationGroupService.batchSave(getEvaluationGroupList());
                if (null != callBack) {
                    callBack.callBackObject(evaluationStep);
                }
            } else {
                MessageDialog.openWarning(getShell(), stepNameText.getText() + Messages.Tips_saveFail);
            }
            this.getShell().close();
        }
    }
    
    private List<EvaluationGroup> getEvaluationGroupList() {
        List<EvaluationGroup> evaluationGroupList = new ArrayList<EvaluationGroup>();
        for (Map<String, EvaluationGroup> groupTypeMap : groupTypeMapSupplierMap.values()) {
            for (String type : groupTypeMap.keySet()) {
                if (evaluationStep.getEvalBidType().equals(EvaluationFactorEnum.EVAL_BID_DETAIL)) {
                    if (type.equals(EvaluationFactorEnum.EVAL_BID_BUS) || type.equals(EvaluationFactorEnum.EVAL_BID_TEC)) {
                        EvaluationGroup group = groupTypeMap.get(type);
                        group.setStepId(evaluationStep.getId());
                        if(null == group.getId()){
                            group.setId(UuidUtils.getUUID());
                        }
                        evaluationGroupList.add(group);
                    }
                } else if (evaluationStep.getEvalBidType().equals(type)) {
                    EvaluationGroup group = groupTypeMap.get(type);
                    group.setStepId(evaluationStep.getId());
                    if(null == group.getId()){
                        group.setId(UuidUtils.getUUID());
                    }
                    evaluationGroupList.add(group);
                }
            }
        }
        return evaluationGroupList;
    }
    
    /**
     * @Description: 取消
     * @author chenxw
     */
    private void cancelAction() {
        this.getShell().close();
    }
    
    public CallBack getCallBack() {
        return callBack;
    }
    
    public void setCallBack(CallBack callBack) {
        this.callBack = callBack;
    }
    
    /**
     * Return the initial size of the dialog.
     */
    @Override
    protected Point getInitialSize() {
        return new Point(705, 505);
    }
    
}
