设备类型管理

This commit is contained in:
2025-07-08 11:09:14 +08:00
parent b26cb873b5
commit 08b1f73af1
5 changed files with 87 additions and 31 deletions

View File

@ -58,4 +58,5 @@ public class ResourcesConfig implements WebMvcConfigurer {
public GlobalExceptionHandler globalExceptionHandler() { public GlobalExceptionHandler globalExceptionHandler() {
return new GlobalExceptionHandler(); return new GlobalExceptionHandler();
} }
} }

View File

@ -0,0 +1,18 @@
package com.fuyuanshen.equipment.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.fuyuanshen.equipment.domain.DeviceTypeGrants;
import org.apache.ibatis.annotations.Mapper;
/**
* @author 97433
* @description 针对表【device_type_grants】的数据库操作Mapper
* @createDate 2025-06-19 13:49:33
* @Entity generator.domain.DeviceTypeGrants
*/
@Mapper
public interface DeviceTypeGrantsMapper extends BaseMapper<DeviceTypeGrants> {
}

View File

@ -11,10 +11,12 @@ import com.fuyuanshen.common.mybatis.core.page.TableDataInfo;
import com.fuyuanshen.common.satoken.utils.LoginHelper; import com.fuyuanshen.common.satoken.utils.LoginHelper;
import com.fuyuanshen.equipment.domain.Device; import com.fuyuanshen.equipment.domain.Device;
import com.fuyuanshen.equipment.domain.DeviceType; import com.fuyuanshen.equipment.domain.DeviceType;
import com.fuyuanshen.equipment.domain.DeviceTypeGrants;
import com.fuyuanshen.equipment.domain.form.DeviceTypeForm; import com.fuyuanshen.equipment.domain.form.DeviceTypeForm;
import com.fuyuanshen.equipment.domain.query.DeviceQueryCriteria; import com.fuyuanshen.equipment.domain.query.DeviceQueryCriteria;
import com.fuyuanshen.equipment.domain.query.DeviceTypeQueryCriteria; import com.fuyuanshen.equipment.domain.query.DeviceTypeQueryCriteria;
import com.fuyuanshen.equipment.mapper.DeviceMapper; import com.fuyuanshen.equipment.mapper.DeviceMapper;
import com.fuyuanshen.equipment.mapper.DeviceTypeGrantsMapper;
import com.fuyuanshen.equipment.mapper.DeviceTypeMapper; import com.fuyuanshen.equipment.mapper.DeviceTypeMapper;
import com.fuyuanshen.equipment.service.DeviceTypeService; import com.fuyuanshen.equipment.service.DeviceTypeService;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
@ -22,6 +24,7 @@ import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Date;
import java.util.List; import java.util.List;
import java.util.Objects; import java.util.Objects;
@ -37,6 +40,8 @@ public class DeviceTypeServiceImpl extends ServiceImpl<DeviceTypeMapper, DeviceT
private final DeviceTypeMapper deviceTypeMapper; private final DeviceTypeMapper deviceTypeMapper;
private final DeviceMapper deviceMapper; private final DeviceMapper deviceMapper;
private final DeviceTypeGrantsMapper deviceTypeGrantsMapper;
/** /**
* 分页查询设备类型 * 分页查询设备类型
@ -88,6 +93,14 @@ public class DeviceTypeServiceImpl extends ServiceImpl<DeviceTypeMapper, DeviceT
resources.setOriginalOwnerId(loginUser.getUserId()); resources.setOriginalOwnerId(loginUser.getUserId());
resources.setCreateByName(loginUser.getNickname()); resources.setCreateByName(loginUser.getNickname());
deviceTypeMapper.insert(resources); deviceTypeMapper.insert(resources);
// 自动授权给自己
DeviceTypeGrants deviceTypeGrants = new DeviceTypeGrants();
deviceTypeGrants.setDeviceTypeId(resources.getId());
deviceTypeGrants.setCustomerId(loginUser.getUserId());
deviceTypeGrants.setGrantorCustomerId(loginUser.getUserId());
deviceTypeGrants.setGrantedAt(new Date());
deviceTypeGrantsMapper.insert(deviceTypeGrants);
} }
@ -99,6 +112,11 @@ public class DeviceTypeServiceImpl extends ServiceImpl<DeviceTypeMapper, DeviceT
@Override @Override
@Transactional(rollbackFor = Exception.class) @Transactional(rollbackFor = Exception.class)
public void update(DeviceTypeForm resources) { public void update(DeviceTypeForm resources) {
DeviceTypeGrants deviceTypeGrants = deviceTypeGrantsMapper.selectById(resources.getId());
if (deviceTypeGrants == null) {
throw new RuntimeException("设备类型不存在");
}
resources.setId(deviceTypeGrants.getDeviceTypeId());
DeviceType deviceType = deviceTypeMapper.selectById(resources.getId()); DeviceType deviceType = deviceTypeMapper.selectById(resources.getId());
if (deviceType == null) { if (deviceType == null) {
throw new RuntimeException("设备类型不存在"); throw new RuntimeException("设备类型不存在");
@ -122,28 +140,30 @@ public class DeviceTypeServiceImpl extends ServiceImpl<DeviceTypeMapper, DeviceT
@Transactional(rollbackFor = Exception.class) @Transactional(rollbackFor = Exception.class)
public void deleteAll(List<Long> ids) { public void deleteAll(List<Long> ids) {
List<Long> invalidIds = new ArrayList<>(); deviceTypeGrantsMapper.deleteByIds(ids);
List<Long> invalidId2 = new ArrayList<>(); //
for (Long id : ids) { // List<Long> invalidIds = new ArrayList<>();
DeviceType deviceType = deviceTypeMapper.selectById(id); // List<Long> invalidId2 = new ArrayList<>();
if (deviceType == null || !Objects.equals(deviceType.getOwnerCustomerId(), LoginHelper.getUserId())) { // for (Long id : ids) {
invalidIds.add(id); // DeviceType deviceType = deviceTypeMapper.selectById(id);
} // if (deviceType == null || !Objects.equals(deviceType.getOwnerCustomerId(), LoginHelper.getUserId())) {
DeviceQueryCriteria deviceQueryCriteria = new DeviceQueryCriteria(); // invalidIds.add(id);
deviceQueryCriteria.setDeviceType(id); // }
List<Device> devices = deviceMapper.findAll(deviceQueryCriteria); // DeviceQueryCriteria deviceQueryCriteria = new DeviceQueryCriteria();
if (!devices.isEmpty()) { // deviceQueryCriteria.setDeviceType(id);
invalidId2.add(id); // List<Device> devices = deviceMapper.findAll(deviceQueryCriteria);
} // if (!devices.isEmpty()) {
} // invalidId2.add(id);
if (!invalidIds.isEmpty()) { // }
throw new RuntimeException("以下设备类型无法删除ID 不存在或无权限): " + invalidIds); // }
} // if (!invalidIds.isEmpty()) {
if (!invalidId2.isEmpty()) { // throw new RuntimeException("以下设备类型无法删除ID 不存在或无权限): " + invalidIds);
throw new RuntimeException("以下设备类型无法删除(已绑定设备): " + invalidId2); // }
} // if (!invalidId2.isEmpty()) {
// throw new RuntimeException("以下设备类型无法删除(已绑定设备): " + invalidId2);
deviceTypeMapper.deleteByIds(ids); // }
//
// deviceTypeMapper.deleteByIds(ids);
} }

View File

@ -0,0 +1,19 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.fuyuanshen.equipment.mapper.DeviceTypeGrantsMapper">
<resultMap id="BaseResultMap" type="com.fuyuanshen.equipment.domain.DeviceTypeGrants">
<id property="id" column="id" />
<result property="deviceTypeId" column="device_type_id" />
<result property="customerId" column="customer_id" />
<result property="grantorCustomerId" column="grantor_customer_id" />
<result property="grantedAt" column="granted_at" />
<result property="assignmentId" column="assignment_id" />
</resultMap>
<sql id="Base_Column_List">
id,device_type_id,customer_id,grantor_customer_id,granted_at,assignment_id
</sql>
</mapper>

View File

@ -2,7 +2,7 @@
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" > <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.fuyuanshen.equipment.mapper.DeviceTypeMapper"> <mapper namespace="com.fuyuanshen.equipment.mapper.DeviceTypeMapper">
<resultMap id="BaseResultMap" type="com.fuyuanshen.equipment.domain.DeviceType"> <resultMap id="BaseResultMap" type="com.fuyuanshen.equipment.domain.DeviceType">
<id column="id" property="id"/> <id column="grant_id" property="id"/>
<result column="type_name" property="typeName"/> <result column="type_name" property="typeName"/>
<result column="is_support_ble" property="isSupportBle"/> <result column="is_support_ble" property="isSupportBle"/>
<result column="locate_mode" property="locateMode"/> <result column="locate_mode" property="locateMode"/>
@ -19,29 +19,27 @@
, type_name, is_support_ble, locate_mode, network_way, create_by, update_by, create_time, update_time,communication_mode , type_name, is_support_ble, locate_mode, network_way, create_by, update_by, create_time, update_time,communication_mode
</sql> </sql>
<!-- 查询所有设备类型 --> <!-- 查询所有设备类型 -->
<select id="findAll" resultMap="BaseResultMap"> <select id="findAll" resultMap="BaseResultMap">
SELECT DISTINCT dt.* SELECT DISTINCT dt.* ,dg.id AS grant_id ,dg.create_time AS Dcreate_time
FROM device_type dt FROM device_type dt
JOIN device_type_grants dg ON dt.id = dg.device_type_id
<where> <where>
<if test="criteria.typeName != null and criteria.typeName.trim() != ''"> <if test="criteria.typeName != null and criteria.typeName.trim() != ''">
and dt.type_name like concat('%', TRIM(#{criteria.typeName}), '%') and dt.type_name like concat('%', TRIM(#{criteria.typeName}), '%')
</if> </if>
<if test="criteria.deviceTypeId != null">
and dt.id = #{criteria.deviceTypeId}
</if>
<if test="criteria.customerId != null"> <if test="criteria.customerId != null">
and dt.owner_customer_id = #{criteria.customerId} and dg.customer_id = #{criteria.customerId}
</if> </if>
</where> </where>
ORDER BY dt.create_time DESC ORDER BY Dcreate_time DESC
</select> </select>
<!-- 获取已经分配的设备类型 --> <!-- 获取已经分配的设备类型 -->
<select id="getAssignType" resultType="com.fuyuanshen.equipment.domain.DeviceType"> <select id="getAssignType" resultType="com.fuyuanshen.equipment.domain.DeviceType">
SELECT * SELECT *
FROM device_type FROM device_type
WHERE owner_customer_id = #{customerId} AND original_device_id = #{deviceType} WHERE owner_customer_id = #{customerId}
AND original_device_id = #{deviceType}
</select> </select>
</mapper> </mapper>