123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233 |
- package com.fjhx.utils;
- import cn.hutool.core.date.DateTime;
- import cn.hutool.core.date.DateUtil;
- import cn.hutool.core.util.ObjectUtil;
- import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
- import com.baomidou.mybatisplus.core.toolkit.Wrappers;
- import org.springblade.core.log.exception.ServiceException;
- import org.springblade.core.secure.utils.AuthUtil;
- import java.util.HashMap;
- import java.util.Map;
- public class WrapperUtil {
- private Map<String, String> condition;
- private QueryWrapper<?> wrapper;
- private WrapperUtil() {
- }
-
- public static WrapperUtil init() {
- WrapperUtil wrapperUtil = new WrapperUtil();
- wrapperUtil.wrapper = Wrappers.query();
- wrapperUtil.condition = new HashMap<>();
- return wrapperUtil;
- }
-
- public static WrapperUtil init(Map<String, String> condition) {
- WrapperUtil wrapperUtil = new WrapperUtil();
- wrapperUtil.wrapper = Wrappers.query();
- wrapperUtil.condition = condition;
- return wrapperUtil;
- }
-
- public static WrapperUtil init(Map<String, String> condition, QueryWrapper<?> wrapper) {
- WrapperUtil wrapperUtil = new WrapperUtil();
- wrapperUtil.wrapper = wrapper;
- wrapperUtil.condition = condition;
- return wrapperUtil;
- }
-
- public QueryWrapper<?> getWrapper() {
- return wrapper;
- }
-
- public WrapperUtil periodOfTime(String field) {
- Object beginTimeObj = condition.get("beginTime");
- Object endTimeObj = condition.get("endTime");
- if (ObjectUtil.isAllNotEmpty(beginTimeObj, endTimeObj)) {
- try {
- DateTime beginTime = DateUtil.beginOfDay(DateUtil.parse(beginTimeObj.toString()));
- DateTime endTime = DateUtil.endOfDay(DateUtil.parse(endTimeObj.toString()));
- wrapper.between(field, beginTime, endTime);
- } catch (Exception e) {
- throw new ServiceException("传入日期格式解析错误");
- }
- }
- return this;
- }
-
- public WrapperUtil notDelete() {
- wrapper.eq("del_flag", 0);
- return this;
- }
-
- public WrapperUtil notDelete(String prefix) {
- if (ObjectUtil.isNotEmpty(prefix)) {
- wrapper.eq(prefix + ".del_flag", 0);
- } else {
- wrapper.eq("del_flag", 0);
- }
- return this;
- }
-
- public WrapperUtil notDelete(String... prefix) {
- for (String item : prefix) {
- if (ObjectUtil.isNotEmpty(prefix)) {
- wrapper.eq(item + ".del_flag", 0);
- }
- }
- return this;
- }
-
- public WrapperUtil createTimeDesc(String prefix) {
- if (ObjectUtil.isNotEmpty(prefix)) {
- wrapper.orderByDesc(prefix + ".id");
- } else {
- wrapper.orderByDesc("id");
- }
- return this;
- }
-
- public WrapperUtil createTimeDesc() {
- createTimeDesc(null);
- return this;
- }
-
- public WrapperUtil keyword(String... fields) {
- Object keyword = condition.get("keyword");
- if (ObjectUtil.isNotEmpty(keyword)) {
- wrapper.and(q -> {
- for (String field : fields) {
- q.or().like(field, keyword);
- }
- });
- }
- return this;
- }
-
- public WrapperUtil eq(String field, String mapKey) {
- Object value = condition.get(mapKey);
- if (ObjectUtil.isNotEmpty(value)) {
- wrapper.eq(field, value);
- }
- return this;
- }
-
- public WrapperUtil eq(String field, String mapKey, String exceptionSpecification) {
- Object value = condition.get(mapKey);
- if (ObjectUtil.isNotEmpty(value)) {
- wrapper.eq(field, value);
- } else {
- throw new ServiceException(exceptionSpecification);
- }
- return this;
- }
-
- public WrapperUtil like(String field, String mapKey) {
- Object value = condition.get(mapKey);
- if (ObjectUtil.isNotEmpty(value)) {
- wrapper.like(field, value);
- }
- return this;
- }
-
- public WrapperUtil eqTenantId(String field) {
- if (ObjectUtil.isEmpty(field)) {
- wrapper.eq("tenant_id", AuthUtil.getTenantId());
- } else {
- wrapper.eq(field + ".tenant_id", AuthUtil.getTenantId());
- }
- return this;
- }
-
- public WrapperUtil eqTenantId() {
- wrapper.eq("tenant_id", AuthUtil.getTenantId());
- return this;
- }
- }
|