<?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.TbCountryMapper">

    <resultMap type="TbCountry" id="TbCountryResult">
        <result property="countryId" column="country_id"/>
        <result property="countryName" column="country_name"/>
        <result property="countryNameUS" column="country_nameUS"/>
        <result property="countryNameRU" column="country_nameRU"/>
        <result property="countryNameES" column="country_nameES"/>
        <result property="discount" column="discount"/>
        <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="selectTbCountryVo">
        select country_id,
               country_name,
               country_nameUS,
               country_nameRU,
               country_nameES,
               discount,
               del_flag,
               create_time,
               create_by,
               update_time,
               update_by,
               remark
        from tb_country
    </sql>

    <select id="selectTbCountryList" parameterType="TbCountry" resultMap="TbCountryResult">
        <include refid="selectTbCountryVo"/>
        <where>
            <if test="countryName != null and countryName != ''">
                and country_name like concat('%', #{countryName}, '%') escape '/'
            </if>
            <if test="countryNameUS != null and countryNameUS != ''">
                and country_nameUS like concat('%', #{countryNameUS}, '%') escape '/'
            </if>
            <if test="countryNameRU != null and countryNameRU != ''">
                and country_nameRU like concat('%', #{countryNameRU}, '%') escape '/'
            </if>
            <if test="countryNameES != null and countryNameES != ''">
                and country_nameES like concat('%',#{countryNameES},'%') escape '/'
            </if>
            <if test="discount != null ">and discount = #{discount}</if>
            <if test="beginTime != null and beginTime != ''"><!-- 开始时间检索 -->
                and date_format(create_time,'%y%m%d') &gt;= date_format(#{beginTime},'%y%m%d')
            </if>
            <if test="endTime != null and endTime != ''"><!-- 结束时间检索 -->
                and date_format(create_time,'%y%m%d') &lt;= date_format(#{endTime},'%y%m%d')
            </if>
            <if test="ids != null and ids.size > 0">
                and country_id in
                <foreach collection="ids" item="id" open="(" separator="," close=")">
                    #{id}
                </foreach>
            </if>
            and del_flag = '0'
        </where>
        order by country_id desc
    </select>

    <select id="selectTbCountryByCountryId" parameterType="Long" resultMap="TbCountryResult">
        <include refid="selectTbCountryVo"/>
        where country_id = #{countryId}
    </select>

    <select id="getAllNationList" resultMap="TbCountryResult">
        <include refid="selectTbCountryVo"/>
        where del_flag = '0'
    </select>
    <select id="selectCountryByAreaId" resultMap="TbCountryResult">
        select distinct c.country_id,
        c.country_name,
        c.country_nameUS,
        c.country_nameRU,
        c.country_nameES,
        c.discount,
        c.del_flag,
        c.create_time,
        c.create_by,
        c.update_time,
        c.update_by,
        c.remark
        from tb_country c
        join tb_country_area ca on c.country_id = ca.country_id
        where c.del_flag = '0' and ca.del_flag = '0'
        <if test="areaId != null and areaId != 0">
            and ca.area_id = #{areaId}
        </if>
    </select>

    <select id="selectTbCountryByCountryName" resultType="java.lang.Integer">
        select count(1) from tb_country where country_name = #{countryName} and del_flag = '0'
        <if test="countryId != null">
            and country_id != #{countryId}
        </if>
    </select>
    <select id="selectCountryByAreaIds" resultType="com.ruoyi.project.business.domain.TbCountry">
        select distinct c.country_id,
        c.country_name,
        c.country_nameUS,
        c.country_nameRU,
        c.country_nameES,
        c.discount,
        c.del_flag,
        c.create_time,
        c.create_by,
        c.update_time,
        c.update_by,
        c.remark
        from tb_country c
        join tb_country_area ca on c.country_id = ca.country_id
        where c.del_flag = '0' and ca.del_flag = '0'
        <foreach collection="array" item="areaId" open=" and ca.area_id in (" separator="," close=")">
            #{areaId}
        </foreach>
    </select>

    <insert id="insertTbCountry" parameterType="TbCountry" useGeneratedKeys="true" keyProperty="countryId">
        insert into tb_country
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="countryName != null">country_name,</if>
            <if test="countryNameUS != null">country_nameUS,</if>
            <if test="countryNameRU != null">country_nameRU,</if>
            <if test="countryNameES != null">country_nameES,</if>
            <if test="discount != null">discount,</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="countryName != null">#{countryName},</if>
            <if test="countryNameUS != null">#{countryNameUS},</if>
            <if test="countryNameRU != null">#{countryNameRU},</if>
            <if test="countryNameES != null">#{countryNameES},</if>
            <if test="discount != null">#{discount},</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>

    <update id="updateTbCountry" parameterType="TbCountry">
        update tb_country
        <trim prefix="SET" suffixOverrides=",">
            <if test="countryName != null">country_name = #{countryName},</if>
            <if test="countryNameUS != null">country_nameUS= #{countryNameUS},</if>
            <if test="countryNameRU != null">country_nameRU= #{countryNameRU},</if>
            <if test="countryNameES != null">country_nameES= #{countryNameES},</if>
            <if test="discount != null">discount = #{discount},</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="deleteTbCountryByCountryId" parameterType="Long">
        update tb_country
        set del_flag = '1'
        where country_id = #{countryId}
    </update>

    <update id="deleteTbCountryByCountryIds" parameterType="String">
        update tb_country set del_flag = '1' where country_id in
        <foreach item="countryId" collection="array" open="(" separator="," close=")">
            #{countryId}
        </foreach>
    </update>
</mapper>