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

import java.text.Format;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import cn.com.countrygarden.bi.main.dto.ContractStatisticalarpojnameDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageRowBounds;
import com.github.pagehelper.util.StringUtil;

import cn.com.countrygarden.bi.main.dao.dup.ContractStatisticalMapper;
import cn.com.countrygarden.bi.main.dao.dup.TbHsprContractMapper;
import cn.com.countrygarden.bi.main.model.PageData;
import cn.com.countrygarden.bi.main.model.dup.ContractStatistical;
import cn.com.countrygarden.bi.main.model.dup.ContractTimedExecute;
import cn.com.countrygarden.bi.main.model.dup.ContranctStaticalDto;
import cn.com.countrygarden.bi.main.model.dup.TbHsprContract;
import cn.com.countrygarden.bi.main.service.dup.ContractStatisticalService;
import cn.com.countrygarden.bi.main.util.DateUtil;

/**
 * 
 * 
 * <pre>
 * 。合同录入统计ServiceImpl
 * </pre>
 * 
 * @author developer developer@meicloud.com
 * @version 1.00.00
 * 
 *          <pre>
 * 修改记录
 *    修改后版本:     修改人：  修改日期:     修改内容:
 *          </pre>
 */
@Service
@Transactional
public class ContractStatisticalServiceImpl implements ContractStatisticalService {

	@Resource
	private ContractStatisticalMapper contractStatisticalMapper;

	@Autowired
	private TbHsprContractMapper tbHsprContractMapper;

	/**
	 * 全部合同录入数据
	 */
	@Override
	public List<ContractStatistical> AllContractStatisticalSelect(ContractStatistical contractStatistical) {
		// 查询未录入的时候,要对比ERP数据库的合同表数据,如果当前DUP数据库中的合同编号ERP合同表存在,则要删除DUP合同的数据.
		PageRowBounds pageRowBounds = new PageRowBounds(contractStatistical.getPageIndex(),
				contractStatistical.getPageSize());

		// 查询未录入
		List<ContractStatistical> list = contractStatisticalMapper.contractStatisticalSelect(contractStatistical,
				pageRowBounds);
		int count = 40 - list.size();
		if (count > 0) {
			List<ContractStatistical> list1 = tbHsprContractMapper
					.contractStatisticalSelectNoExamineAll(contractStatistical);
			// 遍历存放40条数据
			for (int i = 0; i < list1.size(); i++) {
				list1.get(i).setIsAuditing("0");
				if (count > 0) {
					list.add(list1.get(i));
					count--;
				}
			}
		}
		return list;
	}

	@Override
	public List<ContractStatistical> AllContractStatisticalSelect1(ContractStatistical contractStatistical) {
		// 查询未录入
		List<ContractStatistical> list = contractStatisticalMapper.contractStatisticalSelect1(contractStatistical);
		List<ContractStatistical> list1 = null;
		// 查询ERP的数据
		if (StringUtil.isEmpty(contractStatistical.getNoImport())) {
			list1 = tbHsprContractMapper.contractStatisticalSelectNoExamineAll1(contractStatistical);
			for (ContractStatistical contractStatistical2 : list1) {
				list.add(contractStatistical2);
			}
		}
		return list;
	}

	/**
	 * 合同未录入统计模糊查询
	 */
	@Override
	public PageData ContractStatisticalSelect(ContractStatistical contractStatistical) {
		// 查询未录入的时候,要对比ERP数据库的合同表数据,如果当前DUP数据库中的合同编号ERP合同表存在,则要删除DUP合同的数据.
		PageRowBounds pageRowBounds = new PageRowBounds(contractStatistical.getPageIndex(),
				contractStatistical.getPageSize());
		System.out.println("----ContractStatisticalSelect--------  "+JSONObject.toJSONString(contractStatistical));
		List<ContractStatistical> list1 = contractStatisticalMapper.contractStatisticalSelect(contractStatistical,
				pageRowBounds);
		PageData pageData = new PageData(list1, pageRowBounds);
		return pageData;
	}

	/**
	 * 合同未录入统计模糊查询==>调度用!
	 */
	@Override
	public PageData ContractStatisticalSelect1(ContractStatistical contractStatistical) {
		// 查询未录入的时候,要对比ERP数据库的合同表数据,如果当前DUP数据库中的合同编号ERP合同表存在,则要删除DUP合同的数据.
		PageRowBounds pageRowBounds = new PageRowBounds(contractStatistical.getPageIndex(),
				contractStatistical.getPageSize());
		List<ContractStatistical> list1 = contractStatisticalMapper.contractStatisticalSelect(contractStatistical,
				pageRowBounds);
		PageData pageData = new PageData(list1, pageRowBounds);
		return pageData;
	}

	/**
	 * 查询合同未审核的数量
	 */
	@Override
	public PageData contractStatisticalSelectNoExamine(ContractStatistical contractStatistical) {

		PageRowBounds pageRowBounds = new PageRowBounds(contractStatistical.getPageIndex(),
				contractStatistical.getPageSize());
		List<ContractStatistical> list = tbHsprContractMapper.contractStatisticalSelectNoExamine(contractStatistical,
				pageRowBounds);

		PageData pageData = new PageData(list, pageRowBounds);
		return pageData;
	}

	/**
	 * 查询合同已审核的数量
	 */
	@Override
	public PageData contractStatisticalSelectYesExamine(ContractStatistical contractStatistical) {
		PageRowBounds pageRowBounds = new PageRowBounds(contractStatistical.getPageIndex(),
				contractStatistical.getPageSize());
		contractStatistical.setIsAuditings(1);
		List<ContractStatistical> list = tbHsprContractMapper.contractStatisticalSelectYesExamine(contractStatistical,
				pageRowBounds);
		PageData pageData = new PageData(list, pageRowBounds);
		return pageData;
	}

