<?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.yonge.cooleshow.biz.dal.dao.UserBankCardDao">
	<resultMap id="BaseResultMap" type="com.yonge.cooleshow.biz.dal.entity.UserBankCard">
            <result column="id_" property="id" />
	        <result column="user_id_" property="userId" />
	        <result column="name_" property="name" />
	        <result column="bank_name_" property="bankName" />
	        <result column="bank_card_" property="bankCard" />
	        <result column="phone_" property="phone" />
	        <result column="bank_code_" property="bankCode" />
	        <result column="default_flag_" property="defaultFlag" />
	        <result column="create_time_" property="createTime" />
	        <result column="update_time_" property="updateTime" />
		</resultMap>

    <!-- 表字段 -->
    <sql id="baseColumns">
         t.id_ as id
        , t.user_id_ as userId
        , t.name_ as `name`
        , t.bank_name_ as bankName
        , t.bank_card_ as bankCard
        , t.phone_ as phone
        , t.bank_code_ as bankCode
        , t.default_flag_ as defaultFlag
        , t.create_time_ as createTime
        , t.update_time_ as updateTime
        , t.del_flag_ as delFlag
        </sql>

    <select id="detail" resultType="com.yonge.cooleshow.biz.dal.vo.UserBankCardVo">
        SELECT
            <include refid="baseColumns"/>
        FROM user_bank_card t
        where t.del_flag_ = 0 and t.id_ = #{id}
    </select>
    
    <select id="selectPage" resultType="com.yonge.cooleshow.biz.dal.vo.UserBankCardVo">
		SELECT         
        	<include refid="baseColumns" />
		FROM user_bank_card t
		where t.del_flag_ = 0
        <if test="param.userId !=null">
            AND t.user_id_ = #{param.userId}
        </if>
	</select>

    <select id="getDefaultBankByUserId" resultType="com.yonge.cooleshow.biz.dal.vo.UserBankCardVo">
        SELECT
            <include refid="baseColumns"/>
        FROM user_bank_card t
        where t.del_flag_ = 0 and  t.user_id_ = #{userId}
        order by default_flag_ desc,create_time_ desc
        limit 1
    </select>

    <select id="getBankByUserIdAndCardId" resultType="com.yonge.cooleshow.biz.dal.vo.UserBankCardVo">
        SELECT
            <include refid="baseColumns"/>
        FROM user_bank_card t
        where t.del_flag_ = 0
        and  t.user_id_ = #{userId}
        and t.id_ = #{bankCardId}
    </select>

    <update id="deleteById">
        update user_bank_card set del_flag_ = 1 where id_ = #{id} and del_flag_ = 0
    </update>

    <update id="deleteByUserId">
        update user_bank_card set del_flag_ = 1 where user_id_ = #{userId} and del_flag_ = 0
    </update>
</mapper>