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

import com.github.pagehelper.PageInfo;
import com.hzya.frame.basedao.entity.ConditionType;
import com.hzya.frame.basedao.entity.ConnectType;
import com.hzya.frame.basedao.entity.ModifyCriterion;
import com.hzya.frame.basedao.entity.OrderCriterion;
import com.hzya.frame.basedao.entity.RequestDisposeEntity;
import com.hzya.frame.basedao.entity.ReturnCriterion;
import com.hzya.frame.basedao.entity.WhereCriterion;
import com.hzya.frame.basedao.service.BaseDaoUtil;
import com.hzya.frame.basedao.service.JdbcService;
import org.apache.commons.collections.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;


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

/**
 * @Author lvleigang
 * @Description 连接对外提供的类
 * @Date 1:45 下午 2022/12/3
 **/
@Service
public class BaseDaoUtilImpl implements BaseDaoUtil {

    private final Logger logger = LoggerFactory.getLogger(this.getClass());
    private static HashMap<String, List<ReturnCriterion>> filedMap;
    private static List<OrderCriterion> orderCriterionList;
    static {
        filedMap = new HashMap<>();
        List<ReturnCriterion> returnCriteria = new ArrayList<>();
        ReturnCriterion id = new ReturnCriterion("id");
        returnCriteria.add(id);
        ReturnCriterion age = new ReturnCriterion("age");
        returnCriteria.add(age);
        ReturnCriterion name = new ReturnCriterion("name");
        returnCriteria.add(name);
        ReturnCriterion updateDate = new ReturnCriterion("update_date", "updateDate");
        returnCriteria.add(updateDate);
        ReturnCriterion hireDate = new ReturnCriterion("hire_date", "hireDate");
        returnCriteria.add(hireDate);
        ReturnCriterion price = new ReturnCriterion("price");
        returnCriteria.add(price);
        filedMap.put("sys_testjdbc", returnCriteria);
    }
    static {
        orderCriterionList = new ArrayList<>();
        orderCriterionList.add(new OrderCriterion("id", "ASC"));
    }

    @Resource
    JdbcService jdbcService;

    /**
     * @param tablename 表名称
     * @return java.lang.Integer
     * @Author lvleigang
     * @Description 查询数量
     * @Date 8:46 上午 2022/12/7
     **/
    @Override
    public Integer selectCount(String tablename) {
        //设置对象
        RequestDisposeEntity example = setJdbcExample(tablename, null, null, null, null, null);
        //调用方法
        Integer counts = jdbcService.countByExample(example);
        return counts;
    }



    /**
     * @param tablename 表名称
     * @param whereList 条件
     * @return java.lang.Integer
     * @Author lvleigang
     * @Description 根据条件查询数量
     * @Date 9:06 上午 2022/12/7
     **/
    @Override
    public Integer selectCount(String tablename, List<WhereCriterion> whereList) {
        //设置对象
        RequestDisposeEntity example = setJdbcExample(tablename, null, null, whereList, null, null);
        //调用方法
        Integer counts = jdbcService.countByExample(example);
        return counts;
    }

    /**
     * 查询一个对象
     *
     * @param tablename 表名
     * @param whereList 条件列表
     * @return
     */
    @Override
    public HashMap<String, Object> selectOne(String tablename, List<WhereCriterion> whereList) {
        //设置对象
        RequestDisposeEntity example = setJdbcExample(tablename, null, null, whereList, null, null);
        //调用方法
        List<HashMap<String, Object>> list = jdbcService.selectByExample(example);
        if (CollectionUtils.isNotEmpty(list)) {
            if (list.size() == 1){
                return list.get(0);
            }else {
                throw new RuntimeException("select one but found"+list.size());
            }
        }
        return null;
    }

