<?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.bonus.canteen.core.account.mapper.AccCardMapper">
    <resultMap type="com.bonus.canteen.core.account.domain.AccountCard" id="AccCardResult">
        <result property="id"    column="id"    />
        <result property="userId"    column="user_id"    />
        <result property="accountId"    column="account_id"    />
        <result property="serialNum"    column="serial_num"    />
        <result property="cardNature"    column="card_nature"    />
        <result property="cardType"    column="card_type"    />
        <result property="cardStatus"    column="card_status"    />
        <result property="deposit"    column="deposit"    />
        <result property="productCost"    column="product_cost"    />
        <result property="validityDate"    column="validity_date"    />
        <result property="createBy"    column="create_by"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateBy"    column="update_by"    />
        <result property="updateTime"    column="update_time"    />
    </resultMap>

    <sql id="selectAccCardVo">
        select ac.id, ac.user_id, ac.account_id, ac.serial_num, ac.card_nature,
               ac.card_type, ac.card_status, ac.deposit,ac.product_cost,
               ac.validity_date, ac.create_by, ac.create_time, ac.update_by,ac.update_time, su.nick_name
        from account_card ac
        left join sys_user su on su.user_id = ac.user_id
    </sql>

    <select id="selectAccCardList" parameterType="com.bonus.canteen.core.account.domain.AccountCard" resultMap="AccCardResult">
        <include refid="selectAccCardVo"/>
        <where>
            <if test="userId != null "> and ac.user_id = #{userId}</if>
            <if test="accountId != null "> and ac.account_id = #{accountId}</if>
            <if test="serialNum != null  and serialNum != ''"> and ac.serial_num = #{serialNum}</if>
            <if test="cardNature != null  and cardNature != ''"> and ac.card_nature = #{cardNature}</if>
            <if test="cardType != null "> and ac.card_type = #{cardType}</if>
            <if test="deposit != null "> and ac.deposit = #{deposit}</if>
            <if test="productCost != null "> and ac.product_cost = #{productCost}</if>
            <if test="validityDate != null "> and ac.validity_date = #{validityDate}</if>
        </where>
    </select>

    <select id="selectAccCardCountByUserId" parameterType="Long" resultType="Integer">
        select count(1)
        from account_card
        where user_id = #{userId} and card_status in (1,4,5)
    </select>

    <select id="selectAccCardCountByCardSerialNum" parameterType="String" resultType="Integer">
        select count(1)
        from account_card
        where serial_num = #{serialNum}
    </select>

    <select id="selectAccCardById" parameterType="Long" resultMap="AccCardResult">
        <include refid="selectAccCardVo"/>
        where ac.id = #{id}
    </select>
        
    <insert id="insertAccCard" parameterType="com.bonus.canteen.core.account.domain.AccountCard" useGeneratedKeys="true" keyProperty="id">
        insert into account_card
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="userId != null">user_id,</if>
            <if test="accountId != null">account_id,</if>
            <if test="serialNum != null and serialNum != ''">serial_num,</if>
            <if test="cardNature != null">card_nature,</if>
            <if test="cardType != null">card_type,</if>
            <if test="cardStatus != null">card_status,</if>
            <if test="deposit != null">deposit,</if>
            <if test="productCost != null">product_cost,</if>
            <if test="validityDate != null">validity_date,</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>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="userId != null">#{userId},</if>
            <if test="accountId != null">#{accountId},</if>
            <if test="serialNum != null and serialNum != ''">#{serialNum},</if>
            <if test="cardNature != null">#{cardNature},</if>
            <if test="cardType != null">#{cardType},</if>
            <if test="cardStatus != null">#{cardStatus},</if>
            <if test="deposit != null">#{deposit},</if>
            <if test="productCost != null">#{productCost},</if>
            <if test="validityDate != null">#{validityDate},</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>
         </trim>
    </insert>

    <update id="updateAccCard" parameterType="com.bonus.canteen.core.account.domain.AccountCard">
        update account_card
        <trim prefix="SET" suffixOverrides=",">
            <if test="userId != null">user_id = #{userId},</if>
            <if test="accountId != null">account_id = #{accountId},</if>
            <if test="serialNum != null and serialNum != ''">serial_num = #{serialNum},</if>
            <if test="cardNature != null">card_nature = #{cardNature},</if>
            <if test="cardType != null">card_type = #{cardType},</if>
            <if test="cardStatus != null">card_status = #{cardStatus},</if>
            <if test="deposit != null">deposit = #{deposit},</if>
            <if test="productCost != null">product_cost = #{productCost},</if>
            <if test="validityDate != null">validity_date = #{validityDate},</if>
            <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>
        </trim>
        where account_id = #{accountId}
    </update>

    <delete id="deleteAccCardById" parameterType="Long">
        delete from account_card where id = #{id}
    </delete>

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