<?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.DeviceAlarmRuleMapper">
    <!-- 结果集 -->
    <resultMap type="com.zcxk.rmcp.core.entity.DeviceAlarmRule" id="DeviceAlarmRuleMap">
        <result property="id" column="id" jdbcType="INTEGER"/>
        <result property="productCategroyId" column="product_categroy_id" jdbcType="INTEGER"/>
        <result property="tenantId" column="tenant_id" jdbcType="VARCHAR"/>
        <result property="deviceType" column="device_type" jdbcType="INTEGER"/>
        <result property="ruleName" column="rule_name" jdbcType="VARCHAR"/>
        <result property="measuringCode" column="measuring_code" jdbcType="VARCHAR"/>
        <result property="expression" column="expression" jdbcType="VARCHAR"/>
        <result property="alarmTypeId" column="alarm_type_id" jdbcType="INTEGER"/>
        <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="ruleDesc" column="rule_desc" jdbcType="VARCHAR"/>
        <result property="value1" column="value1" jdbcType="VARCHAR"/>
        <result property="value2" column="value2" jdbcType="VARCHAR"/>
        <result property="measuringDataType" column="measuring_data_type" jdbcType="INTEGER"/>
        <result property="specification" column="specification" jdbcType="VARCHAR"/>
    </resultMap>

    <!-- 基本字段 -->
    <sql id="Base_Column_List">
        a.id, a.product_categroy_id, a.tenant_id, a.device_type,
