<?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.ho.flow.mapper.WorkOrderHisMapper">


    <sql id="Base_Column_List">
        `id`
        , `order_id`, `group_id`, `dept_id`, `station_id`,
        `from_id`, `phone`, `to_id`, `event_id`, `event_level`, `desc`, `create_time`,
        `receive_time`, `complete_time`, `work_order_status`, `work_order_type`
    </sql>

    <select id="selectByPrimaryKey" parameterType="java.lang.Long" resultType="com.ho.flow.vo.WorkOrderOld">
        select
        <include refid="Base_Column_List"/>
        from work_order
        where id = #{id,jdbcType=BIGINT}
    </select>

    <select id="selectByOrderId" resultType="com.ho.flow.vo.WorkOrderOld">
        select
        <include refid="Base_Column_List"/>
        from work_order
        where order_id = #{orderId}
    </select>

    <select id="selectByStatus" resultType="com.ho.flow.vo.WorkOrderOld">
        select
        <include refid="Base_Column_List"/>
        from work_order
        where order_id = #{orderId}
    </select>

    <select id="selectByPageInfo" resultType="com.ho.flow.vo.resp.workorder.WorkOrderOldPageRespVO">
        select
        <include refid="Base_Column_List"/>
        from work_order
        <where>
            and work_order_status != 4
            <if test="orderId != null and orderId != ''">
                and order_id LIKE concat('%',#{orderId},'%')
            </if>
            <if test="groupId != null">
                and group_id = #{groupId}
            </if>
            <if test="stationId != null">
                and station_id = #{stationId}
            </if>
            <if test="stationIds != null and stationIds.size() !=0">
                and station_id in
                <foreach collection="stationIds" item="item" open="(" close=")" separator=",">
                    #{item}
                </foreach>
            </if>
            <if test="orderIds != null and orderIds.size() !=0">
                and order_id in
                <foreach collection="orderIds" item="item" open="(" close=")" separator=",">
                    #{item}
                </foreach>
            </if>
            <if test="eventLevel != null">
                and event_level = #{eventLevel}
            </if>
            <if test="workOrderStatus != null">
                and work_order_status = #{workOrderStatus}
            </if>
            <if test="workOrderType != null">
                and work_order_type = #{workOrderType}
            </if>
            <if test="beginTime != null and endTime != null">
                and create_time between #{beginTime} and #{endTime}
            </if>
            <if test="toIds != null and toIds.size() != 0">
                and to_id in
                <foreach collection="toIds" open="(" close=")" separator="," item="item">
                    #{item}
                </foreach>
            </if>
            <if test="roleLeader != null and roleLeader != ''">
                and (to_id = #{toId}
                or from_id = #{fromId})
            </if>
            <if test="roleWorker != null and roleWorker != ''">
                and (to_id = #{toId}
                or from_id = #{fromId})
            </if>
            <if test="roleOther != null and roleOther != ''">
                and (to_id = #{toId}
                or from_id = #{fromId})
            </if>

            order by create_time desc,receive_time desc
        </where>
    </select>

    <select id="selectByHisPageInfo" resultType="com.ho.flow.vo.resp.workorder.WorkOrderOldPageRespVO">
        select
        <include refid="Base_Column_List"/>
        from work_order
        <where>
            work_order_status = 4
            <if test="orderId != null and orderId != ''">
                and order_id LIKE concat('%',#{orderId},'%')
            </if>
            <if test="stationId != null">
                and station_id = #{stationId}
            </if>
            <if test="stationIds != null and stationIds.size() !=0">
                and station_id in
                <foreach collection="stationIds" item="item" open="(" close=")" separator=",">
                    #{item}
                </foreach>
            </if>
            <if test="eventLevel != null">
                and event_level = #{eventLevel}
            </if>
            <if test="workOrderStatus != null">
                and work_order_status = #{workOrderStatus}
            </if>
            <if test="workOrderType != null">
                and work_order_type = #{workOrderType}
            </if>
            <if test="beginTime != null and endTime != null">
                and create_time between #{beginTime} and #{endTime}
            </if>
            order by create_time desc,receive_time desc
        </where>
    </select>

    <select id="selectByEventId" resultType="com.ho.flow.vo.WorkOrderOld">
        select
        <include refid="Base_Column_List"/>
        from work_order
        where event_id = #{eventId,jdbcType=BIGINT}
    </select>
    <select id="selectByEventIds" resultType="com.ho.flow.vo.WorkOrderOld">
        select
        <include refid="Base_Column_List"/>
        from work_order
        where event_id in
        <foreach collection="eventIds" item="eventId" open="(" separator="," close=")">
            #{eventId}
        </foreach>
    </select>

    <select id="selectByOrderIds" resultType="com.ho.flow.vo.resp.workorder.WorkOrderOldPageRespVO">
        select
        <include refid="Base_Column_List"/>
        from work_order
        where order_id in
        <foreach collection="orderIds" item="item" open="(" separator="," close=")">
            #{item}
        </foreach>
    </select>


    <delete id="deleteByPrimaryKey">
        Delete
        from work_order
        where order_id in
        <foreach collection="orderIds" item="orderId" open="(" separator="," close=")">
            #{orderId}
        </foreach>
    </delete>

    <delete id="delete">
        Delete
        from work_order
        where order_id = #{orderId}
    </delete>

    <insert id="insertSelective" keyColumn="id" keyProperty="id" parameterType="com.ho.flow.vo.WorkOrderOld"
            useGeneratedKeys="true">
        insert into work_order
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="id != null">id,</if>
            <if test="orderId != null">order_id,</if>
            <if test="groupId != null">group_id,</if>
            <if test="deptId != null">dept_id,</if>
            <if test="stationId != null">station_id,</if>
            <if test="fromId != null">from_id,</if>
            <if test="phone != null">phone,</if>
            <if test="toId != null">to_id,</if>
            <if test="eventId != null">event_id,</if>
            <if test="eventLevel != null">event_level,</if>
            <if test="desc != null">`desc`,</if>
            <if test="createTime != null">create_time,</if>
            <if test="receiveTime != null">receive_time,</if>
            <if test="completeTime != null">complete_time,</if>
            <if test="workOrderStatus != null">work_order_status,</if>
            <if test="workOrderType != null">work_order_type</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="id != null">#{id,jdbcType=BIGINT},</if>
            <if test="orderId != null">#{orderId,jdbcType=VARCHAR},</if>
            <if test="groupId != null">#{groupId,jdbcType=INTEGER},</if>
            <if test="deptId != null">#{deptId,jdbcType=INTEGER},</if>
            <if test="stationId != null">#{stationId,jdbcType=INTEGER},</if>
            <if test="fromId != null">#{fromId,jdbcType=VARCHAR},</if>
            <if test="phone != null">#{phone,jdbcType=VARCHAR},</if>
            <if test="toId != null">#{toId,jdbcType=VARCHAR},</if>
            <if test="eventId != null">#{eventId,jdbcType=BIGINT},</if>
            <if test="eventLevel != null">#{eventLevel,jdbcType=INTEGER},</if>
            <if test="desc != null">#{desc,jdbcType=VARCHAR},</if>
            <if test="createTime != null">#{createTime,jdbcType=TIMESTAMP},</if>
            <if test="receiveTime != null">#{receiveTime,jdbcType=TIMESTAMP},</if>
            <if test="completeTime != null">#{completeTime,jdbcType=TIMESTAMP},</if>
            <if test="workOrderStatus != null">#{workOrderStatus,jdbcType=INTEGER},</if>
            <if test="workOrderType != null">#{workOrderType,jdbcType=INTEGER},</if>
        </trim>
    </insert>

    <insert id="insertBatch">
        insert into work_order
        ( id,order_id, group_id,
        dept_id, station_id, from_id,
        phone, to_id, event_id, event_level,
        `desc`, receive_time, complete_time,
        work_order_status, work_order_type)
        values
        <foreach item="item" collection="list" index="index" separator=",">
            (
            #{item.id},
            #{item.orderId},#{item.stationId},#{item.deptId},#{item.groupId},
            #{item.fromId},#{item.phone},#{item.toId},
            #{item.eventId},#{item.eventLevel},#{item.desc},
            #{item.receiveTime},#{item.completeTime},
            #{item.workOrderStatus},#{item.workOrderType}
            )
        </foreach>
    </insert>

    <update id="updateReturnWorkOrder">
        update work_order
        <set>
            <if test="workOrderStatus != null">
                work_order_status = #{workOrderStatus,jdbcType=INTEGER},
            </if>
            <if test="phone != null">
                phone = null,
            </if>
            <if test="toId != null">
                to_id = null,
            </if>
            <if test="receiveTime != null">
                receive_time = null,
            </if>
        </set>
        where order_id = #{orderId}
    </update>

    <update id="updateByPrimaryKeySelective">
        update work_order
        <set>
            <if test="groupId != null">
                group_id = #{groupId,jdbcType=INTEGER},
            </if>
            <if test="deptId != null">
                dept_id = #{deptId,jdbcType=INTEGER},
            </if>
            <if test="stationId != null">
                station_id = #{stationId,jdbcType=INTEGER},
            </if>
            <if test="fromId != null">
                from_id = #{fromId,jdbcType=VARCHAR},
            </if>
            <if test="phone != null">
                phone = #{phone,jdbcType=VARCHAR},
            </if>
            <if test="toId != null">
                to_id = #{toId,jdbcType=VARCHAR},
            </if>
            <if test="eventId != null">
                event_id = #{eventId,jdbcType=BIGINT},
            </if>
            <if test="eventLevel != null">
                event_level = #{eventLevel,jdbcType=INTEGER},
            </if>
            <if test="desc != null">
                `desc` = #{desc,jdbcType=VARCHAR},
            </if>
            <if test="createTime != null">
                create_time = #{createTime,jdbcType=TIMESTAMP},
            </if>
            <if test="receiveTime != null">
                receive_time = #{receiveTime,jdbcType=TIMESTAMP},
            </if>
            <if test="completeTime != null">
                complete_time = #{completeTime,jdbcType=TIMESTAMP},
            </if>
            <if test="workOrderStatus != null">
                work_order_status = #{workOrderStatus,jdbcType=INTEGER},
            </if>
            <if test="workOrderType != null">
                work_order_type = #{workOrderType,jdbcType=INTEGER}
            </if>
        </set>
        where order_id = #{orderId}
    </update>

    <update id="updateByPrimaryKey" parameterType="com.ho.flow.vo.WorkOrderOld">
        update work_order
        set group_id          = #{groupId,jdbcType=INTEGER},
            dept_id           = #{deptId,jdbcType=INTEGER},
            station_id        = #{stationId,jdbcType=INTEGER},
            from_id           = #{fromId,jdbcType=VARCHAR},
            phone             = #{phone,jdbcType=VARCHAR},
            to_id             = #{toId,jdbcType=VARCHAR},
            event_id          = #{eventId,jdbcType=BIGINT},
            event_level       = #{eventLevel,jdbcType=INTEGER},
            `desc`            = #{desc,jdbcType=VARCHAR},
            create_time       = #{createTime,jdbcType=TIMESTAMP},
            receive_time      = #{receiveTime,jdbcType=TIMESTAMP},
            complete_time     = #{completeTime,jdbcType=TIMESTAMP},
            work_order_status = #{workOrderStatus,jdbcType=INTEGER},
            work_order_order  = #{workOrderType,jdbcType=INTEGER}
        where order_id = #{orderId,jdbcType=VARCHAR}
    </update>
</mapper>
