<?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"    />
        <result property="address"    column="address"    />
    </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>
        order by t.allot_time desc
    </select>
    <select id="selectWaitingWorkOrder" resultMap="TWorkOrderResult">
        SELECT o.*,IF(o.`order_type`=1,IFNULL(p.`address`,""),IFNULL(t.`address`,"")) AS address FROM t_work_order o
        LEFT JOIN t_hidden_trouble t ON t.trouble_id = o.resource_id
        LEFT JOIN t_inspection_plan p ON p.plan_id = o.resource_id
        WHERE
        <choose>
            <when test="type==1">
                <choose>
                    <when test="waitingtype==0">
                        o.order_status &lt; 2
                    </when>
                    <when test="waitingtype==1">
                        o.order_status = 0
                    </when>
                    <otherwise>
                        o.order_status = 1
                    </otherwise>
                </choose>
            </when>
            <otherwise>
                 o.order_status = #{waitingtype}
            </otherwise>
        </choose>
        <if test="searchStr!=null and searchStr!=''">
            AND o.order_name LIKE concat('%',#{searchStr},'%')
        </if>
        and (o.appoint_inspector is null  OR o.appoint_inspector = #{userId})
        order by o.allot_time desc
    </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>
    <select id="orderdtail" resultType="com.zehong.system.domain.TWorkOrder">
        SELECT t.*,
        <choose>
            <when test='type=="3"'>
                p.device_addr as address,
            </when>
            <otherwise>
                p.address AS address,
            </otherwise>
        </choose>
        <choose>
            <when test='type=="1"'>
                "" as longitude,
            </when>
            <otherwise>
                p.longitude as longitude,
            </otherwise>
        </choose>
        <choose>
            <when test='type=="1"'>
                "" as latitude,
            </when>
            <otherwise>
                p.latitude as latitude,
            </otherwise>
        </choose>
        <choose>
            <when test='type!="2"'>
                "" as pictureUrl
            </when>
            <otherwise>
                p.picture_url as pictureUrl
            </otherwise>
        </choose>
        FROM (SELECT order_id as orderId,order_name as orderName,order_type as orderType,
        order_status as orderStatus,allot_time as allotTime,remarks,resource_id as resourceId
        FROM t_work_order WHERE order_id=#{orderId})t
        <choose>
            <when test='type=="1"'>
                LEFT JOIN t_inspection_plan p ON p.plan_id = t.resourceId
            </when>
            <when test='type=="2"'>
                LEFT JOIN t_hidden_trouble p ON p.trouble_id = t.resourceId
            </when>
            <otherwise>
                LEFT JOIN t_device_alarm  a ON a.alarm_id = t.resourceId
                LEFT JOIN t_device_info p ON p.device_id = a.`device_id`
            </otherwise>
        </choose>

    </select>
</mapper>