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

import cn.com.countrygarden.bi.main.constant.wcx.ApplicationContextRegister;
import cn.com.countrygarden.bi.main.exception.ServiceException;
import cn.com.countrygarden.bi.main.service.dataMigration.BatchCommitDataServise;
import org.apache.ibatis.session.ExecutorType;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class BatchCommitDataServiceImpl implements BatchCommitDataServise {
	public static final String BD_PSNDOC_MAPPER = "cn.com.countrygarden.bi.main.dao.dataMigration.BdPsndocMapper.insertOrUpdatePlus";
	public static final String HI_PSNDOC_WORK_MAPPER = "cn.com.countrygarden.bi.main.dao.dataMigration.HiPsndocWorkMapper.insertOrUpdatePlus";
	public static final String HI_PSNJOB_MAPPER = "cn.com.countrygarden.bi.main.dao.dataMigration.HiPsnJobMapper.insertOrUpdatePlus";
	public static final String HI_PSNDOC_TRAIN_MAPPER = "cn.com.countrygarden.bi.main.dao.dataMigration.HiPsndocTrainMapper.insertOrUpdatePlus";
	public static final String BD_TYPE_MAPPER = "cn.com.countrygarden.bi.main.dao.dataMigration.BdTypeMapper.insertOrUpdatePlus";

	public static final String BD_PSNDOC_MAPPER_UPDATE = "cn.com.countrygarden.bi.main.dao.dataMigration.BdPsndocMapper.update";
	/**
	 * 批量插入数据
	 */
	@Override
	public <T> void batchHandle(String mappaerId, int batchNum, List<T> dataObj) {
		SqlSessionFactory sqlSessionFactory = (SqlSessionFactory)ApplicationContextRegister.getApplicationContext().getBean("sqlSessionFactory");
		SqlSession session = sqlSessionFactory.openSession(ExecutorType.BATCH, false);
		// 进位取整，获得计算得提交的次数
		int commitNum = (int) Math.ceil(dataObj.size() / (double) batchNum);
		//每次插入队列
		List<T> insertList = new ArrayList<T>(batchNum);
		Map<String,Object> intMap = new HashMap<String,Object>();
		try {
			//开始序号-start，结束序号-stop
			int start, stop;
			for (int i = 0; i < commitNum; i++) {
				intMap.remove("bd");
				insertList.clear();
				start = i * batchNum;
				//判断最后一条序号
				stop = Math.min(i * batchNum + batchNum - 1, dataObj.size() - 1);
				for (int j = start; j <= stop; j++) {
					insertList.add(dataObj.get(j));
				}
				intMap.put("bd",insertList);
				session.insert(mappaerId, intMap);
				session.commit();
				session.clearCache();
			 }
		} catch (Exception e) {
			// 没有提交的数据可以回滚
			session.rollback();
			e.printStackTrace();
			throw new ServiceException("批量插入数据时发生错误！");
		} finally {
			session.close();
		}

	}
	
	@Override
	public <T> void batchHandleByDate(String mappaerId, int batchNum, Date date,List<T> dataObj) {
		SqlSessionFactory sqlSessionFactory = ApplicationContextRegister.getApplicationContext().getBean(SqlSessionFactory.class);
		SqlSession session = sqlSessionFactory.openSession(ExecutorType.BATCH, false);
		// 进位取整，获得计算得提交的次数
		int commitNum = (int) Math.ceil(dataObj.size() / (double) batchNum);
		//每次插入队列
		List<T> insertList = new ArrayList<T>(batchNum);
		Map<String,Object> intMap = new HashMap<String,Object>();
		intMap.put("date",date);
		try {
			//开始序号-start，结束序号-stop
			int start, stop;
			for (int i = 0; i < commitNum; i++) {
				intMap.remove("bd");
				insertList.clear();
				start = i * batchNum;
				//判断最后一条序号
				stop = Math.min(i * batchNum + batchNum - 1, dataObj.size() - 1);
				for (int j = start; j <= stop; j++) {
					insertList.add(dataObj.get(j));
				}
				intMap.put("bd",insertList);
				session.update(mappaerId, intMap);
				session.commit();
				session.clearCache();
			 }
		} catch (Exception e) {
			// 没有提交的数据可以回滚
			session.rollback();
			e.printStackTrace();
			throw new ServiceException("批量插入数据时发生错误！");
		} finally {
			session.close();
		}

	}
}
