<?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.njcn.process.mapper.ThsWarnStrategyMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.njcn.process.pojo.po.ThsWarnStrategy">
        <id column="Id" property="id"/>
        <result column="Create_Time" property="createTime"/>
        <result column="Update_TIme" property="updateTime"/>
        <result column="Name" property="name"/>
        <result column="Grade" property="grade"/>
        <result column="Operation" property="operation"/>
        <result column="type" property="type"/>
        <result column="State" property="state"/>
        <result column="Create_By" property="createBy"/>
        <result column="Update_By" property="updateBy"/>
    </resultMap>

    <resultMap id="StrategyResultMap" type="com.njcn.process.pojo.param.StrategyParam$StrategyUpdateParam">
        <id column="Id" property="id"/>
        <result column="Name" property="name"/>
        <result column="Grade" property="grade"/>
        <result column="Operation" property="operation"/>
        <collection
                property="monitorTag"
                column="{id = id}"
                select="com.njcn.process.mapper.ThsWarnStrategyMapper.getMonitorTagDetail">
        </collection>
        <collection
                property="indicatorTypes"
                column="{id = id}"
                select="com.njcn.process.mapper.ThsWarnStrategyMapper.getStrategyDetail">
        </collection>
    </resultMap>
    <select id="selectUpdateStrategy" resultMap="StrategyResultMap">
        SELECT
        ts.Id AS id,
        ts.NAME AS NAME,
        ts.Grade AS grade,
        ts.Operation AS operation
        FROM
        ths_warn_strategy ts
        WHERE
        id = #{id}
    </select>
    <select id="getStrategyDetail" resultType="java.lang.String">
        SELECT
        Ass_Id
        FROM
        ths_warn_strategy_ass ts
        WHERE
        Warn_Id = #{id} and ts.Type=1
    </select>
    <!--查询策略的集合-->
    <select id="selectStrategyList" parameterType="com.njcn.prepare.harmonic.pojo.param.SuperviseParam"
            resultType="com.njcn.harmonic.pojo.vo.ThsStrategyVo">
        select
        tws.id,
        tws.name,
        tws.grade,
        tws.operation,
        tws.type,
        Monitor_Id as monitorId,
        tda.dept_Id as deptId
        from
        ths_warn_strategy tws
        INNER join ths_dept_alarm tda on
        tws.Id = tda.Alarm_Id
        <where>
            <choose>
                <when test="param.initType==0">
                    tws.State = 1 and tws.Grade in(0,2)
                </when>
                <when test="param.initType==1">
                    tws.State = 1 and tws.Grade in(1,2)
                </when>
            </choose>
            <if test="param.deptId !=null and param.deptId !=''">
                and tda.Dept_Id=#{param.deptId}
            </if>
            <if test="param.type !=null">
                and tda.type=#{param.type}
            </if>
            and tws.Big_Type=1
        </where>
    </select>

    <select id="getMonitorTagDetail" resultType="java.lang.String">
        SELECT
        Ass_Id
        FROM
        ths_warn_strategy_ass ts
        WHERE
        Warn_Id = #{id} and ts.Type=0
    </select>

</mapper>
