<?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.yijia.customer.mapper.CustomerCardUseRecordMapper">

    <resultMap type="CustomerCardUseRecord" id="CustomerCardUseRecordResult">
        <result property="id"    column="id"    />
        <result property="orderNo"    column="order_no"    />
        <result property="unionId"    column="union_id"    />
        <result property="customerNo"    column="customer_no"    />
        <result property="customerName"    column="customer_name"    />
        <result property="usageType"    column="usage_type"    />
        <result property="payType"    column="pay_type"    />
        <result property="cardOilsType"    column="card_oils_type"    />
        <result property="amt"    column="amt"    />
        <result property="presentAmt"    column="present_amt"    />
        <result property="balance"    column="balance"    />
        <result property="createTime"    column="create_time"    />
        <result property="stationId"    column="station_id"    />
        <result property="stationName"    column="station_name"    />
        <result property="status"    column="status"    />
        <result property="xfAmt"    column="xf_amt"    />
        <result property="czAmt"    column="cz_amt"    />
    </resultMap>

    <sql id="selectCustomerCardUseRecordVo">
        select c.id, c.order_no, c.union_id, c.customer_no, c.customer_name, c.usage_type, c.pay_type,
         c.card_oils_type, c.amt, c.present_amt, c.balance, c.create_time, c.station_id,d.dept_name as station_name,
         c.status from customer_card_use_record c join sys_dept d on c.station_id = d.dept_id
    </sql>

    <select id="selectCustomerCardUseRecordList" parameterType="CustomerCardUseRecord" resultMap="CustomerCardUseRecordResult">
        <include refid="selectCustomerCardUseRecordVo"/>
        <where>
            <if test="orderNo != null  and orderNo != ''"> and c.order_no = #{orderNo}</if>
            <if test="unionId != null  and unionId != ''"> and c.union_id = #{unionId}</if>
            <if test="customerNo != null  and customerNo != ''"> and c.customer_no = #{customerNo}</if>
            <if test="customerName != null  and customerName != ''"> and c.customer_name like concat('%', #{customerName}, '%')</if>
            <if test="usageType != null  and usageType != ''"> and c.usage_type = #{usageType}</if>
            <if test="payType != null  and payType != ''"> and c.pay_type = #{payType}</if>
            <if test="cardOilsType != null  and cardOilsType != ''"> and c.card_oils_type = #{cardOilsType}</if>
            <if test="amt != null "> and c.amt = #{amt}</if>
            <if test="presentAmt != null "> and c.present_amt = #{presentAmt}</if>
            <if test="balance != null "> and c.balance = #{balance}</if>
            <if test="stationId != null "> and c.station_id = #{stationId}</if>
            <if test="stationIdList != null ">
                and c.station_id in
                <foreach item="item" index="index" collection="stationIdList"
                         open="(" separator="," close=")">
                    #{item}
                </foreach>
            </if>
            <if test="status != null  and status != ''"> and c.status = #{status}</if>
        </where>
        order by id desc
    </select>

    <select id="selectCustomerCardUseRecordById" parameterType="Long" resultMap="CustomerCardUseRecordResult">
        <include refid="selectCustomerCardUseRecordVo"/>
        where id = #{id}
    </select>
    <select id="listCZXFData" parameterType="CustomerCardUseRecord" resultMap="CustomerCardUseRecordResult">
        select sum(CASE usage_type  WHEN '+' THEN  amt ELSE 0 END ) cz_amt,
        sum(CASE usage_type  WHEN '-' THEN amt ELSE 0 END ) xf_amt
        from customer_card_use_record c
        <where>
            <if test="orderNo != null  and orderNo != ''"> and c.order_no = #{orderNo}</if>
            <if test="unionId != null  and unionId != ''"> and c.union_id = #{unionId}</if>
            <if test="customerNo != null  and customerNo != ''"> and c.customer_no = #{customerNo}</if>
            <if test="customerName != null  and customerName != ''"> and c.customer_name like concat('%', #{customerName}, '%')</if>
            <if test="usageType != null  and usageType != ''"> and c.usage_type = #{usageType}</if>
            <if test="payType != null  and payType != ''"> and c.pay_type = #{payType}</if>
            <if test="cardOilsType != null  and cardOilsType != ''"> and c.card_oils_type = #{cardOilsType}</if>
            <if test="amt != null "> and c.amt = #{amt}</if>
            <if test="presentAmt != null "> and c.present_amt = #{presentAmt}</if>
            <if test="balance != null "> and c.balance = #{balance}</if>
            <if test="stationId != null "> and c.station_id = #{stationId}</if>
            <if test="status != null  and status != ''"> and c.status = #{status}</if>
            <if test="stationIdList != null ">
                and station_id in
                <foreach item="item" index="index" collection="stationIdList"
                         open="(" separator="," close=")">
                    #{item}
                </foreach>
            </if>
            <if test="beginTime != null and beginTime != ''"><!-- 开始时间检索 -->
                AND date_format(create_time,'%Y-%m-%d %H:%i:%s') &gt;= date_format(#{beginTime},'%Y-%m-%d %H:%i:%s')
            </if>
            <if test="endTime != null and endTime != ''"><!-- 结束时间检索 -->
                AND date_format(create_time,'%Y-%m-%d %H:%i:%s') &lt;= date_format(#{endTime},'%Y-%m-%d %H:%i:%s')
            </if>
        </where>
     </select>
    <insert id="insertCustomerCardUseRecord" parameterType="CustomerCardUseRecord" useGeneratedKeys="true" keyProperty="id">
        insert into customer_card_use_record
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="orderNo != null">order_no,</if>
            <if test="unionId != null">union_id,</if>
            <if test="customerNo != null">customer_no,</if>
            <if test="customerName != null">customer_name,</if>
            <if test="usageType != null">usage_type,</if>
            <if test="payType != null">pay_type,</if>
            <if test="cardOilsType != null">card_oils_type,</if>
            <if test="amt != null">amt,</if>
            <if test="presentAmt != null">present_amt,</if>
            <if test="balance != null">balance,</if>
            <if test="createTime != null">create_time,</if>
            <if test="stationId != null">station_id,</if>
            <if test="status != null">status,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="orderNo != null">#{orderNo},</if>
            <if test="unionId != null">#{unionId},</if>
            <if test="customerNo != null">#{customerNo},</if>
            <if test="customerName != null">#{customerName},</if>
            <if test="usageType != null">#{usageType},</if>
            <if test="payType != null">#{payType},</if>
            <if test="cardOilsType != null">#{cardOilsType},</if>
            <if test="amt != null">#{amt},</if>
            <if test="presentAmt != null">#{presentAmt},</if>
            <if test="balance != null">#{balance},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="stationId != null">#{stationId},</if>
            <if test="status != null">#{status},</if>
         </trim>
    </insert>

    <update id="updateCustomerCardUseRecord" parameterType="CustomerCardUseRecord">
        update customer_card_use_record
        <trim prefix="SET" suffixOverrides=",">
            <if test="orderNo != null">order_no = #{orderNo},</if>
            <if test="unionId != null">union_id = #{unionId},</if>
            <if test="customerNo != null">customer_no = #{customerNo},</if>
            <if test="customerName != null">customer_name = #{customerName},</if>
            <if test="usageType != null">usage_type = #{usageType},</if>
            <if test="payType != null">pay_type = #{payType},</if>
            <if test="cardOilsType != null">card_oils_type = #{cardOilsType},</if>
            <if test="amt != null">amt = #{amt},</if>
            <if test="presentAmt != null">present_amt = #{presentAmt},</if>
            <if test="balance != null">balance = #{balance},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="stationId != null">station_id = #{stationId},</if>
            <if test="status != null">status = #{status},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteCustomerCardUseRecordById" parameterType="Long">
        delete from customer_card_use_record where id = #{id}
    </delete>

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

</mapper>
