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

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

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.eclipse.swt.core.CallBack;
import org.eclipse.swt.core.utils.MessageCallBack;

import com.gx.obe.business.entity.SupplierEvaluationFactor;
import com.gx.obe.common.widget.thread.MyRunnable;
import com.gx.obe.util.utils.CollectionUtils;
import com.gx.obe.web.entity.Supplier;
import com.gx.obe.web.entity.evaluation.EvaluationFactor;
import com.gx.obe.web.service.SupplierEvaluationFactorService;
import com.gx.obe.web.service.SupplierService;
import com.gx.obe.web.service.evaluation.EvaluationFactorService;

/**
 * @Description:导入供应商指标意见
 * @author chenxw
 */
public class ImportSupplierFactorOpinionThread extends MyRunnable {
	
    private SupplierService supplierService = new SupplierService();
	private EvaluationFactorService evaluationFactorService = new EvaluationFactorService();
	private SupplierEvaluationFactorService supplierEvaluationFactorService = new SupplierEvaluationFactorService();
	
	private final String excelFilePath;
	private final MessageCallBack callBack;
	private final String tenderId;
	private List<Supplier> supplierList;
	private List<EvaluationFactor> factorList;
	
	public ImportSupplierFactorOpinionThread(String tenderId, String excelFilePath, CallBack callBack) {
		this.tenderId = tenderId;
		this.excelFilePath = excelFilePath;
		this.callBack = new MessageCallBack(callBack);
	}
	
	public void run() {
		XSSFWorkbook xssfWorkbook = null;
		try {
			xssfWorkbook = new XSSFWorkbook(excelFilePath);
		} catch (Exception e) {}
		
		if (null == xssfWorkbook) {
			callBack.callBackMsg(false, "文件格式异常，请检查所选模板是否有误！");
			callBack.after();
			return;
		}
		
		int sheetIndex = 0;
		int titleSupplierIndex = 5;
		int titleFactorIndex = 1;
		
		callBack.callBackMsg("开始导入清标评议意见!");
		if (CollectionUtils.isNull(supplierList)) supplierList = supplierService.getOpenBidSuccessByParentId(tenderId);
		if (CollectionUtils.isNull(factorList)) {
			factorList = evaluationFactorService.getFactorListByTenderId(tenderId);
		}
		
		Map<String, String> supplierIdMap = getSupplierIdMap(supplierList);
		Map<String, String> factorCodeMap = getFactorCodeMap(factorList);
		Map<String, Map<String, SupplierEvaluationFactor>> OpinionSupplierIdMapFactorCodeMap = getOpinionSupplierIdMapFactorCodeMap();
		
		Sheet sheet = xssfWorkbook.getSheetAt(sheetIndex);
		
		Row supplierRow = sheet.getRow(titleSupplierIndex);
		
		List<SupplierEvaluationFactor> opinionList = new ArrayList<SupplierEvaluationFactor>();
		for (int rowIndex = titleSupplierIndex + 1; rowIndex < sheet.getLastRowNum() + 1; rowIndex++) {
			Row row = sheet.getRow(rowIndex);
			if (null == row) {
				continue;
			}
			
			Cell factorCell = row.getCell(titleFactorIndex);
			
			String factorName = getStringCellValue(factorCell);
			
			String factorCode = factorCodeMap.get(factorName);
			if (null == factorCode) {
				continue;
			}
			
			Map<String, SupplierEvaluationFactor> OpinionSupplierIdMap = OpinionSupplierIdMapFactorCodeMap.get(factorCode);
			if (null == OpinionSupplierIdMap) {
				OpinionSupplierIdMap = new HashMap<String, SupplierEvaluationFactor>();
				OpinionSupplierIdMapFactorCodeMap.put(factorCode, OpinionSupplierIdMap);
			}
			for (int cellIndex = titleFactorIndex + 1; cellIndex < row.getLastCellNum(); cellIndex++) {
				
				Cell supplierCell = supplierRow.getCell(cellIndex);
				
				String supplierName = getStringCellValue(supplierCell);
				
				String supplierId = supplierIdMap.get(supplierName);
				if (null == supplierId) {
					continue;
				}
				SupplierEvaluationFactor opinion = OpinionSupplierIdMap.get(supplierId);
				if (null == opinion) {
					opinion = new SupplierEvaluationFactor();
					opinion.setTenderId(tenderId);
					opinion.setSupplierId(supplierId);
					opinion.setFactorCode(factorCode);
					OpinionSupplierIdMap.put(supplierId, opinion);
				}
				
				Cell cell = row.getCell(cellIndex);
				
				String cellValue = getStringCellValue(cell);
				if (null != cellValue && cellValue.trim().length() > 0) {
					if (cellValue.length() > 2000) {
						cellValue = cellValue.substring(0, 2000);
					}
					opinion.setOpinion(cellValue);
					opinionList.add(opinion);
				}
			}
		}
		if (CollectionUtils.isNull(opinionList)) {
			callBack.callBackMsg(false, "未检测到可导入的评议意见，请检查所选模板是否有误！");
		} else {
			int sccessCount = supplierEvaluationFactorService.batchSaveOrUpdate(opinionList, new String[] { "opinion" });
			if (sccessCount > 0) {
				callBack.callBackMsg(true, "导入成功！");
			} else {
				callBack.callBackMsg(false, "导入失败！");
			}
		}
		
		callBack.after();
	}
	
