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

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.PageRowBounds;

import cn.com.countrygarden.bi.main.dao.common.OrganMapper;
import cn.com.countrygarden.bi.main.dao.project.ProjectSignInfoMapper;
import cn.com.countrygarden.bi.main.dao.project.ProjectSignItemMapper;
import cn.com.countrygarden.bi.main.dao.project.ProjectTargetMapper;
import cn.com.countrygarden.bi.main.model.PageData;
import cn.com.countrygarden.bi.main.model.common.Organ;
import cn.com.countrygarden.bi.main.model.project.ProjectSign;
import cn.com.countrygarden.bi.main.model.project.ProjectSignInfo;
import cn.com.countrygarden.bi.main.model.project.ProjectTarget;
import cn.com.countrygarden.bi.main.service.project.ProjectSignInfoService;
import cn.com.countrygarden.bi.main.service.project.impl.vo.ProjectSignItemVO;
/**
 * 签约明细报表报表
 * 
 * <pre>
 * 。
 * </pre>
 * 
 * @author developer developer@meicloud.com
 * @version 1.00.00
 * 
 *          <pre>
 * 修改记录
 *    修改后版本:     修改人：  修改日期:     修改内容:
 * </pre>
 */
@Service
@Transactional
public class ProjectSignInfoServiceImpl implements ProjectSignInfoService{
	@Autowired
	  private  ProjectSignInfoMapper mapper;
	@Autowired
	private ProjectTargetMapper projectTargetMapper;
	@Autowired
	private ProjectSignItemMapper projectSignItemMapper;
	@Autowired
	private OrganMapper organMapper;