	/**
	 * 合同统计定时调度调用的插入数据方法
	 */
	@Override
	public boolean inserContranctStatical(List<ContractStatistical> contractStatisticalList) {

		int i = 0;
		for (ContractStatistical dto : contractStatisticalList) {
			contractStatisticalMapper.insertdto(dto);
			i++;
		}
		return i == contractStatisticalList.size();
	}

	/**
	 * 统计数量
	 */
	@Override
	public ContranctStaticalDto selecttotal() {

		// 未录入
		int b = contractStatisticalMapper.contractStatisticalSelectNoImport();
		// 已审核
		int c = tbHsprContractMapper.contractStatisticalSelectYesExamineNumber();
		// 未审核
		int d = tbHsprContractMapper.contractStatisticalSelectNOExamineNumber();
		// 全部
		int a = b + c + d;
		ContranctStaticalDto contranctStaticalDto = new ContranctStaticalDto();
		contranctStaticalDto.setTotalPage(a);
		contranctStaticalDto.setNoImportNumber(b);
		contranctStaticalDto.setYesexamineTypeNumber(c);
		contranctStaticalDto.setNoExamineTypeNumber(d);
		return contranctStaticalDto;
	}

	/**
	 * 查询所有的区域
	 */
	@Override
	public List<ContractStatistical> findOrderCode() {
		return tbHsprContractMapper.findOrderCode();
	}

	/**
	 * 查询对应区域下的所有的项目
	 */
	@Override
	public List<ContractStatistical> findCommName(String OrganCode) {
		return tbHsprContractMapper.findCommName(OrganCode);
	}

	/**
	 * 根据项目id查询区域code
	 */
	@Override
	public Map<String, Object> findCommidAndOrganCode(Map<String, Object> map) {
		Map<String, Object> m = tbHsprContractMapper.findCommidAndOrganCode(map);
		return m;
	}

	/**
	 * 查询所有的项目-可模糊查询
	 */
	@Override
	public List<ContractStatistical> findAllCommName(String CommName) {
		Map<String, Object> map = new HashMap<String, Object>();
		if (!StringUtil.isEmpty(CommName)) {
			map.put("CommName", CommName);
		}
		return tbHsprContractMapper.findAllCommName(map);
	}

	/**
	 * 查询所有的流程编号
	 */
	@Override
	public List<String> findAllBipNumber() {
		return tbHsprContractMapper.findAllBipNumber();
	}

	// 时间+1天
	public String findDate(String dates) {
		Date date;
		try {
			date = new SimpleDateFormat("yyyy-MM-dd").parse(dates);
			Format f = new SimpleDateFormat("yyyy-MM-dd");
			System.out.println("今天是:" + dates);
			Calendar c = Calendar.getInstance();
			c.setTime(date);
			c.add(Calendar.DAY_OF_MONTH, 1);// 今天+1天
			Date tomorrow = c.getTime();
			System.out.println("明天是:" + f.format(tomorrow));

			return f.format(tomorrow);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

	@SuppressWarnings("null")
	@Override
	public ContractTimedExecute findDateStart() {
		ContractTimedExecute bipDto = null;
		ContractTimedExecute bipDtos = new ContractTimedExecute();
		String now = null;
		// 查库动作--获取最新的数据
		bipDto = contractStatisticalMapper.findDateStart();
		if (bipDto == null) {
			ContractTimedExecute contractTimedExecutes =new ContractTimedExecute();
			contractTimedExecutes.setStartDate("2017-01-01");
			// 当前时间转换字符串
			now = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
			System.out.println(now);
			contractTimedExecutes.setEndDate(now);
		bipDtos.setCreateDate(new Date());
		bipDtos.setStartDate(now);
		bipDtos.setEndDate(findDate(now));
		// 插入下一次查询的时间
		contractStatisticalMapper.insterContractTimedExecute(bipDtos);
		return contractTimedExecutes;
		}
		//准备下一次查询的时间
		bipDtos.setCreateDate(new Date());
		bipDtos.setStartDate(bipDto.getEndDate());
		bipDtos.setEndDate(findDate(bipDto.getEndDate()));
		// 插入下一次查询的时间
		 int a = contractStatisticalMapper.insterContractTimedExecute(bipDtos);
		return bipDto;
	}
	@Override
	public List<ContractStatisticalarpojnameDTO> areaquname(String areaid) {
		// TODO Auto-generated method stub
		return tbHsprContractMapper.areaquname(areaid);
	}

	@Override
	public List<ContractStatisticalarpojnameDTO> projectname(Integer projectId) {
		// TODO Auto-generated method stub
		return tbHsprContractMapper.projectname(projectId);
	}

    @Override
    public int DeleteEnteredContractStatistical(ContractStatistical contractStatistical) {    
        List<String> lists = contractStatisticalMapper.findBIPnumber(contractStatistical);
        int i=0;
        for (String str : lists) {
            // 遍历查询当前未录入的数据,是否已录入
            String BipNumber = tbHsprContractMapper.findBipNumber(str);
            // if(isString)
            if (!StringUtil.isEmpty(BipNumber)) {
                i++;
                // 不等于空的时候,删除DUP合同统计里的数据
                contractStatisticalMapper.deleteBIP(BipNumber);
            }
        }
        return i;
    }

}
