Wrappers.java 8.89 KB
package com.huaheng.common.utils;

import com.baomidou.mybatisplus.core.conditions.ISqlSegment;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.segments.MergeSegments;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.huaheng.common.utils.security.ShiroUtils;

import java.util.Collections;
import java.util.Map;

/**
 * @BelongsProject: wms_wuhangelinmei
 * @BelongsPackage: com.huaheng.common.utils
 * @Author: zhouhong
 * @CreateTime: 2022-07-13  16:14
 * @Description: TODO
 * @Version: 1.0
 */
public class Wrappers {
    /**
     * true查询所有  false按照登录仓库查询
     */
    private static boolean dataAllByWarehouse=false;
    private static String warehouselist=null;

    /**
     * 空的 EmptyWrapper
     */
    private static final QueryWrapper<?> emptyWrapper = new EmptyWrapper<>();

    private Wrappers() {
        // ignore
    }
    public static void setWarehouseList(String str){
        warehouselist=str;
    }
    public static void initWarehouseList(){
        warehouselist=null;
    }

    public static void setWarehouseFindAll(boolean tag){
        dataAllByWarehouse=tag;
    }
    public static void initWarehouseFindAll(){
        if(dataAllByWarehouse){
            dataAllByWarehouse=false;
        }
    }
    /**
     * 获取 QueryWrapper&lt;T&gt;
     *
     * @param <T> 实体类泛型
     * @return QueryWrapper&lt;T&gt;
     */
    public static <T> QueryWrapper<T> query() {
        return new QueryWrapper<>();
    }

    /**
     * 获取 QueryWrapper&lt;T&gt;
     *
     * @param entity 实体类
     * @param <T>    实体类泛型
     * @return QueryWrapper&lt;T&gt;
     */
    public static <T> QueryWrapper<T> query(T entity) {
        return new QueryWrapper<>(entity);
    }

    /**
     * 获取 LambdaQueryWrapper&lt;T&gt;
     * 1.默认查询当前登录仓库数据,未登录则查询所有
     * 2.dataAllByWarehouse默认为false,查询当前仓库数据,为true时,查询所有仓库数据
     * 3.dataAllByWarehouse用完要设为false,不影响后面的查询
     * 4.也可以指定查询哪几个仓库warehouselist,如:"CS0001,CS0002"
     * @param <T> 实体类泛型
     * @return LambdaQueryWrapper&lt;T&gt;
     */
    /*   也可以指定查询哪几个仓库warehouselist,如:"CS0001,CS0002"
    if(StringUtils.isNotEmpty(warehouselist)){
        String[] str_spplit=warehouselist.split(",");
        wrapper.in("warehouseCode",str_spplit);
    }else{
        wrapper.eq("warehouseCode", ShiroUtils.getWarehouseCode());
    }*/
    public static <T> LambdaQueryWrapper<T> lambdaQuery() {
        QueryWrapper<T> wrapper=new QueryWrapper<>();
        if(ShiroUtils.getUser()!=null&&StringUtils.isNotEmpty(ShiroUtils.getWarehouseCode())){
            if (!dataAllByWarehouse){
                wrapper.eq("warehouseCode", ShiroUtils.getWarehouseCode());
            }
        }
        initWarehouseFindAll();
        LambdaQueryWrapper<T> result=wrapper.lambda();
        return  result;
    }
    /**
     * 1.表没有warehouseCode字段的用该方法
     * 2.查询所有仓库数据
     * 获取 LambdaQueryWrapper&lt;T&gt;
     * @param <T> 实体类泛型
     * @return LambdaQueryWrapper&lt;T&gt;
     */
    public static <T> LambdaQueryWrapper<T> lambdaQueryNoWarehouse() {
        return new LambdaQueryWrapper<>();
    }

    /**
     * 获取 LambdaQueryWrapper&lt;T&gt;
     *
     * @param entity 实体类
     * @param <T>    实体类泛型
     * @return LambdaQueryWrapper&lt;T&gt;
     */
    public static <T> LambdaQueryWrapper<T> lambdaQuery(T entity) {
        QueryWrapper<T> wrapper=new QueryWrapper<>(entity);
        if(ShiroUtils.getUser()!=null){
            if (!dataAllByWarehouse){
                wrapper.eq("warehouseCode", ShiroUtils.getWarehouseCode());
            }
        }
        initWarehouseFindAll();
        LambdaQueryWrapper<T> result=wrapper.lambda();
        return  result;
    }
    /**
     * 获取 LambdaQueryWrapper&lt;T&gt;
     *
     * @param entity 实体类
     * @param <T>    实体类泛型
     * @return LambdaQueryWrapper&lt;T&gt;
     */
    public static <T> LambdaQueryWrapper<T> lambdaQueryNoWarehouse(T entity) {
        return new LambdaQueryWrapper<>(entity);
    }