	@Override
	public PageData search(String year, Integer page, Integer size) {
		PageRowBounds pageRowBounds = new PageRowBounds(page,size);
		List<ProjectSignInfo> plist = mapper.search(year, pageRowBounds);
		return new PageData( plist,pageRowBounds);
	}
	@Override
	public void statistical(String year) {
		
		ProjectTarget pt = new ProjectTarget();
		pt.setYear(year);
		List<ProjectTarget> ptList = projectTargetMapper.select(pt);
		List<ProjectSignInfo>  psInfo = new ArrayList<ProjectSignInfo>();
		
		//第一季度
		for (ProjectTarget projectTarget : ptList) {
			Map<String,String> map = new HashMap<String,String>();
			Organ organ = new Organ();
			organ.setOrganCode(projectTarget.getOrganId());
			organ = organMapper.selectOne(organ);
			map.put("year", year+"-");
			map.put("organId", projectTarget.getOrganId());
			List<ProjectSignItemVO> psiList =projectSignItemMapper.findByYear(map);
			BigDecimal singForOne = new BigDecimal("0");
			BigDecimal singForTwo = new BigDecimal("0");
			BigDecimal singForThree = new BigDecimal("0");
			BigDecimal singForFour = new BigDecimal("0");
			BigDecimal singForAll = new BigDecimal("0");
			for (ProjectSignItemVO projectSignItemVO : psiList) {
				
				if(projectSignItemVO.getInMonth().equals(year+"-01")
				 || projectSignItemVO.getInMonth().equals(year+"-02")
				 || projectSignItemVO.getInMonth().equals(year+"-03")) {
					singForOne.add(projectSignItemVO.getYearIncome());
				}
				if(projectSignItemVO.getInMonth().equals(year+"-04")
						 || projectSignItemVO.getInMonth().equals(year+"-05")
						 || projectSignItemVO.getInMonth().equals(year+"-06")) {
					singForTwo.add(projectSignItemVO.getYearIncome());
						}
				if(projectSignItemVO.getInMonth().equals(year+"-07")
						 || projectSignItemVO.getInMonth().equals(year+"-08")
						 || projectSignItemVO.getInMonth().equals(year+"-09")) {
					singForThree.add(projectSignItemVO.getYearIncome());
						}
				if(projectSignItemVO.getInMonth().equals(year+"-10")
						 || projectSignItemVO.getInMonth().equals(year+"-11")
						 || projectSignItemVO.getInMonth().equals(year+"-12")) {
					singForFour.add(projectSignItemVO.getYearIncome());
						}
			}
			singForAll.add(singForOne).add(singForTwo).add(singForThree).add(singForFour);
			ProjectSignInfo projectSignInfo = new ProjectSignInfo();
			projectSignInfo.setYear(year);
			projectSignInfo.setOrganName(organ.getName());
			
			projectSignInfo.setFirstQuarterTarget(new BigDecimal(projectTarget.getSaturationIncomeOne()));
			projectSignInfo.setTwoQuarterTarget(new BigDecimal(projectTarget.getSaturationIncomeTwo()));
			projectSignInfo.setThreeQuarterTarget(new BigDecimal(projectTarget.getSaturationIncomeThree()));
			projectSignInfo.setFourQuarterTarget(new BigDecimal(projectTarget.getSaturationIncomeFour()));
			projectSignInfo.setYearQuarterTarget(new BigDecimal(projectTarget.getSaturationIncomeAll()));
			
			projectSignInfo.setFirstQuarterProgress(singForOne.divide(projectSignInfo.getFirstQuarterTarget().multiply(new BigDecimal(100d))));
			projectSignInfo.setTwoQuarterProgress(singForTwo.divide(projectSignInfo.getTwoQuarterTarget().multiply(new BigDecimal(100d))));
			projectSignInfo.setThreeQuarterProgress(singForThree.divide(projectSignInfo.getThreeQuarterTarget().multiply(new BigDecimal(100d))));
			projectSignInfo.setFourQuarterProgress(singForFour.divide(projectSignInfo.getFourQuarterTarget().multiply(new BigDecimal(100d))));
			projectSignInfo.setYearQuarterProgress(singForAll.divide(projectSignInfo.getYearQuarterTarget().multiply(new BigDecimal(100d))));
			psInfo.add(projectSignInfo);
		}
		ProjectSignInfo t = new ProjectSignInfo();
		for (int i = 0; i < psInfo.size()-1; i++) {
            for (int j = 0; j < psInfo.size() - 1 - i; j++) {
                if (psInfo.get(j).getYearQuarterTarget().doubleValue() > psInfo.get(j+1).getYearQuarterTarget().doubleValue()) {
                    t = psInfo.get(j);
                    psInfo.set(j, psInfo.get(j+1));
                    psInfo.set(j+1, t);
                }
            }
		}
		ProjectSignInfo deletep= new ProjectSignInfo();
		deletep.setYear(year);
		mapper.delete(deletep);
		for(int i = 1 ;i < psInfo.size();i++) {
			psInfo.get(i).setNo(i+"");
			mapper.insert(psInfo.get(i));
		}
		
	}
	@Override
	public List<ProjectSignInfo> findListByYear(String year){
		ProjectSignInfo p = new ProjectSignInfo();
		p.setYear(year);
		
		return mapper.select(p);
	}
	@Override
	public BigDecimal getAllInfo(String year){

		ProjectTarget pt = new ProjectTarget();
		pt.setYear(year);
		List<ProjectTarget> ptList = projectTargetMapper.select(pt);
		List<ProjectSignInfo>  psInfo = new ArrayList<ProjectSignInfo>();
		
		//第一季度
		for (ProjectTarget projectTarget : ptList) {
			Map<String,String> map = new HashMap<String,String>();
			Organ organ = new Organ();
			organ.setOrganCode(projectTarget.getOrganId());
			organ = organMapper.selectOne(organ);
			map.put("year", year+"-");
			map.put("organId", projectTarget.getOrganId());
			List<ProjectSignItemVO> psiList =projectSignItemMapper.findByYear(map);
			BigDecimal singForOne = new BigDecimal("0");
			BigDecimal singForTwo = new BigDecimal("0");
			BigDecimal singForThree = new BigDecimal("0");
			BigDecimal singForFour = new BigDecimal("0");
			BigDecimal singForAll = new BigDecimal("0");
			for (ProjectSignItemVO projectSignItemVO : psiList) {
				
				if(projectSignItemVO.getInMonth().equals(year+"-01")
				 || projectSignItemVO.getInMonth().equals(year+"-02")
				 || projectSignItemVO.getInMonth().equals(year+"-03")) {
					singForOne.add(projectSignItemVO.getYearIncome());
				}
				if(projectSignItemVO.getInMonth().equals(year+"-04")
						 || projectSignItemVO.getInMonth().equals(year+"-05")
						 || projectSignItemVO.getInMonth().equals(year+"-06")) {
					singForTwo.add(projectSignItemVO.getYearIncome());
						}
				if(projectSignItemVO.getInMonth().equals(year+"-07")
						 || projectSignItemVO.getInMonth().equals(year+"-08")
						 || projectSignItemVO.getInMonth().equals(year+"-09")) {
					singForThree.add(projectSignItemVO.getYearIncome());
						}
				if(projectSignItemVO.getInMonth().equals(year+"-10")
						 || projectSignItemVO.getInMonth().equals(year+"-11")
						 || projectSignItemVO.getInMonth().equals(year+"-12")) {
					singForFour.add(projectSignItemVO.getYearIncome());
						}
			}
			singForAll.add(singForOne).add(singForTwo).add(singForThree).add(singForFour);
			ProjectSignInfo projectSignInfo = new ProjectSignInfo();
			projectSignInfo.setYear(year);
			projectSignInfo.setOrganName(organ.getName());
			
			projectSignInfo.setFirstQuarterTarget(new BigDecimal(projectTarget.getSaturationIncomeOne()));
			projectSignInfo.setTwoQuarterTarget(new BigDecimal(projectTarget.getSaturationIncomeTwo()));
			projectSignInfo.setThreeQuarterTarget(new BigDecimal(projectTarget.getSaturationIncomeThree()));
			projectSignInfo.setFourQuarterTarget(new BigDecimal(projectTarget.getSaturationIncomeFour()));
			projectSignInfo.setYearQuarterTarget(new BigDecimal(projectTarget.getSaturationIncomeAll()));
			
			projectSignInfo.setFirstQuarterProgress(singForOne);
			projectSignInfo.setTwoQuarterProgress(singForTwo);
			projectSignInfo.setThreeQuarterProgress(singForThree);
			projectSignInfo.setFourQuarterProgress(singForFour);
			projectSignInfo.setYearQuarterProgress(singForAll);
			psInfo.add(projectSignInfo);
		}
		BigDecimal allSing = new BigDecimal(0);
		BigDecimal allTarget = new BigDecimal(0);
		for (ProjectSignInfo projectSignInfo : psInfo) {
			if(projectSignInfo.getYearQuarterTarget() != null) {
				allTarget.add(projectSignInfo.getYearQuarterTarget());
			}
			if(projectSignInfo.getYearQuarterProgress() != null) {
				allSing.add(projectSignInfo.getYearQuarterProgress());
			}
		}
		if(allSing.doubleValue() == 0d || allTarget.doubleValue() == 0d) {
			return new BigDecimal(0);
		}
		return allSing.divide(allTarget).multiply(new BigDecimal(100));
	}
	/**
	 * 获取本年实际年度收入
	 * @param year
	 * @return
	 */
	@Override
	public BigDecimal getSingAllByYear(String year) {

		ProjectTarget pt = new ProjectTarget();
		pt.setYear(year);
		List<ProjectTarget> ptList = projectTargetMapper.select(pt);
		List<ProjectSignInfo>  psInfo = new ArrayList<ProjectSignInfo>();
		
		//第一季度
		for (ProjectTarget projectTarget : ptList) {
			Map<String,String> map = new HashMap<String,String>();
			Organ organ = new Organ();
			organ.setOrganCode(projectTarget.getOrganId());
			organ = organMapper.selectOne(organ);
			map.put("year", year+"-");
			map.put("organId", projectTarget.getOrganId());
			List<ProjectSignItemVO> psiList =projectSignItemMapper.findByYear(map);
			BigDecimal singForOne = new BigDecimal("0");
			BigDecimal singForTwo = new BigDecimal("0");
			BigDecimal singForThree = new BigDecimal("0");
			BigDecimal singForFour = new BigDecimal("0");
			BigDecimal singForAll = new BigDecimal("0");
			for (ProjectSignItemVO projectSignItemVO : psiList) {
				
				if(projectSignItemVO.getInMonth().equals(year+"-01")
				 || projectSignItemVO.getInMonth().equals(year+"-02")
				 || projectSignItemVO.getInMonth().equals(year+"-03")) {
					singForOne.add(projectSignItemVO.getYearIncome());
				}
				if(projectSignItemVO.getInMonth().equals(year+"-04")
						 || projectSignItemVO.getInMonth().equals(year+"-05")
						 || projectSignItemVO.getInMonth().equals(year+"-06")) {
					singForTwo.add(projectSignItemVO.getYearIncome());
						}
				if(projectSignItemVO.getInMonth().equals(year+"-07")
						 || projectSignItemVO.getInMonth().equals(year+"-08")
						 || projectSignItemVO.getInMonth().equals(year+"-09")) {
					singForThree.add(projectSignItemVO.getYearIncome());
						}
				if(projectSignItemVO.getInMonth().equals(year+"-10")
						 || projectSignItemVO.getInMonth().equals(year+"-11")
						 || projectSignItemVO.getInMonth().equals(year+"-12")) {
					singForFour.add(projectSignItemVO.getYearIncome());
						}
			}
			singForAll.add(singForOne).add(singForTwo).add(singForThree).add(singForFour);
			ProjectSignInfo projectSignInfo = new ProjectSignInfo();
			projectSignInfo.setYear(year);
			projectSignInfo.setOrganName(organ.getName());
			
			projectSignInfo.setFirstQuarterTarget(new BigDecimal(projectTarget.getSaturationIncomeOne()));
			projectSignInfo.setTwoQuarterTarget(new BigDecimal(projectTarget.getSaturationIncomeTwo()));
			projectSignInfo.setThreeQuarterTarget(new BigDecimal(projectTarget.getSaturationIncomeThree()));
			projectSignInfo.setFourQuarterTarget(new BigDecimal(projectTarget.getSaturationIncomeFour()));
			projectSignInfo.setYearQuarterTarget(new BigDecimal(projectTarget.getSaturationIncomeAll()));
			
			projectSignInfo.setFirstQuarterProgress(singForOne);
			projectSignInfo.setTwoQuarterProgress(singForTwo);
			projectSignInfo.setThreeQuarterProgress(singForThree);
			projectSignInfo.setFourQuarterProgress(singForFour);
			projectSignInfo.setYearQuarterProgress(singForAll);
			psInfo.add(projectSignInfo);
		}
		BigDecimal allSing = new BigDecimal(0);
		
		for (ProjectSignInfo projectSignInfo : psInfo) {
		
			if(projectSignInfo.getYearQuarterProgress() != null) {
				allSing.add(projectSignInfo.getYearQuarterProgress());
			}
		}
		if(allSing.doubleValue() == 0d) {
			return new BigDecimal(0);
		}
		return allSing;
	}
	/**
	 * 获取本年实际年度收入
	 * @param year
	 * @return
	 */
	@Override
	public BigDecimal getSingAllByMonth(String year,String month) {

		ProjectTarget pt = new ProjectTarget();
		pt.setYear(year);
		List<ProjectTarget> ptList = projectTargetMapper.select(pt);
		List<ProjectSignInfo>  psInfo = new ArrayList<ProjectSignInfo>();
		
		//第一季度
		
			Map<String,String> map = new HashMap<String,String>();
			
			map.put("year", year+"-");
		//	map.put("organId", projectTarget.getOrganId());
			List<ProjectSignItemVO> psiList =projectSignItemMapper.findByYear(map);
			
			BigDecimal singForAll = new BigDecimal("0");
			for (ProjectSignItemVO projectSignItemVO : psiList) {
				if(projectSignItemVO.getInMonth().equals(year+"-"+month)) {
					singForAll.add(projectSignItemVO.getMonthIncome());
				}
			}
			
		
		return singForAll;
	}
}
