Przeglądaj źródła

巡检区域相关接口和封装方法

jichaobo 2 lat temu
rodzic
commit
1d8a60d1ee

+ 80 - 0
service-fire/service-fire-biz/src/main/java/com/usky/fire/controller/web/BaseController.java

@@ -0,0 +1,80 @@
+package com.usky.fire.controller.web;
+
+
+
+import com.github.pagehelper.PageHelper;
+import com.github.pagehelper.PageInfo;
+import com.usky.common.core.bean.ApiResult;
+import com.usky.common.core.exception.BusinessErrorCode;
+import com.usky.common.core.util.DateUtils;
+import com.usky.fire.controller.web.page.PageDomain;
+import com.usky.fire.controller.web.page.TableDataInfo;
+import com.usky.fire.controller.web.page.TableSupport;
+import com.usky.fire.service.util.SqlUtil;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.http.HttpStatus;
+import org.springframework.web.bind.WebDataBinder;
+import org.springframework.web.bind.annotation.InitBinder;
+
+import java.beans.PropertyEditorSupport;
+import java.util.Date;
+import java.util.List;
+import java.util.Objects;
+
+/**
+ * web层通用数据处理
+ */
+public class BaseController {
+    protected final Logger logger = LoggerFactory.getLogger(BaseController.class);
+
+    /**
+     * 将前台传递过来的日期格式的字符串,自动转化为Date类型
+     */
+    @InitBinder
+    public void initBinder(WebDataBinder binder) {
+        // Date 类型转换
+        binder.registerCustomEditor(Date.class, new PropertyEditorSupport() {
+            @Override
+            public void setAsText(String text) {
+                setValue(DateUtils.parseDate(text));
+            }
+        });
+    }
+
+    /**
+     * 设置请求分页数据
+     */
+    protected void startPage() {
+        PageDomain pageDomain = TableSupport.buildPageRequest();
+        Integer pageNum = pageDomain.getPageNum();
+        Integer pageSize = pageDomain.getPageSize();
+        if (Objects.nonNull(pageNum) && Objects.nonNull(pageSize)) {
+            String orderBy = SqlUtil.escapeOrderBySql(pageDomain.getOrderBy());
+            PageHelper.startPage(pageNum, pageSize, orderBy);
+        }
+    }
+
+    /**
+     * 响应请求分页数据
+     */
+    @SuppressWarnings({"rawtypes", "unchecked"})
+    protected TableDataInfo getDataTable(List<?> list) {
+        TableDataInfo rspData = new TableDataInfo();
+        rspData.setCode(HttpStatus.OK.value());
+        rspData.setRows(list);
+        rspData.setMsg("查询成功");
+        rspData.setTotal(new PageInfo(list).getTotal());
+        return rspData;
+    }
+
+    /**
+     * 响应返回结果
+     *
+     * @param rows 影响行数
+     * @return 操作结果
+     */
+    protected ApiResult<Void> toAjax(int rows) {
+        return rows > 0 ? ApiResult.success() : ApiResult.error(BusinessErrorCode.BIZ_MODIFY_FAIL.getCode(), BusinessErrorCode.BIZ_MODIFY_FAIL.getDefaultMessage());
+    }
+}

+ 8 - 8
service-fire/service-fire-biz/src/main/java/com/usky/fire/controller/web/PatrolInspectionAreaController.java

@@ -2,10 +2,11 @@ package com.usky.fire.controller.web;
 
 
 import com.usky.common.core.bean.ApiResult;
-import com.usky.common.core.bean.CommonPage;
 import com.usky.fire.domain.PatrolInspectionArea;
 import com.usky.fire.service.PatrolInspectionAreaService;
 import com.usky.fire.service.vo.PatrolInspectionAreaVo;
+import com.usky.fire.controller.web.BaseController;
+import com.usky.fire.controller.web.page.TableDataInfo;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.web.bind.annotation.*;
 
@@ -19,7 +20,7 @@ import java.util.List;
  */
 @RestController
 @RequestMapping("/patrolInspectionArea")
