package cn.com.countrygarden.bi.main.service.dup.impl;

import java.io.BufferedInputStream;
import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import com.github.pagehelper.PageRowBounds;

import cn.com.countrygarden.bi.main.dao.dup.ParkingSpacesTransferDetailMapper;
import cn.com.countrygarden.bi.main.model.PageData;
import cn.com.countrygarden.bi.main.model.dup.ParkingSpacesTransferDetail;
import cn.com.countrygarden.bi.main.model.dup.ParkingSpacesTransferReportsDto;
import cn.com.countrygarden.bi.main.model.dup.SiteResources;
import cn.com.countrygarden.bi.main.service.dup.ContractStatisticalService;
import cn.com.countrygarden.bi.main.service.dup.ParkingSpacesTransferDetailService;

/**
 * <pre>
 * 。车位移交数据ServiceImpl
 * </pre>
 * 
 * @author developer developer@meicloud.com
 * @version 1.00.00
 * 
 *          <pre>
 * 修改记录
 *    修改后版本:     修改人：  修改日期:     修改内容:
 *          </pre>
 */
@Service
@Transactional
public class ParkingSpacesTransferDetailServiceImpl extends ExcelHelper implements ParkingSpacesTransferDetailService {

	@Resource
	private ParkingSpacesTransferDetailMapper parkingSpacesTransferDetailMapper;

	@Resource
	private ContractStatisticalService contractStatisticalService;

	/**
	 * 车位移交明细--模糊查询
	 */
	@Override
	public PageData findParkingSpacesTransferDetailLike(ParkingSpacesTransferDetail parkingSpacesTransferDetail) {
		PageRowBounds pageRowBounds = new PageRowBounds(parkingSpacesTransferDetail.getPageIndex(),
				parkingSpacesTransferDetail.getPageSize());
		List<SiteResources> list = parkingSpacesTransferDetailMapper
				.findParkingSpacesTransferDetailLike(parkingSpacesTransferDetail, pageRowBounds);
		PageData PageData = new PageData(list, pageRowBounds);
		return PageData;
	}

	/**
	 * 车位移交明细-修改
	 */
	@Override
	public boolean updateParkingSpacesTransferDetail(ParkingSpacesTransferDetail parkingSpacesTransferDetail) {
		return parkingSpacesTransferDetailMapper.updateParkingSpacesTransferDetail(parkingSpacesTransferDetail) == 1;
	}

	/**
	 * 车位移交明细-删除
	 */
	@Override
	public boolean deleteParkingSpacesTransferDetail(ParkingSpacesTransferDetail parkingSpacesTransferDetail) {
		return parkingSpacesTransferDetailMapper.deleteParkingSpacesTransferDetail(parkingSpacesTransferDetail) == 1;
	}

	/**
	 * 车位移交明细-新增
	 * 
	 * @param parkingSpacesTransferDetail
	 * @return
	 */
	public boolean addParkingSpacesTransferDetail(ParkingSpacesTransferDetail parkingSpacesTransferDetail) {
		return parkingSpacesTransferDetailMapper.addParkingSpacesTransferDetail(parkingSpacesTransferDetail) == 1;
	}

