| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201 |
- package com.poyee.base.mapper.provider;
- import com.poyee.base.dto.BaseDto;
- import com.poyee.base.dto.BaseReq;
- import com.poyee.base.mapper.provider.domain.LeftJoinInfo;
- import com.poyee.base.mapper.provider.domain.MPage;
- import com.poyee.base.mapper.provider.domain.TableInfo;
- import com.poyee.base.mapper.provider.domain.WhereInfo;
- import com.poyee.base.mapper.provider.util.InsertUtil;
- import com.poyee.base.mapper.provider.util.SelectUtil;
- import com.poyee.base.mapper.provider.util.SqlUtil;
- import com.poyee.base.mapper.provider.util.UpdateUtil;
- import com.poyee.common.exception.ServiceException;
- import com.poyee.enums.DbMethodEnums;
- import lombok.extern.slf4j.Slf4j;
- import java.util.HashMap;
- import java.util.List;
- import java.util.Map;
- import java.util.Objects;
- import java.util.concurrent.ConcurrentHashMap;
- import java.util.concurrent.CopyOnWriteArrayList;
- /**
- * 提供基础的 SQL 构建功能,包括左连接和 WHERE 条件的处理。
- */
- @Slf4j
- public abstract class BaseProvider {
- // 数据库操作类型
- public DbMethodEnums dbMethod;
- // 表计数器
- public int tableCount = 0;
- //
- public boolean isPage = false;
- // 存储 insert
- public Object insert;
- // 记录 UPDATE
- public Object update;
- // 记录 SELECT
- public Object select;
- // 主表信息
- public TableInfo superTable;
- //分页查询
- public MPage mPage;
- //标记是否前端传 排序字段
- public boolean isFrontSort = false;
- //分页查询
- public String pageCountSql;
- // 记录 INSERT key-value(线程安全)
- public final Map<String, Object> insertMap = new HashMap<>();
- // 记录 UPDATE key-value(线程安全)
- public final Map<String, Object> updateMap = new HashMap<>();
- // 存储表信息的映射(线程安全)
- public final Map<String, TableInfo> tableInfos = new ConcurrentHashMap<>();
- // 记录 WHERE 条件(线程安全)
- public final List<WhereInfo> whereInfos = new CopyOnWriteArrayList<>();
- // 记录 SELECT 列(线程安全)
- public final List<String> selectColumns = new CopyOnWriteArrayList<>();
- // 记录 LEFT JOIN 信息(线程安全)
- public final List<LeftJoinInfo> leftJoinInfos = new CopyOnWriteArrayList<>();
- // 记录 ORDER BY 信息(线程安全)
- public final Map<Integer,String> orderByMap = new HashMap<>();
- /**
- * @param req
- * @param clazz
- * @param <T>
- * @param <R>
- * @return
- */
- public <T extends BaseReq,R extends BaseDto> MPage<R> mPage(T req, Class<R> clazz){
- MPage mPage = new MPage();
- Map<String,Object> map = new HashMap<>();
- map.put("req",req);
- map.put("clazz",clazz);
- String pageSql = pageWrapper(map);
- mPage.setCountSql(pageCountSql);
- mPage.setPageSql(pageSql);
- return mPage;
- }
- /**
- * @param object
- * @return
- */
- public String selectById(Object object) {
- // 检查传入的对象是否非空
- if (Objects.nonNull(object)) {
- dbMethod = DbMethodEnums.SELECT_BY_ID;
- // 组装 SELECT 语句
- SelectUtil.init(this).checkSelectBean(object).initSelect(select);
- // 生成最终的 SQL 字符串
- return SqlUtil.init(this).buildOne().toSql();
- }
- throw new ServiceException("传入对象为空!");
- }
- /**
- * @param object
- * @return
- */
- public String selectOne(Object object) {
- // 检查传入的对象是否非空
- if (Objects.nonNull(object)) {
- dbMethod = DbMethodEnums.SELECT;
- // 组装 SELECT 语句
- SelectUtil.init(this).checkSelectBean(object).initSelect(select);
- // 生成最终的 SQL 字符串
- return SqlUtil.init(this).buildOne().toSql();
- }
- throw new ServiceException("传入对象为空!");
- }
- /**
- * 处理查询对象 生成 SQL 字符串。
- * @param object
- * @return
- */
- public String selectSum(Object object){
- // 检查传入的对象是否非空
- if (Objects.nonNull(object)) {
- dbMethod = DbMethodEnums.SELECT;
- // 组装 SELECT 语句
- SelectUtil.init(this).checkSelectBean(object).initSelect(select);
- // 生成最终的 SQL 字符串
- return SqlUtil.init(this).buildSum().toSql();
- }
- throw new ServiceException("传入对象为空!");
- }
- /**
- * 处理新增对象 生成 SQL 字符串。
- * @param object
- * @return
- */
- public String save(Object object) {
- // 检查传入的对象是否非空
- if (Objects.nonNull(object)) {
- dbMethod = DbMethodEnums.INSERT;
- InsertUtil.init(this).checkInsertBean(object).initInsert();
- // 生成最终的 SQL 字符串
- String sql = SqlUtil.init(this).toSql();
- log.info("SQL:{}",sql);
- return sql;
- }
- // 如果传入的对象为空,则返回 null
- throw new ServiceException("传入的对象为空!");
- }
- public String update(Object object) {
- // 检查传入的对象是否非空
- if (Objects.nonNull(object)) {
- dbMethod = DbMethodEnums.UPDATE;
- UpdateUtil.init(this).checkUpdateBean(object).initUpdate();
- // 生成最终的 SQL 字符串
- return SqlUtil.init(this).toSql();
- }
- throw new ServiceException("传入的对象为空!");
- }
- /**
- *
- * @param object
- * @return
- */
- public String pageWrapper(Object object) {
- // 检查传入的对象是否非空
- if (Objects.nonNull(object)) {
- dbMethod = DbMethodEnums.SELECT;
- isPage = true;
- // 组装 SELECT 语句
- SelectUtil.init(this).checkSelectBean(object).initSelect(select);
- // 生成最终的 SQL 字符串
- return SqlUtil.init(this).buildPage().toSql();
- }
- // 如果传入的对象为空,则返回 null
- throw new ServiceException("传入的对象为空!");
- }
- /**
- * 处理对象的左连接和 WHERE 条件,生成 SQL 字符串。
- *
- * @param object 需要处理的对象
- * @return 生成的 SQL 字符串
- */
- public String leftJoinWrapper(Object object) {
- // 检查传入的对象是否非空
- if (Objects.nonNull(object)) {
- dbMethod = DbMethodEnums.SELECT;
- // 组装 SELECT 语句
- SelectUtil.init(this).checkSelectBean(object).initSelect(select);
- // 生成最终的 SQL 字符串
- return SqlUtil.init(this).toSql();
- }
- // 如果传入的对象为空,则返回 null
- throw new ServiceException("传入的对象为空!");
- }
- }
|