-public class PatrolInspectionAreaController {
+public class PatrolInspectionAreaController extends BaseController {
 
     @Autowired
     private PatrolInspectionAreaService patrolInspectionAreaService;
@@ -70,10 +71,10 @@ public class PatrolInspectionAreaController {
      * @return
      */
     @GetMapping("patrolInspectionAreaList")
-    public ApiResult<CommonPage<PatrolInspectionArea>> patrolInspectionAreaList(@RequestParam(value = "areaName", required = false) String areaName,
-                                                                                @RequestParam(value = "pageNum", required = false, defaultValue = "0") Integer pageNum,
-                                                                                @RequestParam(value = "pageSize", required = false, defaultValue = "10") Integer pageSize,
-                                                                                @RequestParam(value = "id", required = false, defaultValue = "0") Integer id) {
+    public ApiResult< List<PatrolInspectionArea>> patrolInspectionAreaList(@RequestParam(value = "areaName", required = false) String areaName,
+                                                             @RequestParam(value = "pageNum", required = false, defaultValue = "0") Integer pageNum,
+                                                             @RequestParam(value = "pageSize", required = false, defaultValue = "10") Integer pageSize,
+                                                             @RequestParam(value = "id", required = false, defaultValue = "0") Integer id) {
         return ApiResult.success(patrolInspectionAreaService.patrolInspectionAreaList(id, areaName, pageNum, pageSize));
     }
 
@@ -88,5 +89,4 @@ public class PatrolInspectionAreaController {
     }
 
 
-}
-
+}

+ 68 - 0
service-fire/service-fire-biz/src/main/java/com/usky/fire/controller/web/page/PageDomain.java

@@ -0,0 +1,68 @@
+package com.usky.fire.controller.web.page;
+
+
+import com.usky.common.core.util.StringUtils;
+
+/**
+ * 分页数据
+ */
+public class PageDomain {
+    /**
+     * 当前记录起始索引
+     */
+    private Integer pageNum;
+
+    /**
+     * 每页显示记录数
+     */
+    private Integer pageSize;
+
+    /**
+     * 排序列
+     */
+    private String orderByColumn;
+    /**
+     * 排序的方向 "desc" 或者 "asc".
+     */
+
+    private String isAsc;
+
+    public String getOrderBy() {
+        if (StringUtils.isBlank(orderByColumn)) {
+            return "";
+        }
+        return StringUtils.toUnderScoreCase(orderByColumn) + " " + isAsc;
+    }
+
+    public Integer getPageNum() {
+        return pageNum;
+    }
+
+    public void setPageNum(Integer pageNum) {
+        this.pageNum = pageNum;
+    }
+
+    public Integer getPageSize() {
+        return pageSize;
+    }
+
+    public void setPageSize(Integer pageSize) {
+        this.pageSize = pageSize;
+    }
+
+    public String getOrderByColumn() {
+        return orderByColumn;
+    }
+
+    public void setOrderByColumn(String orderByColumn) {
+        this.orderByColumn = orderByColumn;
+    }
+
+    public String getIsAsc() {
+        return isAsc;
+    }
+
+    public void setIsAsc(String isAsc) {
+        this.isAsc = isAsc;
+    }
+}

+ 80 - 0
service-fire/service-fire-biz/src/main/java/com/usky/fire/controller/web/page/TableDataInfo.java

@@ -0,0 +1,80 @@
+package com.usky.fire.controller.web.page;
+
+import java.io.Serializable;
+import java.util.List;
+
+/**
+ * 表格分页数据对象
+ */
+public class TableDataInfo implements Serializable {
+    private static final long serialVersionUID = 1L;
+
+    /**
+     * 总记录数
+     */
+    private long total;
+
+    /**
+     * 列表数据
+     */
+    private List<?> rows;
+
+    /**
+     * 消息状态码
+     */
+    private int code;
+
+    /**
+     * 消息内容
+     */
+    private String msg;
+
+    /**
+     * 表格数据对象
+     */
+    public TableDataInfo() {
+    }
+
+    /**
+     * 分页
+     *
+     * @param list  列表数据
+     * @param total 总记录数
+     */
+    public TableDataInfo(List<?> list, int total) {
+        this.rows = list;
+        this.total = total;
+    }
+
+    public long getTotal() {
+        return total;
+    }
+
+    public void setTotal(long total) {
+        this.total = total;
+    }
+
+    public List<?> getRows() {
+        return rows;
+    }
+
+    public void setRows(List<?> rows) {
+        this.rows = rows;
+    }
+
+    public int getCode() {
+        return code;
+    }
+
+    public void setCode(int code) {
+        this.code = code;
+    }
+
+    public String getMsg() {
+        return msg;
+    }
+
+    public void setMsg(String msg) {
+        this.msg = msg;
+    }
+}

+ 45 - 0
service-fire/service-fire-biz/src/main/java/com/usky/fire/controller/web/page/TableSupport.java

@@ -0,0 +1,45 @@
+package com.usky.fire.controller.web.page;
+
+
+import com.usky.common.core.util.ServletUtils;
+
+/**
+ * 表格数据处理
+ */
+public class TableSupport {
+    /**
+     * 当前记录起始索引
+     */
+    public static final String PAGE_NUM = "pageNum";
+
+    /**
+     * 每页显示记录数
+     */
+    public static final String PAGE_SIZE = "pageSize";
+
+    /**
+     * 排序列
+     */
+    public static final String ORDER_BY_COLUMN = "orderByColumn";
+
+    /**
+     * 排序的方向 "desc" 或者 "asc".
+     */
+    public static final String IS_ASC = "isAsc";
+
+    /**
+     * 封装分页对象
+     */
+    public static PageDomain getPageDomain() {
+        PageDomain pageDomain = new PageDomain();
+        pageDomain.setPageNum(ServletUtils.getParameterToInt(PAGE_NUM));
+        pageDomain.setPageSize(ServletUtils.getParameterToInt(PAGE_SIZE));
+        pageDomain.setOrderByColumn(ServletUtils.getParameter(ORDER_BY_COLUMN));
+        pageDomain.setIsAsc(ServletUtils.getParameter(IS_ASC));
+        return pageDomain;
+    }
+
+    public static PageDomain buildPageRequest() {
+        return getPageDomain();
+    }
+}

+ 8 - 0
service-fire/service-fire-biz/src/main/java/com/usky/fire/domain/PatrolInspectionArea.java

@@ -1,9 +1,13 @@
 package com.usky.fire.domain;
 
 import com.baomidou.mybatisplus.annotation.IdType;
+import com.baomidou.mybatisplus.annotation.TableField;
 import com.baomidou.mybatisplus.annotation.TableId;
 import java.time.LocalDateTime;
 import java.io.Serializable;
+import java.util.ArrayList;
+import java.util.List;
+
 import lombok.Data;
 import lombok.EqualsAndHashCode;
 import lombok.experimental.Accessors;
@@ -64,5 +68,9 @@ public class PatrolInspectionArea implements Serializable {
      */
     private Integer enable;
 
+    /** 子菜单 */
+    @TableField(exist = false)
+    private List<PatrolInspectionArea> children = new ArrayList<PatrolInspectionArea>();
+
 
 }

+ 1 - 1
service-fire/service-fire-biz/src/main/java/com/usky/fire/service/PatrolInspectionAreaService.java

@@ -23,7 +23,7 @@ public interface PatrolInspectionAreaService extends CrudService<PatrolInspectio
 
     void delPatrolInspectionArea(Integer id);
 
-    CommonPage<PatrolInspectionArea> patrolInspectionAreaList(Integer id, String areaName, Integer pageNum, Integer pageSize);
+    List<PatrolInspectionArea> patrolInspectionAreaList(Integer id, String areaName, Integer pageNum, Integer pageSize);
 
     List<PatrolInspectionArea> patrolInspectionAreaSelect();
 }

+ 5 - 9
service-fire/service-fire-biz/src/main/java/com/usky/fire/service/impl/PatrolInspectionAreaServiceImpl.java

@@ -15,6 +15,8 @@ import org.springframework.stereotype.Service;
 import org.springframework.transaction.annotation.Transactional;
 
 import java.time.LocalDateTime;
+import java.util.ArrayList;
+import java.util.Iterator;
 import java.util.List;
 import java.util.Map;
 
@@ -103,8 +105,7 @@ public class PatrolInspectionAreaServiceImpl extends AbstractCrudService<PatrolI
      * @param pageSize 每页条数
      * @return
      */
-    public CommonPage<PatrolInspectionArea> patrolInspectionAreaList(Integer id, String areaName, Integer pageNum, Integer pageSize) {
-        IPage<PatrolInspectionArea> page = new Page<PatrolInspectionArea>(pageNum, pageSize);
+    public List<PatrolInspectionArea> patrolInspectionAreaList(Integer id, String areaName, Integer pageNum, Integer pageSize) {
         LambdaQueryWrapper<PatrolInspectionArea> queryWrapper = Wrappers.lambdaQuery();
         queryWrapper.eq(PatrolInspectionArea::getTenantId, SecurityUtils.getTenantId())
                 .eq(PatrolInspectionArea::getEnable, 1);
@@ -112,15 +113,10 @@ public class PatrolInspectionAreaServiceImpl extends AbstractCrudService<PatrolI
             queryWrapper.eq(PatrolInspectionArea::getId, id);
         }
         if (!"".equals(areaName) && areaName != null) {
-            queryWrapper.eq(PatrolInspectionArea::getAreaName, areaName);
+            queryWrapper.like(PatrolInspectionArea::getAreaName, areaName);
         }
         queryWrapper.orderByDesc(PatrolInspectionArea::getId);
-//        Integer total = this.list(queryWrapper).size();
-        IPage<PatrolInspectionArea> a = baseMapper.selectPage(page, queryWrapper);
-        page = this.page(page, queryWrapper);
-//        this.ToCommonPage(page);
-
-        return new CommonPage<>(page.getRecords(), page.getTotal(), page.getSize(), page.getCurrent());
+        return this.list(queryWrapper);
     }
 
 

+ 82 - 0
service-fire/service-fire-biz/src/main/java/com/usky/fire/service/util/OnlineMethod.java

@@ -0,0 +1,82 @@
+package com.usky.fire.service.util;
+
+import com.usky.fire.domain.PatrolInspectionArea;
+
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.List;
+
+public class OnlineMethod {
+
+    /**
+     * 根据父节点的ID获取所有子节点
+     *
+     * @param list 分类表
+     * @param parentId 传入的父节点ID
+     * @return String
+     */
+    public List<PatrolInspectionArea> getChildPerms(List<PatrolInspectionArea> list, int parentId)
+    {
+        List<PatrolInspectionArea> returnList = new ArrayList<PatrolInspectionArea>();
+        for (Iterator<PatrolInspectionArea> iterator = list.iterator(); iterator.hasNext();)
+        {
+            PatrolInspectionArea t = (PatrolInspectionArea) iterator.next();
+            // 一、根据传入的某个父节点ID,遍历该父节点的所有子节点
+            if (t.getAreaFid() == parentId)
+            {
+                recursionFn(list, t);
+                returnList.add(t);
+            }
+        }
+        return returnList;
+    }
+
+    /**
+     * 递归列表
+     *
+     * @param list
+     * @param t
+     */
+    private void recursionFn(List<PatrolInspectionArea> list, PatrolInspectionArea t)
+    {
+        // 得到子节点列表
+        List<PatrolInspectionArea> childList = getChildList(list, t);
+        t.setChildren(childList);
+        for (PatrolInspectionArea tChild : childList)
+        {
+            if (hasChild(list, tChild))
+            {
+                recursionFn(list, tChild);
+            }
+        }
+    }
+
+
+    /**
+     * 得到子节点列表
+     */
+    private List<PatrolInspectionArea> getChildList(List<PatrolInspectionArea> list, PatrolInspectionArea t)
+    {
+        List<PatrolInspectionArea> tlist = new ArrayList<PatrolInspectionArea>();
+        Iterator<PatrolInspectionArea> it = list.iterator();
+        while (it.hasNext())
+        {
+            PatrolInspectionArea n = (PatrolInspectionArea) it.next();
+            if (n.getAreaFid().longValue() == t.getId().longValue())
+            {
+                tlist.add(n);
+            }
+        }
+        return tlist;
+    }
+
+
+    /**
+     * 判断是否有子节点
+     */
+    private boolean hasChild(List<PatrolInspectionArea> list, PatrolInspectionArea t)
+    {
+        return getChildList(list, t).size() > 0 ? true : false;
+    }
+
+}

+ 62 - 0
service-fire/service-fire-biz/src/main/java/com/usky/fire/service/util/SqlUtil.java

@@ -0,0 +1,62 @@
+package com.usky.fire.service.util;
+
+
+import com.usky.common.core.exception.BusinessException;
+import com.usky.common.core.util.StringUtils;
+
+/**
+ * sql操作工具类
+ * 
+ * @author ruoyi
+ */
+public class SqlUtil
+{
+    /**
+     * 定义常用的 sql关键字
+     */
+    public static String SQL_REGEX = "select |insert |delete |update |drop |count |exec |chr |mid |master |truncate |char |and |declare ";
+
+    /**
+     * 仅支持字母、数字、下划线、空格、逗号、小数点(支持多个字段排序)
+     */
+    public static String SQL_PATTERN = "[a-zA-Z0-9_\\ \\,\\.]+";
+
+    /**
+     * 检查字符,防止注入绕过
+     */
+    public static String escapeOrderBySql(String value)
+    {
+        if (StringUtils.isNotEmpty(value) && !isValidOrderBySql(value))
+        {
+            throw new BusinessException("参数不符合规范,不能进行查询");
+        }
+        return value;
+    }
+
+    /**
+     * 验证 order by 语法是否符合规范
+     */
+    public static boolean isValidOrderBySql(String value)
+    {
+        return value.matches(SQL_PATTERN);
+    }
+
+    /**
+     * SQL关键字检查
+     */
+    public static void filterKeyword(String value)
+    {
+        if (StringUtils.isEmpty(value))
+        {
+            return;
+        }
+        String[] sqlKeywords = StringUtils.split(SQL_REGEX, "\\|");
+        for (String sqlKeyword : sqlKeywords)
+        {
+            if (StringUtils.indexOfIgnoreCase(value, sqlKeyword) > -1)
+            {
+                throw new BusinessException("参数存在SQL注入风险");
+            }
+        }
+    }
+}