package com.wechat.global.base.dao.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.ibatis.session.RowBounds;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.dao.DataAccessException;

import com.wechat.global.base.dao.Pagination;
import com.wechat.global.base.dao.PagingResult;
import com.wechat.global.base.interceptor.Page;
import com.wechat.model.dbEntity.BaseEntity;
import com.wechat.model.dto.BasePageDto;

/**
 * @ClassName: BaseDao
 * @Description: 数据库操作基本实现类
 */
public abstract class BaseDao {

    @Resource
    public SqlSessionTemplate sqlSession;

    /**
     * @param sqlSession the sqlSession to set
     */
    public void setSqlSession(SqlSessionTemplate sqlSession) {
        this.sqlSession = sqlSession;
    }

    public Integer insert(String statementName, BaseEntity entity) {
        entity.setCreateDate(new Date());
        entity.setUpdateDate(new Date());
        return sqlSession.insert(statementName, entity);
    }

    /**
     * 更新
     * 
     * @param statementName
     * @param entity
     * @return
     */
    public Integer update(String statementName, BaseEntity entity) {
        entity.setUpdateDate(new Date());
        return sqlSession.update(statementName, entity);
    }

    /**
     * 删除
     * 
     * @param statementName
     * @param entity
     * @return
     */
    public int delete(String statementName, BaseEntity entity) {
        return sqlSession.delete(statementName, entity);
    }

    public <T> PagingResult<T> selectPagination(String statementName, String countStatementName, Pagination pagination)
            throws RuntimeException {
        try {
            // 默认为第一页
            int page = pagination.getPage();
            // 默认每页15个
            int size = pagination.getSize();
            //页码默认10
            int pageNoSize = pagination.getPageNoSize();

            RowBounds rowBounds = new RowBounds((page - 1) * size, size);

            Map<Object, Object> param = pagination.getParams();
            if (param != null) {
                param.put("orderColumn", pagination.getOrderColumn());
            }
            else {
                param = new HashMap<Object, Object>();
                param.put("orderColumn", pagination.getOrderColumn());
            }

            List<T> resultList = sqlSession.selectList(statementName, param, rowBounds);
            int totalSize = count(countStatementName, pagination.getParams());

            PagingResult<T> pagingResult = new PagingResult<T>();
            pagingResult.setCurrentPage(page);
            pagingResult.setTotalSize(totalSize);
            pagingResult.setPageSize(size);
            pagingResult.setResultList(resultList);
            pagingResult.setPageNoSize(pageNoSize);
            return pagingResult;
        }
        catch (DataAccessException e) {
            throw new RuntimeException(e);
        }
    }
    
    
	/**
	 * 查询分页数据
	 * @param <T>
	 * @param page 分页对象
	 * @param entity
	 * @return
	 */
	public <T> Page<T> findPage(Page<T> page, String statementName, BasePageDto<T> pageDto) {
		pageDto.setPage(page);
		page.setResultList(sqlSession.selectList(statementName, pageDto));
		return page;
	}

    public <T> T selectOne(String statementName, Object obj) throws RuntimeException {
        try {
            return sqlSession.selectOne(statementName, obj);
        }
        catch (DataAccessException e) {
            throw new RuntimeException(e);
        }
    }
    
    public <T> T selectOne(String statementName) throws RuntimeException {
        try {
            return sqlSession.selectOne(statementName);
        }
        catch (DataAccessException e) {
            throw new RuntimeException(e);
        }
    }

    public int count(String statementName, Object param) throws RuntimeException {
        try {
            return sqlSession.selectOne(statementName, param);
        }
        catch (DataAccessException e) {
            throw new RuntimeException(e);
        }
    }

    public <T> List<T> select(String statementName) throws RuntimeException {
        try {
            return sqlSession.selectList(statementName);
        }
        catch (DataAccessException e) {
            throw new RuntimeException(e);
        }
    }

    public <T> List<T> select(String statementName, Object obj) throws RuntimeException {
        try {
            return sqlSession.selectList(statementName, obj);
        }
        catch (DataAccessException e) {
            throw new RuntimeException(e);
        }
    }
}
