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

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.eclipse.swt.SWT;
import org.eclipse.swt.core.MessageDialog;
import org.eclipse.swt.core.widgets.ESepator;
import org.eclipse.swt.custom.CLabel;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;

import com.gx.obe.ColorConstants;
import com.gx.obe.business.entity.SupplierEvaluationStep;
import com.gx.obe.business.enumeration.SupplierEnum;
import com.gx.obe.common.icons.IconsActivator;
import com.gx.obe.components.core.Constants;
import com.gx.obe.components.core.I18NEnum;
import com.gx.obe.components.core.enumeration.TenderProjectEnum;
import com.gx.obe.evaluation.step.inter.IRecommonedTable;
import com.gx.obe.message.Messages;
import com.gx.obe.util.utils.LogUtils;
import com.gx.obe.util.utils.NumericChineseUtils;
import com.gx.obe.util.utils.ObjectUtils;
import com.gx.obe.util.utils.StringUtils;
import com.gx.obe.web.entity.Supplier;
import com.gx.obe.web.entity.SupplierEvaluationResult;
import com.gx.obe.web.entity.auth.AuthUser;
import com.gx.obe.web.entity.evaluation.EvaluationStep;
import com.gx.obe.web.entity.expert.ExpertEvaluationResult;
import com.gx.obe.web.entity.tender.TenderProjectEntity;
import com.gx.obe.web.entity.tender.TenderProjectRuleEntity;
import com.gx.obe.web.service.ProjectRuleService;
import com.gx.obe.web.service.SupplierEvaluationStepService;
import com.gx.obe.web.service.expert.ExpertEvaluationResultService;
import com.gx.obe.web.service.expert.ExpertEvaluationStepService;
import com.gx.obe.web.service.expert.ExpertService;
import com.swtdesigner.ResourceManager;
import com.swtdesigner.SWTResourceManager;

import de.kupzog.ktable.KTable;
import de.kupzog.ktable.SWTX;
import de.kupzog.ktable.editors.KTableCellEditorCheckbox2;
import de.kupzog.ktable.editors.KTableCellEditorText;
import de.kupzog.ktable.model.CommonKtableCModel;
import de.kupzog.ktable.renderers.CellAlignmentRender;
import de.kupzog.ktable.renderers.CellContentRender;
import de.kupzog.ktable.renderers.CellEditContentRender;
import de.kupzog.ktable.renderers.CellEditorRender;
import de.kupzog.ktable.renderers.CellImageRender;
import de.kupzog.ktable.renderers.CellRender;
import de.kupzog.ktable.renderers.CheckableCellRenderer;
import de.kupzog.ktable.renderers.FixedWrapCellRenderer;

/**
 * @Description: 推荐
 * @author guoyr
 */
public class RecommonedRankEvaluationTableComposite extends Composite implements IRecommonedTable{
	
	private TenderProjectEntity tenderProjectEntity;
	private TenderProjectRuleEntity tenderProjectRuleEntity;
	private EvaluationStep evaluationStep;
	private AuthUser CURRENT_USER;
	private Text evaluationOpinion_text;
	private Combo defaultEvalOpinionCombo;
	private ProjectRuleService projectRuleService;
	private ExpertEvaluationStepService expertEvaluationStepService ;
	private SupplierEvaluationStepService supplierEvaluationStepService ;
	private ExpertEvaluationResultService expertEvaluationResultService;
	private ExpertService expertService;
	private Composite supplierComposite;
	private List<Supplier> supplierList;
	private Composite recommonedContentComposite;
	private Composite composite_3;
	private String errorMessage;
	private Label notLabel;
	private int recommendRankCount;
	private HashMap<String, Integer> supplierRankMap;
	private EvaluationStep recommonedRank;
	private List<AuthUser> userList;
	private String supplierName;
	private boolean isShowError = false;
	private Map<String, Map<Integer, Integer>> supplierRankVoteMap;
	private Map<String, ExpertEvaluationResult> expertEvaluationResultMap;
	
	{
		LogUtils.logClass(RecommonedRankEvaluationTableComposite.class);
	}
	
