<?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.xinelu.manage.mapper.refundinfo.RefundInfoMapper">

    <resultMap type="RefundInfo" id="RefundInfoResult">
        <result property="id" column="id"/>
        <result property="patientId" column="patient_id"/>
        <result property="nurseStationPersonId" column="nurse_station_person_id"/>
        <result property="orderNo" column="order_no"/>
        <result property="refundNo" column="refund_no"/>
        <result property="outRefundNo" column="out_refund_no"/>
        <result property="transactionNo" column="transaction_no"/>
        <result property="refundReason" column="refund_reason"/>
        <result property="refundType" column="refund_type"/>
        <result property="wechatRefundStatus" column="wechat_refund_status"/>
        <result property="alipayRefundStatus" column="alipay_refund_status"/>
        <result property="orderTotalPrice" column="order_total_price"/>
        <result property="refundPrice" column="refund_price"/>
        <result property="currency" column="currency"/>
        <result property="channel" column="channel"/>
        <result property="userreceivedaccount" column="userReceivedAccount"/>
        <result property="successTime" column="success_time"/>
        <result property="refundNotifyContent" column="refund_notify_content"/>
        <result property="applyRefundReturnContent" column="apply_refund_return_content"/>
        <result property="refundMerchantType" column="refund_merchant_type"/>
        <result property="delFlag" column="del_flag"/>
        <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="selectRefundInfoVo">
        select id,
               patient_id,
               nurse_station_person_id,
               order_no,
               refund_no,
               out_refund_no,
               transaction_no,
               refund_reason,
               refund_type,
               wechat_refund_status,
               alipay_refund_status,
               order_total_price,
               refund_price,
               currency,
               channel,
               userReceivedAccount,
               success_time,
               refund_notify_content,
               apply_refund_return_content,
               refund_merchant_type,
               del_flag,
               create_by,
               create_time,
               update_by,
               update_time
        from refund_info
    </sql>

    <select id="selectRefundInfoList" parameterType="RefundInfo" resultMap="RefundInfoResult">
        <include refid="selectRefundInfoVo"/>
        <where>
            <if test="patientId != null ">
                and patient_id = #{patientId}
            </if>
            <if test="nurseStationPersonId != null ">
                and nurse_station_person_id = #{nurseStationPersonId}
            </if>
            <if test="orderNo != null  and orderNo != ''">
                and order_no = #{orderNo}
            </if>
            <if test="refundNo != null  and refundNo != ''">
                and refund_no = #{refundNo}
            </if>
            <if test="outRefundNo != null  and outRefundNo != ''">
                and out_refund_no = #{outRefundNo}
            </if>
            <if test="transactionNo != null  and transactionNo != ''">
                and transaction_no = #{transactionNo}
            </if>
            <if test="refundReason != null  and refundReason != ''">
                and refund_reason = #{refundReason}
            </if>
            <if test="refundType != null  and refundType != ''">
                and refund_type = #{refundType}
            </if>
            <if test="wechatRefundStatus != null  and wechatRefundStatus != ''">
                and wechat_refund_status = #{wechatRefundStatus}
            </if>
            <if test="alipayRefundStatus != null  and alipayRefundStatus != ''">
                and alipay_refund_status = #{alipayRefundStatus}
            </if>
            <if test="orderTotalPrice != null ">
                and order_total_price = #{orderTotalPrice}
            </if>
            <if test="refundPrice != null ">
                and refund_price = #{refundPrice}
            </if>
            <if test="currency != null  and currency != ''">
                and currency = #{currency}
            </if>
            <if test="channel != null  and channel != ''">
                and channel = #{channel}
            </if>
            <if test="userreceivedaccount != null  and userreceivedaccount != ''">
                and userReceivedAccount = #{userreceivedaccount}
            </if>
            <if test="successTime != null ">
                and success_time = #{successTime}
            </if>
            <if test="refundNotifyContent != null  and refundNotifyContent != ''">
                and refund_notify_content = #{refundNotifyContent}
            </if>
            <if test="applyRefundReturnContent != null  and applyRefundReturnContent != ''">
                and apply_refund_return_content = #{applyRefundReturnContent}
            </if>
            <if test="refundMerchantType != null  and refundMerchantType != ''">
                and refund_merchant_type = #{refundMerchantType}
            </if>
        </where>
    </select>

    <select id="selectRefundInfoById" parameterType="Long"
            resultMap="RefundInfoResult">
        <include refid="selectRefundInfoVo"/>
        where id = #{id}
    </select>

    <insert id="insertRefundInfo" parameterType="RefundInfo" useGeneratedKeys="true"
            keyProperty="id">
        insert into refund_info
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="patientId != null">patient_id,
            </if>
            <if test="nurseStationPersonId != null">nurse_station_person_id,
            </if>
            <if test="orderNo != null">order_no,
            </if>
            <if test="refundNo != null">refund_no,
            </if>
            <if test="outRefundNo != null">out_refund_no,
            </if>
            <if test="transactionNo != null">transaction_no,
            </if>
            <if test="refundReason != null">refund_reason,
            </if>
            <if test="refundType != null">refund_type,
            </if>
            <if test="wechatRefundStatus != null">wechat_refund_status,
            </if>
            <if test="alipayRefundStatus != null">alipay_refund_status,
            </if>
            <if test="orderTotalPrice != null">order_total_price,
            </if>
            <if test="refundPrice != null">refund_price,
            </if>
            <if test="currency != null">currency,
            </if>
            <if test="channel != null">channel,
            </if>
            <if test="userreceivedaccount != null">userReceivedAccount,
            </if>
            <if test="successTime != null">success_time,
            </if>
            <if test="refundNotifyContent != null">refund_notify_content,
            </if>
            <if test="applyRefundReturnContent != null">apply_refund_return_content,
            </if>
            <if test="refundMerchantType != null">refund_merchant_type,
            </if>
            <if test="delFlag != null">del_flag,
            </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="patientId != null">#{patientId},
            </if>
            <if test="nurseStationPersonId != null">#{nurseStationPersonId},
            </if>
            <if test="orderNo != null">#{orderNo},
            </if>
            <if test="refundNo != null">#{refundNo},
            </if>
            <if test="outRefundNo != null">#{outRefundNo},
            </if>
            <if test="transactionNo != null">#{transactionNo},
            </if>
            <if test="refundReason != null">#{refundReason},
            </if>
            <if test="refundType != null">#{refundType},
            </if>
            <if test="wechatRefundStatus != null">#{wechatRefundStatus},
            </if>
            <if test="alipayRefundStatus != null">#{alipayRefundStatus},
            </if>
            <if test="orderTotalPrice != null">#{orderTotalPrice},
            </if>
            <if test="refundPrice != null">#{refundPrice},
            </if>
            <if test="currency != null">#{currency},
            </if>
            <if test="channel != null">#{channel},
            </if>
            <if test="userreceivedaccount != null">#{userreceivedaccount},
            </if>
            <if test="successTime != null">#{successTime},
            </if>
            <if test="refundNotifyContent != null">#{refundNotifyContent},
            </if>
            <if test="applyRefundReturnContent != null">#{applyRefundReturnContent},
            </if>
            <if test="refundMerchantType != null">#{refundMerchantType},
            </if>
            <if test="delFlag != null">#{delFlag},
            </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="updateRefundInfo" parameterType="RefundInfo">
        update refund_info
        <trim prefix="SET" suffixOverrides=",">
            <if test="patientId != null">patient_id =
                #{patientId},
            </if>
            <if test="nurseStationPersonId != null">nurse_station_person_id =
                #{nurseStationPersonId},
            </if>
            <if test="orderNo != null">order_no =
                #{orderNo},
            </if>
            <if test="refundNo != null">refund_no =
                #{refundNo},
            </if>
            <if test="outRefundNo != null">out_refund_no =
                #{outRefundNo},
            </if>
            <if test="transactionNo != null">transaction_no =
                #{transactionNo},
            </if>
            <if test="refundReason != null">refund_reason =
                #{refundReason},
            </if>
            <if test="refundType != null">refund_type =
                #{refundType},
            </if>
            <if test="wechatRefundStatus != null">wechat_refund_status =
                #{wechatRefundStatus},
            </if>
            <if test="alipayRefundStatus != null">alipay_refund_status =
                #{alipayRefundStatus},
            </if>
            <if test="orderTotalPrice != null">order_total_price =
                #{orderTotalPrice},
            </if>
            <if test="refundPrice != null">refund_price =
                #{refundPrice},
            </if>
            <if test="currency != null">currency =
                #{currency},
            </if>
            <if test="channel != null">channel =
                #{channel},
            </if>
            <if test="userreceivedaccount != null">userReceivedAccount =
                #{userreceivedaccount},
            </if>
            <if test="successTime != null">success_time =
                #{successTime},
            </if>
            <if test="refundNotifyContent != null">refund_notify_content =
                #{refundNotifyContent},
            </if>
            <if test="applyRefundReturnContent != null">apply_refund_return_content =
                #{applyRefundReturnContent},
            </if>
            <if test="refundMerchantType != null">refund_merchant_type =
                #{refundMerchantType},
            </if>
            <if test="delFlag != null">del_flag =
                #{delFlag},
            </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 id = #{id}
    </update>

    <delete id="deleteRefundInfoById" parameterType="Long">
        delete
        from refund_info
        where id = #{id}
    </delete>

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

    <update id="updateRefundInfoByNo" parameterType="refundInfo">
        update refund_info
        set wechat_refund_status  = #{wechatRefundStatus},
            success_time          = #{successTime},
            refund_notify_content = #{refundNotifyContent},
            update_time           = now()
        where order_no = #{orderNo}
          and out_refund_no = #{outRefundNo}
          and wechat_refund_status &lt;&gt; #{wechatRefundStatus}
    </update>

    <update id="updateBatchRefundStatus">
        UPDATE refund_info
        SET wechat_refund_status = #{weChatRefundStatus},
            success_time         = #{successTime},
            update_time          = now()
        WHERE del_flag = 0
          and order_no = #{orderNo}
          and wechat_refund_status &lt;&gt; #{weChatRefundStatus}
    </update>

    <select id="getRefundInfoByOrderNo" parameterType="string" resultType="int">
        select count(1) refundCount
        from refund_info
        where order_no = #{orderNo}
    </select>
</mapper>
