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


    <sql id="Base_Column_List">
        id
        ,plan_title,title,
        planned_time,create_time,update_time,
        priority,phone,device_id,
        station_id,user_id,`desc`,order_id,
        process_id,group_id,event_id,order_type,deleted
    </sql>

    <select id="selectByPrimaryKey" resultType="com.ho.flow.entity.WorkOrder">
        select
        <include refid="Base_Column_List"/>
        from work_order
        where id = #{id}
    </select>

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

    <select id="selectByOrderIdAndTitle" resultType="com.ho.flow.vo.resp.workorder.WorkOrderRespVO">
        select
        <include refid="Base_Column_List"/>
        from work_order
        where order_id = #{orderId}
        <if test="title != null and title != ''">
            and title LIKE CONCAT('%',#{title},'%')
        </if>
    </select>

    <select id="selectByProcessInstId" resultType="com.ho.flow.vo.resp.workorder.WorkOrderRespVO">
        select
        <include refid="Base_Column_List"/>
        from work_order
        where process_id = #{processInstId}
    </select>

    <select id="selectByProcessInstIdAndTitle" resultType="com.ho.flow.vo.resp.workorder.WorkOrderRespVO">
        select
        <include refid="Base_Column_List"/>
        from work_order
        where process_id = #{processInstanceId}
        <if test="title != null and title != ''">
            and title LIKE CONCAT('%',#{title},'%')
        </if>
    </select>

    <select id="selectByInfo" resultType="com.ho.flow.vo.resp.workorder.WorkOrderRespVO">
        select
        <include refid="Base_Column_List"/>
        from work_order
        <where>
            <if test="stationId !=null">
                and station_id = #{stationId}
            </if>
            <if test="processId !=null and processId != ''">
                and process_id = #{processId}
            </if>
            <if test="orderType !=null ">
                and order_type = #{orderType}
            </if>
            <if test="orderId !=null and orderId != ''">
                and order_id LIKE CONCAT('%',#{orderId},'%')
            </if>

            <if test="orderIds != null and orderIds.size() !=0">
                and order_id in
                <foreach collection="orderIds" item="item" open="(" close=")" separator=",">
                    #{item}
                </foreach>
            </if>
        </where>
    </select>
    <select id="selectByStationId" resultType="java.lang.Integer">
        select count(1)
        from work_order
        where station_id = #{stationId}
          and deleted = 1
    </select>


    <delete id="deleteByPrimaryKey">
        delete
        from work_order
        where id = #{id,jdbcType=INTEGER}
    </delete>

    <delete id="deleteByIds">
        delete
        from work_order
        where id in
        <foreach collection="ids" item="item" open="(" close=")" separator=",">
            #{item}
        </foreach>
    </delete>
    <insert id="insert" keyColumn="id" keyProperty="id" parameterType="com.ho.flow.entity.WorkOrder"
            useGeneratedKeys="true">
        insert into work_order
        ( id, plan_title, title
        , planned_time, create_time, update_time
        , priority, phone, device_id
        , station_id, `desc`, order_id
        , process_id, group_id, event_id, order_type)
        values ( #{id}, #{planTitle,jdbcType=VARCHAR}, #{title,jdbcType=VARCHAR}
               , #{plannedTime,jdbcType=TIMESTAMP}, #{createTime,jdbcType=TIMESTAMP}, #{updateTime,jdbcType=TIMESTAMP}
               , #{priority,jdbcType=INTEGER}, #{phone}, #{deviceId,jdbcType=INTEGER}
               , #{stationId,jdbcType=INTEGER}, #{desc,jdbcType=VARCHAR}, #{orderId,jdbcType=VARCHAR}
               , #{processId,jdbcType=VARCHAR}, #{groupId,jdbcType=INTEGER}, #{eventId}, #{orderType,jdbcType=INTEGER})
    </insert>
    <insert id="insertSelective" parameterType="com.ho.flow.entity.WorkOrder">
        insert into work_order
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="id != null">id,</if>
            <if test="planTitle != null">plan_title,</if>
            <if test="title != null">title,</if>
            <if test="plannedTime != null">planned_time,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="priority != null">priority,</if>
            <if test="phone != null">phone,</if>
            <if test="deviceId != null">device_id,</if>
            <if test="stationId != null">station_id,</if>
            <if test="userId != null">user_id,</if>
            <if test="desc != null">`desc`,</if>
            <if test="orderId != null">order_id,</if>
            <if test="processId != null">process_id,</if>
            <if test="groupId != null">group_id,</if>
            <if test="eventId != null">event_id,</if>
            <if test="orderType != null">order_type,</if>
            <if test="deleted != null">deleted,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="id != null">#{id},</if>
            <if test="planTitle != null">#{planTitle,jdbcType=VARCHAR},</if>
            <if test="title != null">#{title,jdbcType=VARCHAR},</if>
            <if test="plannedTime != null">#{plannedTime,jdbcType=TIMESTAMP},</if>
            <if test="createTime != null">#{createTime,jdbcType=TIMESTAMP},</if>
            <if test="updateTime != null">#{updateTime,jdbcType=TIMESTAMP},</if>
            <if test="priority != null">#{priority,jdbcType=INTEGER},</if>
            <if test="phone != null">#{phone},</if>
            <if test="deviceId != null">#{deviceId,jdbcType=INTEGER},</if>
            <if test="stationId != null">#{stationId,jdbcType=INTEGER},</if>
            <if test="userId != null">#{userId},</if>
            <if test="desc != null">#{desc,jdbcType=VARCHAR},</if>
            <if test="orderId != null">#{orderId,jdbcType=VARCHAR},</if>
            <if test="processId != null">#{processId,jdbcType=VARCHAR},</if>
            <if test="groupId != null">#{groupId,jdbcType=INTEGER},</if>
            <if test="eventId != null">#{eventId},</if>
            <if test="orderType != null">#{orderType,jdbcType=INTEGER},</if>
            <if test="deleted != null">#{deleted},</if>
        </trim>
    </insert>
    <update id="updateByPrimaryKeySelective" parameterType="com.ho.flow.entity.WorkOrder">
        update work_order
        <set>
            <if test="planTitle != null">
                plan_title = #{planTitle,jdbcType=VARCHAR},
            </if>
            <if test="title != null">
                title = #{title,jdbcType=VARCHAR},
            </if>
            <if test="plannedTime != null">
                planned_time = #{plannedTime,jdbcType=TIMESTAMP},
            </if>
            <if test="createTime != null">
                create_time = #{createTime,jdbcType=TIMESTAMP},
            </if>
            <if test="updateTime != null">
                update_time = #{updateTime,jdbcType=TIMESTAMP},
            </if>
            <if test="priority != null">
                priority = #{priority,jdbcType=INTEGER},
            </if>
            <if test="phone != null">
                phone = #{phone},
            </if>
            <if test="deviceId != null">
                device_id = #{deviceId,jdbcType=INTEGER},
            </if>
            <if test="stationId != null">
                station_id = #{stationId,jdbcType=INTEGER},
            </if>
            <if test="desc != null">
                `desc` = #{desc,jdbcType=VARCHAR},
            </if>
            <if test="orderId != null">
                order_id = #{orderId,jdbcType=VARCHAR},
            </if>
            <if test="processId != null">
                process_id = #{processId,jdbcType=VARCHAR},
            </if>
            <if test="groupId != null">
                group_id = #{groupId,jdbcType=INTEGER},
            </if>
            <if test="eventId != null">
                event_id = #{eventId},
            </if>
            <if test="orderType != null">
                order_type = #{orderType,jdbcType=INTEGER},
            </if>
            <if test="deleted != null">
                deleted = #{deleted},
            </if>
        </set>

        where id = #{id}
    </update>
    <update id="updateByPrimaryKey" parameterType="com.ho.flow.entity.WorkOrder">
        update work_order
        set plan_title   = #{planTitle,jdbcType=VARCHAR},
            title        = #{title,jdbcType=VARCHAR},
            planned_time = #{plannedTime,jdbcType=TIMESTAMP},
            create_time  = #{createTime,jdbcType=TIMESTAMP},
            update_time  = #{updateTime,jdbcType=TIMESTAMP},
            priority     = #{priority,jdbcType=INTEGER},
            phone        = #{phone,jdbcType=INTEGER},
            device_id    = #{deviceId,jdbcType=INTEGER},
            station_id   = #{stationId,jdbcType=INTEGER},
            `desc`       = #{desc,jdbcType=VARCHAR},
            order_id     = #{orderId,jdbcType=VARCHAR},
            process_id   = #{processId,jdbcType=VARCHAR} group_id     = #{groupId,jdbcType=INTEGER}
            event_id = #{eventId}
            order_type = #{orderType,jdbcType=INTEGER}
        where id = #{id}
    </update>
    <update id="setEventNull">
        update work_order
        set event_id = null
    </update>
</mapper>
