<?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.project.business.mapper.TbCountryAreaMapper">
    
    <resultMap type="TbCountryArea" id="TbCountryAreaResult">
        <result property="countryId"    column="country_id"    />
        <result property="areaId"    column="area_id"    />
        <result property="delFlag"    column="del_flag"    />
        <result property="createTime"    column="create_time"    />
        <result property="createBy"    column="create_by"    />
        <result property="updateTime"    column="update_time"    />
        <result property="updateBy"    column="update_by"    />
        <result property="remark"    column="remark"    />
    </resultMap>

    <sql id="selectTbCountryAreaVo">
        select country_id, area_id, del_flag, create_time, create_by, update_time, update_by, remark from tb_country_area
    </sql>

    <select id="selectTbCountryAreaList" parameterType="TbCountryArea" resultMap="TbCountryAreaResult">
        <include refid="selectTbCountryAreaVo"/>
        <where>  
            <if test="areaId != null  and areaId != ''"> and area_id = #{areaId}</if>
        </where>
    </select>
    
    <select id="selectTbCountryAreaByCountryId" parameterType="Long" resultMap="TbCountryAreaResult">
        <include refid="selectTbCountryAreaVo"/>
        where country_id = #{countryId}
    </select>
        
    <insert id="insertTbCountryArea" parameterType="TbCountryArea" useGeneratedKeys="true" keyProperty="countryId">
        insert into tb_country_area
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="areaId != null">area_id,</if>
            <if test="delFlag != null">del_flag,</if>
            <if test="createTime != null">create_time,</if>
            <if test="createBy != null">create_by,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="updateBy != null">update_by,</if>
            <if test="remark != null">remark,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="areaId != null">#{areaId},</if>
            <if test="delFlag != null">#{delFlag},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="updateBy != null">#{updateBy},</if>
            <if test="remark != null">#{remark},</if>
         </trim>
    </insert>

    <insert id="bindCountryArea">
        insert into tb_country_area(country_id, area_id, create_time)
        values (#{countryId}, #{areaId}, now())
    </insert>

    <update id="updateTbCountryArea" parameterType="TbCountryArea">
        update tb_country_area
        <trim prefix="SET" suffixOverrides=",">
            <if test="areaId != null">area_id = #{areaId},</if>
            <if test="delFlag != null">del_flag = #{delFlag},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="createBy != null">create_by = #{createBy},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="remark != null">remark = #{remark},</if>
        </trim>
        where country_id = #{countryId}
    </update>

    <update id="updateTbCountryAreaById">
        update tb_country_area set country_id = #{countryId}, update_time = now()
        where area_id = #{areaId}
    </update>

    <delete id="deleteTbCountryAreaByCountryId" parameterType="Long">
        delete from tb_country_area where country_id = #{countryId}
    </delete>

    <delete id="deleteTbCountryAreaByCountryIds" parameterType="String">
        delete from tb_country_area where country_id in 
        <foreach item="countryId" collection="array" open="(" separator="," close=")">
            #{countryId}
        </foreach>
    </delete>

    <delete id="deleteTbCountryAreaByAreaIds" parameterType="String">
        delete from tb_country_area where area_id in
        <foreach item="areaId" collection="areaIds" open="(" separator="," close=")">
            #{areaId}
        </foreach>
    </delete>

    <delete id="deleteTbCountryAreaByAreaId" parameterType="Long">
        delete from tb_country_area where area_id = #{areaId}
    </delete>

</mapper>