package com.ssj.sys.utils;

import com.ssj.bean.sys.fx.domain.Device;
import com.ssj.bean.sys.fx.domain.Merchant;
import com.ssj.framework.basic.utils.DateHelper;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFDateUtil;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.*;


public class ExcelUtils {
	
    private   static Logger logger = LoggerFactory.getLogger(ExcelUtils.class);

  	public static List<Device> readDeviceFile(File file) throws Exception {
  		List<Device> devices=new ArrayList<Device>();
  		String batchCode=DateHelper.format(new Date(), "yyyyMMddHHmm");
  		InputStream is = new FileInputStream(file);
  		@SuppressWarnings("resource")
  		Workbook wb = new XSSFWorkbook(is);
  		Sheet sheet = wb.getSheetAt(0);
  		int rowIndex = 0;
  		Row row = null;
  		for (Iterator<Row> it = sheet.rowIterator(); it.hasNext();) {
  			row = it.next();
  			if (rowIndex++ == 0) {
  				continue;
  			}
  			if (row == null) {
  				break;
  			}
  			//以前工厂发货会用imei 现在没有了
//  			if(StringUtils.isEmpty(gettxt(row.getCell(0)))){
//  				continue;
//  			}
  			
  			Device device=new Device();
  			device.setCreateTime(new Date());
  			device.setBatchCode(batchCode);
  			device.setImei(gettxt(row.getCell(0)));
  			device.setDeviceName(gettxt(row.getCell(1)));
  			device.setDeviceColor(gettxt(row.getCell(2)));
  			device.setDeviceRealNumber(gettxt(row.getCell(3)));
  			device.setDeviceNumber(gettxt(row.getCell(4)));
  			device.setDeviceModel(gettxt(row.getCell(5)));
  			device.setDeviceCode(gettxt(row.getCell(6)));
  			device.setDeviceSn(gettxt(row.getCell(7)));
  			device.setPosCode(gettxt(row.getCell(8)));
  			device.setRemarks(gettxt(row.getCell(9)));
  			
  			device.setEmsNumber(gettxt(row.getCell(10)));
  			device.setEmsAddress(gettxt(row.getCell(11)));
  			device.setEmsUsername(gettxt(row.getCell(12)));
  			device.setEmsTelephone(gettxt(row.getCell(13)));
  			
			device.setIsSpan(1);
  			devices.add(device);
  		}
  		return devices;
  	}
    
    
    
    
	public static Map<String,Object> readMerchantFile(File file) throws Exception {
		Map<String,Object> reqMap=new HashMap<String, Object>();
		Map<String,String> password=new HashMap<String, String>();
		List<Merchant> merchants=new ArrayList<Merchant>();
		
		InputStream is = new FileInputStream(file);
		@SuppressWarnings("resource")
		Workbook wb = new XSSFWorkbook(is);
		Sheet sheet = wb.getSheetAt(0);
		Row title = sheet.getRow(0);
		String[] titles = new String[title.getPhysicalNumberOfCells()];
		int columnCount = titles.length;
		int rowIndex = 0;
		Row row = null;
		for (Iterator<Row> it = sheet.rowIterator(); it.hasNext();) {
			row = it.next();
			if (rowIndex++ == 0) {
				continue;
			}
			if (row == null) {
				break;
			}
			for (int i = 0; i < columnCount; i++) {
				logger.info(gettxt(row.getCell(i)));
			}
			if(StringUtils.isEmpty(gettxt(row.getCell(0)))){
  				continue;
  			}
			
			Merchant merchant=new Merchant();
			merchant.setCreateTime(new Date());
			merchant.setPosCode(gettxt(row.getCell(0)));
			merchant.setParentPosCode(gettxt(row.getCell(1)));
			password.put(merchant.getPosCode(), "111111");
			merchant.setUnitName(gettxt(row.getCell(2)));
			//merchant.setType(1);
			merchant.setLevel("一级代理".equals(gettxt(row.getCell(3)))?1:2);	
			merchant.setLegalPerson(gettxt(row.getCell(4)));
			merchant.setPhone(gettxt(row.getCell(5)));
			merchant.setDeputyName(gettxt(row.getCell(6)));
			merchant.setDeputyPhone(gettxt(row.getCell(7)));
			String sstr=gettxt(row.getCell(8));
			if("待签".equals(sstr)){
				merchant.setStatus(1);
			}else if("已签待交付".equals(sstr)){
				merchant.setStatus(2);
			}else if("已签已交付".equals(sstr)){
				merchant.setStatus(3);
			}else{
				merchant.setStatus(4);
			} 
			merchant.setSignTime(
					StringUtils.isNotEmpty(gettxt(row.getCell(9)))?
							DateHelper.parseDate(gettxt(row.getCell(9)), "yyyy-MM-dd"):null);
			merchant.setRemarks(gettxt(row.getCell(17)));
			merchants.add(merchant);
		}
		reqMap.put("merchants",merchants);
		reqMap.put("password",password);
		return reqMap;
	}
	
	
	public static String gettxt(Cell cell){
		if(cell==null){
			return "";
		}
		Object o=null;
		switch (cell.getCellType()) {
		case XSSFCell.CELL_TYPE_BOOLEAN:
			o = cell.getBooleanCellValue();
			break;
		case XSSFCell.CELL_TYPE_NUMERIC:
			o = cell.getNumericCellValue();
			if (HSSFDateUtil.isCellDateFormatted(cell)) {
				o = DateUtil.getJavaDate(cell.getNumericCellValue());
			}
			break;
		case XSSFCell.CELL_TYPE_STRING:
			o = cell.getStringCellValue();
			break;
		case XSSFCell.CELL_TYPE_ERROR:
			o = cell.getErrorCellValue();
			break;
		case XSSFCell.CELL_TYPE_BLANK:
			o = null;
			break;
		case XSSFCell.CELL_TYPE_FORMULA:
			o = cell.getCellFormula();
			break;
		default:
			o = null;
			break;
		}
		String reqStr="";
		if(o!=null){
			reqStr=o.toString();
		}
		if(reqStr.endsWith(".0")){
			reqStr=reqStr.substring(0,reqStr.length()-2);
		}	
		return reqStr;
	}
	
	

}
