package com.keao.edu.common.service.impl;

/**
 * GenericServiceImpl.java 
 * Copyright © 2015-2015
 * 
 * @author pengdc
 * @create 2015年7月13日
 */

import com.keao.edu.common.dal.BaseDAO;
import com.keao.edu.common.page.PageInfo;
import com.keao.edu.common.page.QueryInfo;
import com.keao.edu.common.service.BaseService;
import com.keao.edu.util.collection.MapUtil;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;

import java.io.Serializable;
import java.math.BigDecimal;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * SERVICE操作基类
 * @param <PK>
 * @param <T>
 */
public abstract class BaseServiceImpl<PK extends Serializable, T> implements BaseService<PK, T> {

	public abstract BaseDAO<PK, T> getDAO();

	@Autowired
	private SqlSessionFactory sqlSessionFactory;

	/**
	 * 通过主键id加载对象
	 * @param id
	 * @return
	 */
	public T get(final PK id) {
		return this.getDAO().get(id);
	}

	/**
	 * 更新实体对象
	 * @param bean
	 * @return int
	 */
	public int update(T bean) {
		return this.getDAO().update(bean);
	}

	/**
	 * 通过主键id删除对象
	 * @param id
	 * @return int
	 */
	public int delete(final PK id) {
		return this.getDAO().delete(id);
	}

	/**
	 * 写入实体对象
	 * @param bean
	 * @return int
	 */
	public long insert(T bean) {
		return this.getDAO().insert(bean);
	}

	/**
	 * 通过参数查找所有结果集
	 * @param params
	 * @return
	 */
	public List<T> findAll(Map<String, Object> params) {
		return this.getDAO().findAll(params);
	}

	/**
	 * 通过参数查找结果集，适合分页场景
	 * @param queryInfo
	 * @return
	 */
	public PageInfo<T> queryPage(QueryInfo queryInfo) {
		PageInfo<T> pageInfo = new PageInfo<T>(queryInfo.getPage(), queryInfo.getRows());
		Map<String, Object> params = new HashMap<String, Object>();
		MapUtil.populateMap(params, queryInfo);
		
		List<T> dataList = null;
		int count = this.findCount(params);
		if (count > 0) {
			pageInfo.setTotal(count);
			params.put("offset", pageInfo.getOffset());
			dataList = this.getDAO().queryPage(params);
		}
		if (count == 0) {
			dataList = new ArrayList<T>();
		}
		pageInfo.setRows(dataList);
		return pageInfo;
	}

	/**
	 * 通过参数查找结果集数目
	 * @author pengdc
	 * @param params
	 * @return
	 */
	public int findCount(Map<String, Object> params) {
		return this.getDAO().queryCount(params);
	}

