<?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.system.dal.mysql.biz.DeviceProductMapper">

    <!--
        一般情况下，尽可能使用 Mapper 进行 CRUD 增删改查即可。
        无法满足的场景，例如说多表关联查询，才使用 XML 编写 SQL。
        代码生成器暂时只生成 Mapper XML 文件本身，更多推荐 MybatisX 快速开发插件来生成查询。
        文档可见：https://www.iocoder.cn/MyBatis/x-plugins/
     -->
    <select id="getPage" resultType="cn.iocoder.yudao.module.system.controller.admin.biz.vo.DeviceProductRespVO">
        SELECT
            dp.id,
            dp.device_model_name,
            dp.model_name,
            dp.device_manufacturer_id,
            dp.device_manufacturer_name,
            dp.device_code,
            dp.device_mac,
            dp.device_name,
            dp.device_type_name,
            dp.serial_code,
            dp.validate_code,
            dp.sn_code,
            dp.create_time,
            dp.online_status,
            dp.binding_type,
            dp.binding_status,
            dp.binding_user_name,
            dp.binding_name,
            st.name AS "tenantName"
        FROM
            device_product dp
        LEFT JOIN system_tenant st ON dp.tenant_id = st.id
        WHERE dp.deleted = 0
        <if test="pageVO.tenantId != null">
            AND dp.tenant_id = #{pageVO.tenantId}
        </if>
        <if test="pageVO.deviceManufacturerId != null">
            AND dp.device_manufacturer_id = #{pageVO.deviceManufacturerId}
        </if>
        <if test="pageVO.snCode != null">
            AND dp.sn_code = #{pageVO.snCode}
        </if>
        <if test="pageVO.bindingUserId != null">
            AND dp.binding_user_id = #{pageVO.bindingUserId}
        </if>
        <if test="pageVO.bindingId != null">
            AND dp.binding_id = #{pageVO.bindingId}
        </if>
        <if test="pageVO.modelId != null">
            AND dp.model_id = #{pageVO.modelId}
        </if>
        <if test="pageVO.onlineStatus != null">
            AND dp.online_status = #{pageVO.onlineStatus}
        </if>
        ORDER BY dp.update_time DESC
    </select>



    <select id="getAllDeviceProductPage" resultType="cn.iocoder.yudao.module.system.controller.admin.biz.vo.DeviceProductRespVO">
        SELECT
            dp.id,
            dp.device_model_name,
            dp.model_name,
            dp.device_manufacturer_id,
            dp.device_manufacturer_name,
            dp.device_code,
            dp.device_mac,
            dp.device_name,
            dp.device_type_name,
            dp.serial_code,
            dp.validate_code,
            dp.sn_code,
            dp.create_time,
            dp.online_status,
            dp.binding_type,
            dp.binding_status,
            dp.tenant_id,
            st.name AS "tenantName"
        FROM
            device_product dp
        LEFT JOIN system_tenant st ON dp.tenant_id = st.id
        WHERE dp.deleted = 0
        <if test="pageVO.tenantId != null">
            AND dp.tenant_id = #{pageVO.tenantId}
        </if>
        <if test="pageVO.deviceManufacturerId != null">
            AND dp.device_manufacturer_id = #{pageVO.deviceManufacturerId}
        </if>
        <if test="pageVO.snCode != null">
            AND dp.sn_code = #{pageVO.snCode}
        </if>
        <if test="pageVO.bindingUserId != null">
            AND dp.binding_user_id = #{pageVO.bindingUserId}
        </if>
        <if test="pageVO.bindingId != null">
            AND dp.binding_id = #{pageVO.bindingId}
        </if>
        <if test="pageVO.modelId != null">
            AND dp.model_id = #{pageVO.modelId}
        </if>
        <if test="pageVO.onlineStatus != null">
            AND dp.online_status = #{pageVO.onlineStatus}
        </if>
        ORDER BY dp.update_time DESC
    </select>

    <select id="selectDetailById" resultType="cn.iocoder.yudao.module.system.controller.admin.biz.vo.DeviceProductDetailVO">
        SELECT
            dp.id,
            dp.model_id,
            dp.device_model_name,
            dp.model_name,
            dp.device_manufacturer_id,
            dp.device_manufacturer_name,
            dp.device_code,
            dp.device_mac,
            dp.sn_code,
            dp.device_name,
            dp.serial_code,
            dp.validate_code,
            dp.device_type,
            dp.device_type_name,
            dp.produce_date,
            dp.product_code,
            dp.channel_id,
            dp.last_use_time,
            dp.sale_status,
            dp.sale_date,
            dp.binding_type,
            dp.binding_status,
            dp.binding_date,
            dp.binding_user_id,
            dp.binding_user_name,
            dp.binding_id,
            dp.binding_name,
            dp.association_userid,
            dp.association_username,
            dp.remark,
            dp.device_phone,
            dp.device_position,
            dp.install_position,
            dp.is_new,
            dp.online_status,
            dp.create_time
        FROM
            device_product dp
        WHERE
            dp.id = #{id}
            AND deleted = 0
    </select>

    <select id="selectWarnUser" resultType="cn.iocoder.yudao.module.system.dal.dataobject.biz.DeviceWarnUserDO">
        SELECT
            dwu.id,
            CONCAT_WS('', su.nickname, sd.NAME ) AS "name",
            dwu.type
        FROM
            device_warn_user dwu
                LEFT JOIN system_users su ON dwu.user_id = su.id
                LEFT JOIN system_dept sd ON dwu.dept_id = sd.id
        WHERE
            dwu.deleted = 0
    </select>


</mapper>