    /**
     * 查询一个对象
     *
     * @param tablename           表名
     * @param returnCriterionList 返回字段列表
     * @param whereList           查询条件列表
     * @return
     */
    @Override
    public HashMap<String, Object> selectOne(String tablename, List<ReturnCriterion> returnCriterionList, List<WhereCriterion> whereList) {
        //设置对象
        RequestDisposeEntity example = setJdbcExample(tablename, null, returnCriterionList, whereList, null, null);
        //调用方法
        List<HashMap<String, Object>> list = jdbcService.selectByExample(example);
        if (CollectionUtils.isNotEmpty(list)) {
            if (list.size() == 1){
                return list.get(0);
            }else {
                throw new RuntimeException("select one but found"+list.size());
            }
        }
        return null;
    }

    /**
     * 根据id查询
     *
     * @param tablename
     * @param id
     * @return
     */
    @Override
    public HashMap<String, Object> selectById(String tablename, String id) {
        List<WhereCriterion> whereList = new ArrayList<>();
        WhereCriterion whereCriterion = new WhereCriterion("","id", ConditionType.等于.getValue(), id,"", ConnectType.AND);
        whereList.add(whereCriterion);
        //设置对象
        RequestDisposeEntity example = setJdbcExample(tablename, null, null, whereList, null, null);
        //调用方法
        List<HashMap<String, Object>> list = jdbcService.selectByExample(example);
        if (CollectionUtils.isNotEmpty(list)) {
            if (list.size() == 1){
                return list.get(0);
            }else {
                throw new RuntimeException("select one but found"+list.size());
            }
        }
        return null;
    }

    /**
     * 根据id查询
     *
     * @param tablename           表名
     * @param id
     * @param returnCriterionList 返回字段列表
     * @return
     */
    @Override
    public HashMap<String, Object> selectById(String tablename, String id, List<ReturnCriterion> returnCriterionList) {
        List<WhereCriterion> whereList = new ArrayList<>();
        WhereCriterion whereCriterion = new WhereCriterion("","id", ConditionType.等于.getValue(), id,"", ConnectType.AND);
        whereList.add(whereCriterion);
        //设置对象
        RequestDisposeEntity example = setJdbcExample(tablename, null, returnCriterionList, whereList, null, null);
        //调用方法
        List<HashMap<String, Object>> list = jdbcService.selectByExample(example);
        if (CollectionUtils.isNotEmpty(list)) {
            if (list.size() == 1){
                return list.get(0);
            }else {
                throw new RuntimeException("select one but found"+list.size());
            }
        }
        return null;
    }

    /**
     * @param tablename
     * @return java.util.List<java.util.HashMap < java.lang.String, java.lang.Object>>
     * @Author lvleigang
     * @Description 根据表名查询数据
     * @Date 9:08 上午 2022/12/7
     **/
    @Override
    public List<HashMap<String, Object>> selectList(String tablename) {
        //设置对象
        RequestDisposeEntity example = setJdbcExample(tablename, null, null, null, null, null);
        //调用方法
        List<HashMap<String, Object>> list = jdbcService.selectByExample(example);
        return list;
    }

    /**
     * @param tablename 表名称
     * @param whereList 查询条件
     * @return java.util.List<java.util.HashMap < java.lang.String, java.lang.Object>>
     * @Author lvleigang
     * @Description 根据表名和条件查询数据
     * @Date 9:15 上午 2022/12/7
     **/
    @Override
    public List<HashMap<String, Object>> selectWhereList(String tablename, List<WhereCriterion> whereList) {
        //设置对象
        RequestDisposeEntity example = setJdbcExample(tablename, null, null, whereList, null, null);
        //调用方法
        List<HashMap<String, Object>> list = jdbcService.selectByExample(example);
        return list;
    }

