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

    <resultMap id="OrderFineRecordResult" type="com.ghy.payment.domain.OrderTimeoutRecord">
        <id property="id"                   column="id"/>
        <result property="orderDetailId"    column="order_detail_id"/>
        <result property="workerId"         column="worker_id"/>
        <result property="deptId"           column="dept_id"/>
        <result property="fineStatus"       column="fine_status"/>
        <result property="orderStatus"      column="order_status"/>
        <result property="payMoney"         column="pay_money"/>
        <result property="createTime"       column="create_time"/>
    </resultMap>

    <sql id="selectOrderFineRecord">
        SELECT id, order_detail_id, worker_id, dept_id, fine_status, order_status, pay_money, create_time
        FROM order_timeout_record
    </sql>

    <select id="selectByDetailIdAndStatus" resultMap="OrderFineRecordResult">
        <include refid="selectOrderFineRecord"/>
        WHERE order_detail_id = #{orderDetailId} AND order_status = #{orderStatus} order by create_time desc
    </select>

    <select id="selectList" parameterType="com.ghy.payment.domain.OrderTimeoutRecord"
            resultMap="OrderFineRecordResult">
        <include refid="selectOrderFineRecord"/>
        <where>
            <if test="orderDetailId != null">
                AND order_detail_id = #{orderDetailId}
            </if>
            <if test="workerId != null">
                AND worker_id = #{workerId}
            </if>
            <if test="orderDetailIds != null and orderDetailIds != ''">
                AND order_detail_id IN
                <foreach collection="orderDetailIds" item="orderDetailId" open="(" separator="," close=")">
                    #{orderDetailId}
                </foreach>
            </if>
            <if test="orderStatus != null">
                AND order_status = #{orderStatus}
            </if>
        </where>
    </select>

    <select id="selectUnFine" resultMap="OrderFineRecordResult">
        <include refid="selectOrderFineRecord"/>
        WHERE worker_id = #{workerId}
        AND dept_id = #{deptId}
        AND fine_status = 0
        ORDER BY create_time
    </select>

    <insert id="insert" parameterType="com.ghy.payment.domain.OrderTimeoutRecord" useGeneratedKeys="true" keyProperty="id">
        INSERT INTO order_timeout_record (order_detail_id, worker_id, dept_id, order_status, pay_money)
        VALUES (#{orderDetailId}, #{workerId}, #{deptId}, #{orderStatus}, #{payMoney})
    </insert>

<!--    <update id="update">-->
<!--        UPDATE order_timeout_record SET fine_status = #{fineStatus}-->
<!--        WHERE id = #{id}-->
<!--    </update>-->

    <update id="updateFineStatus">
        UPDATE order_timeout_record SET fine_status = #{fineStatus}
        WHERE id = #{id}
    </update>

</mapper>
