<?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.yami.shop.dao.OfflineHandleEventMapper">

    <resultMap id="offlineHandleEventMap" type="com.yami.shop.bean.model.OfflineHandleEvent">
        <id column="event_id" property="eventId"/>
        <result column="handle_type" property="handleType"/>
        <result column="handle_id" property="handleId"/>
        <result column="shop_id" property="shopId"/>
        <result column="handler_id" property="handlerId"/>
        <result column="status" property="status"/>
        <result column="offline_reason" property="offlineReason"/>
        <result column="event_create_time" property="createTime"/>
        <result column="update_time" property="updateTime"/>
        <result column="handler" property="handler"/>
        <collection property="offlineHandleEventItemList" javaType="list"
                    ofType="com.yami.shop.bean.model.OfflineHandleEventItem">
            <id column="event_item_id" property="eventItemId"/>
            <result column="event_id" property="eventId"/>
            <result column="reapply_reason" property="reapplyReason"/>
            <result column="refuse_reason" property="refuseReason"/>
            <result column="item_create_time" property="createTime"/>
            <result column="reapply_time" property="reapplyTime"/>
            <result column="audit_time" property="auditTime"/>
        </collection>
    </resultMap>
    <sql id="event_item_sql">
         ei.event_item_id,
         ei.event_id as item_event_id,
         ei.reapply_reason,
         ei.refuse_reason,
         ei.create_time as item_create_time,
         ei.reapply_time,
         ei.audit_time
    </sql>
    <sql id="event_sql">
        e.event_id ,
        e.handle_type,
        e.shop_id,
        e.handler_id,
        e.status,
        e.offline_reason,
        e.create_time as event_create_time,
        e.update_time
    </sql>

    <update id="updateToSuccess">
      UPDATE tz_offline_handle_event ohe SET ohe.status = 3,ohe.handler_id = #{sysUserId},ohe.update_time = now()
      WHERE ohe.shop_id = #{shopId} AND ohe.handle_type = #{handleType} AND ohe.handle_id = #{handleId}
    </update>

    <select id="getProcessingEventByHandleTypeAndHandleId" resultMap="offlineHandleEventMap">
        SELECT
        u.`username`as handler,
        <include refid="event_sql"/>,
        <include refid="event_item_sql"/>
        FROM tz_offline_handle_event e
        LEFT JOIN tz_offline_handle_event_item ei ON e.`event_id` = ei.`event_id`
        LEFT JOIN `tz_sys_user` u ON u.`user_id` = e.`handler_id`
        WHERE e.handle_type =#{handleType} AND e.status != 3 AND e.`handle_id` = #{handleId}
    </select>
    <update id="updateToApply">
      UPDATE tz_offline_handle_event e SET e.`update_time` = NOW(),e.`status` = 2
      WHERE e.`event_id` = #{eventId}
    </update>
</mapper>