    /**
     * @param tablename               表名称
     * @param returnCriterionListList 返回字段
     * @return java.util.List<java.util.HashMap < java.lang.String, java.lang.Object>>
     * @Author lvleigang
     * @Description 根据表名查询数据，且设置返回字段
     * @Date 9:15 上午 2022/12/7
     **/
    @Override
    public List<HashMap<String, Object>> selectReturnList(String tablename, List<ReturnCriterion> returnCriterionListList) {
        //设置对象
        RequestDisposeEntity example = setJdbcExample(tablename, null, returnCriterionListList, null, null, null);
        //调用方法
        List<HashMap<String, Object>> list = jdbcService.selectByExample(example);
        return list;
    }

    /**
     * @param tablename 表名称
     * @param order     排序
     * @return java.util.List<java.util.HashMap < java.lang.String, java.lang.Object>>
     * @Author lvleigang
     * @Description 根据表名排序查询数据
     * @Date 9:16 上午 2022/12/7
     **/
    @Override
    public List<HashMap<String, Object>> selectList(String tablename, List<OrderCriterion> order) {
        //设置对象
        RequestDisposeEntity example = setJdbcExample(tablename, order, null, null, null, null);
        //调用方法
        List<HashMap<String, Object>> list = jdbcService.selectByExample(example);
        return list;
    }

    /**
     * @param tablename 表名称
     * @param order     排序
     * @param whereList 条件
     * @return java.util.List<java.util.HashMap < java.lang.String, java.lang.Object>>
     * @Author lvleigang
     * @Description 根据表名和条件，排序查询数据
     * @Date 9:17 上午 2022/12/7
     **/
    @Override
    public List<HashMap<String, Object>> selectWhereList(String tablename, List<OrderCriterion> order, List<WhereCriterion> whereList) {
        //设置对象
        RequestDisposeEntity example = setJdbcExample(tablename, order, null, whereList, null, null);
        //调用方法
        List<HashMap<String, Object>> list = jdbcService.selectByExample(example);
        return list;
    }

    /**
     * @param tablename               表名称
     * @param order                   排序
     * @param returnCriterionListList 返回字段
     * @return java.util.List<java.util.HashMap < java.lang.String, java.lang.Object>>
     * @Author lvleigang
     * @Description 根据表名和返回字段，排序查询数据
     * @Date 9:17 上午 2022/12/7
     **/
    @Override
    public List<HashMap<String, Object>> selectReturnList(String tablename, List<OrderCriterion> order, List<ReturnCriterion> returnCriterionListList) {
        //设置对象
        RequestDisposeEntity example = setJdbcExample(tablename, order, returnCriterionListList, null, null, null);
        //调用方法
        List<HashMap<String, Object>> list = jdbcService.selectByExample(example);
        return list;
    }

    /**
     * @param tablename               表名称
     * @param order                   排序
     * @param returnCriterionListList 返回
     * @param whereList               条件
     * @return java.util.List<java.util.HashMap < java.lang.String, java.lang.Object>>
     * @Author lvleigang
     * @Description 根据表名称和条件，设置返回字段排序查询时间
     * @Date 9:18 上午 2022/12/7
     **/
    @Override
    public List<HashMap<String, Object>> selectList(String tablename, List<OrderCriterion> order, List<ReturnCriterion> returnCriterionListList, List<WhereCriterion> whereList) {
        //设置对象
        RequestDisposeEntity example = setJdbcExample(tablename, order, returnCriterionListList, whereList, null, null);
        //调用方法
        logger.info("getWheres:"+example.toString());
        List<HashMap<String, Object>> list = jdbcService.selectByExample(example);
        return list;
    }
    /**
     * @param tablename               表名称
     * @param returnCriterionListList 返回
     * @param whereList               条件
     * @return java.util.List<java.util.HashMap < java.lang.String, java.lang.Object>>
     * @Author lvleigang
     * @Description 根据表名称和条件，设置返回字段排序查询时间
     * @Date 9:18 上午 2022/12/7
     **/
    @Override
    public List<HashMap<String, Object>> selectList(String tablename, List<ReturnCriterion> returnCriterionListList, List<WhereCriterion> whereList) {
        //设置对象
        RequestDisposeEntity example = setJdbcExample(tablename, null, returnCriterionListList, whereList, null, null);
        //调用方法
        List<HashMap<String, Object>> list = jdbcService.selectByExample(example);
        return list;
    }

