<?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.zcxk.rmcp.core.dao.AlarmTypeMapper">
    <!-- 结果集 -->
    <resultMap type="com.zcxk.rmcp.core.entity.AlarmType" id="AlarmTypeMap">
        <result property="id" column="id" jdbcType="INTEGER"/>
        <result property="productCategroyId" column="product_categroy_id" jdbcType="INTEGER"/>
        <result property="deviceTypeId" column="device_type_id" jdbcType="INTEGER"/>
        <result property="name" column="name" jdbcType="VARCHAR"/>
        <result property="status" column="status" jdbcType="INTEGER"/>
        <result property="createDate" column="create_date" jdbcType="TIMESTAMP"/>
        <result property="createBy" column="create_by" jdbcType="VARCHAR"/>
        <result property="updateDate" column="update_date" jdbcType="TIMESTAMP"/>
        <result property="updateBy" column="update_by" jdbcType="VARCHAR"/>
        <result property="typeDesc" column="type_desc" jdbcType="VARCHAR"/>
        <result property="tenantId" column="tenant_id" jdbcType="VARCHAR"/>
        <result property="alarmStatus" column="alarm_status" jdbcType="INTEGER"/>
    </resultMap>

    <!-- 基本字段 -->
    <sql id="Base_Column_List">
        rt.id, rt.product_categroy_id, rt.device_type_id,
rt.name, rt.status, rt.create_date, rt.create_by, rt.update_date, rt.update_by,
rt.type_desc, rt.tenant_id, rt.alarm_status    </sql>

    <!-- 查询单个 -->
    <select id="selectById" resultMap="AlarmTypeMap">
        select
        <include refid="Base_Column_List"/>
        from rmcp_alarm_type rt
        where id = #{id}
    </select>


    <!-- 查询全部 -->
    <select id="selectAll" resultMap="AlarmTypeMap">
        select
        <include refid="Base_Column_List"/>
        from rmcp_alarm_type
    </select>

    <!--通过实体作为筛选条件查询-->
    <select id="selectList" resultType="com.zcxk.rmcp.api.dto.alarm.AlarmTypeDto">
        select
        <include refid="Base_Column_List"/>,ifnull(rule.numberOfWaterMeters,0)numberOfWaterMeters
               ,CONCAT_ws("/",e.name,b.product_model,b.product_name) product_name,c.name categoryName
        from rmcp_alarm_type  rt left join (select count(*) numberOfWaterMeters,alarm_type_id
        from rmcp_device_alarm_rule where status=1 group by alarm_type_id ) rule on rt.id=rule.alarm_type_id
        join rmcp_product b on rt.device_type_id=b.id
        join rmcp_product_category c on rt.product_categroy_id=c.id
        join rmcp_manufacturer e on b.manufacturer_id=e.id
        <where>
        rt.status=1


            <if test="deviceTypeId != null">
                and rt.device_type_id = #{deviceTypeId}
            </if>
            <if test="productCategroyId != null">
                and rt.product_categroy_id = #{productCategroyId}
            </if>
            <if test="name != null and name != ''">
                and rt.name  like concat('%',#{name} ,'%')
            </if>

            <if test="typeDesc != null and typeDesc != ''">
                and rt.type_desc   like concat('%',#{typeDesc} ,'%')
            </if>
            <if test="tenantId != null and tenantId != ''">
                and rt.tenant_id = #{tenantId}
            </if>

        </where>
        order by rt.create_date desc
    </select>

    <!-- 新增所有列 -->
    <insert id="insert" keyProperty="id" useGeneratedKeys="true">
        insert into rmcp_alarm_type(id, product_categroy_id, device_type_id, name, status, create_date, create_by,
                                    update_date, update_by, type_desc, tenant_id, alarm_status)
        values (#{id}, #{productCategroyId}, #{deviceTypeId}, #{name}, #{status}, #{createDate}, #{createBy},
                #{updateDate}, #{updateBy}, #{typeDesc}, #{tenantId}, #{alarmStatus})
    </insert>

    <!-- 批量新增 -->
    <insert id="batchInsert">
        insert into rmcp_alarm_type(id, product_categroy_id, device_type_id, name, status, create_date, create_by,
        update_date, update_by, type_desc, tenant_id, alarm_status)
        values
        <foreach collection="alarmTypes" item="item" index="index" separator=",">
            (
            #{item.id}, #{item.productCategroyId}, #{item.deviceTypeId}, #{item.name}, #{item.status},
            #{item.createDate}, #{item.createBy}, #{item.updateDate}, #{item.updateBy}, #{item.typeDesc},
            #{item.tenantId}, #{item.alarmStatus} )
        </foreach>
    </insert>

    <!-- 通过主键修改数据 -->
    <update id="update">
        update rmcp_alarm_type
        <set>
            <if test="productCategroyId != null">
                product_categroy_id = #{productCategroyId},
            </if>
            <if test="deviceTypeId != null">
                device_type_id = #{deviceTypeId},
            </if>
            <if test="name != null and name != ''">
                name = #{name},
            </if>
            <if test="status != null">
                status = #{status},
            </if>
            <if test="createDate != null">
                create_date = #{createDate},
            </if>
            <if test="createBy != null and createBy != ''">
                create_by = #{createBy},
            </if>
            <if test="updateDate != null">
                update_date = #{updateDate},
            </if>
            <if test="updateBy != null and updateBy != ''">
                update_by = #{updateBy},
            </if>
            <if test="typeDesc != null and typeDesc != ''">
                type_desc = #{typeDesc},
            </if>
            <if test="tenantId != null and tenantId != ''">
                tenant_id = #{tenantId},
            </if>
            <if test="alarmStatus != null">
                alarm_status = #{alarmStatus},
            </if>
        </set>
        where id = #{id}
    </update>

    <!--通过主键删除-->
    <delete id="deleteById">
        delete
        from rmcp_alarm_type
        where id = #{id}
    </delete>
    <update id="delete">
        update rmcp_alarm_type set status=0 where id
        in
        <foreach collection="list" item="item" open="(" close=")" separator=",">
            #{item}
        </foreach>
    </update>

    <!-- 总数 -->
    <select id="count" resultType="int">
        select count(*)
        from rmcp_alarm_type
    </select>
    <select id="selectTypesWithRules" resultType="com.zcxk.rmcp.api.dto.alarm.AlarmTypeDto">
        select  <include refid="Base_Column_List"/>,rule.cn
        from rmcp_alarm_type rt left join (select ifnull(count(*),0)cn,alarm_type_id
        from rmcp_device_alarm_rule where status=1 group by alarm_type_id ) rule
        on rt.id=rule.alarm_type_id
        where  rule.cn>0 and rt.id in
        <foreach collection="list" item="item" open="(" close=")" separator=",">
            #{item}
        </foreach>
    </select>

</mapper>
