Browse Source

字典模块开发

chenxiaofei 1 month ago
parent
commit
e82502c18f

+ 23 - 40
sckw-modules/sckw-system/src/main/java/com/sckw/system/repository/SysDictTypeRepository.java

@@ -1,15 +1,16 @@
 package com.sckw.system.repository;
 
-import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
+import com.baomidou.mybatisplus.core.metadata.IPage;
+import com.baomidou.mybatisplus.core.toolkit.Wrappers;
 import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
 import com.sckw.system.dao.SysDictTypeDao;
 import com.sckw.system.model.SysDictType;
 import org.apache.commons.lang3.StringUtils;
 import org.springframework.stereotype.Repository;
-import org.springframework.stereotype.Service;
 
 import java.util.List;
+import java.util.Objects;
 
 /**
  * 字典类型服务层(MyBatis-Plus 版本)
@@ -23,11 +24,11 @@ public class SysDictTypeRepository extends ServiceImpl<SysDictTypeDao, SysDictTy
      * 根据类型查询字典类型
      */
     public SysDictType selectByType(String type,String name) {
-        LambdaQueryWrapper<SysDictType> wrapper = new LambdaQueryWrapper<>();
-        wrapper.eq(SysDictType::getType, type)
+        return getOne(Wrappers.<SysDictType>lambdaQuery()
+                .eq(SysDictType::getType, type)
                 .eq(SysDictType::getName, name)
-                .eq(SysDictType::getDelFlag, 0);
-        return this.getOne(wrapper);
+                .eq(SysDictType::getDelFlag, 0)
+                .last("LIMIT 1"));
     }
 
     /**
@@ -47,45 +48,27 @@ public class SysDictTypeRepository extends ServiceImpl<SysDictTypeDao, SysDictTy
     /**
      * 分页查询字典类型
      */
-    public Page<SysDictType> selectPage(int pageNum, int pageSize, String name, String type, Integer status) {
-        Page<SysDictType> page = new Page<>(pageNum, pageSize);
-        
-        LambdaQueryWrapper<SysDictType> wrapper = new LambdaQueryWrapper<>();
-        wrapper.eq(SysDictType::getDelFlag, 0);
-        
-        if (StringUtils.isNotBlank(name)) {
-            wrapper.like(SysDictType::getName, name);
-        }
-        if (StringUtils.isNotBlank(type)) {
-            wrapper.like(SysDictType::getType, type);
-        }
-        if (status != null) {
-            wrapper.eq(SysDictType::getStatus, status);
-        }
-        
-        wrapper.orderByDesc(SysDictType::getCreateTime);
-        
-        return this.page(page, wrapper);
+    public IPage<SysDictType> selectPage(int pageNum, int pageSize, String name, String type, Integer status) {
+        return this.page(new Page<>(pageNum, pageSize), Wrappers.<SysDictType>lambdaQuery()
+                .eq(SysDictType::getDelFlag, 0)
+                .eq(Objects.nonNull( status), SysDictType::getStatus, status)
+                .like(StringUtils.isNotBlank( name),SysDictType::getName, name)
+                .like(StringUtils.isNotBlank(type), SysDictType::getType, type)
+                .orderByDesc(SysDictType::getCreateTime)
+               );
     }
 
     /**
      * 搜索字典类型(支持下拉选择)
      */
     public List<SysDictType> searchForSelect(String keyword) {
-        LambdaQueryWrapper<SysDictType> wrapper = new LambdaQueryWrapper<>();
-        wrapper.select(SysDictType::getId, SysDictType::getName, SysDictType::getType)
-               .eq(SysDictType::getDelFlag, 0)
-               .eq(SysDictType::getStatus, 0);
-        
-        if (StringUtils.isNotBlank(keyword)) {
-            wrapper.and(w -> w.like(SysDictType::getName, keyword)
-                             .or()
-                             .like(SysDictType::getType, keyword));
-        }
-        
-        wrapper.orderByDesc(SysDictType::getCreateTime)
-               .last("LIMIT 100");
-        
-        return this.list(wrapper);
+        return list(Wrappers.<SysDictType>lambdaQuery()
+                .select(SysDictType::getId, SysDictType::getName, SysDictType::getType)
+                .eq(SysDictType::getDelFlag, 0)
+                .eq(SysDictType::getStatus, 0)
+                .and(StringUtils.isNotBlank(keyword),w -> w.like(SysDictType::getName, keyword)
+                        .or()
+                        .like(SysDictType::getType, keyword))
+                .last("LIMIT 100"));
     }
 }

+ 2 - 1
sckw-modules/sckw-system/src/main/java/com/sckw/system/service/SysDictTypeService.java

@@ -1,6 +1,7 @@
 package com.sckw.system.service;
 
 import com.alibaba.fastjson.JSON;
+import com.baomidou.mybatisplus.core.metadata.IPage;
 import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
 import com.sckw.core.utils.IdWorker;
 import com.sckw.core.web.context.LoginUserHolder;
@@ -94,7 +95,7 @@ public class SysDictTypeService {
         log.info("分页查询字典类型,请求参数:{}", JSON.toJSONString(reqVo));
         int pageNum = reqVo.getPageNum();
         int pageSize = reqVo.getPageSize();
-        Page<SysDictType> page = sysDictTypeService.selectPage(pageNum, pageSize, reqVo.getName(), reqVo.getType(), reqVo.getStatus());
+        IPage<SysDictType> page = sysDictTypeService.selectPage(pageNum, pageSize, reqVo.getName(), reqVo.getType(), reqVo.getStatus());
         List<SysDictType> records = page.getRecords();
         if (CollectionUtils.isEmpty(records)) {
             return PageDataResult.empty(reqVo.getPageNum(), reqVo.getPageSize());