    /**
     * @param tablename
     * @return java.util.List<java.util.HashMap < java.lang.String, java.lang.Object>>
     * @Author lvleigang
     * @Description 根据表名分页查询数据，使用默认分页
     * @Date 9:08 上午 2022/12/7
     **/
    @Override
    public PageInfo selectPage(String tablename) {
        //设置对象
        RequestDisposeEntity example = setJdbcExample(tablename, null, null, null, null, null);
        //调用方法
        PageInfo pageInfo = jdbcService.queryPageByExample(example);
        return pageInfo;
    }

    /**
     * @param tablename
     * @return java.util.List<java.util.HashMap < java.lang.String, java.lang.Object>>
     * @Author lvleigang
     * @Description 根据表名分页查询数据
     * @Date 9:08 上午 2022/12/7
     **/
    @Override
    public PageInfo selectPage(String tablename, Integer pageNum, Integer pageSize) {
        //设置对象
        RequestDisposeEntity example = setJdbcExample(tablename, null, null, null, pageNum, pageSize);
        //调用方法
        PageInfo pageInfo = jdbcService.queryPageByExample(example);
        return pageInfo;
    }

    /**
     * @param tablename 表名称
     * @param whereList 查询条件
     * @return java.util.List<java.util.HashMap < java.lang.String, java.lang.Object>>
     * @Author lvleigang
     * @Description 根据表名和条件分页查询数据
     * @Date 9:15 上午 2022/12/7
     **/
    @Override
    public PageInfo selectWherePage(String tablename, List<WhereCriterion> whereList) {
        //设置对象
        RequestDisposeEntity example = setJdbcExample(tablename, null, null, whereList, null, null);
        //调用方法
        PageInfo pageInfo = jdbcService.queryPageByExample(example);
        return pageInfo;
    }

    /**
     * @param tablename 表名称
     * @param whereList 查询条件
     * @return java.util.List<java.util.HashMap < java.lang.String, java.lang.Object>>
     * @Author lvleigang
     * @Description 根据表名和条件分页查询数据
     * @Date 9:15 上午 2022/12/7
     **/
    @Override
    public PageInfo selectWherePage(String tablename, List<WhereCriterion> whereList, Integer pageNum, Integer pageSize) {
        //设置对象
        RequestDisposeEntity example = setJdbcExample(tablename, null, null, whereList, pageNum, pageSize);
        //调用方法
        PageInfo pageInfo = jdbcService.queryPageByExample(example);
        return pageInfo;
    }

    /**
     * @param tablename               表名称
     * @param returnCriterionListList 返回字段
     * @return java.util.List<java.util.HashMap < java.lang.String, java.lang.Object>>
     * @Author lvleigang
     * @Description 根据表名分页查询数据，且设置返回字段
     * @Date 9:15 上午 2022/12/7
     **/
    @Override
    public PageInfo selectReturnPage(String tablename, List<ReturnCriterion> returnCriterionListList) {
        //设置对象
        RequestDisposeEntity example = setJdbcExample(tablename, null, returnCriterionListList, null, null, null);
        //调用方法
        PageInfo pageInfo = jdbcService.queryPageByExample(example);
        return pageInfo;
    }

    /**
     * @param tablename               表名称
     * @param returnCriterionListList 返回字段
     * @return java.util.List<java.util.HashMap < java.lang.String, java.lang.Object>>
     * @Author lvleigang
     * @Description 根据表名分页查询数据，且设置返回字段
     * @Date 9:15 上午 2022/12/7
     **/
    @Override
    public PageInfo selectReturnPage(String tablename, List<ReturnCriterion> returnCriterionListList, Integer pageNum, Integer pageSize) {
        //设置对象
        RequestDisposeEntity example = setJdbcExample(tablename, null, returnCriterionListList, null, pageNum, pageSize);
        //调用方法
        PageInfo pageInfo = jdbcService.queryPageByExample(example);
        return pageInfo;
    }

