<?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.hw.ems.base.mapper.BaseTypeRelationMapper">

    <resultMap type="BaseTypeRelation" id="BaseTypeRelationResult">
        <result property="objid"    column="objid"    />
        <result property="monitorId"    column="monitor_id"    />
        <result property="type"    column="type"    />
        <result property="relationId"    column="relation_id"    />
        <result property="remark"    column="remark"    />
        <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="monitorType"    column="monitor_type"    />
        <result property="monitorName"    column="monitor_name"    />
        <result property="unitName"    column="unit_name"    />
        <result property="rate"    column="rate"    />
        <result property="formulaMode"    column="formula_mode"    />
    </resultMap>

    <sql id="selectBaseTypeRelationVo">
        select base_type_relation.objid,
               base_type_relation.monitor_id,
               type,
               relation_id,
               base_type_relation.remark,
               base_type_relation.create_by,
               base_type_relation.create_time,
               base_type_relation.update_by,
               base_type_relation.update_time,
               m.monitor_type,
               m.monitor_name,
               case
                   when base_type_relation.type = 0 then bi.build_name
                   end unit_name,
               base_type_relation.rate, base_type_relation.formula_mode
        from base_type_relation
                 left join base_monitor_info m on m.monitor_id = base_type_relation.monitor_id
                 left join base_build_info bi on base_type_relation.relation_id = bi.objid
   </sql>

    <select id="selectBaseTypeRelationList" parameterType="BaseTypeRelation" resultMap="BaseTypeRelationResult">
        <include refid="selectBaseTypeRelationVo"/>
        <where>
            m.monitor_id != 'S01_00001' and  m.monitor_id != 'S02_00001'
            <if test="monitorId != null  and monitorId != ''"> and base_type_relation.monitor_id = #{monitorId}</if>
            <if test="(type != null  and type != '') or type == 0 "> and base_type_relation.type = #{type}</if>
            <if test="relationId != null  and relationId != '' "> and base_type_relation.relation_id = #{relationId}</if>
            <if test="monitorType != null and monitorType != ''"> and m.monitor_type = #{monitorType}</if>
        </where>
    </select>

    <select id="selectBaseTypeRelationByObjid" parameterType="Long" resultMap="BaseTypeRelationResult">
        <include refid="selectBaseTypeRelationVo"/>
        where base_type_relation.objid = #{objid}
    </select>
    <select id="selectFirstOrderTypeRelationList" parameterType="BaseTypeRelation" resultMap="BaseTypeRelationResult">
        select btr.* from (
                              select btr.objid,
                                     btr.monitor_id,
                                     type,
                                     relation_id,
                                     btr.remark,
                                     btr.create_by,
                                     btr.create_time,
                                     btr.update_by,
                                     btr.update_time,
                                     m.monitor_type,
                                     m.monitor_name,
                                     case
                                         when btr.type = 0 then bi.build_name
                                         when btr.type = 1 then bt.business_name
                                         when btr.type = 2 then st.subentry_name end unit_name,
                                     btr.rate,
                                     btr.formula_mode
                              from base_type_relation btr
                                       left join base_monitor_info m on m.monitor_id = btr.monitor_id
                                       left join base_build_info bi on btr.relation_id = bi.objid and bi.parent_id = 1
                        ) btr
        <where>
            btr.unit_name is not null
            <if test="monitorId != null  and monitorId != ''"> and btr.monitor_id = #{monitorId}</if>
            <if test="(type != null  and type != '') or type == 0 "> and btr.type = #{type}</if>
            <if test="relationId != null  and relationId != '' "> and btr.relation_id = #{relationId}</if>
            <if test="monitorType != null and monitorType != ''"> and btr.monitor_type = #{monitorType}</if>
        </where>
    </select>

    <insert id="insertBaseTypeRelation" parameterType="BaseTypeRelation" useGeneratedKeys="true" keyProperty="objid">
        insert into base_type_relation
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="monitorId != null">monitor_id,</if>
            <if test="type != null">type,</if>
            <if test="relationId != null">relation_id,</if>
            <if test="remark != null">remark,</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="rate != null">rate,</if>
            <if test="formulaMode != null">formula_mode,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="monitorId != null">#{monitorId},</if>
            <if test="type != null">#{type},</if>
            <if test="relationId != null">#{relationId},</if>
            <if test="remark != null">#{remark},</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="rate != null">#{rate},</if>
            <if test="formulaMode != null">#{formulaMode},</if>
         </trim>
    </insert>

    <update id="updateBaseTypeRelation" parameterType="BaseTypeRelation">
        update base_type_relation
        <trim prefix="SET" suffixOverrides=",">
            <if test="monitorId != null">monitor_id = #{monitorId},</if>
            <if test="type != null">type = #{type},</if>
            <if test="relationId != null">relation_id = #{relationId},</if>
            <if test="remark != null">remark = #{remark},</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="rate != null">rate = #{rate},</if>
            <if test="formulaMode != null">formula_mode = #{formulaMode},</if>
        </trim>
        where objid = #{objid}
    </update>

    <delete id="deleteBaseTypeRelationByObjid" parameterType="Long">
        delete from base_type_relation where objid = #{objid}
    </delete>

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

</mapper>
