<?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.frequency.bankcard.mapper.BankCardMapper">

    <resultMap type="com.ruoyi.frequency.bankcard.entity.BankCard" id="BankCardResult">
        <result property="id"    column="id"    />
        <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="delFlag"    column="del_flag"    />
        <result property="userType"    column="user_type"    />
        <result property="userId"    column="user_id"    />
        <result property="bank"    column="bank"    />
        <result property="openingBranch"    column="opening_branch"    />
        <result property="fullName"    column="full_name"    />
        <result property="bankCard"    column="bank_card"    />
        <result property="isDefault"    column="is_default"    />
        <association property="userInfo"    column="user_id" javaType="com.ruoyi.vo.UserInfo" resultMap="userInfo" />
    </resultMap>

    <resultMap id="userInfo" type="com.ruoyi.vo.UserInfo">
        <id     property="userId"    column="user_id"     />
        <result property="userType"  column="user_type"   />
        <result property="idNo"  column="id_no"   />
        <result property="nickname"  column="nickname"   />
        <result property="avatar" column="avatar"   />
        <result property="mobile" column="mobile"   />
    </resultMap>


    <sql id="selectBankCardVo">
        select
            t.id ,
            t.create_by ,
            t.create_time ,
            t.update_by ,
            t.update_time ,
            t.del_flag ,
            t.user_type ,
            t.user_id ,
            t.bank ,
            t.opening_branch ,
            t.full_name ,
            t.bank_card ,
            t.is_default
        from t_bank_card AS t
    </sql>

    <select id="selectBankCardList" parameterType="com.ruoyi.frequency.bankcard.entity.BankCard" resultMap="BankCardResult">
        <include refid="selectBankCardVo"/>
        <where> AND t.del_flag != 2
            <if test="userType != null "> and t.user_type = #{userType}</if>
            <if test="userId != null "> and t.user_id = #{userId}</if>
            <if test="bank != null  and bank != ''"> and t.bank = #{bank}</if>
            <if test="openingBranch != null  and openingBranch != ''"> and t.opening_branch = #{openingBranch}</if>
            <if test="fullName != null  and fullName != ''"> and t.full_name like concat('%', #{fullName}, '%')</if>
            <if test="bankCard != null  and bankCard != ''"> and t.bank_card = #{bankCard}</if>
            <if test="isDefault != null "> and t.is_default = #{isDefault}</if>
            <if test="delFlag != null and delFlag != ''"> and t.del_flag = #{delFlag}</if>
        </where>
        order by t.create_time desc
    </select>

    <select id="selectBankCardById" parameterType="Long" resultMap="BankCardResult">
        <include refid="selectBankCardVo"/>
        where t.id = #{id}
    </select>

    <insert id="insertBankCard" parameterType="com.ruoyi.frequency.bankcard.entity.BankCard">
        insert into t_bank_card
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="id != null">id,</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="delFlag != null">del_flag,</if>
            <if test="userType != null">user_type,</if>
            <if test="userId != null">user_id,</if>
            <if test="bank != null">bank,</if>
            <if test="openingBranch != null">opening_branch,</if>
            <if test="fullName != null">full_name,</if>
            <if test="bankCard != null">bank_card,</if>
            <if test="isDefault != null">is_default,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="id != null">#{id},</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="delFlag != null">#{delFlag},</if>
            <if test="userType != null">#{userType},</if>
            <if test="userId != null">#{userId},</if>
            <if test="bank != null">#{bank},</if>
            <if test="openingBranch != null">#{openingBranch},</if>
            <if test="fullName != null">#{fullName},</if>
            <if test="bankCard != null">#{bankCard},</if>
            <if test="isDefault != null">#{isDefault},</if>
        </trim>
    </insert>

    <update id="updateBankCard" parameterType="com.ruoyi.frequency.bankcard.entity.BankCard">
        update t_bank_card
        <trim prefix="SET" suffixOverrides=",">
            <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="delFlag != null">del_flag = #{delFlag},</if>
            <if test="userType != null">user_type = #{userType},</if>
            <if test="userId != null">user_id = #{userId},</if>
            <if test="bank != null">bank = #{bank},</if>
            <if test="openingBranch != null">opening_branch = #{openingBranch},</if>
            <if test="fullName != null">full_name = #{fullName},</if>
            <if test="bankCard != null">bank_card = #{bankCard},</if>
            <if test="isDefault != null">is_default = #{isDefault},</if>
        </trim>
        where id = #{id}
    </update>

    <select id="bankCardPage" resultType="com.ruoyi.controller.resp.BankCardPageResp">
        select
            t.id ,
            t.create_by ,
            t.create_time ,
            t.update_by ,
            t.update_time ,
            t.del_flag ,
            t.user_type ,
            t.user_id ,
            t.bank ,
            t.opening_branch ,
            t.full_name ,
            t.bank_card ,
            t.is_default
        from t_bank_card AS t
        where t.del_flag = 1
          AND t.user_type = #{userVo.userType}
          AND t.user_id = #{userVo.userId}
        order by t.create_time desc
    </select>

    <select id="bankCardDetail" resultType="com.ruoyi.controller.resp.BankCardDetailResp">
        select
            t.id ,
            t.create_by ,
            t.create_time ,
            t.update_by ,
            t.update_time ,
            t.del_flag ,
            t.user_type ,
            t.user_id ,
            t.bank ,
            t.opening_branch ,
            t.full_name ,
            t.bank_card ,
            t.is_default
        from t_bank_card AS t
        where t.id = #{bankCardId}
    </select>
</mapper>