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

    <resultMap type="com.ho.common.tools.entity.OrderForm" id="OrderForm">
        <id property="id" column="id"/>
        <result property="orderNum" column="order_num"/>
        <result property="stationId" column="station_id"/>
        <result property="userId" column="user_id"/>
        <result property="eventId" column="event_id"/>
        <result property="deviceId" column="device_id"/>
        <result property="title" column="title"/>
        <result property="remark" column="remark"/>
        <result property="plannedTime" column="planned_time"/>
        <result property="createTime" column="create_time"/>
        <result property="priority" column="priority"/>
        <result property="orderType" column="order_type"/>
        <result property="status" column="status"/>
        <result property="suggestion" column="suggestion"/>
        <result property="processInstanceId" column="processInstance_id"/>
        <result property="processInstanceKey" column="processInstance_key"/>
        <collection property="orderFiles" javaType="java.util.List" resultMap="orderFile"/>
    </resultMap>


    <resultMap type="com.ho.common.tools.entity.file.OrderFile" id="orderFile">
        <id property="url" column="url"/>
        <result property="fileName" column="file_name"/>
        <result property="orderFormId" column="order_form_id"/>
        <result property="sort" column="sort"/>
    </resultMap>

    <insert id="insertOrderFormOne" useGeneratedKeys="true" keyProperty="id">
        insert into order_form
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="orderNum != null ">order_num,</if>
            <if test="stationId != null ">station_id,</if>
            <if test="userId != null ">user_id,</if>
            <if test="eventId != null ">event_id,</if>
            <if test="deviceId != null ">device_id,</if>
            <if test="title != null ">title,</if>
            <if test="remark != null ">remark,</if>
            <if test="plannedTime != null ">planned_time,</if>
            <if test="createTime != null ">create_time,</if>
            <if test="priority != null ">priority,</if>
            <if test="orderType != null ">order_type,</if>
            <if test="processInstanceId != null ">processInstance_id,</if>
            <if test="processInstanceKey != null ">processInstance_key,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="orderNum != null ">#{orderNum},</if>
            <if test="stationId != null ">#{stationId},</if>
            <if test="userId != null ">#{userId},</if>
            <if test="eventId != null ">#{eventId},</if>
            <if test="deviceId != null ">#{deviceId},</if>
            <if test="title != null ">#{title},</if>
            <if test="remark != null ">#{remark},</if>
            <if test="plannedTime != null ">#{plannedTime},</if>
            <if test="createTime != null ">#{createTime},</if>
            <if test="priority != null ">#{priority},</if>
            <if test="orderType != null ">#{orderType},</if>
            <if test="processInstanceId != null ">#{processInstanceId},</if>
            <if test="processInstanceKey != null ">#{processInstanceKey},</if>
        </trim>
    </insert>
    <insert id="insertOrderFileOne">
        insert into order_file
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="url != null ">url,</if>
            <if test="fileName != null ">file_name,</if>
            <if test="orderFormId != null ">order_form_id,</if>
            <if test="sort != null ">sort,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="url != null ">#{url},</if>
            <if test="fileName != null ">#{fileName},</if>
            <if test="orderFormId != null ">#{orderFormId},</if>
            <if test="sort != null ">#{sort},</if>
        </trim>
    </insert>

    <delete id="deleteFile">
        delete
        from order_file
        where order_form_id = #{orderFormId}
    </delete>
    <update id="update">
        update order_form
        <set>
            <if test="orderNum != null ">order_num = #{orderNum},</if>
            <if test="stationId != null ">station_id = #{stationId},</if>
            <if test="userId != null ">user_id = #{userId},</if>
            <if test="eventId != null ">event_id = #{eventId},</if>
            <if test="deviceId != null ">device_id = #{deviceId},</if>
            <if test="title != null ">title = #{title},</if>
            <if test="remark != null ">remark = #{remark},</if>
            <if test="priority != null ">priority = #{priority},</if>
            <if test="plannedTime != null ">planned_time = #{plannedTime},</if>
            <if test="createTime != null ">create_time = #{createTime},</if>
            <if test="orderType != null ">order_type = #{orderType},</if>
            <if test="processInstanceId != null ">processInstance_id = #{processInstanceId},</if>
            <if test="processInstanceKey != null ">processInstance_key = #{processInstanceKey},</if>
        </set>
        where id = #{id}
    </update>
    <update id="updateStatus">
        update order_form
        <set>
            <if test="id != null">
                status = 1,
            </if>
            <if test="suggestion != null and suggestion != ''">
                suggestion = #{suggestion},
            </if>
        </set>
        where id = #{id}
    </update>


    <select id="selectByProcessInstanceId" resultMap="OrderForm">
        select form.*,file.url url ,file.file_name file_name,file.order_form_id order_form_id,file.sort sort
        from order_form form
        left join order_file file on form.id = file.order_form_id
        <where>
            <if test="processInstanceId != null and processInstanceId != ''">
                form.processInstance_id = #{processInstanceId}
            </if>
        </where>
    </select>
    <select id="selectByUserId" resultMap="OrderForm">
        select form.*,file.url url ,file.file_name file_name,file.order_form_id order_form_id,file.sort sort
        from order_form form
        left join order_file file on form.id = file.order_form_id
        <where>
            <if test="userId != null and userId != ''">
               and form.user_id = #{userId}
            </if>
            <if test="userId != null and userId != ''">
                and form.status = 0
            </if>
        </where>
        limit 1
    </select>

    <select id="selectByOrderId" resultMap="OrderForm">
        select form.*,file.url url ,file.file_name file_name,file.order_form_id order_form_id,file.sort sort
        from order_form form
        left join order_file file on form.id = file.order_form_id
        where form.id = #{orderFormId}
    </select>

    <select id="selectFinishByUserId" resultMap="OrderForm">
        select form.*,file.url url ,file.file_name file_name,file.order_form_id order_form_id,file.sort sort
        from order_form form
        left join order_file file on form.id = file.order_form_id
        <where>
            <if test="userId != null and userId != ''">
                and form.user_id = #{userId}
            </if>
            <if test="userId != null and userId != ''">
                and form.status = 1
            </if>
        </where>
    </select>

    <select id="getInspectionOrders" resultType="com.ho.common.tools.entity.OrderForm">
        select  form.* from order_form form where id in (
        select distinct a.order_id from business_db.inspection_fault a , business_db.inspection_report  b
        where a.report_id = b.id and a.order_id is not null and b.station_id =#{stationId})
    </select>

    <select id="selectOrderFormList" resultType="com.ho.common.tools.entity.OrderForm">
        select form.*,file.url url ,file.file_name file_name,file.order_form_id order_form_id,file.sort sort
        from order_form form
        left join order_file file on form.id = file.order_form_id
        <where>
            <if test="userId != null and userId != ''">
                and form.user_id = #{userId}
            </if>
            <if test="userId != null and userId != ''">
                and form.status = 0
            </if>
        </where>
    </select>

</mapper>