<?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.crm.ucard.dao.mapper.CardCountryMapper">

    <select id="getCardCountryList" resultType="com.crm.rely.backend.core.dto.ucard.CardCountryDto"
            parameterType="com.crm.rely.backend.core.entity.ucard.CardCountryEntity">
        select uc.`code`,uc.`standard_code`,uc.`en_name`,uc.`pcode`,uc.`area_code`,
               uc.`card_category`,uc.`add_time`,uc.`add_user`,uc.`add_ip`,uc.`note`,
        COALESCE(sc.name, uc.en_name) as cn_name
        from ucard_country uc
        left join sys_country sc
        on uc.`code` = sc.`code`
        <where>
            <if test="cardCategory != null and cardCategory > 0">
                and uc.`card_category` = #{cardCategory}
            </if>
            <choose>
                <when test="code!=null and code!=''">
                    and uc.`pcode` = #{code}
                </when>
                <otherwise>
                    and uc.`pcode` is null
                </otherwise>
            </choose>
        </where>
    </select>

    <insert id="batchSave" parameterType="java.util.List">
        insert ignore into ucard_country(`code`,`standard_code`,`cn_name`,`en_name`,`pcode`,`area_code`,`card_category`,`add_time`,`add_user`,`add_ip`,`note`)
        values
        <foreach collection="list" item="item" index="index" separator=",">
            (#{item.code},#{item.standardCode},#{item.cnName},#{item.enName},#{item.pcode},#{item.areaCode},#{item.cardCategory},#{item.addTime},#{item.addUser},#{item.addIp},#{item.note})
        </foreach>
    </insert>

    <delete id="deleteByCardCategory">
        delete
        from ucard_country
        where `card_category` = #{cardCategory}
    </delete>
    
</mapper> 