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

    <resultMap type="AfterServiceRecord" id="AfterServiceRecordResult">
        <result property="id"    column="id"    />
        <result property="customerReasonType"    column="customer_reason_type"    />
        <result property="customerReason"    column="customer_reason"    />
        <result property="orderDetailId"    column="order_detail_id"    />
        <result property="operType"    column="oper_type"    />
        <result property="workerFeedbackResult"    column="worker_feedback_result"    />
        <result property="workerFeedbackReasonType"    column="worker_feedback_reason_type"    />
        <result property="workerFeedbackReason"    column="worker_feedback_reason"    />
        <result property="refund"    column="refund"    />
        <result property="agreedRefund"    column="agreed_refund"    />
        <result property="customerFinalCheck"    column="customer_final_check"    />
        <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"    />
        <result property="originalRefund"    column="original_refund"    />
        <result property="refundApplyTime"    column="refund_apply_time"    />
    </resultMap>

    <sql id="selectAfterServiceRecordVo">
        select id, customer_reason_type, customer_reason, order_detail_id, oper_type, worker_feedback_result,
               worker_feedback_reason_type, worker_feedback_reason, refund, agreed_refund, original_refund,
               customer_final_check, create_by, create_time, update_by, update_time, remark, refund_apply_time
        from after_service_record
    </sql>

    <select id="selectAfterServiceRecordList" parameterType="AfterServiceRecord" resultMap="AfterServiceRecordResult">
        <include refid="selectAfterServiceRecordVo"/>
        <where>
            <if test="customerReasonType != null "> and customer_reason_type = #{customerReasonType}</if>
            <if test="customerReason != null  and customerReason != ''"> and customer_reason = #{customerReason}</if>
            <if test="orderDetailId != null "> and order_detail_id = #{orderDetailId}</if>
            <if test="operType != null "> and oper_type = #{operType}</if>
            <if test="workerFeedbackResult != null "> and worker_feedback_result = #{workerFeedbackResult}</if>
            <if test="workerFeedbackReasonType != null "> and worker_feedback_reason_type = #{workerFeedbackReasonType}</if>
            <if test="workerFeedbackReason != null  and workerFeedbackReason != ''"> and worker_feedback_reason = #{workerFeedbackReason}</if>
            <if test="refund != null "> and refund = #{refund}</if>
            <if test="agreedRefund != null "> and agreed_refund = #{agreedRefund}</if>
            <if test="customerFinalCheck != null "> and customer_final_check = #{customerFinalCheck}</if>
            <if test="excludeAfterServiceFinished">
                and ( customer_final_check = '0' or customer_final_check is null )
            </if>
        </where>
    </select>

    <select id="selectAfterServiceRecordById" parameterType="String" resultMap="AfterServiceRecordResult">
        <include refid="selectAfterServiceRecordVo"/>
        where id = #{id}
    </select>

    <select id="unfinished" resultMap="AfterServiceRecordResult" parameterType="Long">
        <include refid="selectAfterServiceRecordVo"/>
        WHERE order_detail_id = #{orderDetailId} AND customer_final_check IS NULL
    </select>

    <insert id="insertAfterServiceRecord" parameterType="AfterServiceRecord" useGeneratedKeys="true" keyProperty="id">
        insert into after_service_record
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="customerReasonType != null">customer_reason_type,</if>
            <if test="customerReason != null">customer_reason,</if>
            <if test="orderDetailId != null">order_detail_id,</if>
            <if test="operType != null">oper_type,</if>
            <if test="workerFeedbackResult != null">worker_feedback_result,</if>
            <if test="workerFeedbackReasonType != null">worker_feedback_reason_type,</if>
            <if test="workerFeedbackReason != null">worker_feedback_reason,</if>
            <if test="refund != null">refund,</if>
            <if test="agreedRefund != null">agreed_refund,</if>
            <if test="customerFinalCheck != null">customer_final_check,</if>
            <if test="createBy != null">create_by,</if>
            <if test="updateBy != null">update_by,</if>
            <if test="remark != null">remark,</if>
            create_time, update_time
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="customerReasonType != null">#{customerReasonType},</if>
            <if test="customerReason != null">#{customerReason},</if>
            <if test="orderDetailId != null">#{orderDetailId},</if>
            <if test="operType != null">#{operType},</if>
            <if test="workerFeedbackResult != null">#{workerFeedbackResult},</if>
            <if test="workerFeedbackReasonType != null">#{workerFeedbackReasonType},</if>
            <if test="workerFeedbackReason != null">#{workerFeedbackReason},</if>
            <if test="refund != null">#{refund},</if>
            <if test="agreedRefund != null">#{agreedRefund},</if>
            <if test="customerFinalCheck != null">#{customerFinalCheck},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="updateBy != null">#{updateBy},</if>
            <if test="remark != null">#{remark},</if>
            NOW(), NOW()
        </trim>
    </insert>

    <update id="updateAfterServiceRecord" parameterType="AfterServiceRecord">
        update after_service_record
        <trim prefix="SET" suffixOverrides=",">
            <if test="customerReasonType != null">customer_reason_type = #{customerReasonType},</if>
            <if test="customerReason != null">customer_reason = #{customerReason},</if>
            <if test="orderDetailId != null">order_detail_id = #{orderDetailId},</if>
            <if test="operType != null">oper_type = #{operType},</if>
            <if test="originalRefund != null">original_refund = #{originalRefund},</if>
            <if test="refundApplyTime != null">refund_apply_time = #{refundApplyTime},</if>
            <if test="workerFeedbackResult != null">worker_feedback_result = #{workerFeedbackResult},</if>
            <if test="workerFeedbackReasonType != null">worker_feedback_reason_type = #{workerFeedbackReasonType},</if>
            <if test="workerFeedbackReason != null">worker_feedback_reason = #{workerFeedbackReason},</if>
            <if test="refund != null">refund = #{refund},</if>
            <if test="agreedRefund != null">agreed_refund = #{agreedRefund},</if>
            <if test="customerFinalCheck != null">customer_final_check = #{customerFinalCheck},</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="remark != null">remark = #{remark},</if>
            update_time = NOW()
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteAfterServiceRecordById" parameterType="String">
        delete from after_service_record where customer_final_check != 1 and id = #{id}
    </delete>

    <delete id="deleteAfterServiceRecordByIds" parameterType="String">
        delete from after_service_record where (customer_final_check is NULL or customer_final_check = 0) and id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>

</mapper>
