<?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.gyee.wisdom.alarm.sharding.mapper.Alertrule2Mapper">
    <resultMap id="BaseResultMap" type="com.gyee.wisdom.alarm.sharding.entity.Alertrule2">
        <!--
          WARNING - @mbg.generated
          This element is automatically generated by MyBatis Generator, do not modify.
        -->
        <id column="id" property="id" />
        <result column="name"  property="name" />
        <result column="description"  property="description" />
        <result column="expression"  property="expression" />
        <result column="rank"  property="rank" />
        <result column="rank"  property="rank" />
        <result column="enabled"  property="enabled" />
        <result column="modelId"  property="modelId" />
        <result column="ednaValue"  property="ednaValue" />
        <result column="category"  property="category" />
        <result column="range"  property="range" />
        <result column="station"  property="station" />
        <result column="windturbine"  property="windturbine" />
        <result column="line"  property="line" />
        <result column="project"  property="project" />
        <result column="electrical"  property="electrical" />
        <result column="taskstart"  property="taskstart" />
        <result column="relatedParts"  property="relatedParts" />
    </resultMap>
    <resultMap type="Alertrule2" id="Alertrule2Result">
        <result property="id"    column="id"    />
        <result property="name"    column="name"    />
        <result property="station"    column="station"    />
        <result property="relatedParts"    column="relatedParts"    />
        <association  property="windPowerStation"  column="station"     javaType="WindPowerStation"  select="com.gyee.wisdom.alarm.sharding.mapper.WindPowerStationMapper.selectByid"/>
        <association  property="datadictionary"  column="relatedParts"     javaType="Datadictionary"  select="com.gyee.wisdom.alarm.sharding.mapper.DatadictionaryMapper.selectBycode"/>
    </resultMap>
    <select id="pageQueryAll" parameterType="java.util.Map" resultMap="Alertrule2Result">
        select a.* from  ALERTRULE2 a
        left join windpowerstation w on w.id = a.station
        <where>
            1=1

            <if test="name !=null and name !=''">
                and a.name like  '%${name}%'
            </if>
            <if test="station !=null and station !=''">
                and a.station like  '%${station}%'
            </if>
            <if test="modelId !=null and modelId !=''">
                and a.modelid like  '%${modelId}%'
            </if>
            <if test="rank !=null and rank !=''">
                and a.rank = #{rank}
            </if>
            <if test="category !=null and category !=''">
                and a.category = #{category}
            </if>
<!--            and a.range = 0-->
        </where>
        order by a.createtime desc
    </select>

    <select id="getMaxEdnaValue" parameterType="java.util.Map" resultType="java.lang.Integer">
        select max(a.ednavalue) from  ALERTRULE2 a
    </select>
    <select id="getAllByStationIdAndModelId" parameterType="java.util.Map" resultType="com.gyee.wisdom.alarm.sharding.entity.Alertrule2">
        select * from  ALERTRULE2 a
        <where>
            1=1
            <if test="idString !=null and idString !=''">
                and a.id like  '%${idString}%'
            </if>
        </where>
    </select>
    <select id="getById" parameterType="java.util.Map" resultType="com.gyee.wisdom.alarm.sharding.entity.Alertrule2">
        select * from  ALERTRULE2 a
        <where>
            1=1
            <if test="idString !=null and idString !=''">
                and a.id = #{idString}
            </if>
        </where>
    </select>

    <select id="quertByrelatedParts"  resultType="java.util.HashMap">
        select a.name from  ALERTRULE2 a
        where a.relatedparts = #{relatedParts}
        group by a.name
    </select>

    <select id="queryTree" resultMap="Alertrule2Result" >
        select distinct a.relatedparts,a.name,d.name from alertrule2 a
        left join DATADICTIONARY d
        on d.code = a.relatedparts
        where
        relatedparts is not null
    </select>

    <select id="queryMap" resultType="com.gyee.wisdom.alarm.sharding.entity.Alertrule2">
        select t.relatedparts,d.name from
            (select a.relatedparts from alertrule2 a
             where relatedparts  is not null
             group by a.relatedparts) t
        left join DATADICTIONARY d
        on d.code = t.relatedparts
    </select>

    <update id="updateByAlertrule2Id" parameterType="com.gyee.wisdom.alarm.sharding.entity.Alertrule2">
        update alertrule2 a
        <set >
            <if test="name != null">a.name = #{name},</if>
            <if test="description != null">a.description = #{description},</if>
            <if test="expression != null">a.expression = #{expression},</if>
            <if test="tag != null">a.tag = #{tag},</if>
            <if test="rank != null">a.rank = #{rank},</if>
            <if test="enabled != null">a.enabled = #{enabled},</if>
            <if test="modelId != null">a.modelId = #{modelId},</if>
            <if test="ednaValue != null and ednaValue!=0">a.ednaValue = #{ednaValue},</if>
            <if test="category != null">a.category = #{category},</if>
            <if test="range != null">a.range = #{range},</if>
            <if test="station != null">a.station = #{station},</if>
            <if test="windturbine != null">a.windturbine = #{windturbine},</if>
            <if test="line != null">a.line = #{line},</if>
            <if test="project != null">a.project = #{project},</if>
            <if test="electrical != null">a.electrical = #{electrical},</if>
            <if test="taskstart != null">a.taskstart = #{taskstart},</if>
            <if test="relatedParts != null">a.relatedParts = #{relatedParts},</if>
            <if test="createtime != null">a.createtime = #{createtime}</if>
        </set>
        where a.id = #{id}
    </update>

    <insert id="insertAlerture2" parameterType="com.gyee.wisdom.alarm.sharding.entity.Alertrule2" useGeneratedKeys="true" keyProperty="id">
        insert into alertrule2
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="id != null">id,</if>
            <if test="name != null">Name,</if>
            <if test="description != null">description,</if>
            <if test="expression != null">expression,</if>
            <if test="tag != null">tag,</if>
            <if test="rank != null">rank,</if>
            <if test="enabled != null">enabled,</if>
            <if test="modelId != null">modelId,</if>
            <if test="ednaValue != null">ednaValue,</if>
            <if test="category != null">category,</if>
            <if test="range != null">range,</if>
            <if test="station != null">station,</if>
            <if test="windturbine != null">windturbine,</if>
            <if test="line != null">line,</if>
            <if test="project != null">project,</if>
            <if test="electrical != null">electrical,</if>
            <if test="taskstart != null">taskstart,</if>
            <if test="relatedParts != null">relatedParts,</if>
            <if test="createtime != null">createtime</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="id != null">#{id},</if>
            <if test="name != null">#{name},</if>
            <if test="description != null">#{description},</if>
            <if test="expression != null">#{expression},</if>
            <if test="tag != null">#{tag},</if>
            <if test="rank != null">#{rank},</if>
            <if test="enabled != null">#{enabled},</if>
            <if test="modelId != null">#{modelId},</if>
            <if test="ednaValue != null">#{ednaValue},</if>
            <if test="category != null">#{category},</if>
            <if test="range != null">#{range},</if>
            <if test="station != null">#{station},</if>
            <if test="windturbine != null">#{windturbine},</if>
            <if test="line != null">#{line},</if>
            <if test="project != null">#{project},</if>
            <if test="electrical != null">#{electrical},</if>
            <if test="taskstart != null">#{taskstart},</if>
            <if test="relatedParts != null">#{relatedParts},</if>
            <if test="createtime != null">#{createtime}</if>
        </trim>
    </insert>