	@Override
	public <K extends List, Y, Z> Map<Y,Z> getMap(String tableName, String columnKey, String columnValue, K ids, Class<Y> keyType, Class<Z> valueType){
		StringBuffer sql=new StringBuffer();
		Map<Y,Z> result=new HashMap();
		try {
			Connection connection = sqlSessionFactory.openSession().getConnection();
			sql.append("SELECT ").append(columnKey).append(",").append(columnValue).append(" FROM ").append(tableName).append(" WHERE ").append(columnKey).append(" IN (").append(StringUtils.join(ids, ",")).append(")");
			PreparedStatement ps = connection.prepareStatement(sql.toString());
			ResultSet resultSet = ps.executeQuery();
			while (resultSet.next()){
				Y key;
				Z value;
				if(keyType.isAssignableFrom(BigDecimal.class)){
					key = (Y) BigDecimal.class.getDeclaredConstructor(String.class).newInstance(resultSet.getString(1));
				}else if(keyType.isAssignableFrom(String.class)){
					key = (Y) resultSet.getString(1);
				}else{
					key = (Y) keyType.getMethod("valueOf", String.class).invoke(null,resultSet.getString(1));
				}
				if(valueType.isAssignableFrom(BigDecimal.class)){
					value = (Z) BigDecimal.class.getDeclaredConstructor(String.class).newInstance(resultSet.getString(2));
				}else if(valueType.isAssignableFrom(String.class)){
					value = (Z) resultSet.getString(2);
				}else{
					value = (Z) valueType.getMethod("valueOf", String.class).invoke(null,resultSet.getString(2));
				}
				result.put(key, value);
			}
			if(resultSet!=null){
				resultSet.close();
			}
			if(ps!=null){
				ps.close();
			}
			if(connection!=null){
				connection.close();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

		return result;
	}

	@Override
	public <K extends List, Y, Z> Map<Y,Z> getMap(String tableName, String columnKey, String columnValue, Map<String,Object> paramMap, Class<Y> keyType, Class<Z> valueType){
		StringBuffer sql=new StringBuffer();
		Map<Y,Z> result = new HashMap();
		try {
			Connection connection = sqlSessionFactory.openSession().getConnection();
			sql.append("SELECT ").append(columnKey).append(",").append(columnValue).append(" FROM ").append(tableName).append(" WHERE ");
			boolean resultFlag = false;
			for (Map.Entry<String, Object> entry : paramMap.entrySet()) {
				if(entry.getValue() instanceof List){
					List entryValue = (List) entry.getValue();
					if(!CollectionUtils.isEmpty(entryValue)){
						sql.append(entry.getKey()).append(" IN (").append(StringUtils.join(entryValue, ",")).append(") AND ");
						resultFlag = true;
					}
				}else {
					sql.append(entry.getKey()).append(" = ").append(entry.getValue()).append(" AND ");
					resultFlag = true;
				}
			}
			if (!resultFlag){
				return result;
			}
			sql.append(" 1 = 1 GROUP BY ").append(columnKey);
			PreparedStatement ps = connection.prepareStatement(sql.toString());
			ResultSet resultSet = ps.executeQuery();
			while (resultSet.next()){
				Y key;
				Z value;
				if(keyType.isAssignableFrom(BigDecimal.class)){
					key = (Y) BigDecimal.class.getDeclaredConstructor(String.class).newInstance(resultSet.getString(1));
				}else if(keyType.isAssignableFrom(String.class)){
					key = (Y) resultSet.getString(1);
				}else{
					key = (Y) keyType.getMethod("valueOf", String.class).invoke(null,resultSet.getString(1));
				}
				if(valueType.isAssignableFrom(BigDecimal.class)){
					value = (Z) BigDecimal.class.getDeclaredConstructor(String.class).newInstance(resultSet.getString(2));
				}else if(valueType.isAssignableFrom(String.class)){
					value = (Z) resultSet.getString(2);
				}else{
					value = (Z) valueType.getMethod("valueOf", String.class).invoke(null,resultSet.getString(2));
				}
				result.put(key, value);
			}
			if(resultSet!=null){
				resultSet.close();
			}
			if(ps!=null){
				ps.close();
			}
			if(connection!=null){
				connection.close();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

		return result;
	}

	@Override
	public <K extends List> Map<String, String> getMap(String tableName, String columnKey, String columnValue, K ids) {
		StringBuffer sql=new StringBuffer();
		Map<String, String> result=new HashMap();
		try {
			Connection connection = sqlSessionFactory.openSession().getConnection();
			sql.append("SELECT ").append(columnKey).append(",").append(columnValue).append(" FROM ").append(tableName).append(" WHERE ").append(columnKey).append(" IN (").append(StringUtils.join(ids, ",")).append(")");
			PreparedStatement ps = connection.prepareStatement(sql.toString());
			ResultSet resultSet = ps.executeQuery();
			while (resultSet.next()){
				result.put(resultSet.getString(1), resultSet.getString(2));
			}
			if(resultSet!=null){
				resultSet.close();
			}
			if(ps!=null){
				ps.close();
			}
			if(connection!=null){
				connection.close();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

		return result;
	}
}
