package com.hzya.frame.basedao.service.impl;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hzya.frame.basedao.entity.RequestDisposeEntity;
import com.hzya.frame.basedao.service.JdbcService;
import com.hzya.frame.datasource.DataSourceUtil;
import com.hzya.frame.web.exception.BaseSystemException;
import org.apache.ibatis.session.SqlSession;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;

/**
 * @Author lvleigang
 * @Description jdbd 连接对外提供的类
 * @Date 1:45 下午 2022/12/3
 **/
@Service
public class JdbcServiceImpl implements JdbcService {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());
    //默认每页查询数量
    private final Integer pageSize = 10;


    @Resource
    private SqlSession sqlSession;
    /**
     * @param example
     * @return int
     * @Author lvleigang
     * @Description 查询总数
     * @Date 2:51 下午 2022/12/3
     **/
    @Override
    public int countByExample(RequestDisposeEntity example) throws BaseSystemException {
        if (example == null) {
            throw new RuntimeException("The Example cannot be empty");
        }
        //try(SqlSession sqlSession = new DataSourceUtil().getDefaultSqlSession();) {
            int counts = sqlSession.selectOne("countByExample", example);
            return counts;
        //}
    }

    /**
     * @param example
     * @return java.util.List<java.lang.Object>
     * @Author lvleigang
     * @Description 查询数据
     * @Date 2:53 下午 2022/12/3
     **/
    @Override
    public List<HashMap<String, Object>> selectByExample(RequestDisposeEntity example)  throws BaseSystemException {
        if (example == null) {
            throw new RuntimeException("The Example cannot be empty");
        }
        if (example.isHavenOrder()) {
            PageHelper.orderBy(example.getOrderString());
        }
        //try(SqlSession sqlSession = new DataSourceUtil().getDefaultSqlSession();) {
            List<HashMap<String, Object>> list = sqlSession.selectList("selectByExample", example);
            return list;
        //}
    }
    /**
     * @param sql
     * @return java.util.List<java.lang.Object>
     * @Author lvleigang
     * @Description 自定义sql查询查询
     * @Date 2:53 下午 2022/12/3
     **/
    @Override
    public List<HashMap<String, Object>> selectByExample(String sql)  throws BaseSystemException {
        if (sql == null || "".equals(sql)) {
            throw new RuntimeException("The sql cannot be empty");
        }
        //try(SqlSession sqlSession = new DataSourceUtil().getDefaultSqlSession();) {
            List<HashMap<String, Object>> list = sqlSession.selectList("selectBySql", sql);
            return list;
        //}
    }
    /**
     * @param example
     * @return PageInfo
     * @Author lvleigang
     * @Description 分页查询数据
     * @Date 2:53 下午 2022/12/3
     **/
    @Override
    public PageInfo queryPageByExample(RequestDisposeEntity example)  throws BaseSystemException {
        if (example == null) {
            throw new RuntimeException("The Example cannot be empty");
        }
        if (example.isHavenOrder()) {
            PageHelper.startPage(example.getPageNum() != null ? example.getPageNum() : 1, example.getPageSize() != null ? example.getPageSize() : pageSize, example.getOrderString());
        } else {
            PageHelper.startPage(example.getPageNum() != null ? example.getPageNum() : 1, example.getPageSize() != null ? example.getPageSize() : pageSize);
        }
        PageInfo pageInfo = null;
        long timeMillis1 = System.currentTimeMillis();
        //try(SqlSession sqlSession = new DataSourceUtil().getDefaultSqlSession();) {
            long timeMillis2 = System.currentTimeMillis();
            List<HashMap<String, Object>> list = sqlSession.selectList("selectByExample", example);
            long timeMillis3 = System.currentTimeMillis();
            String sql ="select * from sys_module WHERE ( lvl = '0' ) AND ( sts = 'Y' )";
            List<HashMap<String, Object>> list1 = sqlSession.selectList("selectBySql", sql);
            long timeMillis4 = System.currentTimeMillis();
            logger.error("获取session时间:{}", timeMillis2 - timeMillis1);
            logger.error("拼接查询时间:{}", timeMillis3 - timeMillis2);
            logger.error("sql查询时间:{}", timeMillis4 - timeMillis3);
            pageInfo = new PageInfo(list);
        //}
        long timeMillis5 = System.currentTimeMillis();
        logger.error("总体执行时间:{}", timeMillis5 - timeMillis1);

        return pageInfo;
    }

    @Override
    public PageInfo queryPageByExample(String sql) {
        if (sql == null || "".equals(sql)) {
            throw new RuntimeException("The sql cannot be empty");
        }
        PageHelper.startPage( 1,pageSize);
        //try(SqlSession sqlSession = new DataSourceUtil().getDefaultSqlSession();) {
            List<HashMap<String, Object>> list = sqlSession.selectList("selectByExample", sql);
            PageInfo pageInfo = new PageInfo(list);
            return pageInfo;
        //}
    }

    /**
     * @param example
     * @return int
     * @Author lvleigang
     * @Description 添加数据
     * @Date 2:52 下午 2022/12/3
     **/
    @Override
    public Integer insert(RequestDisposeEntity example)  throws BaseSystemException {
        if (example == null) {
            throw new RuntimeException("The Example cannot be empty");
        }
        if (!example.isHaveModifys()) {
            throw new RuntimeException("The ModifyExamples cannot be empty");
        }
        //try(SqlSession sqlSession = new DataSourceUtil().getDefaultSqlSession();) {
            int insertCount = sqlSession.insert("insert", example);
            return insertCount;
        //}
    }

    /**
     * @param example
     * @return int
     * @Author lvleigang
     * @Description 批量添加数据
     * @Date 2:53 下午 2022/12/3
     **/
    @Override
    public int insertSelective(RequestDisposeEntity example)  throws BaseSystemException {
        if (example == null) {
            throw new RuntimeException("The Example cannot be empty");
        }
        if (!example.isHaveBatchFileds()) {
            throw new RuntimeException("The BatchFileds cannot be empty");
        }
        if (!example.isHaveBatchModifys()) {
            throw new RuntimeException("The BatchModifys cannot be empty");
        }
        //try(SqlSession sqlSession = new DataSourceUtil().getDefaultSqlSession();) {
            int insertCount = sqlSession.insert("insertSelective", example);
            return insertCount;
        //}
    }

    /**
     * @param example
     * @return int
     * @Author lvleigang
     * @Description 删除
     * @Date 2:52 下午 2022/12/3
     **/
    @Override
    public int deleteByExample(RequestDisposeEntity example)  throws BaseSystemException {
        if (example == null) {
            throw new RuntimeException("The Example cannot be empty");
        }
        //try(SqlSession sqlSession = new DataSourceUtil().getDefaultSqlSession();) {
            int deleteCount = sqlSession.insert("deleteByExample", example);
            return deleteCount;
        //}
    }

    /**
     * @param example
     * @return int
     * @Author lvleigang
     * @Description 根据条件修改
     * @Date 2:54 下午 2022/12/3
     **/
    @Override
    public int updateByExample(RequestDisposeEntity example)  throws BaseSystemException {
        if (example == null) {
            throw new RuntimeException("The Example cannot be empty");
        }
        if (!example.isHaveModifys()) {
            throw new RuntimeException("The ModifyExamples cannot be empty");
        }
        //try(SqlSession sqlSession = new DataSourceUtil().getDefaultSqlSession();) {
            int updateCount = sqlSession.insert("updateByExample", example);
            return updateCount;
        //}
    }


}