    /**
     * @param tablename 表名称
     * @param order     排序
     * @return java.util.List<java.util.HashMap < java.lang.String, java.lang.Object>>
     * @Author lvleigang
     * @Description 根据表名排序查询数据
     * @Date 9:16 上午 2022/12/7
     **/
    @Override
    public PageInfo selectPage(String tablename, List<OrderCriterion> order) {
        //设置对象
        RequestDisposeEntity example = setJdbcExample(tablename, order, null, null, null, null);
        //调用方法
        PageInfo pageInfo = jdbcService.queryPageByExample(example);
        return pageInfo;
    }

    /**
     * @param tablename 表名称
     * @param order     排序
     * @return java.util.List<java.util.HashMap < java.lang.String, java.lang.Object>>
     * @Author lvleigang
     * @Description 根据表名排序查询数据
     * @Date 9:16 上午 2022/12/7
     **/
    @Override
    public PageInfo selectPage(String tablename, List<OrderCriterion> order, Integer pageNum, Integer pageSize) {
        //设置对象
        RequestDisposeEntity example = setJdbcExample(tablename, order, null, null, pageNum, pageSize);
        //调用方法
        PageInfo pageInfo = jdbcService.queryPageByExample(example);
        return pageInfo;
    }

    /**
     * @param tablename 表名称
     * @param order     排序
     * @param whereList 条件
     * @return java.util.List<java.util.HashMap < java.lang.String, java.lang.Object>>
     * @Author lvleigang
     * @Description 根据表名和条件，排序查询数据
     * @Date 9:17 上午 2022/12/7
     **/
    @Override
    public PageInfo selectWherePage(String tablename, List<OrderCriterion> order, List<WhereCriterion> whereList) {
        //设置对象
        RequestDisposeEntity example = setJdbcExample(tablename, order, null, whereList, null, null);
        //调用方法
        PageInfo pageInfo = jdbcService.queryPageByExample(example);
        return pageInfo;
    }

    /**
     * @param tablename 表名称
     * @param order     排序
     * @param whereList 条件
     * @return java.util.List<java.util.HashMap < java.lang.String, java.lang.Object>>
     * @Author lvleigang
     * @Description 根据表名和条件，排序查询数据
     * @Date 9:17 上午 2022/12/7
     **/
    @Override
    public PageInfo selectWherePage(String tablename, List<OrderCriterion> order, List<WhereCriterion> whereList, Integer pageNum, Integer pageSize) {
        //设置对象
        RequestDisposeEntity example = setJdbcExample(tablename, order, null, whereList, pageNum, pageSize);
        //调用方法
        PageInfo pageInfo = jdbcService.queryPageByExample(example);
        return pageInfo;
    }

    /**
     * @param tablename               表名称
     * @param order                   排序
     * @param returnCriterionListList 返回字段
     * @return java.util.List<java.util.HashMap < java.lang.String, java.lang.Object>>
     * @Author lvleigang
     * @Description 根据表名和返回字段，排序查询数据
     * @Date 9:17 上午 2022/12/7
     **/
    @Override
    public PageInfo selectReturnPage(String tablename, List<OrderCriterion> order, List<ReturnCriterion> returnCriterionListList) {
        //设置对象
        RequestDisposeEntity example = setJdbcExample(tablename, order, returnCriterionListList, null, null, null);
        //调用方法
        PageInfo pageInfo = jdbcService.queryPageByExample(example);
        return pageInfo;
    }

