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

import java.io.BufferedInputStream;
import java.io.IOException;
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.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.SellDataManagementMapper;
import cn.com.countrygarden.bi.main.model.PageData;
import cn.com.countrygarden.bi.main.model.dup.SellDataManagement;
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.SellDataManagementService;

/**
 * <pre>
 * 。营销数据管理ServiceImpl
 * </pre>
 * 
 * @author developer developer@meicloud.com
 * @version 1.00.00
 * 
 *          <pre>
 * 修改记录
 *    修改后版本:     修改人：  修改日期:     修改内容:
 * </pre>
 */
@Service
@Transactional
public class SellDataManagementServiceImpl  implements SellDataManagementService{
	
	@Resource
	private SellDataManagementMapper sellDataManagementMapper;
	
	@Resource
	private ContractStatisticalService contractStatisticalService;
	/**
	 * 模糊查询营销数据管理
	 */
	@Override
	public PageData findAllSellDataManagementControllerLike(SellDataManagement sellDataManagement) {
		//带条件查询有多少条数据
		PageRowBounds pageRowBounds = new PageRowBounds(sellDataManagement.getPageIndex(), sellDataManagement.getPageSize());
		List<SellDataManagement> list = sellDataManagementMapper.findAllSiteResourcesControllerLike(sellDataManagement, pageRowBounds);
		//判断是否为尾页
		//查询一共有多少条数据
//		SellDataManagement dto=sellDataManagementMapper.findtotalNumber(sellDataManagement);
//		list.add(dto);
		PageData PageData = new PageData(list, pageRowBounds);
		return PageData;
	}
	/**
	 * 更新营销数据
	 */
	@Override
	public boolean updateSellDataManagement(SellDataManagement sellDataManagement) {
		return  sellDataManagementMapper.updateSellDataManagement(sellDataManagement)==1;
	}
	/**
	 * 删除营销数据
	 */
	@Override
	public boolean deleteSellDataManagement(SellDataManagement sellDataManagement) {
		return sellDataManagementMapper.deleteSellDataManagement(sellDataManagement)==1;
	}
	
	/**
	 * 新增营销数据
	 */
	@Override
	public boolean addSellDataManagement(SellDataManagement sellDataManagement) {
		return sellDataManagementMapper.addSellDataManagement(sellDataManagement)==1;
	}
	
	/**
	 * 导入营销数据管理
	 */
	@Override
	public void excelToObject(MultipartFile mf, String fileName, String userCode) {
		Workbook xwb = null;
		BufferedInputStream bf = null;
		Sheet sheet = null;
		try {
			bf = new BufferedInputStream(mf.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) {
			}
			SellDataManagement sellDataManagement = new SellDataManagement();
			// 获取数据
//			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;
//			}
			//区域
			sellDataManagement.setOrganName(row.getCell(1).getStringCellValue());
//			sellDataManagement.setCommId((int)maps.get("CommID"));
//			sellDataManagement.setOrganCode((String)maps.get("OrganCode"));
			//项目
			sellDataManagement.setCommName(row.getCell(2).getStringCellValue());
			//楼盘
//			sellDataManagement.setRoom(row.getCell(3).getStringCellValue());
			//认购套数
			sellDataManagement.setPurchasingNumber(row.getCell(3).getColumnIndex());
			//认购金额
			sellDataManagement.setPurchasingMoney(row.getCell(4).getColumnIndex());
			sellDataManagement.setSignatureNumber(row.getCell(5).getColumnIndex());
			sellDataManagement.setSignatureMoney(row.getCell(6).getColumnIndex());
			sellDataManagement.setPurchasingDate(row.getCell(7).getDateCellValue());
			sellDataManagement.setSignatureDate(row.getCell(8).getDateCellValue());
			sellDataManagement.setCreateDate(new Date());
			sellDataManagement.setVerifytype("0");
			boolean a =this.addSellDataManagement(sellDataManagement);
			if (a != true) {
				try {
					throw new Exception("插入失败");
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
	}
	
	//返回数量total
	@Override
	public SellDataManagement findtotalNumber(SellDataManagement sellDataManagement) {
		return sellDataManagementMapper.findtotalNumber(sellDataManagement);
	}
	
	
}
