<?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.zehong.system.mapper.TWorkOrderMapper">
    
    <resultMap type="TWorkOrder" id="TWorkOrderResult">
        <result property="orderId"    column="order_id"    />
        <result property="orderType"    column="order_type"    />
        <result property="resourceId"    column="resource_id"    />
        <result property="orderName"    column="order_name"    />
        <result property="orderStatus"    column="order_status"    />
        <result property="archiving"    column="archiving"    />
        <result property="updateTime"    column="update_time"    />
        <result property="appointInspector"    column="appoint_inspector"    />
        <result property="allotTime"    column="allot_time"    />
        <result property="actualInspector"    column="actual_inspector"    />
        <result property="actualTime"    column="actual_time"    />
        <result property="remarks"    column="remarks"    />
    </resultMap>

    <select id="getWorkOrderId" resultType="String">
        select nextval('seq_work_order_id') as id
    </select>

    <sql id="selectTWorkOrderVo">
        select t.order_id, t.order_type, t.resource_id, t.order_name, t.order_status, t.archiving, t.update_time, t.appoint_inspector, t.allot_time, t.actual_inspector, t.actual_time, t.remarks
        from t_work_order t
        left join sys_user s on t.appoint_inspector = s.user_id
    </sql>

    <select id="selectTWorkOrderList" parameterType="TWorkOrderForm" resultMap="TWorkOrderResult">
        <include refid="selectTWorkOrderVo"/>
        <where>
            <if test="orderId != null  and orderId != ''"> and t.order_id like concat('%',#{orderId},'%')</if>
            <if test="orderType != null  and orderType != ''"> and t.order_type = #{orderType}</if>
            <if test="orderName != null  and orderName != ''"> and t.order_name like concat('%', #{orderName}, '%')</if>
            <if test="orderStatus != null  and orderStatus != ''"> and t.order_status = #{orderStatus}</if>
            <if test="appointInspector != null "> and t.appoint_inspector = #{appointInspector}</if>
            <if test="startAllotTime != null "> and t.allot_time &gt;= #{startAllotTime}</if>
            <if test="endAllotTime != null "> and t.allot_time &lt;= #{endAllotTime}</if>
            <if test="userName != null  and userName != ''"> and s.user_name = #{userName}</if>
        </where>
    </select>
    
    <select id="selectTWorkOrderById" parameterType="String" resultMap="TWorkOrderResult">
        <include refid="selectTWorkOrderVo"/>
        where t.order_id = #{orderId}
    </select>
        
    <insert id="insertTWorkOrder" parameterType="TWorkOrder">
        insert into t_work_order
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="orderId != null">order_id,</if>
            <if test="orderType != null">order_type,</if>
            <if test="resourceId != null">resource_id,</if>
            <if test="orderName != null">order_name,</if>
            <if test="orderStatus != null">order_status,</if>
            <if test="archiving != null">archiving,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="appointInspector != null">appoint_inspector,</if>
            <if test="allotTime != null">allot_time,</if>
            <if test="actualInspector != null">actual_inspector,</if>
            <if test="actualTime != null">actual_time,</if>
            <if test="remarks != null">remarks,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="orderId != null">#{orderId},</if>
            <if test="orderType != null">#{orderType},</if>
            <if test="resourceId != null">#{resourceId},</if>
            <if test="orderName != null">#{orderName},</if>
            <if test="orderStatus != null">#{orderStatus},</if>
            <if test="archiving != null">#{archiving},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="appointInspector != null">#{appointInspector},</if>
            <if test="allotTime != null">#{allotTime},</if>
            <if test="actualInspector != null">#{actualInspector},</if>
            <if test="actualTime != null">#{actualTime},</if>
            <if test="remarks != null">#{remarks},</if>
         </trim>
    </insert>

    <update id="updateTWorkOrder" parameterType="TWorkOrder">
        update t_work_order
        <trim prefix="SET" suffixOverrides=",">
            <if test="orderType != null">order_type = #{orderType},</if>
            <if test="orderName != null">order_name = #{orderName},</if>
            <if test="orderStatus != null">order_status = #{orderStatus},</if>
            <if test="archiving != null">archiving = #{archiving},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="appointInspector != null">appoint_inspector = #{appointInspector},</if>
            <if test="allotTime != null">allot_time = #{allotTime},</if>
            <if test="actualInspector != null">actual_inspector = #{actualInspector},</if>
            <if test="actualTime != null">actual_time = #{actualTime},</if>
            <if test="remarks != null">remarks = #{remarks},</if>
        </trim>
        where order_id = #{orderId}
    </update>

    <delete id="deleteTWorkOrderById" parameterType="String">
        delete from t_work_order where order_id = #{orderId}
    </delete>

    <delete id="deleteTWorkOrderByIds" parameterType="String">
        delete from t_work_order where order_id in 
        <foreach item="orderId" collection="array" open="(" separator="," close=")">
            #{orderId}
        </foreach>
    </delete>
</mapper>