    /**
     * @param tablename               表名称
     * @param order                   排序
     * @param returnCriterionListList 返回字段
     * @return java.util.List<java.util.HashMap < java.lang.String, java.lang.Object>>
     * @Author lvleigang
     * @Description 根据表名和返回字段，排序查询数据
     * @Date 9:17 上午 2022/12/7
     **/
    @Override
    public PageInfo selectReturnPage(String tablename, List<OrderCriterion> order, List<ReturnCriterion> returnCriterionListList, Integer pageNum, Integer pageSize) {
        //设置对象
        RequestDisposeEntity example = setJdbcExample(tablename, order, returnCriterionListList, null, pageNum, pageSize);
        //调用方法
        PageInfo pageInfo = jdbcService.queryPageByExample(example);
        return pageInfo;
    }

    /**
     * @param tablename               表名称
     * @param order                   排序
     * @param returnCriterionListList 返回
     * @param whereList               条件
     * @return java.util.List<java.util.HashMap < java.lang.String, java.lang.Object>>
     * @Author lvleigang
     * @Description 根据表名称和条件，设置返回字段排序查询时间
     * @Date 9:18 上午 2022/12/7
     **/
    @Override
    public PageInfo selectPage(String tablename, List<OrderCriterion> order, List<ReturnCriterion> returnCriterionListList, List<WhereCriterion> whereList) {
        //设置对象
        RequestDisposeEntity example = setJdbcExample(tablename, order, returnCriterionListList, whereList, null, null);
        //调用方法
        PageInfo pageInfo = jdbcService.queryPageByExample(example);
        return pageInfo;
    }

    /**
     * @param tablename               表名称
     * @param order                   排序
     * @param returnCriterionListList 返回
     * @param whereList               条件
     * @return java.util.List<java.util.HashMap < java.lang.String, java.lang.Object>>
     * @Author lvleigang
     * @Description 根据表名称和条件，设置返回字段排序查询时间
     * @Date 9:18 上午 2022/12/7
     **/
    @Override
    public PageInfo selectPage(String tablename, List<OrderCriterion> order, List<ReturnCriterion> returnCriterionListList, List<WhereCriterion> whereList, Integer pageNum, Integer pageSize) {
        RequestDisposeEntity example = setJdbcExample(tablename, order, returnCriterionListList, whereList, pageNum, pageSize);
        //调用方法
        PageInfo pageInfo = jdbcService.queryPageByExample(example);
        return pageInfo;
    }


    /**
     * @param tablename      表名称
     * @param modifyExamples 单个对象值
     * @return java.lang.Integer
     * @Author lvleigang
     * @Description 添加单条记录
     * @Date 9:27 上午 2022/12/7
     **/
    @Override
    public Integer insterOne(String tablename, List<ModifyCriterion> modifyExamples) {
        checkTableName(tablename);
        checklist(modifyExamples);
        //设置表
        RequestDisposeEntity example = new RequestDisposeEntity(tablename);
        example.setModifys(modifyExamples);
        //调用方法
        Integer counts = jdbcService.insert(example);
        return counts;
    }



    /**
     * @param tablename               表名称
     * @param filedExamples           字段名称
     * @param selectiveModifyExamples 多个对象值
     * @return java.lang.Integer
     * @Author lvleigang
     * @Description 添加多条记录
     * @Date 9:27 上午 2022/12/7
     **/
    @Override
    public Integer insterList(String tablename, List<String> filedExamples, List<List<ModifyCriterion>> selectiveModifyExamples) {
        checkTableName(tablename);
        checklist(filedExamples);
        checklist(selectiveModifyExamples);
        //设置表
        RequestDisposeEntity example = new RequestDisposeEntity(tablename);
        example.setBatchFileds(filedExamples);
        example.setBatchModifys(selectiveModifyExamples);
        //调用查询
        Integer counts = jdbcService.insertSelective(example);
        return counts;
    }

