fastbee/fastbee-service/fastbee-iot-service/src/main/resources/mapper/iot/ProjectMapper.xml

252 lines
13 KiB
XML
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?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.fastbee.iot.mapper.ProjectMapper">
<resultMap type="com.fastbee.iot.domain.Project" id="ProjectResult">
<result property="projectId" column="project_id" />
<result property="projectName" column="project_name" />
<result property="projectCode" column="project_code" />
<result property="tenantId" column="tenant_id" />
<result property="tenantName" column="tenant_name" />
<result property="responsibleUserId" column="responsible_user_id" />
<result property="responsibleUserName" column="responsible_user_name" />
<result property="longitude" column="longitude" />
<result property="latitude" column="latitude" />
<result property="address" column="address" />
<result property="projectStatus" column="project_status" />
<result property="startTime" column="start_time" />
<result property="endTime" column="end_time" />
<result property="projectDesc" column="project_desc" />
<result property="delFlag" column="del_flag" />
<result property="createBy" column="create_by" />
<result property="createTime" column="create_time" />
<result property="updateBy" column="update_by" />
<result property="updateTime" column="update_time" />
<result property="remark" column="remark" />
<result property="deviceCount" column="device_count" />
<result property="onlineCount" column="online_count" />
<result property="offlineCount" column="offline_count" />
</resultMap>
<resultMap type="com.fastbee.iot.domain.Device" id="DeviceResult">
<result property="deviceId" column="device_id" />
<result property="deviceName" column="device_name" />
<result property="serialNumber" column="serial_number" />
<result property="productId" column="product_id" />
<result property="productName" column="product_name" />
<result property="status" column="status" />
<result property="longitude" column="longitude" />
<result property="latitude" column="latitude" />
</resultMap>
<sql id="selectProjectVo">
select project_id, project_name, project_code, tenant_id, tenant_name,
responsible_user_id, responsible_user_name, longitude, latitude, address,
project_status, start_time, end_time, project_desc, del_flag,
create_by, create_time, update_by, update_time, remark
from iot_project
</sql>
<select id="selectProjectList" parameterType="com.fastbee.iot.domain.Project" resultMap="ProjectResult">
SELECT
p.project_id, p.project_name, p.project_code, p.tenant_id, p.tenant_name,
p.responsible_user_id, p.responsible_user_name, p.longitude, p.latitude, p.address,
p.project_status, p.start_time, p.end_time, p.project_desc, p.del_flag,
p.create_by, p.create_time, p.update_by, p.update_time, p.remark,
COUNT(DISTINCT d.device_id) AS device_count,
SUM(CASE WHEN d.status = 3 THEN 1 ELSE 0 END) AS online_count,
SUM(CASE WHEN d.device_id IS NOT NULL AND d.status != 3 THEN 1 ELSE 0 END) AS offline_count
FROM iot_project p
LEFT JOIN iot_device d ON d.project_id = p.project_id AND d.del_flag = '0'
<where>
p.del_flag = '0'
<if test="projectName != null and projectName != ''"> and p.project_name like concat('%', #{projectName}, '%')</if>
<if test="projectCode != null and projectCode != ''"> and p.project_code = #{projectCode}</if>
<if test="tenantId != null"> and p.tenant_id = #{tenantId}</if>
<if test="responsibleUserId != null"> and p.responsible_user_id = #{responsibleUserId}</if>
<if test="params.deptId != null and params.deptId != 0">
and p.responsible_user_id in (
select u.user_id
from sys_user u
where u.del_flag = '0'
and (u.dept_id = #{params.deptId}
or u.dept_id in (
select t.dept_id
from sys_dept t
where find_in_set(#{params.deptId}, t.ancestors)
))
)
</if>
<if test="projectStatus != null"> and p.project_status = #{projectStatus}</if>
</where>
GROUP BY p.project_id
order by p.create_time desc
</select>
<select id="selectProjectById" parameterType="Long" resultMap="ProjectResult">
<include refid="selectProjectVo"/>
where project_id = #{projectId} and del_flag = '0'
</select>
<select id="selectDeviceIdsByProjectId" parameterType="Long" resultType="Long">
select device_id from iot_device where project_id=#{projectId} and del_flag='0'
</select>
<select id="selectProjectsByUserId" parameterType="Long" resultMap="ProjectResult">
<include refid="selectProjectVo"/>
where responsible_user_id = #{userId} and del_flag = '0'
order by create_time desc
</select>
<select id="selectProjectsByTenantId" parameterType="Long" resultMap="ProjectResult">
<include refid="selectProjectVo"/>
where tenant_id = #{tenantId} and del_flag = '0'
order by create_time desc
</select>
<!-- 查询用户负责的项目设备统计(JOIN查询) -->
<select id="selectProjectDeviceStatsByUserId" parameterType="Long" resultType="map">
SELECT
COUNT(DISTINCT d.device_id) as totalDevices,
SUM(CASE WHEN d.status = 3 THEN 1 ELSE 0 END) as onlineDevices,
SUM(CASE WHEN d.status != 3 THEN 1 ELSE 0 END) as offlineDevices
FROM iot_device d
INNER JOIN iot_project p ON d.project_id = p.project_id
WHERE p.responsible_user_id = #{userId}
AND p.del_flag='0'
AND d.del_flag='0'
</select>
<!-- 查询用户负责的项目设备列表(JOIN查询) -->
<select id="selectProjectDevicesByUserId" parameterType="Long" resultMap="DeviceResult">
SELECT DISTINCT d.device_id, d.device_name, d.serial_number, d.product_id, d.product_name, d.status, d.longitude, d.latitude
FROM iot_device d
INNER JOIN iot_project p ON d.project_id = p.project_id
WHERE p.responsible_user_id = #{userId}
AND p.del_flag='0'
AND d.del_flag='0'
ORDER BY d.device_id DESC
</select>
<!-- 查询设备归属用户的设备统计(现有逻辑) -->
<select id="selectUserDeviceStats" parameterType="Long" resultType="map">
SELECT
COUNT(d.device_id) as totalDevices,
SUM(CASE WHEN d.status = 3 THEN 1 ELSE 0 END) as onlineDevices,
SUM(CASE WHEN d.status != 3 THEN 1 ELSE 0 END) as offlineDevices
FROM iot_device d
INNER JOIN iot_device_user du ON d.device_id = du.device_id
WHERE du.user_id = #{userId}
AND d.del_flag='0'
</select>
<!-- 查询设备归属用户的设备列表(现有逻辑) -->
<select id="selectUserDevices" parameterType="Long" resultMap="DeviceResult">
SELECT d.device_id, d.device_name, d.serial_number, d.product_id, d.product_name, d.status, d.longitude, d.latitude
FROM iot_device d
INNER JOIN iot_device_user du ON d.device_id = du.device_id
WHERE du.user_id = #{userId}
AND d.del_flag='0'
ORDER BY d.device_id DESC
</select>
<insert id="insertProject" parameterType="com.fastbee.iot.domain.Project" useGeneratedKeys="true" keyProperty="projectId">
insert into iot_project
<trim prefix="(" suffix=")" suffixOverrides=",">
<if test="projectName != null and projectName != ''">project_name,</if>
<if test="projectCode != null and projectCode != ''">project_code,</if>
<if test="tenantId != null">tenant_id,</if>
<if test="tenantName != null and tenantName != ''">tenant_name,</if>
<if test="responsibleUserId != null">responsible_user_id,</if>
<if test="responsibleUserName != null and responsibleUserName != ''">responsible_user_name,</if>
<if test="longitude != null">longitude,</if>
<if test="latitude != null">latitude,</if>
<if test="address != null and address != ''">address,</if>
<if test="projectStatus != null">project_status,</if>
<if test="startTime != null">start_time,</if>
<if test="endTime != null">end_time,</if>
<if test="projectDesc != null and projectDesc != ''">project_desc,</if>
<if test="delFlag != null">del_flag,</if>
<if test="createBy != null">create_by,</if>
<if test="createTime != null">create_time,</if>
<if test="updateBy != null">update_by,</if>
<if test="updateTime != null">update_time,</if>
<if test="remark != null">remark,</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides=",">
<if test="projectName != null and projectName != ''">#{projectName},</if>
<if test="projectCode != null and projectCode != ''">#{projectCode},</if>
<if test="tenantId != null">#{tenantId},</if>
<if test="tenantName != null and tenantName != ''">#{tenantName},</if>
<if test="responsibleUserId != null">#{responsibleUserId},</if>
<if test="responsibleUserName != null and responsibleUserName != ''">#{responsibleUserName},</if>
<if test="longitude != null">#{longitude},</if>
<if test="latitude != null">#{latitude},</if>
<if test="address != null and address != ''">#{address},</if>
<if test="projectStatus != null">#{projectStatus},</if>
<if test="startTime != null">#{startTime},</if>
<if test="endTime != null">#{endTime},</if>
<if test="projectDesc != null and projectDesc != ''">#{projectDesc},</if>
<if test="delFlag != null">#{delFlag},</if>
<if test="createBy != null">#{createBy},</if>
<if test="createTime != null">#{createTime},</if>
<if test="updateBy != null">#{updateBy},</if>
<if test="updateTime != null">#{updateTime},</if>
<if test="remark != null">#{remark},</if>
</trim>
</insert>
<update id="updateProject" parameterType="com.fastbee.iot.domain.Project">
update iot_project
<trim prefix="SET" suffixOverrides=",">
<if test="projectName != null and projectName != ''">project_name = #{projectName},</if>
<if test="projectCode != null and projectCode != ''">project_code = #{projectCode},</if>
<if test="tenantId != null">tenant_id = #{tenantId},</if>
<if test="tenantName != null and tenantName != ''">tenant_name = #{tenantName},</if>
<if test="responsibleUserId != null">responsible_user_id = #{responsibleUserId},</if>
<if test="responsibleUserName != null and responsibleUserName != ''">responsible_user_name = #{responsibleUserName},</if>
<if test="longitude != null">longitude = #{longitude},</if>
<if test="latitude != null">latitude = #{latitude},</if>
<if test="address != null and address != ''">address = #{address},</if>
<if test="projectStatus != null">project_status = #{projectStatus},</if>
<if test="startTime != null">start_time = #{startTime},</if>
<if test="endTime != null">end_time = #{endTime},</if>
<if test="projectDesc != null and projectDesc != ''">project_desc = #{projectDesc},</if>
<if test="delFlag != null">del_flag = #{delFlag},</if>
<if test="createBy != null">create_by = #{createBy},</if>
<if test="createTime != null">create_time = #{createTime},</if>
<if test="updateBy != null">update_by = #{updateBy},</if>
<if test="updateTime != null">update_time = #{updateTime},</if>
<if test="remark != null">remark = #{remark},</if>
</trim>
where project_id = #{projectId}
</update>
<delete id="deleteProjectByIds" parameterType="String">
update iot_project set del_flag = '2' where project_id in
<foreach item="projectId" collection="array" open="(" separator="," close=")">
#{projectId}
</foreach>
</delete>
<delete id="deleteProjectById" parameterType="Long">
update iot_project set del_flag = '2' where project_id = #{projectId}
</delete>
<!-- 查询项目设备统计(包含设备总数、在线数、离线数、预警数) -->
<select id="selectProjectDeviceStats" parameterType="Long" resultMap="ProjectResult">
SELECT
p.*,
COUNT(DISTINCT d.device_id) as device_count,
SUM(CASE WHEN d.status = 3 THEN 1 ELSE 0 END) as online_count,
SUM(CASE WHEN d.status != 3 THEN 1 ELSE 0 END) as offline_count
FROM iot_project p
LEFT JOIN iot_device d ON d.project_id = p.project_id AND d.del_flag = '0'
WHERE p.project_id = #{projectId} AND p.del_flag = '0'
GROUP BY p.project_id
</select>
</mapper>