<?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.ghy.payment.mapper.FinancialChangeRecordMapper">

    <resultMap id="FinancialChangeRecordResult" type="FinancialChangeRecord">
        <id property="id"                   column="id"/>
        <result property="orderDetailId"    column="order_detail_id"/>
        <result property="orderMasterId"    column="order_master_id"/>
        <result property="paymentId"        column="payment_id"/>
        <result property="reverseId"        column="reverse_id"/>
        <result property="changeMoney"      column="change_money"/>
        <result property="oneMoney"      column="one_money"/>
        <result property="twoMoney"      column="two_money"/>
        <result property="threeMoney"      column="three_money"/>
        <result property="deptMoney"      column="dept_money"/>
        <result property="deptAddMoney"      column="dept_add_money"/>
        <result property="leaderMoney"      column="leader_money"/>
        <result property="workerMoney"      column="worker_money"/>
        <result property="status"           column="status"/>
        <result property="payStatus"        column="pay_status"/>
        <result property="leftMoney"        column="left_money"/>
    </resultMap>

    <sql id="selectFinancialChangeRecordVo">
        SELECT id, order_detail_id, order_master_id, payment_id, reverse_id,
               change_money, status, pay_status, type, left_money, remark FROM financial_change_record
    </sql>

    <select id="selectNotPayRecordByDetailId" parameterType="Long" resultMap="FinancialChangeRecordResult">
        <include refid="selectFinancialChangeRecordVo" />
        where order_detail_id = #{orderDetailId} and pay_status = 0;
    </select>

    <select id="selectFinancialChangeRecordList" parameterType="FinancialChangeRecord" resultMap="FinancialChangeRecordResult">
        <include refid="selectFinancialChangeRecordVo"/>
        <where>
            <if test="orderDetailId != null "> and order_detail_id = #{orderDetailId}</if>
            <if test="changeMoney != null "> and change_money = #{changeMoney}</if>
            <if test="status != null "> and status = #{status}</if>
            <if test="payStatus != null "> and pay_status = #{payStatus}</if>
        </where>
        order by id desc
    </select>

    <select id="selectByDetailIds" parameterType="String" resultMap="FinancialChangeRecordResult">
        <include refid="selectFinancialChangeRecordVo"/>
        where order_detail_id in ( ${ids} )
    </select>

    <select id="selectByMasterId" parameterType="Long" resultMap="FinancialChangeRecordResult">
        <include refid="selectFinancialChangeRecordVo"/>
        where order_master_id = #{orderMasterId}
    </select>

    <select id="selectFinancialChangeRecordById" parameterType="Long" resultMap="FinancialChangeRecordResult">
        <include refid="selectFinancialChangeRecordVo"/>
        where id = #{id}
    </select>

    <insert id="insertFinancialChangeRecord" parameterType="FinancialChangeRecord" useGeneratedKeys="true" keyProperty="id">
        insert into financial_change_record
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="orderDetailId != null">order_detail_id,</if>
            <if test="orderMasterId != null">order_master_id,</if>
            <if test="paymentId != null">payment_id,</if>
            <if test="reverseId != null">reverse_id,</if>
            <if test="oneMoney != null">one_money,</if>
            <if test="twoMoney != null">two_money,</if>
            <if test="threeMoney != null">three_money,</if>
            <if test="deptMoney != null">dept_money,</if>
            <if test="deptAddMoney != null">dept_add_money,</if>
            <if test="leaderMoney != null">leader_money,</if>
            <if test="workerMoney != null">worker_money,</if>
            <if test="changeMoney != null">change_money,</if>
            <if test="status != null">status,</if>
            <if test="payStatus != null">pay_status,</if>
            <if test="type != null">type,</if>
            <if test="remark != null">remark,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="orderDetailId != null">#{orderDetailId},</if>
            <if test="orderMasterId != null">#{orderMasterId},</if>
            <if test="paymentId != null">#{paymentId},</if>
            <if test="reverseId != null">#{reverseId},</if>
            <if test="oneMoney != null">#{oneMoney},</if>
            <if test="twoMoney != null">#{twoMoney},</if>
            <if test="threeMoney != null">#{threeMoney},</if>
            <if test="deptMoney != null">#{deptMoney},</if>
            <if test="deptAddMoney != null">#{deptAddMoney},</if>
            <if test="leaderMoney != null">#{leaderMoney},</if>
            <if test="workerMoney != null">#{workerMoney},</if>
            <if test="changeMoney != null">#{changeMoney},</if>
            <if test="status != null">#{status},</if>
            <if test="payStatus != null">#{payStatus},</if>
            <if test="type != null">#{type},</if>
            <if test="remark != null">#{remark},</if>
        </trim>
    </insert>

    <update id="updateFinancialChangeRecord" parameterType="FinancialChangeRecord">
        update financial_change_record
        <trim prefix="SET" suffixOverrides=",">
            <if test="orderDetailId != null">order_detail_id = #{orderDetailId},</if>
            <if test="orderMasterId != null">order_master_id = #{orderMasterId},</if>
            <if test="paymentId != null">payment_id = #{paymentId},</if>
            <if test="reverseId != null">reverse_id = #{reverseId},</if>
            <if test="changeMoney != null">change_money = #{changeMoney},</if>
            <if test="oneMoney != null">one_money = #{oneMoney},</if>
            <if test="twoMoney != null">two_money = #{twoMoney},</if>
            <if test="threeMoney != null">three_money = #{threeMoney},</if>
            <if test="deptMoney != null">dept_money = #{deptMoney},</if>
            <if test="deptAddMoney != null">dept_add_money = #{deptAddMoney},</if>
            <if test="leaderMoney != null">leader_money = #{leaderMoney},</if>
            <if test="workerMoney != null">worker_money = #{workerMoney},</if>
            <if test="status != null">status = #{status},</if>
            <if test="payStatus != null">pay_status = #{payStatus},</if>
            <if test="type != null">type = #{type},</if>
            <if test="leftMoney != null">left_money = #{leftMoney},</if>
            <if test="remark != null">remark = #{remark},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteFinancialChangeRecordById" parameterType="Long">
        delete from financial_change_record where id = #{id}
    </delete>

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

    <update id="updatePayStatus">
        UPDATE financial_change_record
        SET pay_status = #{payStatus}
        WHERE id = #{id}
    </update>

    <update id="refundSucceeded">
        UPDATE financial_change_record SET
        pay_status = 3
        WHERE reverse_id = #{reverseId}
    </update>

    <update id="updatePay">
        UPDATE financial_change_record
        SET pay_status = #{payStatus},
            payment_id = #{paymentId},
            update_time = NOW()
        WHERE id = #{id}
    </update>
</mapper>
