<?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.xinda.scada.mapper.ScadaMapper">

    <resultMap type="Scada" id="ScadaResult">
        <result property="id"    column="id"    />
        <result property="guid"    column="guid"    />
        <result property="scadaData"    column="scada_data"    />
        <result property="serialNumbers"    column="serial_numbers"    />
        <result property="deviceName"    column="device_name"    />
        <result property="isMainPage"    column="is_main_page"    />
        <result property="pageName"    column="page_name"    />
        <result property="pageResolution"    column="page_resolution"    />
        <result property="isShare"    column="is_share"    />
        <result property="shareUrl"    column="share_url"    />
        <result property="sharePass"    column="share_pass"    />
        <result property="pageImage"    column="page_image"    />
        <result property="deptId"     column="dept_id"     />
        <result property="tenantId"    column="tenant_id"    />
        <result property="tenantName"    column="tenant_name"    />
        <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="delFlag"    column="del_flag"    />
        <result property="type" column="type"/>
    </resultMap>

    <sql id="selectScadaVo">
        select id, guid, scada_data, serial_numbers, device_name, is_main_page, page_name, page_resolution, is_share, share_url, share_pass, page_image, dept_id,tenant_id, tenant_name, create_by, create_time, update_by, update_time, del_flag, type from scada
    </sql>

    <select id="selectScadaList" parameterType="Scada" resultMap="ScadaResult">
        select s.id, s.guid, s.scada_data, s.serial_numbers, s.device_name, s.is_main_page, s.page_name, s.page_resolution, s.is_share, s.share_url, s.share_pass, s.page_image, s.dept_id, s.tenant_id, s.tenant_name, s.create_by, s.create_time, s.update_by, s.update_time, s.del_flag, s.type
        from scada s
        <where>
            <if test="guid != null  and guid != ''"> and s.guid = #{guid}</if>
            <if test="scadaData != null  and scadaData != ''"> and s.scada_data = #{scadaData}</if>
            <if test="serialNumbers != null  and serialNumbers != ''"> and s.serial_numbers = #{serialNumbers}</if>
            <if test="deviceName != null  and deviceName != ''"> and s.device_name like concat('%', #{deviceName}, '%')</if>
            <if test="isMainPage != null "> and s.is_main_page = #{isMainPage}</if>
            <if test="pageName != null  and pageName != ''"> and s.page_name like concat('%', #{pageName}, '%')</if>
            <if test="pageResolution != null  and pageResolution != ''"> and s.page_resolution = #{pageResolution}</if>
            <if test="isShare != null "> and s.is_share = #{isShare}</if>
            <if test="shareUrl != null  and shareUrl != ''"> and s.share_url = #{shareUrl}</if>
            <if test="sharePass != null  and sharePass != ''"> and s.share_pass = #{sharePass}</if>
            <if test="pageImage != null  and pageImage != ''"> and s.page_image = #{pageImage}</if>
            <if test="deptId != null ">
             and (   s.dept_id in (SELECT de.dept_id FROM  sys_dept de
                WHERE FIND_IN_SET(#{deptId}, de.ancestors) OR de.dept_id = #{deptId})
                )
             </if>
<!--            <if test="tenantName != null  and tenantName != ''"> and tenant_name like concat('%', #{tenantName}, '%')</if>-->
            <if test="type != null">and s.type = #{type}</if>
        </where>
    </select>

    <select id="selectScadaById" parameterType="Long" resultMap="ScadaResult">
        <include refid="selectScadaVo"/>
        where id = #{id}
    </select>

    <select id="selectScadaByGuid" resultType="com.xinda.scada.domain.Scada">
        <include refid="selectScadaVo"/>
        where guid = #{guid}
    </select>

    <select id="getStatusBySerialNumber" resultType="java.lang.Integer">
        select status
        from iot_device
        where serial_number = #{serialNumber}
    </select>

    <select id="selectDeviceProductAlertCount" resultType="com.xinda.scada.vo.ScadaStatisticVO">
        select
        <!--设备数量-->
        (select count(distinct d.device_id,d.user_id)
        from iot_device d
        inner join iot_device_user u on u.device_id = d.device_id
        <where>
            <if test="userId != null and userId != 0"> and u.user_id = #{userId}</if>
            <if test="tenantId != null and tenantId != 0"> and d.tenant_id = #{tenantId}</if>

        </where>
        ) as deviceCount,

        <!--在线设备数量-->
        (select count(distinct d.device_id,d.user_id)
        from iot_device d
        inner join iot_device_user u on u.device_id = d.device_id
        <where>
            d.status=3
            <if test="userId != null and userId != 0"> and u.user_id = #{userId}</if>
            <if test="tenantId != null and tenantId != 0"> and d.tenant_id = #{tenantId}</if>
        </where>
        ) as deviceOnlineCount,

        (
        <!--普通用户查询设备中的产品数量-->
        <if test="userId != null and userId != 0">
            select count(distinct product_id)
            from iot_device
            where user_id = #{userId}
        </if>
        <!--管理员和租户直接查询产品的数量-->
        <if test="userId == null || userId == 0">
            select count(product_id)
            from iot_product
            <where>
                <if test="tenantId != null and tenantId != 0"> and tenant_id = #{tenantId} </if>
            </where>
        </if>
        ) as productCount,

        <!--告警设备数量-->
        (select count(distinct l.serial_number)
        from iot_alert_log l
        left join iot_device d on l.serial_number=d.serial_number
        left join iot_device_user u on d.device_id=u.device_id
        <where>
            <if test="userId != null and userId != 0"> and u.user_id = #{userId}</if>
            <if test="tenantId != null and tenantId != 0"> and d.tenant_id = #{tenantId}</if>
        </where>
        ) as alertDeviceCount,

        <!--离线设备数量-->
        (select count(distinct d.device_id,d.user_id)
        from iot_device d
        inner join iot_device_user u on u.device_id = d.device_id
        <where>
            d.status=4
            <if test="userId != null and userId != 0"> and u.user_id = #{userId}</if>
            <if test="tenantId != null and tenantId != 0"> and d.tenant_id = #{tenantId}</if>
        </where>
        ) as deviceOfflineCount,

        <!--告警未处理数量-->
        (select count(distinct l.serial_number)
        from iot_alert_log l
        left join iot_device d on l.serial_number=d.serial_number
        left join iot_device_user u on d.device_id=u.device_id
        where l.status = 2
        <if test="userId != null and userId != 0"> and u.user_id = #{userId}</if>
        <if test="tenantId != null and tenantId != 0"> and d.tenant_id = #{tenantId}</if>
        ) as alertNotProcessedCount,

        <!--告警已处理数量-->
        (select count(distinct l.serial_number)
        from iot_alert_log l
        left join iot_device d on l.serial_number=d.serial_number
        left join iot_device_user u on d.device_id=u.device_id
        where l.status = 3
        <if test="userId != null and userId != 0"> and u.user_id = #{userId}</if>
        <if test="tenantId != null and tenantId != 0"> and d.tenant_id = #{tenantId}</if>
        ) as alertProcessedCount,

        (select count(distinct alert_log_id)
        from iot_alert_log l
        left join iot_device d on l.serial_number=d.serial_number
        left join iot_device_user u on d.device_id=u.device_id
        <where>
            <if test="userId != null and userId != 0"> and u.user_id = #{userId}</if>
            <if test="tenantId != null and tenantId != 0"> and d.tenant_id = #{tenantId}</if>
        </where>
        ) as alertCount
    </select>

    <select id="listFunctionLogHistory" resultType="com.xinda.scada.vo.ScadaHistoryModelVO">
        select fun_value as value,
        create_time as time,
        identify as identity
        from iot_function_log
        <where>
            <if test="serialNumber != null and serialNumber != ''">
                and serial_number = #{serialNumber}
            </if>
            <if test="beginTime != null and endTime != null">
                and create_time between #{beginTime} and #{endTime}
            </if>
            <if test="identifyList != null and identifyList != ''">
                and identify in
                <foreach collection="identifyList" item="identify" open="(" separator="," close=")">
                    #{identify}
                </foreach>
            </if>
        </where>
        order by create_time desc
    </select>

    <select id="listEventLogHistory" resultType="com.xinda.scada.vo.ScadaHistoryModelVO">
        select log_value as value,
        create_time as time,
        identity as identity
        from iot_event_log
        <where>
            <if test="serialNumber != null and serialNumber != ''">
                and serial_number = #{serialNumber}
            </if>
            <if test="params.beginTime != null and params.beginTime != '' and params.endTime != null and params.endTime != ''">
                and create_time between #{params.beginTime} and #{params.endTime}
            </if>
            <if test="identityList != null and identityList != ''">
                and identity in
                <foreach collection="identityList" item="identity" open="(" separator="," close=")">
                    #{identity}
                </foreach>
            </if>
        </where>
        order by create_time desc
    </select>

    <insert id="insertScada" parameterType="Scada" useGeneratedKeys="true" keyProperty="id">
        insert into scada
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="guid != null">guid,</if>
            <if test="scadaData != null">scada_data,</if>
            <if test="serialNumbers != null">serial_numbers,</if>
            <if test="deviceName != null">device_name,</if>
            <if test="isMainPage != null">is_main_page,</if>
            <if test="pageName != null">page_name,</if>
            <if test="pageResolution != null">page_resolution,</if>
            <if test="isShare != null">is_share,</if>
            <if test="shareUrl != null">share_url,</if>
            <if test="sharePass != null">share_pass,</if>
            <if test="pageImage != null">page_image,</if>
            <if test="deptId != null and deptId != 0">dept_id,</if>
            <if test="tenantId != null">tenant_id,</if>
            <if test="tenantName != null">tenant_name,</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="delFlag != null">del_flag,</if>
            <if test="remark != null">remark,</if>
            <if test="type != null">type,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="guid != null">#{guid},</if>
            <if test="scadaData != null">#{scadaData},</if>
            <if test="serialNumbers != null">#{serialNumbers},</if>
            <if test="deviceName != null">#{deviceName},</if>
            <if test="isMainPage != null">#{isMainPage},</if>
            <if test="pageName != null">#{pageName},</if>
            <if test="pageResolution != null">#{pageResolution},</if>
            <if test="isShare != null">#{isShare},</if>
            <if test="shareUrl != null">#{shareUrl},</if>
            <if test="sharePass != null">#{sharePass},</if>
            <if test="pageImage != null">#{pageImage},</if>
            <if test="deptId != null">#{deptId},</if>
            <if test="tenantId != null">#{tenantId},</if>
            <if test="tenantName != null">#{tenantName},</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="delFlag != null">#{delFlag},</if>
            <if test="remark != null">#{remark},</if>
            <if test="type != null">#{type},</if>
         </trim>
    </insert>

    <update id="updateScada" parameterType="Scada">
        update scada
        <trim prefix="SET" suffixOverrides=",">
            <if test="guid != null">guid = #{guid},</if>
            <if test="scadaData != null">scada_data = #{scadaData},</if>
            <if test="serialNumbers != null">serial_numbers = #{serialNumbers},</if>
            <if test="deviceName != null">device_name = #{deviceName},</if>
            <if test="isMainPage != null">is_main_page = #{isMainPage},</if>
            <if test="pageName != null">page_name = #{pageName},</if>
            <if test="pageResolution != null">page_resolution = #{pageResolution},</if>
            <if test="isShare != null">is_share = #{isShare},</if>
            <if test="shareUrl != null">share_url = #{shareUrl},</if>
            <if test="sharePass != null">share_pass = #{sharePass},</if>
            <if test="pageImage != null">page_image = #{pageImage},</if>
            <if test="deptId != null ">dept_id = #{deptId},</if>
            <if test="tenantId != null">tenant_id = #{tenantId},</if>
            <if test="tenantName != null">tenant_name = #{tenantName},</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="delFlag != null">del_flag = #{delFlag},</if>
            <if test="remark != null">remark = #{remark},</if>
            <if test="type != null">type = #{type},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteScadaById" parameterType="Long">
        delete from scada where id = #{id}
    </delete>

    <delete id="deleteScadaByGuid" parameterType="String">
        delete from scada where guid = #{guid}
    </delete>

    <delete id="deleteScadaByIds" parameterType="String">
        delete from scada where id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>

    <select id="selectUserIdByDeptId" resultType="java.lang.Long">
        select user_id
        from sys_user
        where dept_id = #{deptId}
          and status = 0
          and del_flag = 0
    </select>

    <select id="selectGuidByProductId" resultType="java.lang.String">
        select p.guid from iot_product p
        where p.product_id = #{productId}
          and p.del_flag = '0'
    </select>

    <select id="selectGuidBySceneModelId" resultType="java.lang.String">
        select guid
        from scene_model
        where scene_model_id = #{sceneModelId}
          and del_flag = 0
    </select>

    <select id="selectProductByGuid" resultType="java.lang.Long">
        select product_id
        from iot_product
        where guid = #{guid}
          and del_flag = '0'
    </select>

    <select id="selectSceneModelByGuid" resultType="java.lang.Long">
        select scene_model_id
        from scene_model
        where guid = #{guid}
          and del_flag = 0
    </select>

    <update id="updateProductGuid">
        update iot_product
        set guid = #{guid}
        where product_id = #{productId}
    </update>

    <update id="updateSceneModelGuid">
        update scene_model
        set guid = #{guid}
        where scene_model_id = #{sceneModelId}
    </update>

    <update id="updateDeviceGuid">
        update iot_device
        set guid = #{guid,jdbcType=VARCHAR}
        where serial_number = #{serialNumber,jdbcType=VARCHAR}
    </update>

    <update id="deleteSceneModelByGuids">
        update scene_model
        set guid = null
        where guid in
        <foreach collection="guids" item="guid" open="(" separator="," close=")">
            #{guid}
        </foreach>
    </update>

    <update id="deleteProductByGuids">
        update iot_product
        set guid = null
        where guid in
        <foreach collection="guids" item="guid" open="(" separator="," close=")">
            #{guid}
        </foreach>
    </update>
</mapper>