	/**
	 * @Description: 获取供应商IdMap
	 * @author chenxw
	 * @param supplierList
	 * @return
	 */
	private Map<String, String> getSupplierIdMap(List<Supplier> supplierList) {
		Map<String, String> supplierIdMap = new HashMap<String, String>();
		if (CollectionUtils.isNotNull(supplierList)) {
			for (Supplier supplier : supplierList) {
				supplierIdMap.put(supplier.getSupplierName(), supplier.getId());
			}
		}
		return supplierIdMap;
	}
	
	/**
	 * @Description: 获取指标IdMap
	 * @author chenxw
	 * @param factorList
	 * @return
	 */
	private Map<String, String> getFactorCodeMap(List<EvaluationFactor> factorList) {
		Map<String, String> factorCodeMap = new HashMap<String, String>();
		if (CollectionUtils.isNotNull(factorList)) {
			for (EvaluationFactor evaluationFactor : factorList) {
				factorCodeMap.put(evaluationFactor.getFactorName(), evaluationFactor.getFactorCode());
			}
		}
		return factorCodeMap;
	}
	
	/**
	 * @Description: 获取评议意见
	 * @author chenxw
	 * @return
	 */
	private Map<String, Map<String, SupplierEvaluationFactor>> getOpinionSupplierIdMapFactorCodeMap() {
		List<SupplierEvaluationFactor> evalFactorResultList = supplierEvaluationFactorService.getEntityListByTenderId(tenderId);
		if (CollectionUtils.isNull(evalFactorResultList)) {
			return new HashMap<String, Map<String, SupplierEvaluationFactor>>();
		}
		Map<String, Map<String, SupplierEvaluationFactor>> opinionSupplierIdMapFactorCodeMap = new HashMap<String, Map<String, SupplierEvaluationFactor>>();
		for (SupplierEvaluationFactor supplierEvaluationFactor : evalFactorResultList) {
			String factorCode = supplierEvaluationFactor.getFactorCode();
			String supplierId = supplierEvaluationFactor.getSupplierId();
			if (opinionSupplierIdMapFactorCodeMap.containsKey(factorCode)) {
				opinionSupplierIdMapFactorCodeMap.get(factorCode).put(supplierId, supplierEvaluationFactor);
			} else {
				Map<String, SupplierEvaluationFactor> opinionSupplierIdMap = new HashMap<String, SupplierEvaluationFactor>();
				opinionSupplierIdMap.put(supplierId, supplierEvaluationFactor);
				opinionSupplierIdMapFactorCodeMap.put(factorCode, opinionSupplierIdMap);
			}
		}
		return opinionSupplierIdMapFactorCodeMap;
	}
	
	/**
	 * @Description: 获取字符串数据
	 * @author chenxw
	 * @param cell
	 * @return
	 */
	private String getStringCellValue(Cell cell) {
		
		if (null == cell) {
			return null;
		}
		switch (cell.getCellType()) {
			case HSSFCell.CELL_TYPE_STRING:
				return cell.getStringCellValue();
			case HSSFCell.CELL_TYPE_FORMULA:
				return cell.getCellFormula();
			case HSSFCell.CELL_TYPE_NUMERIC:
				return Double.toString(cell.getNumericCellValue());
			case HSSFCell.CELL_TYPE_BOOLEAN:
				return Boolean.toString(cell.getBooleanCellValue());
			case HSSFCell.CELL_TYPE_BLANK:
				return "";
			case HSSFCell.CELL_TYPE_ERROR:
				return "非法字符";
			default:
				return "未知类型";
		}
	}
	
	public void setSupplierList(List<Supplier> supplierList) {
		this.supplierList = supplierList;
	}
	
	public void setEvaluationFactorList(List<EvaluationFactor> factorList) {
		this.factorList = factorList;
	}
	
}
