<?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.order.mapper.OrderAddSubtractMapper">

    <resultMap id="ColumnMap" type="com.ghy.order.domain.OrderAddSubtract">
        <id property="id" column="id"/>
        <result property="orderMasterId"    column="order_master_id"/>
        <result property="orderDetailId"    column="order_detail_id"/>
        <result property="money"            column="money"/>
        <result property="remark"           column="remark"/>
        <result property="createTime"       column="create_time"/>
        <result property="createBy"         column="create_by"/>

        <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"/>
    </resultMap>

    <sql id="selectColumns">
        SELECT id, order_master_id, order_detail_id, money, remark, create_time, create_by,
               pay_type, pay_status, pay_time, payment_id, reverse_id
        FROM order_add_subtract
    </sql>

    <insert id="insert" parameterType="com.ghy.order.domain.OrderAddSubtract" useGeneratedKeys="true" keyProperty="id">
        INSERT INTO order_add_subtract(order_master_id, order_detail_id, money, remark, create_by, pay_type)
        VALUES (#{orderMasterId}, #{orderDetailId}, #{money}, #{remark}, #{createBy}, #{payType})
    </insert>

    <update id="update" parameterType="com.ghy.order.domain.OrderAddSubtract">
        UPDATE order_add_subtract
        <set>
            <if test="remark != null">remark = #{remark},</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="paymentId != null">payment_id = #{paymentId},</if>
            <if test="reverseId != null">reverse_id = #{reverseId}</if>
        </set>
        WHERE id = #{id}
    </update>

    <update id="updatePay">
        UPDATE order_add_subtract
        SET pay_status = #{payStatus},
            payment_id = #{paymentId}
        WHERE id = #{id}
    </update>

    <select id="select" parameterType="com.ghy.order.domain.OrderAddSubtract" resultMap="ColumnMap">
        <include refid="selectColumns"/>
        <where>
            <if test="orderMasterId != null">
                AND order_master_id = #{orderMasterId}
            </if>
            <if test="orderDetailId != null">
                AND order_detail_id = #{orderDetailId}
            </if>
            <if test="paymentId != null and paymentId != ''">
                AND payment_id = #{paymentId}
            </if>
            <if test="payStatus != null and payStatus != ''">
                AND pay_status = #{payStatus}
            </if>
        </where>
    </select>

    <select id="selectById" parameterType="long" resultMap="ColumnMap">
        <include refid="selectColumns"/>
        WHERE id = #{id}
    </select>

    <select id="selectByOrderDetail" resultMap="ColumnMap">
        <include refid="selectColumns"/>
        WHERE order_detail_id = #{orderDetailId}
    </select>

</mapper>
