<?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="cn.iocoder.yudao.module.product.dal.mysql.deviceinfo.DeviceInfoMapper">

    <!--
        一般情况下，尽可能使用 Mapper 进行 CRUD 增删改查即可。
        无法满足的场景，例如说多表关联查询，才使用 XML 编写 SQL。
        代码生成器暂时只生成 Mapper XML 文件本身，更多推荐 MybatisX 快速开发插件来生成查询。
        文档可见：https://www.iocoder.cn/MyBatis/x-plugins/
     -->

    <select id="selectDeviceInfoPage" resultType="cn.iocoder.yudao.module.product.controller.admin.deviceinfo.vo.DeviceInfoRespVO">
        SELECT pdi.*, pet.env_type_name
        FROM product_device_info pdi
        LEFT JOIN product_env_type pet ON pet.id = pdi.env_type
        <where>
            pdi.deleted = 0
            <if test="pageReqVO.deviceName != null and pageReqVO.deviceName != ''">and pdi.device_name like concat('%',#{pageReqVO.deviceName},'%') ESCAPE '/'</if>
            <if test="pageReqVO.gatewayType != null">and pdi.gateway_type = #{pageReqVO.gatewayType}</if>
            <if test="pageReqVO.deviceType != null">and pdi.device_type = #{pageReqVO.deviceType}</if>
            <if test="pageReqVO.belongBusinessId != null">and pdi.belong_business_id = #{pageReqVO.belongBusinessId}</if>
        </where>
        order by pdi.create_time desc
    </select>

    <resultMap id="queryDeviceEnvTypesMap" type="cn.iocoder.yudao.module.product.controller.admin.deviceinfo.vo.DeviceEnvTypeRespVO">
        <result column="id" property="id" />
        <collection property="SlaveEnvTypes" column="id=id" select="querySlaveEnvTypes"></collection>
    </resultMap>
    <select id="getDeviceEnvTypes" resultMap="queryDeviceEnvTypesMap">
        select pdi.*, pet.env_type_name, pet.env_field_name
        from product_device_info pdi
        LEFT JOIN product_env_type pet ON pet.id = pdi.env_type
        <where>
            pdi.deleted = 0 and pdi.enabled = 1
            <if test="tenantId != null">and pdi.tenant_id = #{tenantId}</if>
            <if test="belongBusinessId != null">and pdi.belong_business_id = #{belongBusinessId}</if>
            <if test="gatewayType != null">and pdi.gateway_type = #{gatewayType}</if>
            <if test="deviceType != null">and pdi.device_type = #{deviceType}</if>
        </where>

    </select>
    <select id="querySlaveEnvTypes" resultType="cn.iocoder.yudao.module.product.controller.admin.deviceinfo.vo.SlaveDeviceEnvTypeRespVO">
        select psd.*, pet.env_type_name, pet.env_field_name
        from product_slave_device psd
        LEFT JOIN product_env_type pet ON pet.id = psd.env_type
        <where>
            psd.deleted = 0
            <if test="id != null">and psd.device_id = #{id}</if>
        </where>
    </select>
</mapper>
