<?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.jsowell.pile.mapper.PileAuthCardMapper">

    <resultMap type="com.jsowell.pile.domain.PileAuthCard" id="PileAuthCardResult">
        <result property="id"    column="id"    />
        <result property="logicCard"    column="logic_card"    />
        <result property="physicsCard"    column="physics_card"    />
        <result property="status"    column="status"    />
        <result property="secretKey"    column="secret_key"    />
        <result property="memberId"    column="member_id"    />
        <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="delFlag"    column="del_flag"    />
    </resultMap>

    <sql id="selectPileAuthCardVo">
        select id, logic_card, physics_card, status, secret_key, member_id, create_time, create_by, update_time, update_by, del_flag from pile_auth_card
    </sql>

    <sql id="Base_Column_List" >
        id, logic_card, physics_card, status, secret_key, member_id, create_time, create_by, update_time, update_by, del_flag
    </sql>

    <select id="selectPileAuthCardList" parameterType="com.jsowell.pile.domain.PileAuthCard" resultMap="PileAuthCardResult">
        <include refid="selectPileAuthCardVo"/>
        <where>
            <if test="logicCard != null  and logicCard != ''"> and logic_card = #{logicCard}</if>
            <if test="physicsCard != null  and physicsCard != ''"> and physics_card = #{physicsCard}</if>
            <if test="status != null  and status != ''"> and status = #{status}</if>
            <if test="secretKey != null  and secretKey != ''"> and secret_key = #{secretKey}</if>
            <if test="memberId != null  and memberId != ''"> and member_id = #{memberId}</if>
        </where>
    </select>

<!--    <select id="selectPileAuthCardById" parameterType="Long" resultMap="PileAuthCardResult">-->
<!--        <include refid="selectPileAuthCardVo"/>-->
<!--        where id = #{id}-->
<!--    </select>-->

    <select id="getCardInfoById" parameterType="Long" resultType="com.jsowell.pile.vo.web.PileAuthCardVO">
        select
            t1.id,
            t1.logic_card as logicCard,
            t1.physics_card as physicsCard,
            t1.status,
            t1.secret_key as secretKey,
            t1.create_time as createTime,
            t1.member_id as memberId,
            t2.mobile_number as phoneNumber,
            t2.nick_name as nickName
        from pile_auth_card t1
            left join member_basic_info t2 on t1.member_id = t2.member_id
        where t1.del_flag = '0'
        and t1.id = #{id}
    </select>


    <insert id="insertPileAuthCard" parameterType="com.jsowell.pile.domain.PileAuthCard" useGeneratedKeys="true" keyProperty="id">
        insert into pile_auth_card
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="logicCard != null">logic_card,</if>
            <if test="physicsCard != null">physics_card,</if>
            <if test="status != null">status,</if>
            <if test="secretKey != null">secret_key,</if>
            <if test="memberId != null">member_id,</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="delFlag != null">del_flag,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="logicCard != null">#{logicCard},</if>
            <if test="physicsCard != null">#{physicsCard},</if>
            <if test="status != null">#{status},</if>
            <if test="secretKey != null">#{secretKey},</if>
            <if test="memberId != null">#{memberId},</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="delFlag != null">#{delFlag},</if>
        </trim>
    </insert>

    <update id="updatePileAuthCard" parameterType="com.jsowell.pile.domain.PileAuthCard">
        update pile_auth_card
        <trim prefix="SET" suffixOverrides=",">
            <if test="logicCard != null">logic_card = #{logicCard},</if>
            <if test="physicsCard != null">physics_card = #{physicsCard},</if>
            <if test="status != null">status = #{status},</if>
            <if test="memberId != null">member_id = #{memberId},</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="delFlag != null">del_flag = #{delFlag},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deletePileAuthCardById" parameterType="Long">
        delete from pile_auth_card where id = #{id}
    </delete>

    <update id="deactivateCard" parameterType="Long">
        update pile_auth_card
        set status = '9'
        where id = #{id,jdbcType=BIGINT}
    </update>

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

    <select id="selectPileAuthCardInfo" resultMap="PileAuthCardResult">
        select
        <include refid="Base_Column_List"/>
        from pile_auth_card
        where status = '1'
        <if test="memberId != null and memberId != ''">
            and member_id = #{memberId,jdbcType=VARCHAR}
        </if>
        <if test="physicsCard != null and physicsCard != ''">
            and physics_card = #{physicsCard,jdbcType=VARCHAR}
        </if>
        <if test="logicCard != null and logicCard != ''">
            and logic_card = #{logicCard,jdbcType=VARCHAR}
        </if>
        limit 1
    </select>

    <update id="unBindingCard">
        update pile_auth_card
            set status = '9', del_flag = '1'
        where member_id = #{memberId,jdbcType=VARCHAR}
         and logic_card = #{logicCard,jdbcType=VARCHAR}
    </update>

    <select id="selectSomeStatusCardInfo" resultMap="PileAuthCardResult">
        select
        <include refid="Base_Column_List"/>
        from
            pile_auth_card
        where 1 = 1
        <if test="status != null and status != ''">
            and status = #{status,jdbcType=VARCHAR}
        </if>
        <if test="logicCard != null and logicCard != ''">
            and logic_card = #{logicCard,jdbcType=VARCHAR}
        </if>
            limit 1
    </select>

    <select id="getPileAuthCardInfoList" resultType="com.jsowell.pile.vo.web.PileAuthCardVO">
        select
            t1.id,
            t1.logic_card as logicCard,
            t1.physics_card as physicsCard,
            t1.status,
            t1.secret_key as secretKey,
            t1.create_time as createTime,
            t1.member_id as memberId,
            t2.mobile_number as phoneNumber,
            t2.nick_name as nickName
        from pile_auth_card t1
            left join member_basic_info t2 on t1.member_id = t2.member_id
        where t1.del_flag = '0'
        <if test="logicCard != null  and logicCard != ''"> and t1.logic_card = #{logicCard}</if>
        <if test="physicsCard != null  and physicsCard != ''"> and t1.physics_card = #{physicsCard}</if>
        <if test="status != null  and status != ''"> and t1.status = #{status}</if>
        <if test="phoneNumber != null and phoneNumber != ''"> and t2.mobile_number = #{phoneNumber}</if>
        <if test="memberId != null  and memberId != ''"> and t1.member_id = #{memberId}</if>
    </select>

    <select id="getAuthCardListByMemberId" resultType="com.jsowell.pile.vo.uniapp.AuthCardVO">
        SELECT
            t1.logic_card AS logicCard,
            t1.status,
            t1.member_id AS memberId,
            t3.principal_balance AS principalBalance,
            t3.gift_balance AS giftBalance,
            t1.create_time AS createTime,
            t2.mobile_number AS phoneNumber
        FROM
            pile_auth_card t1
        JOIN member_basic_info t2 ON t1.member_id = t2.member_id
        JOIN member_wallet_info t3 ON t2.member_id = t3.member_id
        WHERE
            t1.del_flag = '0'
          AND t1.member_id = #{memberId,jdbcType=VARCHAR}
    </select>
</mapper>