	/**
	 * @param parentShell
	 * @param recommonedRank 
	 * @param tenderProjectEntity
	 * @param tenderProjectRuleEntity
	 * @param evaluationStep
	 * @param listener
	 */
	public RecommonedRankEvaluationTableComposite(Composite parentShell, int style, EvaluationStep recommonedRank) {
		super(parentShell,style);
		GridLayout gridLayout = new GridLayout(1, false);
		gridLayout.verticalSpacing = 0;
		gridLayout.horizontalSpacing = 0;
		gridLayout.marginHeight = 0;
		gridLayout.marginWidth = 0;
		setLayout(gridLayout);
		setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
		
		this.recommonedRank = recommonedRank;
		
		composite_3 = new Composite(this, SWT.NONE);
		composite_3.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, false, 1, 1));
		composite_3.setLayout(new GridLayout(1, false));
		
		notLabel = new Label(composite_3, SWT.WRAP);
		notLabel.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		notLabel.setFont(SWTResourceManager.getFont("微软雅黑", 10, SWT.NORMAL));
		notLabel.setForeground(ColorConstants.NOTE_TIPS_COLOR);
		notLabel.setText("请在最右侧选择要推荐的供应商！");
		
		Composite container = new Composite(this, SWT.NONE);
		container.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
		GridLayout gl_container = new GridLayout(1, false);
		gl_container.marginHeight = 1;
		gl_container.marginWidth = 1;
		gl_container.verticalSpacing = 0;
		gl_container.horizontalSpacing = 0;
		container.setLayout(gl_container);
		container.setBackground(ColorConstants.BORDER_LINE_COLOR);
		
		supplierComposite = new Composite(container, SWT.NONE);
		GridData gd_supplierComposite = new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1);
		gd_supplierComposite.heightHint = 150;
		gd_supplierComposite.minimumHeight = 150;
		supplierComposite.setLayoutData(gd_supplierComposite);
		GridLayout gl_supplierComposite = new GridLayout(1, false);
		gl_supplierComposite.horizontalSpacing = 0;
		gl_supplierComposite.marginHeight = 0;
		gl_supplierComposite.verticalSpacing = 0;
		gl_supplierComposite.marginWidth = 0;
		supplierComposite.setLayout(gl_supplierComposite);
		supplierComposite.setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
		
		recommonedContentComposite = new Composite(supplierComposite, SWT.NONE);
		GridLayout gl_recommonedContentComposite = new GridLayout(1, false);
		gl_recommonedContentComposite.verticalSpacing = 0;
		gl_recommonedContentComposite.horizontalSpacing = 0;
		gl_recommonedContentComposite.marginHeight = 0;
		gl_recommonedContentComposite.marginWidth = 0;
		recommonedContentComposite.setLayout(gl_recommonedContentComposite);
		recommonedContentComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
		
		ESepator tableSplabel = new ESepator(container, SWT.SEPARATOR | SWT.HORIZONTAL);
		tableSplabel.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		tableSplabel.setBackground(ColorConstants.SEP_LIGHT_COLOR);
		
		Composite composite_1 = new Composite(container, SWT.NONE);
		composite_1.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1));
		GridLayout gl_composite_1 = new GridLayout(3, false);
		gl_composite_1.marginHeight = 2;
		gl_composite_1.marginWidth = 1;
		composite_1.setLayout(gl_composite_1);
		composite_1.setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
		CLabel label_1 = new CLabel(composite_1, SWT.NONE);
		label_1.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		label_1.setFont(SWTResourceManager.getFont("微软雅黑", 10, SWT.BOLD));
		label_1.setForeground(ColorConstants.NOTE_TIPS_COLOR);
		label_1.setText(Messages.Title_evaluationOpinion);
		
		Label label_2 = new Label(composite_1, SWT.NONE);
		label_2.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		label_2.setFont(SWTResourceManager.getFont("微软雅黑", 10, SWT.NORMAL));
		label_2.setForeground(ColorConstants.NOTE_TIPS_COLOR);
		label_2.setText(Messages.Title_commonOpinions + "：");
		
		defaultEvalOpinionCombo = new Combo(composite_1, SWT.READ_ONLY);
		defaultEvalOpinionCombo.setFont(SWTResourceManager.getFont("微软雅黑", 9, SWT.NORMAL));
		defaultEvalOpinionCombo.setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
		defaultEvalOpinionCombo.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				evaluationOpinionCreater();
			}
		});
		GridData gd_defaultEvalOpinionCombo = new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1);
		gd_defaultEvalOpinionCombo.widthHint = 100;
		defaultEvalOpinionCombo.setLayoutData(gd_defaultEvalOpinionCombo);
		
		evaluationOpinion_text = new Text(container, SWT.WRAP | SWT.V_SCROLL);
		evaluationOpinion_text.setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
		GridData gd_evaluationOpinion_text = new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1);
		gd_evaluationOpinion_text.minimumHeight = 10;
		gd_evaluationOpinion_text.heightHint = 10;
		evaluationOpinion_text.setLayoutData(gd_evaluationOpinion_text);
		evaluationOpinion_text.setFont(SWTResourceManager.getFont("宋体", 10, SWT.NORMAL));
		evaluationOpinion_text.setText("评标基准价=(有效投标人的经评审的投标报价之和/有效投标人个数之和)。\r\n当投标人的经评审的投标报价等于评标基准价时，得基准分30.0分：\r\n投标报价每高出评标基准价1.0%，得分在30.0分基础上扣减0.5分，扣至20.0分为止；\r\n投标报价每低于评标基准价1.0%（总偏离不超过20.0的），得分在30.0分基础上增加0.5分，加至40.0分为止；\r\n投标报价低于评标基准价超过20.0%的，得分均为40.0分。");
		evaluationOpinion_text.setTextLimit(1000);
		supplierEvaluationStepService = new SupplierEvaluationStepService();
		expertService = new ExpertService();
		expertEvaluationStepService = new ExpertEvaluationStepService();
		expertEvaluationResultService = new ExpertEvaluationResultService();
	}

	/** 
	 * @Description: 刷新评标界面
	 * @author chenxw
	 * @param tenderProjectEntity
	 * @param tenderProjectRuleEntity
	 * @param evaluationStep
	 * @param supplierList 
	 */
	public void refresh(TenderProjectEntity tenderProjectEntity, TenderProjectRuleEntity tenderProjectRuleEntity, EvaluationStep evaluationStep, AuthUser curUser, List<Supplier> supplierList){
		this.tenderProjectEntity = tenderProjectEntity;
		this.evaluationStep = evaluationStep;
		this.tenderProjectRuleEntity = tenderProjectRuleEntity;
		this.CURRENT_USER = curUser;
		this.supplierList = supplierList;
		if(null == projectRuleService){
			projectRuleService = new ProjectRuleService();
		}
		supplierName = com.gx.obe.components.core.I18N.getString(I18NEnum.SUPPLIERNAME, tenderProjectEntity.getTenderType());
		tenderProjectRuleEntity = projectRuleService.getProjectRule(tenderProjectEntity.getId());
		if(null == tenderProjectRuleEntity)tenderProjectRuleEntity = new TenderProjectRuleEntity();
		List<SupplierEvaluationStep> querySupplierEvaluationStepList = new ArrayList<SupplierEvaluationStep>();
		List<ExpertEvaluationResult> queryExpertEvaluationResultList = new ArrayList<ExpertEvaluationResult>();
		if (null != recommonedRank) {
			querySupplierEvaluationStepList = supplierEvaluationStepService.getSupplierEvaluationStepByTenderIdAndStepId(tenderProjectEntity.getId(), recommonedRank.getId());
			queryExpertEvaluationResultList = expertEvaluationResultService.getExpertStepAllSupplierEvaluationResultList(tenderProjectEntity.getId(), recommonedRank.getId(), null);
			recommendRankCount = recommonedRank.getEvaluationParamByKey(com.gx.obe.components.core.enumeration.EvaluationStepEnum.RECOMMEND_COUNT, Integer.class, 3);
			userList = expertService.getEvaluationStepUserList(tenderProjectEntity.getId(), recommonedRank);
			
		}
		if (null == userList) {
			userList = new ArrayList<AuthUser>();
		}
		supplierRankMap = new HashMap<String, Integer>();
		supplierRankVoteMap = new HashMap<String, Map<Integer,Integer>>();
		expertEvaluationResultMap = new HashMap<String, ExpertEvaluationResult>();
		for (Supplier supplier : supplierList) {
			for (SupplierEvaluationStep supplierEvaluationStep : querySupplierEvaluationStepList) {
				if (StringUtils.equals(supplier.getId(), supplierEvaluationStep.getSupplierId())) {
					supplierRankMap.put(supplier.getId(), supplierEvaluationStep.getSortNo());
					break;
				}
			}
			
			Map<Integer, Integer> supplierExpertVoteMap = new HashMap<Integer, Integer>();
			for (ExpertEvaluationResult expertEvaluationResult : queryExpertEvaluationResultList) {
				if (StringUtils.equals(supplier.getId(), expertEvaluationResult.getSupplierId())) {
					int sortNo = expertEvaluationResult.getSortNoIntValue();
					if (sortNo == 0) {
						continue;
					}
					int value = supplierExpertVoteMap.containsKey(sortNo) ? supplierExpertVoteMap.get(sortNo) : 0;
					supplierExpertVoteMap.put(expertEvaluationResult.getSortNoIntValue(), ++value);
				}
				for (AuthUser user : userList) {
					if (StringUtils.equals(supplier.getId(), expertEvaluationResult.getSupplierId()) && StringUtils.equals(user.getId(), expertEvaluationResult.getUserId())) {
						expertEvaluationResultMap.put(supplier.getId() + "-" + user.getId(), expertEvaluationResult);
					}
				}
			}
			supplierRankVoteMap.put(supplier.getId(), supplierExpertVoteMap);
			
			SupplierEvaluationResult supplierEvaluationResult = supplier.getSupplierEvaluationResult();
			if(null == supplierEvaluationResult){
				supplierEvaluationResult = new SupplierEvaluationResult();
				supplierEvaluationResult.setTenderId(tenderProjectEntity.getId());
				supplierEvaluationResult.setSupplierId(supplier.getId());
				supplier.setSupplierEvaluationResult(supplierEvaluationResult);
			}
			if (supplierRankMap.containsKey(supplier.getId())) {
				int supplierRank = supplierRankMap.get(supplier.getId());
				supplierEvaluationResult.setSortNo(supplierRank);
				if (supplierRank > 0 && supplierRank <= 3) {
					supplierEvaluationResult.setRecommend(SupplierEnum.RECOMMONED_YES);
				}
			}
		}
		Collections.sort(supplierList, new Comparator<Supplier>() {
			public int compare(Supplier behindSupplier, Supplier frontSupplier) {
				if (supplierRankMap.containsKey(frontSupplier.getId()) && supplierRankMap.containsKey(behindSupplier.getId())) {
					int frontSortNo = supplierRankMap.get(frontSupplier.getId());
					int behindSortNo = supplierRankMap.get(behindSupplier.getId());
					return behindSortNo - frontSortNo;
				}
				return 0;
			}
		});
		
		if(tenderProjectRuleEntity.getRecommendCountNotNull() < 1){
			tenderProjectRuleEntity.setRecommendCount(1);
		}
		drawEvaluationTable();
		defaultEvalOpinionCombo.add("   ", 0);
		defaultEvalOpinionCombo.add(Messages.Template + 1, 1);
		defaultEvalOpinionCombo.add(Messages.Template + 2, 2);
		if(StringUtils.isEmpty(tenderProjectEntity.getEvaluationOpinion())){
			defaultEvalOpinionCombo.select(1);
			evaluationOpinionCreater();
		}else {
			defaultEvalOpinionCombo.select(0);
			evaluationOpinion_text.setText(tenderProjectEntity.getEvaluationOpinion());
		}
	}
	
	/**
	 * @Description: 实现评审表格
	 * @author chenxw 
	 */
	private void drawEvaluationTable(){
		
		for (Control control : recommonedContentComposite.getChildren()) {
			control.dispose();
		}
		
		final int expertColCount = userList.size() + 1;
		final int colCount = (expertColCount) * recommendRankCount + 1;
		
		KTable resultKTable = new KTable(recommonedContentComposite, SWTX.AUTO_SCROLL | SWT.FULL_SELECTION | SWTX.EDIT_ON_KEY);
		resultKTable.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
		final CommonKtableCModel commonKtableCModel = new CommonKtableCModel(resultKTable, 2, 1, supplierList.size(), colCount + 1);
		// 设置列宽
		commonKtableCModel.setColumnMaxWidth(0, 200);
		commonKtableCModel.setColumnMinWidth(0, 150);
		
		commonKtableCModel.addRowSpan(0, 0, 2);
		commonKtableCModel.addRowSpan(0, colCount, 2);
		commonKtableCModel.addRowSpan(0, colCount + 1, 2);
		
		for (int j = 0; j < supplierList.size(); j++) {
			commonKtableCModel.setRowHeight(j, Constants.ROW_HIGHT);
		}
		CellContentRender contentRender = new CellContentRender() {
			public Object receiveContent(int row, int col) {
				//当前连属于第几名
				int recommendCountIndex = ((col -1) / expertColCount) + 1;
				//专家列表索引
				int expertIndex = (col % expertColCount) - 1;
				if (row == 0) {
					if (col == 0) {
						return supplierName;
					} else if (expertIndex == 0 && recommendCountIndex <= recommendRankCount) {
						commonKtableCModel.addColSpan(0, col, expertColCount);
						return "第" + NumericChineseUtils.getChinese(recommendCountIndex) + "名";
					} else if (col == colCount) {
						return "最终排名";
					} else if (col == colCount + 1) {
						return "推荐";
					}
				} else if (row == 1) {
					if (col > 0 && recommendCountIndex <= recommendRankCount) {
						if (expertIndex == -1) {
							return "总票数";
						} else {
							AuthUser expert = userList.get(expertIndex);
							if (null != expert) {
								return expert.getUserName();
							}
						}
					}
				} else {
					Supplier supplier = supplierList.get(row - commonKtableCModel.getFixedHeaderRowCount());
					if (null != supplier) {
						if (col == 0) {
							return supplier.getSupplierName();
						} else if (recommendCountIndex <= recommendRankCount) {
							if (expertIndex == -1) {
								if (supplierRankVoteMap.containsKey(supplier.getId())) {
									Map<Integer, Integer> map = supplierRankVoteMap.get(supplier.getId());
									if (map.containsKey(recommendCountIndex)) {
										return map.get(recommendCountIndex);
									}
								}
							}
						} else if (col == colCount) {
							if (supplierRankMap.containsKey(supplier.getId())) {
								return supplierRankMap.get(supplier.getId());
							}
						} else if (col == colCount + 1) {
							SupplierEvaluationResult supplierEvaluationResult = supplier.getSupplierEvaluationResult();
							if(null != supplierEvaluationResult.getRecommend() && supplierEvaluationResult.getRecommend().equals(SupplierEnum.RECOMMONED_YES)){
								return true;
							}else {
								return false;
							}
						}
					}
				}
				return "";
			}
		};
		commonKtableCModel.setCellContentRender(contentRender);
		
		CellImageRender imageRender = new CellImageRender() {
			public Image receiveCellImage(int row, int col) {
				//当前连属于第几名
				int recommendCountIndex = ((col -1) / expertColCount) + 1;
				//专家列表索引
				int expertIndex = (col % expertColCount) - 1;
				if (row >= commonKtableCModel.getFixedHeaderRowCount()) {
					if (expertIndex != -1) {
						Supplier supplier = supplierList.get(row - commonKtableCModel.getFixedHeaderRowCount());
						AuthUser expert = userList.get(expertIndex);
						if (null != supplier && null != expert) {
							String key = supplier.getId() + "-" + expert.getId();
							if (expertEvaluationResultMap.containsKey(key)) {
								int sortNo = expertEvaluationResultMap.get(key).getSortNoIntValue();
								if (sortNo == recommendCountIndex) {
									return ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, "icons/flag_green.png");
								}
							}
						}
					}
				}
				return null;
			}
		};
		commonKtableCModel.setCellImageRender(imageRender);
		
		// 编辑单元格内容
		CellEditContentRender editContentRender = new CellEditContentRender(){
			public void receiveContent(int row, int col, Object content) {
				if(row >= commonKtableCModel.getFixedRowCount() && col == commonKtableCModel.getColumnCount() -1){
					if(null == content)return;
					Supplier supplier = supplierList.get(row - commonKtableCModel.getFixedHeaderRowCount());
					SupplierEvaluationResult supplierEvaluationResult = supplier.getSupplierEvaluationResult();
					if(content.toString().equals("true")){

						//资审项目不做推荐限制
						if(tenderProjectRuleEntity.getRecommendCountNotNull() > 0 && (null == tenderProjectEntity.getPhaseType() || !tenderProjectEntity.getPhaseType().equals(TenderProjectEnum.PHASE_TYPE_PRE))){
							int recommonedCount = 0;
							for(Supplier _supplier : supplierList){
								if(null != _supplier.getSupplierEvaluationResult().getRecommend() && _supplier.getSupplierEvaluationResult().getRecommend().equals(SupplierEnum.RECOMMONED_YES)){
									recommonedCount ++;
									if(recommonedCount > tenderProjectRuleEntity.getRecommendCountNotNull()){
										break;
									}
								}
							}
							if(recommonedCount + 1 > tenderProjectRuleEntity.getRecommendCountNotNull()){
								if(!isShowError){
									isShowError = true;
									MessageDialog.openError(getShell(), "最多只能推荐" + tenderProjectRuleEntity.getRecommendCount() +"位中标候选人！");
									isShowError = false;
								}
								return;
							}
						}
					
						supplierEvaluationResult.setRecommend(SupplierEnum.RECOMMONED_YES);
					}else {
						supplierEvaluationResult.setRecommend(SupplierEnum.RECOMMONED_NO);
					}
					evaluationOpinionCreater();
				}
			}
		};
		commonKtableCModel.setCellEditContentRender(editContentRender);
		
		// 设置单元格编辑器
        final KTableCellEditorText text = new KTableCellEditorText();
		text.setSingleClickEdit(true);
		CellEditorRender cellEditorRender = new CellEditorRender(){
			public Object receiveCellEditor(int row, int col) {
				if(row >= commonKtableCModel.getFixedRowCount() && col == commonKtableCModel.getColumnCount() -1){
					return new KTableCellEditorCheckbox2();
				}else {
					return null;
				}
			}
		};
		commonKtableCModel.setCellEditorRender(cellEditorRender);
		
		// 设置单元格渲染器
		CellRender cellRender = new CellRender(){
			public Object receiveCellRender(int row, int col) {
				if(commonKtableCModel.isFixedCell(col, row)){
					return new FixedWrapCellRenderer();
				}else if(row >= commonKtableCModel.getFixedRowCount() && col == commonKtableCModel.getColumnCount() -1){
					return new CheckableCellRenderer();
				}else {
					return null;
				}
			}
		};
		commonKtableCModel.setCellRender(cellRender);
		
		// 编辑单元格对齐方式
		CellAlignmentRender cellAlignmentRender = new CellAlignmentRender() {
			public int receiveCellAlignment(int row, int col) {
				if (row >= commonKtableCModel.getFixedHeaderColumnCount() && col == 0) {
					return SWTX.ALIGN_VERTICAL_CENTER | SWTX.ALIGN_HORIZONTAL_LEFT;
				}
				return SWTX.ALIGN_VERTICAL_CENTER | SWTX.ALIGN_HORIZONTAL_CENTER;
			}
		};
		commonKtableCModel.setCellAlignmentRender(cellAlignmentRender);
		
		resultKTable.setModel(commonKtableCModel);
	}
	
	/**
	 * @Description: 生成评审意见
	 * @author guoyr
	 */
	private void evaluationOpinionCreater(){
		int recommendCount = 1;
		StringBuffer supplierNameSb = new StringBuffer("");
		for(Supplier supplier : supplierList){
			SupplierEvaluationResult supplierEvaluationResult = supplier.getSupplierEvaluationResult();
			if(null != supplierEvaluationResult.getRecommend() && supplierEvaluationResult.getRecommend().equals(SupplierEnum.RECOMMONED_YES)){
				if(defaultEvalOpinionCombo.getSelectionIndex() == 1){
					if(recommendCount == 1){
						supplierNameSb.append("    经评标委员会评审，推荐中标候选人如下：\r\n");
					}
					supplierNameSb.append("    第");
					supplierNameSb.append(NumericChineseUtils.getChinese(recommendCount));
					supplierNameSb.append("中标候选人：");
					supplierNameSb.append(supplier.getSupplierName());
					supplierNameSb.append("，");
					
					for (int i = 1; i <= recommendRankCount; i++) {
						if (supplierRankVoteMap.containsKey(supplier.getId())) {
							Map<Integer, Integer> map = supplierRankVoteMap.get(supplier.getId());
							if (map.containsKey(i)) {
								supplierNameSb.append("    第");
								supplierNameSb.append(NumericChineseUtils.getChinese(i));
								supplierNameSb.append("名得票数为：");
								supplierNameSb.append(map.get(i));
								supplierNameSb.append("，");
							} else {
								supplierNameSb.append("    第");
								supplierNameSb.append(NumericChineseUtils.getChinese(i));
								supplierNameSb.append("名得票数为：0");
								supplierNameSb.append("，");
							}
						}
					}
					
					supplierNameSb.append("排名第");
					supplierNameSb.append(NumericChineseUtils.getChinese(supplierEvaluationResult.getSortNo()));
					supplierNameSb.append("；\r\n");
					
				}else if(defaultEvalOpinionCombo.getSelectionIndex() == 2){
					if(recommendCount == 1){
						supplierNameSb.append("    经评标委员会评审一致推荐");
					}
					supplierNameSb.append(supplier.getSupplierName());
					supplierNameSb.append("为第");
					supplierNameSb.append(NumericChineseUtils.getChinese(recommendCount));
					supplierNameSb.append("中标候选人，");

					
					
					supplierNameSb.append("排名第");
					supplierNameSb.append(NumericChineseUtils.getChinese(supplierEvaluationResult.getSortNo()));
					supplierNameSb.append("；\r\n");
				}
				recommendCount ++;
			}
		}
		supplierNameSb.append("共计推荐");
		supplierNameSb.append(recommendCount -1);
		supplierNameSb.append("名中标候选人。");
		if(defaultEvalOpinionCombo.getSelectionIndex() > 0 && recommendCount > 0){
			evaluationOpinion_text.setText(supplierNameSb.toString());
		}else {
			evaluationOpinion_text.setText(ObjectUtils.getObjString(tenderProjectEntity.getEvaluationOpinion()));
		}
	}
	
	public String getErrorMessage() {
		return errorMessage;
	}

	public void setErrorMessage(String errorMessage) {
		this.errorMessage = errorMessage;
	}
	/**
	 * @Description: 提交推荐结果
	 * @author guoyr
	 * @return 
	 */
	public boolean submitRecommonedEvaluationResult(){
		List<SupplierEvaluationResult> supplierEvaluationResultList = new ArrayList<SupplierEvaluationResult>();
		int recommonedCount = 0;
		for(Supplier supplier : supplierList){
			SupplierEvaluationResult supplierEvaluationResult = supplier.getSupplierEvaluationResult();
			if(null != supplierEvaluationResult.getRecommend() && supplierEvaluationResult.getRecommend().equals(SupplierEnum.RECOMMONED_YES)){
				recommonedCount ++;
			}
			supplierEvaluationResultList.add(supplierEvaluationResult);
		}
		if(recommonedCount == 0){
			MessageDialog.openInformation(getShell(), "请至少推荐一名中标候选人！");
			return false;
		}
		tenderProjectEntity.setEvaluationOpinion(evaluationOpinion_text.getText());
		if(!expertEvaluationStepService.submitRecommonedEvaluationResult(tenderProjectEntity.getId(), CURRENT_USER.getId(), evaluationStep, evaluationOpinion_text.getText(), supplierEvaluationResultList)){
			MessageDialog.openInformation(getShell(), Messages.Dialog_warmTips, Messages.Tips_saveEvalResultFailed);
			return false;
		}
		return true;
	}
}