    /**
     * @param tablename      表名称
     * @param modifyExamples 需要修改后的数据
     * @param whereList      条件
     * @return java.lang.Integer
     * @Author lvleigang
     * @Description 修改数据
     * @Date 9:27 上午 2022/12/7
     **/
    @Override
    public Integer update(String tablename, List<ModifyCriterion> modifyExamples, List<WhereCriterion> whereList) {
        checkTableName(tablename);
        checklist(modifyExamples);
        checklist(whereList);
        //设置表
        RequestDisposeEntity example = new RequestDisposeEntity(tablename);
        example.setModifys(modifyExamples);
        //设置条件
        example.setWheres(whereList);
        //调用方法
        Integer counts = jdbcService.updateByExample(example);
        return counts;
    }

    /**
     * @param tablename 表名称
     * @param whereList 条件
     * @return java.lang.Integer
     * @Author lvleigang
     * @Description 逻辑删除数据
     * @Date 9:27 上午 2022/12/7
     **/
    @Override
    public Integer deleteLogic(String tablename, List<ModifyCriterion> modifyExamples, List<WhereCriterion> whereList) {
        checkTableName(tablename);
        checklist(modifyExamples);
        checklist(whereList);
        //设置表
        RequestDisposeEntity example = new RequestDisposeEntity(tablename);
        example.setModifys(modifyExamples);
        //设置条件
        example.setWheres(whereList);
        //调用方法
        Integer counts = jdbcService.updateByExample(example);
        return counts;
    }

    /**
     * @param tablename 表名称
     * @param id        id
     * @return java.lang.Integer
     * @Author lvleigang
     * @Description 删除数据
     * @Date 9:27 上午 2022/12/7
     **/
    @Override
    public Integer deleteLogic(String tablename, String id) {
        checkTableName(tablename);
        //设置表
        RequestDisposeEntity example = new RequestDisposeEntity(tablename);
        //设置修改后的数据
        List<ModifyCriterion> modifyExamples = new ArrayList<>();
        modifyExamples.add(new ModifyCriterion("sts", "N"));
        example.setModifys(modifyExamples);
        List<WhereCriterion> wheres = new ArrayList<>();
        WhereCriterion whereCriterion = new WhereCriterion("(","id", ConditionType.等于.getValue(), id,")", ConnectType.AND);
        wheres.add(whereCriterion);
        example.setWheres(wheres);
        //调用方法
        Integer counts = jdbcService.updateByExample(example);
        return counts;

    }

    /**
     * @param tablename 表名称
     * @param whereList 条件
     * @return java.lang.Integer
     * @Author lvleigang
     * @Description 删除数据
     * @Date 9:27 上午 2022/12/7
     **/
    @Override
    public Integer delete(String tablename, List<WhereCriterion> whereList) {
        checkTableName(tablename);
        //设置表
        RequestDisposeEntity example = new RequestDisposeEntity(tablename);
        if(whereList != null && whereList.size() >0 ){
            //设置条件
            example.setWheres(whereList);
        }
        //调用方法
        Integer counts = jdbcService.deleteByExample(example);
        return counts;
    }

    /**
     * @param tablename 表名称
     * @param id        条件
     * @return java.lang.Integer
     * @Author lvleigang
     * @Description 删除数据
     * @Date 9:27 上午 2022/12/7
     **/
    @Override
    public Integer delete(String tablename, String id) {
        checkTableName(tablename);
        //设置表
        RequestDisposeEntity example = new RequestDisposeEntity(tablename);

        //设置条件
        List<WhereCriterion> wheres = new ArrayList<>();
        WhereCriterion whereCriterion = new WhereCriterion("(","id", ConditionType.等于.getValue(), id,")",ConnectType.AND);
        wheres.add(whereCriterion);
        example.setWheres(wheres);
        //调用方法
        Integer counts = jdbcService.deleteByExample(example);
        return counts;
    }