	/**
	 * 车位移交明细导入
	 */
	@Override
	public void excelToObject(MultipartFile multipartFile, String fileName, String userCode) {
		Workbook xwb = null;
		BufferedInputStream bf = null;
		Sheet sheet = null;
		try {
			bf = new BufferedInputStream(multipartFile.getInputStream());

			if (fileName.endsWith("xls")) { // Excel&nbsp;2003
				xwb = new HSSFWorkbook(bf);
			} else if (fileName.endsWith("xlsx")) { // Excel 2007/2010
				xwb = new XSSFWorkbook(bf);
			}
			sheet = xwb.getSheetAt(0);// 获取第几个页面0开始
			for (int i = 1; i <= sheet.getPhysicalNumberOfRows(); i++) {
				Row row = sheet.getRow(i);
				// 怎么判断是否为结束的字段.(是否为合并单元格;
				if (row == null)
					break;
				if (row.getCell(0) == null)
					try {
						throw new Exception("第" + i + "行，区域Id为空。");
					} catch (Exception e) {
					}
				ParkingSpacesTransferDetail parkingSpacesTransferDetail = new ParkingSpacesTransferDetail();
				// 获取数据
//				Map<String,Object> map=new HashMap<String,Object>();
//				//区域名
//				map.put("OrganName", row.getCell(1).getStringCellValue());
//				map.put("CommName", row.getCell(2).getStringCellValue());
				//根据项目与区域名称获取对应的项目id与区域Code
//				Map<String,Object> maps=contractStatisticalService.findCommidAndOrganCode(map);
//				if(maps==null) {
//					//如果无法获取对应的项目id与区域code,则跳出这次添加
//					continue;
//				}
//				parkingSpacesTransferDetail.setCommId((int)maps.get("CommID"));
//				parkingSpacesTransferDetail.setOrganCode((String)maps.get("OrganCode"));
				parkingSpacesTransferDetail.setOrganName(row.getCell(1).getStringCellValue());
				parkingSpacesTransferDetail.setCommName(row.getCell(2).getStringCellValue());
				parkingSpacesTransferDetail.setParkName(row.getCell(3).getStringCellValue());
				parkingSpacesTransferDetail.setContractAddress(row.getCell(4).getStringCellValue());
				parkingSpacesTransferDetail.setRoomCode(row.getCell(5).getStringCellValue());
				parkingSpacesTransferDetail.setParkingSpacesAttributes(row.getCell(6).getStringCellValue());
				parkingSpacesTransferDetail.setPredictionArea(row.getCell(7).getColumnIndex());
				parkingSpacesTransferDetail.setMeasuredArea(row.getCell(8).getColumnIndex());
				parkingSpacesTransferDetail.setAdvanceDate(row.getCell(9).getDateCellValue());
				parkingSpacesTransferDetail.setCompletionDate(row.getCell(10).getDateCellValue());
				parkingSpacesTransferDetail.setContractRepossessionDate(row.getCell(11).getDateCellValue());
				parkingSpacesTransferDetail.setFixedPrice(row.getCell(12).getColumnIndex());
				parkingSpacesTransferDetail.setPreferentialPrice(row.getCell(13).getColumnIndex());
				parkingSpacesTransferDetail.setPreferentialAveragePrice(row.getCell(14).getColumnIndex());
				parkingSpacesTransferDetail.setAverageStartCost(row.getCell(15).getColumnIndex());
				parkingSpacesTransferDetail.setAverageEndCost(row.getCell(16).getColumnIndex());
				parkingSpacesTransferDetail.setTransferPrice(row.getCell(17).getColumnIndex());
				parkingSpacesTransferDetail.setSuiteModel(row.getCell(18).getStringCellValue());
				parkingSpacesTransferDetail.setPreferential(row.getCell(19).getStringCellValue());
				parkingSpacesTransferDetail.setPaymentMethod(row.getCell(20).getStringCellValue());
				parkingSpacesTransferDetail.setCreateDate(new Date());
				boolean a = this.addParkingSpacesTransferDetail(parkingSpacesTransferDetail);
				if (a != true) {
					try {
						throw new Exception("插入失败");
					} catch (Exception e) {
						e.printStackTrace();
					}
				}
			}
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
	}

	/**
	 * 创建车位转移明细数就的表头
	 */
	public void createParkingSpacesTransferDetailHeader(Workbook workbook, Sheet sheet) {
		int rowIndex = 0;
		CellStyle headerStyle = createHeaderStyle(workbook);
		Row row = sheet.createRow(rowIndex);
		row.setHeight((short) (250 * 2));
		createCell("序号", headerStyle, row, 0);
		createCell("物业区域", headerStyle, row, 1);
		createCell("项目名", headerStyle, row, 8);
		createCell("苑区", headerStyle, row, 2);
		createCell("合同路址", headerStyle, row, 3);
		createCell("房间代码", headerStyle, row, 4);
		createCell("车位属性", headerStyle, row, 5);
		createCell("预测面积", headerStyle, row, 6);
		createCell("实测面积", headerStyle, row, 7);
		createCell("预售证日期", headerStyle, row, 9);
		createCell("竣工日期", headerStyle, row, 10);
		createCell("苑区合同收楼日期", headerStyle, row, 11);
		createCell("定价", headerStyle, row, 12);
		createCell("优惠折后价", headerStyle, row, 13);
		createCell("优惠折后均价", headerStyle, row, 14);
		createCell("分摊前成本", headerStyle, row, 15);
		createCell("分摊后成本", headerStyle, row, 16);
		createCell("拟移交价格", headerStyle, row, 17);
		createCell("房型（标准、微型、子母车位等）", headerStyle, row, 18);
		createCell("优惠折扣（按序号排序使用折扣）", headerStyle, row, 19);
		createCell("付款方式（付款方式折扣比例）", headerStyle, row, 20);

	}

	/**
	 * 创建车位转移明细导出的表体
	 */
	public void createParkingSpacesTransferDetailBody(List<ParkingSpacesTransferDetail> dataList, Workbook workbook,
			Sheet sheet) {
		CellStyle style = createBodyStyle(workbook);
		int rowIndex = 1;
		int a = 1;
		for (ParkingSpacesTransferDetail data : dataList) {
			Row row = sheet.createRow(rowIndex);
			createCell(a + "", style, row, 0);
			a++;
			createCell(data.getOrganName(), style, row, 1);
			createCell(data.getCommName(), style, row, 2);
			createCell(data.getParkName(), style, row, 3);
			createCell(data.getContractAddress(), style, row, 4);
			createCell(data.getRoomCode(), style, row, 5);
			createCell(data.getParkingSpacesAttributes(), style, row, 6);
			createCell(data.getPredictionArea()+"", style, row, 7);
			createCell(data.getMeasuredArea()+"", style, row, 8);
			createCell(changeDate(data.getAdvanceDate()), style, row, 9);
			createCell(changeDate(data.getCompletionDate()), style, row, 10);
			createCell(changeDate(data.getContractRepossessionDate()), style, row, 11);
			createCell(data.getFixedPrice() + "", style, row, 12);
			createCell(data.getPreferentialPrice() + "", style, row, 13);
			createCell(data.getPreferentialAveragePrice() + "", style, row, 14);
			createCell(data.getAverageStartCost() + "", style, row, 15);
			createCell(data.getAverageEndCost() + "", style, row, 16);
			createCell(data.getTransferPrice() + "", style, row, 17);
			createCell(data.getSuiteModel(), style, row, 18);
			createCell(data.getPreferential(), style, row, 19);
			createCell(data.getPaymentMethod(), style, row, 20);

			rowIndex++;
		}
	}

	/**
	 * 日期转字符
	 * 
	 * @param date
	 * @return
	 */
	private String changeDate(Date date) {
		String dateStr = "";
		// format的格式可以任意
		DateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
		try {
			dateStr = sdf.format(date);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return dateStr;
	}
	
	/**
	 * 查询车位移交明细报表
	 */
	@Override
	public PageData ParkingSpacesTransferReports(
			ParkingSpacesTransferReportsDto parkingSpacesTransferReportsDto) {
		
		PageRowBounds pageRowBounds = new PageRowBounds(parkingSpacesTransferReportsDto.getPageIndex(),
				parkingSpacesTransferReportsDto.getPageSize());
		//查询基础数据
		List<ParkingSpacesTransferReportsDto>  list=parkingSpacesTransferDetailMapper.findParkingSpacesTransferReports(parkingSpacesTransferReportsDto,pageRowBounds);
		for (ParkingSpacesTransferReportsDto dto : list) {
			//通过房间代码获取对应的数据
			ParkingSpacesTransferReportsDto parkingSpacesTransferReports=parkingSpacesTransferDetailMapper.finParkingSpacesTransferReportsDto(dto);
			dto.setMeasuredArea(parkingSpacesTransferReports.getMeasuredArea());
			dto.setPredictionArea(parkingSpacesTransferReports.getPredictionArea());
			dto.setNumber(parkingSpacesTransferReports.getNumber());
			dto.setPreferentialPriceTotal(parkingSpacesTransferReports.getPreferentialPriceTotal());
			dto.setDifferenceOfPrices(parkingSpacesTransferReports.getDifferenceOfPrices());
		}
		PageData PageData = new PageData(list, pageRowBounds);
		
		return PageData;
	}

}