a.rule_name,a. measuring_code, a.expression, a.alarm_type_id, a.status, a.create_date, a.create_by,
a.update_date,a. update_by,a. rule_desc, a.value1,a. value2,a. measuring_data_type, a.specification    </sql>

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


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

    <!--通过实体作为筛选条件查询-->
    <select id="selectList" resultType="com.zcxk.rmcp.api.dto.alarm.AlarmRuleDto">
        select
        <include refid="Base_Column_List"/>,CONCAT_ws("/",e.name,b.product_model,b.product_name) product_name,c.name categoryName,d.measuring_name measuringName
        from rmcp_device_alarm_rule a join rmcp_product b on a.device_type=b.id
                                      join rmcp_manufacturer e on b.manufacturer_id=e.id
                                      join rmcp_product_category c on a.product_categroy_id=c.id
                                      join rmcp_product_measuring_point d on (a.measuring_code=d.measuring_code
            and b.id=d.product_id)
        <where>
            a.status=1

            <if test="ruleName != null and ruleName != ''">
                and rule_name  like concat('%',#{ruleName} ,'%')
            </if>

            <if test="alarmTypeId != null">
                and alarm_type_id = #{alarmTypeId}
            </if>


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

    <!-- 新增所有列 -->
    <insert id="insert" keyProperty="id" useGeneratedKeys="true">
        insert into rmcp_device_alarm_rule(id, product_categroy_id, tenant_id, device_type, rule_name, measuring_code,
                                           expression, alarm_type_id, status, create_date, create_by, update_date,
                                           update_by, rule_desc, value1, value2, measuring_data_type, specification)
        values (#{id}, #{productCategroyId}, #{tenantId}, #{deviceType}, #{ruleName}, #{measuringCode}, #{expression},
                #{alarmTypeId}, #{status}, #{createDate}, #{createBy}, #{updateDate}, #{updateBy}, #{ruleDesc},
                #{value1}, #{value2}, #{measuringDataType}, #{specification})
    </insert>

    <!-- 批量新增 -->
    <insert id="batchInsert">
        insert into rmcp_device_alarm_rule(id, product_categroy_id, tenant_id, device_type, rule_name, measuring_code,
        expression, alarm_type_id, status, create_date, create_by, update_date, update_by, rule_desc, value1, value2,
        measuring_data_type, specification)
        values
        <foreach collection="deviceAlarmRules" item="item" index="index" separator=",">
            (
            #{item.id}, #{item.productCategroyId}, #{item.tenantId}, #{item.deviceType}, #{item.ruleName},
            #{item.measuringCode}, #{item.expression}, #{item.alarmTypeId}, #{item.status}, #{item.createDate},
            #{item.createBy}, #{item.updateDate}, #{item.updateBy}, #{item.ruleDesc}, #{item.value1}, #{item.value2},
            #{item.measuringDataType}, #{item.specification} )
        </foreach>
    </insert>

    <!-- 通过主键修改数据 -->
    <update id="update">
        update rmcp_device_alarm_rule
        <set>
            <if test="productCategroyId != null">
                product_categroy_id = #{productCategroyId},
            </if>
            <if test="tenantId != null">
                tenant_id = #{tenantId},
            </if>
            <if test="deviceType != null">
                device_type = #{deviceType},
            </if>
            <if test="ruleName != null and ruleName != ''">
                rule_name = #{ruleName},
            </if>
            <if test="measuringCode != null and measuringCode != ''">
                measuring_code = #{measuringCode},
            </if>
            <if test="expression != null and expression != ''">
                expression = #{expression},
            </if>
            <if test="alarmTypeId != null">
                alarm_type_id = #{alarmTypeId},
            </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="ruleDesc != null and ruleDesc != ''">
                rule_desc = #{ruleDesc},
            </if>
            <if test="value1 != null and value1 != ''">
                value1 = #{value1},
            </if>
            <if test="value2 != null and value2 != ''">
                value2 = #{value2},
            </if>
            <if test="measuringDataType != null">
                measuring_data_type = #{measuringDataType},
            </if>
            <if test="specification != null and specification != ''">
                specification = #{specification},
            </if>
        </set>
        where id = #{id}
    </update>

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

    <!-- 总数 -->
    <select id="count" resultType="int">
        select count(*)
        from rmcp_device_alarm_rule
    </select>
    <select id="getRuleMeasuringList" resultType="com.zcxk.rmcp.api.dto.alarm.RuleMeasuringDto">
         SELECT
            id measuring_id,
            stmd.measuring_name as measuring_point_name,
            stmd.measuring_code as code_name ,
            stmd.measuring_unit as data_unit,
            stmd.measuring_data_type
        FROM rmcp_product_measuring_point stmd
        WHERE stmd.product_id = #{alarmTypeId} and stmd.status = 1
    </select>
    <select id="getMeasuringOptionList" resultType="com.zcxk.rmcp.api.dto.alarm.ConfigDataDto">
        SELECT
            stmdd.data_name as `name`,
            stmdd.data_value as `value`
        from rmcp_product_measuring_dict stmdd
        where stmdd.point_id=#{measuringId}

    </select>
    <select id="findByNameUnique" resultType="java.lang.Integer">
        select count(1) from rmcp_device_alarm_rule
        where status = 1 and rule_name = #{ruleName} and alarm_type_id = #{alarmTypeId}
        <if test="id != null"> and id != #{id} </if>
    </select>
    <select id="countMeasuringCode" resultType="java.lang.Integer">
        select count(1) from rmcp_device_alarm_rule
        where status = 1 and alarm_type_id = #{alarmTypeId} and measuring_code = #{measuringCode}
        <if test="id != null"> and id != #{id} </if>
    </select>
    <select id="findById" resultType="com.zcxk.rmcp.core.entity.DeviceAlarmRule">
        select <include refid="Base_Column_List"/> from rmcp_device_alarm_rule a where status = 1 and id = #{id}

    </select>
    <select id="findTplMeasuringDataDef" resultType="com.zcxk.rmcp.core.entity.TplMeasuringDataDef">
        select stmdd.* from rmcp_product_measuring_point stmd
          left join rmcp_product_measuring_dict stmdd on (stmdd.point_id = stmd.id
           and stmdd.data_value = #{value} and stmdd.status = 1)
        where stmd.status = 1 and stmd.product_id = #{deviceType}
          and stmd.measuring_code = #{measuringCode}

    </select>

</mapper>