    /**
     * @param tablename
     * @return java.util.List<com.hzya.frame.jdbcUtil.entity.ReturnCriterion>
     * @Author lvleigang
     * @Description 根据表名获取缓存中的字段
     * @Date 11:10 上午 2022/12/7
     **/
    private List<ReturnCriterion> getReturnByTablename(String tablename) {
        List<ReturnCriterion> returnCriteria = null;
        if (tablename != null && !"".equals(tablename)) {
            returnCriteria = filedMap.get(tablename);
        }
        return returnCriteria;
    }

    /**
     * @param tablename
     * @return void
     * @Author lvleigang
     * @Description 校验表名称
     * @Date 11:41 上午 2022/12/7
     **/
    private void checkTableName(String tablename) {
        if (tablename == null || "".equals(tablename)) {
            throw new RuntimeException("The tablename cannot be empty");
        }
    }

    /**
     * @param example
     * @param order
     * @return void
     * @Author lvleigang
     * @Description 设置排序
     * @Date 11:41 上午 2022/12/7
     **/
    private void setOrder(RequestDisposeEntity example, List<OrderCriterion> order) {
        if (order != null && order.size() > 0) {
            example.setOrder(order);
        } else {
            example.setOrder(orderCriterionList);
        }
    }

    /**
     * @param example
     * @param tablename
     * @param returnCriterionListList
     * @return void
     * @Author lvleigang
     * @Description 设置查询字段
     * @Date 11:42 上午 2022/12/7
     **/
    private void setReturnFiled(RequestDisposeEntity example, String tablename, List<ReturnCriterion> returnCriterionListList) {
        if (returnCriterionListList != null && returnCriterionListList.size() > 0) {
            example.setReturnCriterions(returnCriterionListList);
        } else {
            //获取缓存中表字段
            List<ReturnCriterion> returnCriterionList = getReturnByTablename(tablename);
            //如果还没有就 查询 *
            if (returnCriterionList != null && returnCriterionList.size() > 0) {
                //设置返回字段
                example.setReturnCriterions(returnCriterionListList);
            }
        }
    }

    /**
     * @param example
     * @param whereList
     * @return void
     * @Author lvleigang
     * @Description 设置where条件
     * @Date 11:43 上午 2022/12/7
     **/
    private void setWhere(RequestDisposeEntity example, List<WhereCriterion> whereList) {
        if (whereList != null && whereList.size() > 0) {
            example.setWheres(whereList);
        }
    }

    /**
     * @param tablename
     * @param order
     * @param returnCriterionListList
     * @param whereList
     * @param pageNum
     * @param pageSize
     * @return com.hzya.frame.jdbcUtil.entity.JdbcExample
     * @Author lvleigang
     * @Description 设置查询对象
     * @Date 11:48 上午 2022/12/7
     **/
    @Override
    public RequestDisposeEntity setJdbcExample(String tablename, List<OrderCriterion> order, List<ReturnCriterion> returnCriterionListList, List<WhereCriterion> whereList, Integer pageNum, Integer pageSize) {
        //校验表名
        checkTableName(tablename);
        //设置表
        RequestDisposeEntity example = new RequestDisposeEntity(tablename);
        //设置排序
        setOrder(example, order);
        //设置查询字段
        setReturnFiled(example, tablename, returnCriterionListList);
        //设置where条件
        setWhere(example, whereList);
        //设置分页
        setPage(example, pageNum, pageSize);
        return example;
    }


    /**
     * @param example
     * @param pageNum
     * @param pageSize
     * @return void
     * @Author lvleigang
     * @Description 设置分页
     * @Date 11:46 上午 2022/12/7
     **/
    private void setPage(RequestDisposeEntity example, Integer pageNum, Integer pageSize) {
        example.setPageNum(pageNum);
        example.setPageSize(pageSize);
    }

    /**
     * @Author lvleigang
     * @Description  校验list
     * @Date 1:45 下午 2022/12/7
     * @param list
     * @return void
     **/
    private void checklist(List list) {
        if(list == null || list.size() == 0 ){
            throw new RuntimeException("The list cannot be empty");
        }
    }
}