    /**
     * 获取 UpdateWrapper&lt;T&gt;
     *
     * @param <T> 实体类泛型
     * @return UpdateWrapper&lt;T&gt;
     */
    public static <T> UpdateWrapper<T> update() {
        return new UpdateWrapper<>();
    }

    /**
     * 获取 UpdateWrapper&lt;T&gt;
     *
     * @param entity 实体类
     * @param <T>    实体类泛型
     * @return UpdateWrapper&lt;T&gt;
     */
    public static <T> UpdateWrapper<T> update(T entity) {
        return new UpdateWrapper<>(entity);
    }

    /**
     * 获取 LambdaUpdateWrapper&lt;T&gt;
     *
     * @param <T> 实体类泛型
     * @return LambdaUpdateWrapper&lt;T&gt;
     */
    public static <T> LambdaUpdateWrapper<T> lambdaUpdate() {
        UpdateWrapper<T> wrapper=new UpdateWrapper<>();
        if(ShiroUtils.getUser()!=null){
            wrapper.eq("warehouseCode", ShiroUtils.getWarehouseCode());
        }
        LambdaUpdateWrapper<T> result=wrapper.lambda();
        return result;
    }
    /**
     * 获取 LambdaUpdateWrapper&lt;T&gt;
     *
     * @param <T> 实体类泛型
     * @return LambdaUpdateWrapper&lt;T&gt;
     */
    public static <T> LambdaUpdateWrapper<T> lambdaUpdateNoWarehouse() {
        return new LambdaUpdateWrapper<>();
    }

    /**
     * 获取 LambdaUpdateWrapper&lt;T&gt;
     *
     * @param entity 实体类
     * @param <T>    实体类泛型
     * @return LambdaUpdateWrapper&lt;T&gt;
     */
    public static <T> LambdaUpdateWrapper<T> lambdaUpdate(T entity) {
        UpdateWrapper<T> wrapper=new UpdateWrapper<>(entity);
        if(ShiroUtils.getUser()!=null){
            wrapper.eq("warehouseCode", ShiroUtils.getWarehouseCode());
        }
        LambdaUpdateWrapper<T> result=wrapper.lambda();
        return result;
    }

    /**
     * 获取 LambdaUpdateWrapper&lt;T&gt;
     *
     * @param entity 实体类
     * @param <T>    实体类泛型
     * @return LambdaUpdateWrapper&lt;T&gt;
     */
    public static <T> LambdaUpdateWrapper<T> lambdaUpdateNoWarehouse(T entity) {
        return new LambdaUpdateWrapper<>(entity);
    }

    /**
     * 获取 EmptyWrapper&lt;T&gt;
     *
     * @param <T> 任意泛型
     * @return EmptyWrapper&lt;T&gt;
     * @see EmptyWrapper
     */
    @SuppressWarnings("unchecked")
    public static <T> QueryWrapper<T> emptyWrapper() {
        return (QueryWrapper<T>) emptyWrapper;
    }

    /**
     * 一个空的QueryWrapper子类该类不包含任何条件
     *
     * @param <T>
     * @see QueryWrapper
     */
    private static class EmptyWrapper<T> extends QueryWrapper<T> {

        private static final long serialVersionUID = -2515957613998092272L;

        @Override
        public T getEntity() {
            return null;
        }

        @Override
        public EmptyWrapper<T> setEntity(T entity) {
            throw new UnsupportedOperationException();
        }

        @Override
        public String getSqlSelect() {
            return null;
        }

        @Override
        public MergeSegments getExpression() {
            return null;
        }

        @Override
        public boolean isEmptyOfWhere() {
            return true;
        }

        @Override
        public boolean isEmptyOfNormal() {
            return true;
        }

        @Override
        public boolean nonEmptyOfEntity() {
            return !isEmptyOfEntity();
        }

        @Override
        public boolean isEmptyOfEntity() {
            return true;
        }

        @Override
        protected void initEntityClass() {
        }

        @Override
        protected Class<T> getCheckEntityClass() {
            throw new UnsupportedOperationException();
        }

        @Override
        public EmptyWrapper<T> last(boolean condition, String lastSql) {
            throw new UnsupportedOperationException();
        }

        @Override
        protected EmptyWrapper<T> doIt(boolean condition, ISqlSegment... sqlSegments) {
            throw new UnsupportedOperationException();
        }

        @Override
        public String getSqlSegment() {
            return null;
        }

        @Override
        public Map<String, Object> getParamNameValuePairs() {
            return Collections.emptyMap();
        }

        @Override
        protected String columnsToString(String... columns) {
            return null;
        }

        @Override
        protected String columnToString(String column) {
            return null;
        }

        @Override
        protected EmptyWrapper<T> instance() {
            throw new UnsupportedOperationException();
        }
    }

}