<?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.FinancialMasterMapper">

    <resultMap id="FinancialMasterResult" type="com.ghy.payment.domain.FinancialMaster">
        <id property="id"                    column="id"/>
        <result property="deptId"            column="dept_id"/>
        <result property="code"              column="code"/>
        <result property="orderMasterId"     column="order_master_id"/>
        <result property="orderMasterCode"   column="order_master_code"/>
        <result property="totalMoney"        column="total_money"/>
        <result property="discountMoney"     column="discount_money"/>
        <result property="payMoney"          column="pay_money"/>
        <result property="serverMoney"          column="server_money"/>
        <result property="payType"           column="pay_type"/>
        <result property="payStatus"         column="pay_status"/>
        <result property="payTime"           column="pay_time"/>
        <result property="paymentId"         column="payment_id"/>
        <result property="reverseId"         column="reverse_id"/>
        <result property="createBy"          column="create_by"/>
        <result property="createTime"        column="create_time"/>
        <result property="updateBy"          column="update_by"/>
        <result property="updateTime"        column="update_time"/>
        <result property="remark"            column="remark"/>
    </resultMap>

    <sql id="selectFinancialMaster">
        SELECT  id, dept_id, code, order_master_id, order_master_code, total_money, discount_money, pay_money, server_money,
               pay_type, pay_status, pay_time, payment_id, reverse_id, create_by, create_time, update_by, update_time, remark
        FROM financial_master
    </sql>

    <select id="selectFinancialMasterList" parameterType="com.ghy.payment.domain.FinancialMaster" resultMap="FinancialMasterResult">
        <include refid="selectFinancialMaster"/>
        <where>
            <if test="orderMasterId != null and orderMasterId != 0">
                AND order_master_id = #{orderMasterId}
            </if>
            <if test="orderMasterIds != null and orderMasterIds.size > 0">
                AND order_master_id IN
                <foreach collection="orderMasterIds" item="orderMasterId" open="(" separator="," close=")">
                    #{orderMasterId}
                </foreach>
            </if>
            <if test="ids != null and ids != ''">
                AND order_master_id IN
                <foreach collection="ids" item="id" open="(" separator="," close=")">
                    #{id}
                </foreach>
            </if>
            <if test="orderMasterCode != null and orderMasterCode != ''">
                AND order_master_code LIKE concat('%', #{orderMasterCode}, '%')
            </if>
            <if test="payType != null">
                AND pay_type = #{payType}
            </if>
            <if test="payStatus != null">
                AND pay_status = #{payStatus}
            </if>
        </where>
    </select>

    <select id="selectById" parameterType="long" resultMap="FinancialMasterResult">
        <include refid="selectFinancialMaster"/> WHERE id = #{financialMasterId}
    </select>

    <select id="selectByOrderMasterId" parameterType="long" resultMap="FinancialMasterResult">
        <include refid="selectFinancialMaster"/> WHERE order_master_id = #{orderMasterId}
    </select>

    <select id="selectByPaymentId" parameterType="String" resultMap="FinancialMasterResult">
        <include refid="selectFinancialMaster"/> WHERE payment_id = #{paymentId}
    </select>

    <select id="selectByOrderMasterIds" resultMap="FinancialMasterResult">
        <include refid="selectFinancialMaster"/> WHERE order_master_id IN
        <foreach collection="orderMasterIds" item="orderMasterId" open="(" separator="," close=")">
            #{orderMasterId}
        </foreach>
    </select>

    <delete id="deleteFinancialMasterByIds" parameterType="Long">
        DELETE FROM financial_master WHERE id IN
        <foreach collection="array" item="financialMasterId" open="(" separator="," close=")">
            #{financialMasterId}
        </foreach>
    </delete>

    <update id="updateFinancialMaster" parameterType="com.ghy.payment.domain.FinancialMaster">
        UPDATE financial_master
        <set>
            <if test="deptId != null">dept_id = #{deptId},</if>
            <if test="code != null and code != ''">`code` = #{code},</if>
            <if test="orderMasterId != null and orderMasterId != 0">order_master_id = #{orderMasterId},</if>
            <if test="orderMasterCode != null and orderMasterCode != ''">order_master_code = #{orderMasterCode},</if>
            <if test="totalMoney != null">total_money = #{totalMoney},</if>
            <if test="serverMoney != null">server_money = #{serverMoney},</if>
            <if test="discountMoney != null">discount_money = #{discountMoney},</if>
            <if test="payType != null">pay_type = #{payType},</if>
            <if test="payStatus != null">pay_status = #{payStatus},</if>
            <if test="payTime != null">pay_time = #{payTime},</if>
            <if test="payMoney != null">pay_money = #{payMoney},</if>
            <if test="paymentId != null and paymentId != ''">payment_id = #{paymentId},</if>
            <if test="reverseId != null and reverseId != ''">reverse_id = #{reverseId},</if>
            <if test="updateBy != null and updateBy != ''">update_by = #{updateBy},</if>
            update_time = SYSDATE()
        </set>
        <where>
            <if test="id !=null and id != 0">
                AND id = #{id}
            </if>
            <if test="orderMasterCode !=null and orderMasterCode != ''">
                AND order_master_code = #{orderMasterCode}
            </if>
        </where>

    </update>

    <update id="paySucceeded">
        UPDATE financial_master SET
            payment_id = #{paymentId},
            pay_status = 1 ,
            pay_type = #{payType},
            pay_time = SYSDATE(),
            update_time = SYSDATE()
        WHERE id = #{id}
    </update>

    <update id="updateOrderStatus">
        UPDATE order_master SET pay_status = #{payStatus} , pay_time = SYSDATE(), update_time = SYSDATE()
        WHERE code = #{orderMasterCode}
    </update>

    <update id="refundSucceeded">
        UPDATE financial_master SET
        pay_status = 3 ,
        update_time = SYSDATE()
        WHERE reverse_id = #{reverseId}
    </update>

    <update id="updatePayStatus">
        UPDATE financial_master SET
        pay_status = #{payStatus} ,
        update_time = NOW()
        WHERE id = #{id}
    </update>

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

    <insert id="insertFinancialMaster" parameterType="com.ghy.payment.domain.FinancialMaster" useGeneratedKeys="true" keyProperty="id">
        INSERT INTO financial_master(
        <if test="deptId != null">dept_id,</if>
        <if test="code != null and code != ''">`code`,</if>
        <if test="orderMasterId != null and orderMasterId != 0">order_master_id,</if>
        <if test="orderMasterCode != null and orderMasterCode != ''">order_master_code,</if>
        <if test="totalMoney != null">total_money,</if>
        <if test="serverMoney != null">server_money,</if>
        <if test="discountMoney != null">discount_money,</if>
        <if test="payMoney != null">pay_money,</if>
        <if test="createBy != null and createBy != ''">create_by,</if>
        create_time
        )VALUES(
        <if test="deptId != null">#{deptId},</if>
        <if test="code != null and code != ''">#{code},</if>
        <if test="orderMasterId != null and orderMasterId != 0">#{orderMasterId},</if>
        <if test="orderMasterCode != null and orderMasterCode != ''">#{orderMasterCode},</if>
        <if test="totalMoney != null">#{totalMoney},</if>
        <if test="serverMoney != null">#{serverMoney},</if>
        <if test="discountMoney != null">#{discountMoney},</if>
        <if test="payMoney != null">#{payMoney},</if>
        <if test="createBy != null and createBy != ''">#{createBy},</if>
        SYSDATE()
        )
    </insert>

</mapper>