<!--    <insert id="insertAlerture2" parameterType="com.gyee.wisdom.alarm.sharding.entity.Alertrule2">-->
<!--        insert into alertrule2 a(a.id, a.Name, a.description, a.expression, a.tag, a.rank,-->
<!--                               a.enabled,a.modelId,a.ednaValue,a.category,a.range,a.station,a.windturbine,a.line,a.project,a.electrical,-->
<!--                               a.taskstart,a.relatedParts,a.createtime)-->
<!--        values (#{id}, #{Name}, #{description},#{expression},-->
<!--                #{tag}, #{rank}, #{enabled},#{modelId}, #{ednaValue}, #{category},#{range},#{station}, #{windturbine}, #{line},-->
<!--                #{project}, #{electrical}, #{taskstart},#{relatedParts},#{createtime})-->
<!--    </insert>-->

    <select id="selectByAlertrule2Id" resultType="com.gyee.wisdom.alarm.sharding.entity.Alertrule2">
        select * from  alertrule2 a where a.id=#{id}
    </select>

    <select id="selectByExample" parameterType="com.gyee.wisdom.alarm.sharding.model.Alertrule2Example" resultMap="BaseResultMap">
        <!--
          WARNING - @mbg.generated
          This element is automatically generated by MyBatis Generator, do not modify.
        -->
        select
        <if test="distinct">
            distinct
        </if>
        *
        <!--        <include refid="Base_Column_List" />-->
        from ALERTRULE2
        <if test="_parameter != null">
            <include refid="Example_Where_Clause" />
        </if>
        <if test="orderByClause != null">
            order by ${orderByClause}
        </if>
    </select>

    <sql id="Base_Column_List">
        <!--
          WARNING - @mbg.generated
          This element is automatically generated by MyBatis Generator, do not modify.
        -->
        id, name, description, expression, tag, rank, enabled, modelid, ednavalue, category,
        range, station, windturbine, line, project, electrical, taskstart, relatedparts
    </sql>

    <sql id="Example_Where_Clause">
        <!--
          WARNING - @mbg.generated
          This element is automatically generated by MyBatis Generator, do not modify.
        -->
        <where>
            <foreach collection="oredCriteria" item="criteria" separator="or">
                <if test="criteria.valid">
                    <trim prefix="(" prefixOverrides="and" suffix=")">
                        <foreach collection="criteria.criteria" item="criterion">
                            <choose>
                                <when test="criterion.noValue">
                                    and ${criterion.condition}
                                </when>
                                <when test="criterion.singleValue">
                                    and ${criterion.condition} #{criterion.value}
                                </when>
                                <when test="criterion.betweenValue">
                                    and ${criterion.condition} #{criterion.value} and #{criterion.secondValue}
                                </when>
                                <when test="criterion.listValue">
                                    and ${criterion.condition}
                                    <foreach close=")" collection="criterion.value" item="listItem" open="(" separator=",">
                                        #{listItem}
                                    </foreach>
                                </when>
                            </choose>
                        </foreach>
                    </trim>
                </if>
            </foreach>
        </where>
    </sql>
</mapper>
