فهرست منبع

增加角色,项目,项目组管理

yangxiaokun 3 سال پیش
والد
کامیت
f24cd0978b

+ 7 - 2
src/main/java/com/zy/bms/common/io/pc/DeviceIO.java

@@ -20,7 +20,12 @@ public class DeviceIO extends PageIO {
      */
     private Integer status;
     /**
-     * 设备
+     * 项目
      */
-    private Integer groupId;
+    private Integer group;
+
+    /**
+     * 项目
+     */
+    private Integer item;
 }

+ 14 - 14
src/main/java/com/zy/bms/common/vo/DeviceFullDataVo.java

@@ -22,7 +22,7 @@ public class DeviceFullDataVo {
     private String deviceId;
 
     /**
-     * 设备MQTT 连接密码
+     * MQTT 密码
      */
     private String password;
 
@@ -32,14 +32,24 @@ public class DeviceFullDataVo {
     private LocalDateTime createTime;
 
     /**
-     * 设备状态,1注册完成,0待确认
+     * 最后一次在线时间
+     */
+    private LocalDateTime lastOnlineTime;
+
+    /**
+     * 1在线,0离线
      */
     private Integer status;
 
     /**
-     * 设备名
+     * 项目
+     */
+    private String item;
+
+    /**
+     * 项目组
      */
-    private String name;
+    private String group;
 
     /**
      * 电量
@@ -66,16 +76,6 @@ public class DeviceFullDataVo {
      */
     private String lat;
 
-    /**
-     * 经度(gcj)
-     */
-    private String lonGcj;
-
-    /**
-     * 纬度(gcj)
-     */
-    private String latGcj;
-
     /**
      * 格式化的地理位置
      */

+ 38 - 1
src/main/java/com/zy/bms/controller/pc/GroupController.java

@@ -2,6 +2,7 @@ package com.zy.bms.controller.pc;
 
 import com.zy.bms.common.ServerResponse;
 import com.zy.bms.common.io.pc.GroupIO;
+import com.zy.bms.model.Group;
 import com.zy.bms.service.IGroupService;
 import org.springframework.web.bind.annotation.PostMapping;
 import org.springframework.web.bind.annotation.RequestMapping;
@@ -24,11 +25,47 @@ public class GroupController {
     private IGroupService groupService;
 
     /**
-     * 分页查询所有项目
+     * 分页查询所有项目
      */
     @PostMapping("listPage")
     public ServerResponse listPage(GroupIO io) {
         return ServerResponse.createBySuccess(groupService.listPage(io));
     }
 
+    /**
+     * 添加项目组
+     */
+    @PostMapping("save.do")
+    public ServerResponse save(Group group) {
+        if (groupService.checkRepeat(group.getCode())) return ServerResponse.createByWarning("添加失败:项目组编号重复");
+        return ServerResponse.createBySuccess(groupService.save(group));
+    }
+
+    /**
+     * 禁用项目组
+     */
+    @PostMapping("forbidden.do")
+    public ServerResponse forbidden(Integer id) {
+        groupService.updateStatus(id, 0);
+        return ServerResponse.createBySuccess();
+    }
+
+    /**
+     * 启用项目组
+     */
+    @PostMapping("permit.do")
+    public ServerResponse permit(Integer id) {
+        groupService.updateStatus(id, 1);
+        return ServerResponse.createBySuccess();
+    }
+
+    /**
+     * 更新项目组信息
+     */
+    @PostMapping("update.do")
+    public ServerResponse update(Group group) {
+        groupService.updateById(group);
+        return ServerResponse.createBySuccess();
+    }
+
 }

+ 19 - 2
src/main/java/com/zy/bms/controller/pc/ItemController.java

@@ -47,8 +47,25 @@ public class ItemController {
      */
     @PostMapping("forbidden.do")
     public ServerResponse forbidden(Integer id) {
-        if (itemService.checkRepeat(item.getCode())) return ServerResponse.createByWarning("添加失败:项目编号重复");
-        return ServerResponse.createBySuccess(itemService.save(item));
+        itemService.updateStatus(id, 0);
+        return ServerResponse.createBySuccess();
     }
 
+    /**
+     * 启用项目
+     */
+    @PostMapping("permit.do")
+    public ServerResponse permit(Integer id) {
+        itemService.updateStatus(id, 1);
+        return ServerResponse.createBySuccess();
+    }
+
+    /**
+     * 更新项目信息
+     */
+    @PostMapping("update.do")
+    public ServerResponse update(Item item) {
+        itemService.updateById(item);
+        return ServerResponse.createBySuccess();
+    }
 }

+ 20 - 1
src/main/java/com/zy/bms/controller/pc/RoleController.java

@@ -2,6 +2,7 @@ package com.zy.bms.controller.pc;
 
 import com.zy.bms.common.ServerResponse;
 import com.zy.bms.common.io.pc.RoleIO;
+import com.zy.bms.model.Role;
 import com.zy.bms.service.IRoleService;
 import org.springframework.web.bind.annotation.PostMapping;
 import org.springframework.web.bind.annotation.RequestMapping;
@@ -25,10 +26,28 @@ public class RoleController {
     private IRoleService roleService;
 
     /**
-     * 分页查询所有项目
+     * 分页查询角色项目
      */
     @PostMapping("listPage")
     public ServerResponse listPage(RoleIO io) {
         return ServerResponse.createBySuccess(roleService.listPage(io));
     }
+
+    /**
+     * 添加角色
+     */
+    @PostMapping("save.do")
+    public ServerResponse save(Role role) {
+        if (roleService.checkRepeat(role.getName())) return ServerResponse.createByWarning("添加失败:角色名重复");
+        return ServerResponse.createBySuccess(roleService.save(role));
+    }
+
+    /**
+     * 更新角色信息
+     */
+    @PostMapping("update.do")
+    public ServerResponse update(Role role) {
+        roleService.updateById(role);
+        return ServerResponse.createBySuccess();
+    }
 }

+ 1 - 1
src/main/java/com/zy/bms/mapper/DeviceMapper.java

@@ -39,5 +39,5 @@ public interface DeviceMapper extends BaseMapper<Device> {
     /**
      * 批量更新设备在线状态
      */
-    void updateDeviceStatus(List<Device> devices);
+    void updateDeviceStatus(@Param("devices") List<Device> devices);
 }

+ 11 - 0
src/main/java/com/zy/bms/mapper/GroupMapper.java

@@ -6,7 +6,9 @@ import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
 import com.zy.bms.common.io.pc.GroupIO;
 import com.zy.bms.common.vo.GroupListVo;
 import com.zy.bms.model.Group;
+import org.apache.ibatis.annotations.Mapper;
 import org.apache.ibatis.annotations.Param;
+import org.springframework.stereotype.Repository;
 
 /**
  * <p>
@@ -16,6 +18,8 @@ import org.apache.ibatis.annotations.Param;
  * @author chenyi
  * @since 2021-05-19
  */
+@Mapper
+@Repository
 public interface GroupMapper extends BaseMapper<Group> {
     /**
      * 分页查询
@@ -24,4 +28,11 @@ public interface GroupMapper extends BaseMapper<Group> {
      * @param io   查询条件
      */
     IPage<GroupListVo> listPage(Page<GroupListVo> page, @Param("io") GroupIO io);
+
+    /**
+     * 项目组编号查重
+     *
+     * @param code 项目组编号
+     */
+    int checkRepeat(@Param("code") String code);
 }

+ 4 - 0
src/main/java/com/zy/bms/mapper/ItemMapper.java

@@ -5,7 +5,9 @@ import com.baomidou.mybatisplus.core.metadata.IPage;
 import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
 import com.zy.bms.common.io.pc.ItemIO;
 import com.zy.bms.model.Item;
+import org.apache.ibatis.annotations.Mapper;
 import org.apache.ibatis.annotations.Param;
+import org.springframework.stereotype.Repository;
 
 /**
  * <p>
@@ -15,6 +17,8 @@ import org.apache.ibatis.annotations.Param;
  * @author chenyi
  * @since 2021-05-19
  */
+@Mapper
+@Repository
 public interface ItemMapper extends BaseMapper<Item> {
     /**
      * 分页查询

+ 11 - 0
src/main/java/com/zy/bms/mapper/RoleMapper.java

@@ -5,7 +5,9 @@ import com.baomidou.mybatisplus.core.metadata.IPage;
 import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
 import com.zy.bms.common.io.pc.RoleIO;
 import com.zy.bms.model.Role;
+import org.apache.ibatis.annotations.Mapper;
 import org.apache.ibatis.annotations.Param;
+import org.springframework.stereotype.Repository;
 
 /**
  * <p>
@@ -15,6 +17,8 @@ import org.apache.ibatis.annotations.Param;
  * @author chenyi
  * @since 2021-05-19
  */
+@Mapper
+@Repository
 public interface RoleMapper extends BaseMapper<Role> {
     /**
      * 分页查询
@@ -23,4 +27,11 @@ public interface RoleMapper extends BaseMapper<Role> {
      * @param io   查询条件
      */
     IPage<Role> listPage(Page<Role> page, @Param("io") RoleIO io);
+
+    /**
+     * 角色名查重
+     *
+     * @param name 角色名
+     */
+    int checkRepeat(@Param("name") String name);
 }

+ 25 - 10
src/main/java/com/zy/bms/model/Device.java

@@ -3,7 +3,6 @@ package com.zy.bms.model;
 import com.baomidou.mybatisplus.annotation.TableId;
 import com.baomidou.mybatisplus.annotation.TableName;
 import lombok.Data;
-import lombok.NoArgsConstructor;
 
 import java.time.LocalDateTime;
 
@@ -16,27 +15,43 @@ public class Device {
     @TableId
     private Integer id;
 
-    //设备随机码
+    /**
+     * 设备随机码
+     */
     private String num;
 
-    //设备MQTT clientId
+    /**
+     * MQTT clientId
+     */
     private String clientId;
 
-    //设备MQTT 连接密码
+    /**
+     * MQTT 密码
+     */
     private String password;
 
-    //创建时间
+    /**
+     * 创建时间
+     */
     private LocalDateTime createTime;
 
-    //权限
-    private String tags;
+    /**
+     * 最后一次在线时间
+     */
+    private LocalDateTime lastOnlineTime;
 
-    //设备状态,1注册完成,0待确认
+    /**
+     * 1:在线 0:离线
+     */
     private Integer status;
 
-    //设备组
+    /**
+     * 设备组
+     */
     private Integer groupId;
 
-    //设备名
+    /**
+     * 设备名
+     */
     private String name;
 }

+ 8 - 3
src/main/java/com/zy/bms/schedule/DeviceStatusSchedule.java

@@ -10,6 +10,7 @@ import org.springframework.scheduling.annotation.EnableAsync;
 import org.springframework.scheduling.annotation.EnableScheduling;
 import org.springframework.scheduling.annotation.Scheduled;
 
+import java.time.LocalDateTime;
 import java.util.List;
 import java.util.Set;
 
@@ -30,17 +31,21 @@ public class DeviceStatusSchedule {
     private DeviceService deviceService;
 
     /**
-     * 更新频率 2分钟
+     * 更新频率 5分钟
      * 定时更新设备在线状态
      */
     @Async
-    @Scheduled(cron = "0 0/2 * * * ?")
+    @Scheduled(cron = "0 0/5 * * * ?")
     public void updateDeviceStatus() {
         //全部的设备
         List<Device> devices = deviceService.list();
         Set<String> online = rabbitMQApi.getOnlineDevice();
         for (Device item : devices) {
-            item.setStatus(online.contains(item.getClientId()) ? 1 : 0);
+            int status = online.contains(item.getClientId()) ? 1 : 0;
+            item.setStatus(status);
+            if (status == 1) {
+                item.setLastOnlineTime(LocalDateTime.now());
+            }
         }
         deviceService.updateDeviceStatus(devices);
     }

+ 14 - 0
src/main/java/com/zy/bms/service/IGroupService.java

@@ -24,4 +24,18 @@ public interface IGroupService extends IService<Group> {
     IPage<GroupListVo> listPage(GroupIO io);
 
 
+    /**
+     * 项目编号查重
+     *
+     * @param code 项目编号
+     */
+    boolean checkRepeat(String code);
+
+    /**
+     * 更新项目组状态
+     *
+     * @param id     主键ID
+     * @param status 状态
+     */
+    void updateStatus(Integer id, Integer status);
 }

+ 1 - 1
src/main/java/com/zy/bms/service/IItemService.java

@@ -30,7 +30,7 @@ public interface IItemService extends IService<Item> {
     boolean checkRepeat(String code);
 
     /**
-     * 更新用户状态
+     * 更新项目状态
      *
      * @param id     主键ID
      * @param status 状态

+ 7 - 0
src/main/java/com/zy/bms/service/IRoleService.java

@@ -21,4 +21,11 @@ public interface IRoleService extends IService<Role> {
      * @param io 查询条件
      */
     IPage<Role> listPage(RoleIO io);
+
+    /**
+     * 角色名查重
+     *
+     * @param name 角色名
+     */
+    boolean checkRepeat(String name);
 }

+ 13 - 0
src/main/java/com/zy/bms/service/impl/GroupServiceImpl.java

@@ -1,5 +1,6 @@
 package com.zy.bms.service.impl;
 
+import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
 import com.baomidou.mybatisplus.core.metadata.IPage;
 import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
@@ -26,4 +27,16 @@ public class GroupServiceImpl extends ServiceImpl<GroupMapper, Group> implements
         Page<GroupListVo> page = new Page<>(io.getCurrent(), io.getSize());
         return baseMapper.listPage(page, io);
     }
+
+    @Override
+    public boolean checkRepeat(String code) {
+        return baseMapper.checkRepeat(code) > 0;
+    }
+
+    @Override
+    public void updateStatus(Integer id, Integer status) {
+        UpdateWrapper<Group> updateWrapper = new UpdateWrapper<>();
+        updateWrapper.set("status", status).eq("id", id);
+        update(updateWrapper);
+    }
 }

+ 0 - 1
src/main/java/com/zy/bms/service/impl/ItemServiceImpl.java

@@ -6,7 +6,6 @@ import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
 import com.zy.bms.common.io.pc.ItemIO;
 import com.zy.bms.mapper.ItemMapper;
-import com.zy.bms.model.Admin;
 import com.zy.bms.model.Item;
 import com.zy.bms.service.IItemService;
 import org.springframework.stereotype.Service;

+ 5 - 0
src/main/java/com/zy/bms/service/impl/RoleServiceImpl.java

@@ -25,4 +25,9 @@ public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements IR
         Page<Role> page = new Page<>(io.getCurrent(), io.getSize());
         return baseMapper.listPage(page, io);
     }
+
+    @Override
+    public boolean checkRepeat(String name) {
+        return baseMapper.checkRepeat(name) > 0;
+    }
 }

