<?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.ruoyi.powerdistribution.mapper.PdmExamineCountyMapper">
    
    <resultMap type="PdmExamineCounty" id="PdmExamineCountyResult">
        <result property="id"    column="id"    />
        <result property="roleNm"    column="role_nm"    />
        <result property="roleCd"    column="role_cd"    />
        <result property="requireCd"    column="require_cd"    />
        <result property="compteHourNumX"    column="compte_hour_num_x"    />
        <result property="amount"    column="amount"    />
    </resultMap>

    <sql id="selectPdmExamineCountyVo">
        select id, role_nm, role_cd, require_cd, compte_hour_num_x, amount from pdm_examine_county
    </sql>

    <select id="selectPdmExamineCountyList" parameterType="PdmExamineCounty" resultMap="PdmExamineCountyResult">
        <include refid="selectPdmExamineCountyVo"/>
        <where>  
            <if test="roleNm != null  and roleNm != ''"> and role_nm = #{roleNm}</if>
            <if test="roleCd != null  and roleCd != ''"> and role_cd = #{roleCd}</if>
            <if test="requireCd != null  and requireCd != ''"> and require_cd = #{requireCd}</if>
            <if test="compteHourNumX != null  and compteHourNumX != ''"> and compte_hour_num_x = #{compteHourNumX}</if>
            <if test="amount != null "> and amount = #{amount}</if>
        </where>
    </select>
    <select id="selectExamineCountyByRequireCode" resultMap="PdmExamineCountyResult">
        select c.id,r.role_cd,r.role_name as role_nm,c.amount, #{requireCd} as require_cd,#{compteHourNumX} as compte_hour_num_x from pdm_examine_role r
        LEFT JOIN pdm_examine_county c on r.role_cd=c.role_cd and c.require_cd=#{requireCd}
        where exists(select 1 from pdm_examine_require t where t.require_type='X' and t.require_cd=#{requireCd})
        order by  #{requireCd},r.role_cd
    </select>
    <select id="selectPdmExamineCountyById" parameterType="Long" resultMap="PdmExamineCountyResult">
        <include refid="selectPdmExamineCountyVo"/>
        where id = #{id}
    </select>
        
    <insert id="insertPdmExamineCounty" parameterType="PdmExamineCounty" useGeneratedKeys="true" keyProperty="id">
        insert into pdm_examine_county
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="roleNm != null">role_nm,</if>
            <if test="roleCd != null">role_cd,</if>
            <if test="requireCd != null">require_cd,</if>
            <if test="compteHourNumX != null">compte_hour_num_x,</if>
            amount,
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="roleNm != null">#{roleNm},</if>
            <if test="roleCd != null">#{roleCd},</if>
            <if test="requireCd != null">#{requireCd},</if>
            <if test="compteHourNumX != null">#{compteHourNumX},</if>
            #{amount},
         </trim>
    </insert>

    <update id="updatePdmExamineCounty" parameterType="PdmExamineCounty">
        update pdm_examine_county
        <trim prefix="SET" suffixOverrides=",">
            <if test="roleNm != null">role_nm = #{roleNm},</if>
            <if test="roleCd != null">role_cd = #{roleCd},</if>
            <if test="requireCd != null">require_cd = #{requireCd},</if>
            <if test="compteHourNumX != null">compte_hour_num_x = #{compteHourNumX},</if>
            amount = #{amount},
        </trim>
        where id = #{id}
    </update>

    <delete id="deletePdmExamineCountyById" parameterType="Long">
        delete from pdm_examine_county where id = #{id}
    </delete>

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