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 com.github.pagehelper.util.StringUtil;

import cn.com.countrygarden.bi.main.dao.dup.MediaResoureceMapper;
import cn.com.countrygarden.bi.main.model.PageData;
import cn.com.countrygarden.bi.main.model.dup.MediaResources;
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.MediaResourcesService;

/**
 * <pre>
 * 。媒体资源ServiceImpl
 * </pre>
 * 
 * @author developer developer@meicloud.com
 * @version 1.00.00
 * 
 *          <pre>
 * 修改记录
 *    修改后版本:     修改人：  修改日期:     修改内容:
 * </pre>
 */
@Service
@Transactional
public class MediaResourcesServiceImpl implements MediaResourcesService {
	
	@Resource
	private MediaResoureceMapper mediaResourceMapper;
	
	@Resource
	private ContractStatisticalService contractStatisticalService;
	
	@Override
	public PageData findMediaResourcesAlllike(MediaResources mediaResources) {
		PageRowBounds pageRowBounds = new PageRowBounds(mediaResources.getPageIndex(), mediaResources.getPageSize());
		List<SiteResources> list = mediaResourceMapper.findMediaResourcesAlllike(mediaResources, pageRowBounds);
		PageData PageData = new PageData(list, pageRowBounds);
		return PageData;
	}
	
	@Override
	public  boolean addMediaResources(MediaResources mediaResources) {
		mediaResources.setCreateDate(new Date());
		mediaResources.setTotal(mediaResources.getInternalUseNumber()+mediaResources.getIdleNumber());
		return mediaResourceMapper.addMediaResources(mediaResources)==1;
	}

	@Override
	public boolean updateMediaResources(MediaResources mediaResources) {
		mediaResources.setTotal(mediaResources.getInternalUseNumber()+mediaResources.getIdleNumber()+mediaResources.getWhetherRet());
		return mediaResourceMapper.updateMediaResources(mediaResources)==1;
	}

	@Override
	public boolean deleteMediaResources(SiteResources siteResources) {
		return mediaResourceMapper.deleteMediaResources(siteResources)==1;
	}

	@Override
	public void excelToObject(MultipartFile mf, String fileName, String OperatorId) {
		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 = 2; i <= sheet.getPhysicalNumberOfRows(); i++) {
			Row row = sheet.getRow(i);
			//怎么判断是否为结束的字段.(是否为合并单元格;
			if (row == null)
				break;
			if (StringUtil.isEmpty(row.getCell(0).getStringCellValue())) {
				break;
			}
			if (row.getCell(0) == null)
				try {
					throw new Exception("第" + i + "行，区域Id为空。");
				} catch (Exception e) {
				}
			MediaResources mediaResources = new MediaResources();
			// 获取数据
			mediaResources.setOrganName(row.getCell(0).getStringCellValue());
			mediaResources.setCommName(row.getCell(1).getStringCellValue());
			if("电梯框架广告".equals(row.getCell(2).getStringCellValue())) {
				mediaResources.setMediaType("1");
			}else if("LED广告".equals(row.getCell(2).getStringCellValue())) {
				mediaResources.setMediaType("2");
			}else if("LED液晶显示屏".equals(row.getCell(2).getStringCellValue())) {
				mediaResources.setMediaType("3");
			}else if("LED广告灯箱".equals(row.getCell(2).getStringCellValue())) {
				mediaResources.setMediaType("4");
			}else if("户外桁架广告".equals(row.getCell(2).getStringCellValue())) {
				mediaResources.setMediaType("5");
			}else if("站牌广告".equals(row.getCell(2).getStringCellValue())) {
				mediaResources.setMediaType("6");
			}else if("楼顶广告".equals(row.getCell(2).getStringCellValue())) {
				mediaResources.setMediaType("7");
			}else if("道闸广告".equals(row.getCell(2).getStringCellValue())) {
				mediaResources.setMediaType("8");
			}else if("横幅广告".equals(row.getCell(2).getStringCellValue())) {
				mediaResources.setMediaType("9");
			}else if("宣传单".equals(row.getCell(2).getStringCellValue())) {
				mediaResources.setMediaType("10");
			}else if("易拉宝".equals(row.getCell(2).getStringCellValue())) {
				mediaResources.setMediaType("11");
			}else if("X展架".equals(row.getCell(2).getStringCellValue())) {
				mediaResources.setMediaType("12");
			}
		
			mediaResources.setLocation(row.getCell(3).getStringCellValue());
			mediaResources.setSpecification(row.getCell(4).getStringCellValue());
			mediaResources.setWhetherRet(row.getCell(5).getColumnIndex());
			mediaResources.setInternalUseNumber(row.getCell(6).getColumnIndex());
			mediaResources.setIdleNumber(row.getCell(6).getColumnIndex());
			mediaResources.setTotal(mediaResources.getInternalUseNumber()+mediaResources.getIdleNumber()+mediaResources.getWhetherRet());
			mediaResources.setCreateDate(new Date());
			// 获取数据
			Map<String,Object> map=new HashMap<String,Object>();
			//区域名
			map.put("OrganName", row.getCell(0).getStringCellValue());
			map.put("CommName", row.getCell(1).getStringCellValue());
			//根据项目与区域名称获取对应的项目id与区域Code
			Map<String,Object> maps=contractStatisticalService.findCommidAndOrganCode(map);
			if(maps==null) {
			//如果无法获取对应的项目id与区域code,则跳出这次添加
				continue;
			}
			mediaResources.setCommId((int)maps.get("CommID"));
			mediaResources.setOrganCode((String)maps.get("OrganCode"));
			/// 调用插入的接口
			boolean a = this.addMediaResources(mediaResources);
			if (a != true) {
				try {
					throw new Exception("插入失败");
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
	}
	
}