+ 1 - 0
src/main/resources/application-dev.yml

@@ -25,6 +25,7 @@ mybatis-plus:
   type-aliases-package: com.zy.bms.model
   mapper-locations: classpath:mapper/*.xml
   configuration:
+    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
     map-underscore-to-camel-case: false  # close "user_id"  use userId
   global-config:
     db-config:

+ 1 - 1
src/main/resources/application.yml

@@ -1,3 +1,3 @@
 spring:
   profiles:
-    active: prod
+    active: dev

+ 19 - 9
src/main/resources/mapper/DeviceMapping.xml

@@ -4,11 +4,13 @@
 
     <select id="getListPage" resultType="com.zy.bms.common.vo.DeviceFullDataVo">
         SELECT
-            t1.num AS deviceNum,
+            t1.num             AS deviceNum,
             t1.password,
             t1.createTime,
             t1.status,
             t1.`name`,
+            t3.`name`           AS group,
+            t4.`name`           AS item,
             t2.deviceId,
             t2.`mode`,
             t2.lon,
@@ -25,15 +27,20 @@
             zy_device t1
         LEFT JOIN
             latest_location t2 ON t1.deviceId = t2.deviceId
+            `group` t3 ON t1.groupId = t3.id
+            item t4 ON t3.itemId = t4.id
         <where>
             <if test="io.num != null and io.num != ''">
-                AND (num LIKE CONCAT("%",#{io.num},"%") OR clientId LIKE CONCAT("%",#{io.num},"%"))
+                AND (t1.num LIKE CONCAT("%",#{io.num},"%") OR t1.clientId LIKE CONCAT("%",#{io.num},"%"))
             </if>
             <if test="io.status != null and io.status > -1">
-                AND status = #{io.status}
+                AND t1.status = #{io.status}
             </if>
-            <if test="io.groupId != null">
-                AND groupId = #{io.groupId}
+            <if test="io.group != null and io.group > -1">
+                AND t1.groupId = #{io.groupId}
+            </if>
+            <if test="io.item != null and io.item > -1">
+                AND t4.id = #{io.item}
             </if>
         </where>
         ORDER BY
@@ -55,10 +62,13 @@
         <foreach collection="devices" item="device">
             WHEN #{device.clientId} THEN #{device.status}
         </foreach>
-        END
-        WHERE clientId IN
-        <foreach collection="devices" item="device" separator="," open="(" close=")">
-            #{device.clientId}
+        END,
+        `lastOnlineTime` = CASE clientId
+        <foreach collection="devices" item="device">
+            <if test="device.status == 1">
+                WHEN #{device.clientId} THEN #{device.lastOnlineTime}
+            </if>
         </foreach>
+        END
     </update>
 </mapper>

+ 4 - 0
src/main/resources/mapper/GroupMapper.xml

@@ -21,4 +21,8 @@
         ORDER BY createTime DESC
     </select>
 
+    <select id="checkRepeat" resultType="java.lang.Integer">
+        SELECT IFNULL( (SELECT 1 FROM `group` WHERE code = #{code} LIMIT 1) ,0)
+    </select>
+
 </mapper>

+ 4 - 0
src/main/resources/mapper/RoleMapper.xml

@@ -14,4 +14,8 @@
         ORDER BY createTime DESC
     </select>
 
+    <select id="checkRepeat" resultType="java.lang.Integer">
+        SELECT IFNULL( (SELECT 1 FROM role WHERE `name` = #{name} LIMIT 1) ,0)
+    </select>
+
 </mapper>