package cn.com.countrygarden.bi.main.controller.project.imports;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.usermodel.BorderStyle;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.FillPatternType;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.VerticalAlignment;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

import cn.com.countrygarden.bi.main.dao.common.CommCopyMapper;
import cn.com.countrygarden.bi.main.dao.common.CommMapper;
import cn.com.countrygarden.bi.main.dao.dup.TbHsprContractMapper;
import cn.com.countrygarden.bi.main.dto.wcx.WcxRegionProjectDto;
import cn.com.countrygarden.bi.main.model.common.Comm;
import cn.com.countrygarden.bi.main.model.common.CommCopy;
import cn.com.countrygarden.bi.main.model.dup.SiteResources;
import cn.com.countrygarden.bi.main.model.project.ProjectSignInfo;
import cn.com.countrygarden.bi.main.service.project.impl.vo.OneProject;
@Component  
public class ProjectImports {
	@Autowired
	CommCopyMapper commCopyMapper;
	@Autowired
	CommMapper commMapper;
	@Autowired
	TbHsprContractMapper tbHsprContractMapper;
	
	 public void createBody(List<OneProject> dataList, Workbook workbook, Sheet sheet) {
	        CellStyle style = createBodyStyle(workbook);
	        int rowIndex = 1;
	       
	        for (OneProject data : dataList) {
	        	//OneProject comm = commMapper.selectByOneId(data.getId());
	        	OneProject commCopy = commCopyMapper.selectByOneId(data.getId());
	        	
	        	  Row row = sheet.createRow(rowIndex);
	        	  if(null == commCopy) {
	        		  createCell(change(data.getRegionName()), style, row, 0,false);
		        	  createCell(change(data.getOrganName()), style, row, 1,false);
		        	  createCell(change(data.getContractorName()), style, row, 2,false);
		        	  createCell(change(data.getName()), style, row, 3,false);
		        	  createCell(change(data.getCooperationfName()), style, row, 4,false);
		        	  createCell(change(data.getStatusName()), style, row, 5,false);
		        	  createCell(change(data.getContractArea()), style, row, 6,false);
		        	  createCell(change(data.getSaturation()), style, row, 7,false);
		        	  createCell(change(data.getCity()), style, row, 8,false);
		        	  createCell(change(data.getJointRightName()), style, row, 9,false);
		        	  createCell(change(data.getJointDuty()), style, row, 10,false);
		        	  createCell(change(data.getCorpTypeName()), style, row, 11,false);
		        	  createCell(change(data.getSignificanceName()), style, row, 12,false);
		        	  createCell(change(data.getDemandName()), style, row, 13,false);
		        	  createCell(change(data.getPropertyFee()), style, row, 14,false);
		        	  createCell(change(data.getMessageFee()), style, row, 15,false);
		        	  createCell(change(data.getIsSubmit()), style, row, 16,false);
		        	  createCell(change(data.getCompetitor()), style, row, 17,false);
		        	  createCell(change(data.getOpinion()), style, row, 18,false);
		        	  createCell(change(data.getCallFile()), style, row, 19,false);
		        	  createCell(change(data.getCallOrganization()), style, row, 20,false);
		        	  
		        	  createCell(change(data.getCallEnvironmen()), style, row, 21,false);
		        	  createCell(change(data.getBusinessName()), style, row, 22,false);
		        	  
		        	  createCell(change(data.getProjectClass()), style, row, 23,false);
		        	  createCell(change(data.getProjectPlan()), style, row, 24,false);
		        	  createCell(change(data.getRemark()), style, row, 25,false);
		        	  createCell(date2String(data.getUpdateTime()), style, row, 26,false);
		        	  List<WcxRegionProjectDto> list =  tbHsprContractMapper.findErpRegionByUserCode(data.getContractorCode());
		        	  if(list == null || list.size() <1) {
		        		  createCell("", createBodyStyle(workbook), row, 27,false);
		        	  }else {
		        		  createCell(list.get(0).getRegionName(),style, row, 27,false);
		        	  }
		        	 
	        	  }else {
	        		  createCell(change(data.getRegionName()), createBodyStyle(workbook), row, 0,changeColor(commCopy.getRegionName(),data.getRegionName()));
		        	  createCell(change(data.getOrganName()), createBodyStyle(workbook), row, 1,changeColor(commCopy.getOrganName(),data.getOrganName()));
		        	  createCell(change(data.getContractorName()), createBodyStyle(workbook), row, 2,changeColor(commCopy.getContractorName(),data.getContractorName()));
		        	  createCell(change(data.getName()), createBodyStyle(workbook), row, 3,changeColor(commCopy.getName(),data.getName()));
		        	  createCell(change(data.getCooperationfName()), createBodyStyle(workbook), row, 4,changeColor(commCopy.getCooperationfName(),data.getCooperationfName()));
		        	  createCell(change(data.getStatusName()), createBodyStyle(workbook), row, 5,changeColor(commCopy.getStatusName(),data.getStatusName()));
		        	  createCell(change(data.getContractArea()), createBodyStyle(workbook), row, 6,changeColor(commCopy.getContractArea(),data.getContractArea()));
		        	  createCell(change(data.getSaturation()), createBodyStyle(workbook), row, 7,changeColor(commCopy.getSaturation(),data.getSaturation()));
		        	  createCell(change(data.getCity()), createBodyStyle(workbook), row, 8,changeColor(commCopy.getCity(),data.getCity()));
		        	  createCell(change(data.getJointRightName()), createBodyStyle(workbook), row, 9,changeColor(commCopy.getJointRightName(),data.getJointRightName()));
		        	  createCell(change(data.getJointDuty()), createBodyStyle(workbook), row, 10,changeColor(commCopy.getJointDuty(),data.getJointDuty()));
		        	  createCell(change(data.getCorpTypeName()), createBodyStyle(workbook), row, 11,changeColor(commCopy.getCorpTypeName(),data.getCorpTypeName()));
		        	  createCell(change(data.getSignificanceName()), createBodyStyle(workbook), row, 12,changeColor(commCopy.getSignificanceName(),data.getSignificanceName()));
		        	  createCell(change(data.getDemandName()), createBodyStyle(workbook), row, 13,changeColor(commCopy.getDemandName(),data.getDemandName()));
		        	  createCell(change(data.getPropertyFee()), createBodyStyle(workbook), row, 14,changeColor(commCopy.getPropertyFee(),data.getPropertyFee()));
		        	  createCell(change(data.getMessageFee()), createBodyStyle(workbook), row, 15,changeColor(commCopy.getMessageFee(),data.getMessageFee()));
		        	  createCell(change(data.getIsSubmit()), createBodyStyle(workbook), row, 16,changeColor(commCopy.getIsSubmit(),data.getIsSubmit()));
		        	  createCell(change(data.getCompetitor()), createBodyStyle(workbook), row, 17,changeColor(commCopy.getCompetitor(),data.getCompetitor()));
		        	  createCell(change(data.getOpinion()), createBodyStyle(workbook), row, 18,changeColor(commCopy.getOpinion(),data.getOpinion()));
		        	  createCell(change(data.getCallFile()), createBodyStyle(workbook), row, 19,changeColor(commCopy.getCallFile(),data.getCallFile()));
		        	  createCell(change(data.getCallOrganization()), createBodyStyle(workbook), row, 20,changeColor(commCopy.getCallOrganization(),data.getCallOrganization()));
		        	  
		        	  createCell(change(data.getCallEnvironmen()), createBodyStyle(workbook), row, 21,changeColor(commCopy.getCallEnvironmen(),data.getCallEnvironmen()));
		        	  createCell(change(data.getBusinessName()), createBodyStyle(workbook), row, 22,changeColor(commCopy.getBusinessName(),data.getBusinessName()));
		        	  
		        	  createCell(change(data.getProjectClass()), createBodyStyle(workbook), row, 23,changeColor(commCopy.getProjectClass(),data.getProjectClass()));
		        	  createCell(change(data.getProjectPlan()), createBodyStyle(workbook), row, 24,changeColor(commCopy.getProjectPlan(),data.getProjectPlan()));
		        	  createCell(change(data.getRemark()), createBodyStyle(workbook), row, 25,changeColor(commCopy.getRemark(),data.getRemark()));
		        	 
		        	  createCell(date2String(data.getUpdateTime()), createBodyStyle(workbook), row, 26,false);
		        	  
		        	  List<WcxRegionProjectDto> list =  tbHsprContractMapper.findErpRegionByUserCode(data.getContractorCode());
		        	  if(list == null || list.size() <1) {
		        		  createCell("", style, row, 27,false);
		        	  }else {
		        		  createCell(list.get(0).getRegionName(),style, row, 27,false);
		        	  }
	        	  }
	        	 Comm c = commMapper.selectByPrimaryKey(data.getId());
	        	 CommCopy cp = new CommCopy();
	        	
	        	 cp.setId(c.getId());
	        	 cp.setName(c.getName());
	        	 cp.setOrganId(c.getOrganId());
	        	 cp.setProjectType(c.getProjectType());
	        	 cp.setStatus(c.getStatus());
	        	 cp.setCorpType(c.getCorpType());
	        	 cp.setProjectNature(c.getProjectNature());
	        	 cp.setArea(c.getArea());
	        	 cp.setChargeArea(c.getChargeArea());
	        	 cp.setContractArea(c.getContractArea());
	        	 cp.setContacts(c.getContacts());
	        	 cp.setContractorCode(c.getContractorCode());
	        	 cp.setContractorName(c.getContractorName());
	        	 cp.setCommNum(c.getCommNum());
	        	 cp.setGeneralType(c.getGeneralType());
	        	 cp.setCooperationfName(c.getCooperationfName());
	        	 cp.setCooperationfContact(c.getCooperationfContact());
	        	 cp.setCooperationfNumber(c.getCooperationfNumber());
	        	 cp.setSaturation(c.getSaturation());
	        	 cp.setProportion(c.getProportion());
	        	 cp.setProjectNumber(c.getProjectNumber());
	        	 cp.setTargetArea(c.getTargetArea());
	        	 cp.setTargetIncome(c.getTargetIncome());
	        	 cp.setSchedule(c.getSchedule());
	        	 cp.setRegion(c.getRegion());
	        	 cp.setCity(c.getCity());
	        	 cp.setRemark(c.getRemark());
	        	 cp.setProjectClassCode(c.getProjectClassCode());
	        	 cp.setProjectPlanCode(c.getProjectPlanCode());
	        	 cp.setBusinessType(c.getBusinessType());
	        	 cp.setJoint(c.getJoint());
	        	 cp.setJointDuty(c.getJointDuty());
	        	 cp.setJointRight(c.getJointRight());
	        	 cp.setSignificance(c.getSignificance());
	        	 cp.setDemand(c.getDemand());
	        	 cp.setPropertyFee(c.getPropertyFee());
	        	 cp.setMessageFee(c.getMessageFee());
	        	 cp.setIsSubmit(c.getIsSubmit());
	        	 cp.setCompetitor(c.getCompetitor());
	        	 cp.setOpinion(c.getOpinion());
	        	 cp.setCallFile(c.getCallFile());
	        	 cp.setCallOrganization(c.getCallOrganization());
	        	 cp.setCallEnvironmen(c.getCallEnvironmen());
	        	 cp.setWarningLight(c.getWarningLight());
	        	 cp.setGmtModified(new Date());
	        	 if(null == commCopy) {
	        		 commCopyMapper.insert(cp);
	        	 }else {
	        		 commCopyMapper.updateByPrimaryKey(cp);
	        	 }
	        	  rowIndex++;
			}
	    }
	 public void createHeader(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,false);
         createCell("区域", headerStyle, row, 1,false);
         createCell("跟进人", headerStyle, row, 2,false);
         createCell("项目名", headerStyle, row, 3,false);
         createCell("甲方名", headerStyle, row, 4,false);
         createCell("项目状态", headerStyle, row, 5,false);
         //这里有图片服务器再做!
         createCell("建筑面积", headerStyle, row, 6,false);
         createCell("年饱和收益", headerStyle, row, 7,false);
         createCell("所在城市", headerStyle, row, 8,false);
         createCell("甲方对接人权利", headerStyle, row, 9,false);
         createCell("甲方对接人职务", headerStyle, row, 10,false);
         createCell("合作模式", headerStyle, row, 11,false);
         createCell("重要性", headerStyle, row, 12,false);
         createCell("需求", headerStyle, row, 13,false);
         createCell("业务费定价预算", headerStyle, row, 14,false);
         createCell("信息费", headerStyle, row, 15,false);
         createCell("是否提交方案", headerStyle, row, 16,false);
         createCell("主要竞争对手", headerStyle, row, 17,false);
         createCell("决策层意见", headerStyle, row, 18,false);
         createCell("招标文件", headerStyle, row, 19,false);
         createCell("招标机构", headerStyle, row, 20,false);
         createCell("评标环境", headerStyle, row, 21,false);
         createCell("业态名", headerStyle, row, 22,false);
         createCell("内部进度", headerStyle, row, 23,false);
         createCell("业务进度", headerStyle, row, 24,false);
         createCell("备注", headerStyle, row, 25,false);
         createCell("最后修改时间", headerStyle, row, 26,false);
         createCell("添加人所属区域", headerStyle, row, 27,false);
    }
	   public CellStyle createHeaderStyle(Workbook workbook) {
	        CellStyle headerStyle = workbook.createCellStyle();
	        headerStyle.setAlignment(HorizontalAlignment.CENTER);
	        headerStyle.setVerticalAlignment(VerticalAlignment.CENTER);
	        headerStyle.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex());
	        headerStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
	        headerStyle.setWrapText(true);
	        headerStyle.setBorderBottom(BorderStyle.THIN); //下边框
	        headerStyle.setBottomBorderColor(IndexedColors.GREY_50_PERCENT.getIndex());
	        headerStyle.setBorderLeft(BorderStyle.THIN);//左边框
	        headerStyle.setLeftBorderColor(IndexedColors.GREY_50_PERCENT.getIndex());
	        headerStyle.setBorderTop(BorderStyle.THIN);//上边框
	        headerStyle.setTopBorderColor(IndexedColors.GREY_50_PERCENT.getIndex());
	        headerStyle.setBorderRight(BorderStyle.THIN);//右边框
	        headerStyle.setRightBorderColor(IndexedColors.GREY_50_PERCENT.getIndex());
	        return headerStyle;
	    }
	   public void createCell(String value, CellStyle style, Row row, int col,boolean color) {
		  
	        Cell cell = row.createCell(col);
	      
	        
	        if(color) {
	        	style.setFillForegroundColor(IndexedColors.RED.getIndex());
		        style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
	        }
  
	        cell.setCellStyle(style);
	       
	       
	        if (value != null) {
	            cell.setCellValue(value);
	        }
	    }

	    public CellStyle createBodyStyle(Workbook workbook) {
	        CellStyle style = workbook.createCellStyle();
	        style.setAlignment(HorizontalAlignment.CENTER);
	        style.setVerticalAlignment(VerticalAlignment.CENTER);
	        style.setWrapText(true);
	        style.setBorderBottom(BorderStyle.THIN); //下边框
	        style.setBottomBorderColor(IndexedColors.GREY_50_PERCENT.getIndex());
	        style.setBorderLeft(BorderStyle.THIN);//左边框
	        style.setLeftBorderColor(IndexedColors.GREY_50_PERCENT.getIndex());
	        style.setBorderTop(BorderStyle.THIN);//上边框
	        style.setTopBorderColor(IndexedColors.GREY_50_PERCENT.getIndex());
	        style.setBorderRight(BorderStyle.THIN);//右边框
	        style.setRightBorderColor(IndexedColors.GREY_50_PERCENT.getIndex());
	        return style;
	    }
	    
	    public String change(Object object) {
	    	if(null == object) {
	    		return "";
	    	}else {
	    		return object+"";
	    	}
	    }
	    public boolean changeColor(Object o1,Object o2) {
	    	if(null == o1 && null == o2) {
	    		return false;
	    	}
	    	if(null == o1 || null == o2) {
	    		return true;
	    	}
	    	
	    	if(o1.equals(o2)) {
	    		return false;
	    	}
	    	return true;
	    }
	    public String date2String(Date time) {
	    	 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  
	    	 if(null == time) {
	    		 return "";
	    	 }else {
	    		 return sdf.format(time);
	    	 }
	    }
}
