<?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.ygtx.gxt.mapper.GxtDeviceRuntimeMapper">

    <resultMap type="GxtDeviceRuntime" id="GxtDeviceRuntimeResult">
        <result property="id" column="id"/>
        <result property="deviceId" column="device_id"/>
        <result property="workOrderProjectNo" column="work_order_project_no"/>
        <result property="faultTime" column="fault_time"/>
        <result property="prevWbtime" column="prev_wbtime"/>
        <result property="statTime" column="stat_time"/>
        <result property="monthPeriod" column="month_period"/>
        <result property="scroe" column="scroe"/>
        <result property="status" column="status"/>
        <result property="createBy" column="create_by"/>
        <result property="createTime" column="create_time"/>
        <result property="updateBy" column="update_by"/>
        <result property="updateTime" column="update_time"/>
        <result property="remark" column="remark"/>
        <result property="repairOrderId" column="repair_order_id"/>
        <result property="workOrderId" column="work_order_id"/>
        <result property="orderType" column="order_type"/>
        <result property="deptId" column="dept_id"/>
        <result property="scoreId" column="score_id"/>
    </resultMap>

    <sql id="selectGxtDeviceRuntimeVo">
        select id, device_id, dept_id, score_id, work_order_project_no, fault_time, prev_wbtime, stat_time, month_period, scroe, status, create_by, create_time, update_by, update_time, remark from gxt_device_runtime
    </sql>

    <select id="selectGxtDeviceRuntimeList" parameterType="GxtDeviceRuntime" resultMap="GxtDeviceRuntimeResult">
        <include refid="selectGxtDeviceRuntimeVo"/>
        <where>
            <if test="deviceId != null "> and device_id = #{deviceId}</if>
            <if test="deptId != null "> and dept_id = #{deptId}</if>
            <if test="scoreId != null "> and score_id = #{scoreId}</if>
            <if test="workOrderProjectNo != null  and workOrderProjectNo != ''"> and work_order_project_no = #{workOrderProjectNo}</if>
            <if test="faultTime != null "> and fault_time = #{faultTime}</if>
            <if test="prevWbtime != null "> and prev_wbtime = #{prevWbtime}</if>
            <if test="statTime != null "> and stat_time = #{statTime}</if>
            <if test="monthPeriod != null  and monthPeriod != ''"> and month_period = #{monthPeriod}</if>
            <if test="scroe != null "> and scroe = #{scroe}</if>
            <if test="status != null "> and status = #{status}</if>
            <if test="createBy != null  and createBy != ''"> and create_by = #{createBy}</if>
            <if test="updateBy != null  and updateBy != ''"> and update_by = #{updateBy}</if>
            <if test="remark != null  and remark != ''"> and remark = #{remark}</if>
        </where>
        order by create_time desc
    </select>

    <select id="selectGxtDeviceRuntimeById" parameterType="Long" resultMap="GxtDeviceRuntimeResult">
        <include refid="selectGxtDeviceRuntimeVo"/>
        where id = #{id}
    </select>

    <select id="selectGxtDeviceRuntimeByRepairOrder" parameterType="GxtDeviceRuntime" resultMap="GxtDeviceRuntimeResult">
        SELECT
        rep.pcs_device_id AS device_id,
        rep.work_order_project_no,
        a.occur_time AS fault_time,
        a.prev_wbtime,
        sr.score AS scroe,
        a.id AS repair_order_id,
        a.order_type,
        sr.id AS score_id
        FROM
        (
        SELECT
        t.id,
        t.occur_time,
        t.repair_order_id,
        t.work_order_id,
        GREATEST(
        t.repair_restart_time,
        IFNULL( t.work_restart_time, t.repair_restart_time )) AS prev_wbtime,
        CASE

        WHEN t.work_restart_time IS NULL THEN
        1
        WHEN t.repair_restart_time >= t.work_restart_time THEN
        1
        WHEN t.work_restart_time > t.repair_restart_time THEN
        2 ELSE 0
        END AS order_type
        FROM
        (
        SELECT
        ro.id,
        ro.occur_time,
        repair_order.id AS repair_order_id,
        repair_order.restart_time AS repair_restart_time,
        work_order.id AS work_order_id,
        work_order.restart_time AS work_restart_time
        FROM
        gxt_repair_order ro
        LEFT JOIN gxt_repair_order repair_order ON repair_order.pcs_device_id = ro.pcs_device_id
        AND repair_order.restart_time = ( SELECT MAX( o.restart_time ) FROM gxt_repair_order o WHERE ro.occur_time > o.restart_time AND o.pcs_device_id = ro.pcs_device_id AND (o.repair_method IS NULL OR o.repair_method != '2') )
        LEFT JOIN gxt_work_order work_order ON work_order.pcs_device_id = ro.pcs_device_id
        AND work_order.restart_time = ( SELECT MAX( oo.restart_time ) FROM gxt_work_order oo WHERE ro.occur_time > oo.restart_time AND oo.pcs_device_id = ro.pcs_device_id AND (oo.repair_method IS NULL OR oo.repair_method != '2') )
        WHERE
            ro.occur_time >= STR_TO_DATE(CONCAT(#{monthPeriod}, '-01 00:00:00'), '%Y-%m-%d %H:%i:%s')
            AND ro.occur_time <![CDATA[<]]> DATE_ADD(STR_TO_DATE(CONCAT(#{monthPeriod}, '-01 00:00:00'), '%Y-%m-%d %H:%i:%s'), INTERVAL 1 MONTH)
            AND (ro.repair_method IS NULL OR ro.repair_method != '2')
        ) t
        ) a
        LEFT JOIN gxt_repair_order rep ON rep.id = a.id
        LEFT JOIN gxt_score_runtime sr ON (
        IFNULL( TIMESTAMPDIFF( HOUR, a.prev_wbtime, a.occur_time )/ 24, 999999 ))>= sr.min_hours
        AND (
        sr.max_hours IS NULL
        OR sr.max_hours >(
        IFNULL( TIMESTAMPDIFF( HOUR, a.prev_wbtime, a.occur_time )/ 24, 999999 )/ 24
        ))
    </select>

    <select id="selectGxtDeviceRuntimeByWorkOrder" parameterType="GxtDeviceRuntime" resultMap="GxtDeviceRuntimeResult">
        SELECT
            t.device_id,
            t.work_order_project_no,
            t.prev_wbtime,
            sr.score AS scroe,
            t.work_order_id,
            sr.id AS score_id
        FROM
            (
                SELECT
                    wo.id AS work_order_id,
                    wo.pcs_device_id AS device_id,
                    wo.work_order_project_no,
                    wo.restart_time AS prev_wbtime,
                    IFNULL( TIMESTAMPDIFF( HOUR, wo.restart_time, #{faultTime} )/ 24, 999999 ) AS time_dif
                FROM
                    gxt_work_order wo
                LEFT JOIN (
                        SELECT
                            pcs_device_id,
                            MAX(restart_time) AS max_restart_time
                        FROM gxt_work_order
                        WHERE
                            pcs_device_id IS NOT NULL
                          AND restart_time IS NOT NULL
                          AND #{faultTime} > restart_time
                          AND (repair_method IS NULL OR repair_method != '2')
                        GROUP BY pcs_device_id
                    ) max_wo ON wo.pcs_device_id = max_wo.pcs_device_id
                        AND wo.restart_time = max_wo.max_restart_time
                WHERE
                    wo.pcs_device_id IS NOT NULL
                  AND (#{faultTime} > wo.restart_time OR wo.restart_time IS NULL)
                  AND (wo.repair_method IS NULL OR wo.repair_method != '2')
            ) t
                LEFT JOIN gxt_score_runtime sr ON t.time_dif >= sr.min_hours
                AND ( sr.max_hours IS NULL OR sr.max_hours > t.time_dif )
                LEFT JOIN gxt_device_runtime dr ON dr.device_id = t.device_id
                AND dr.month_period = #{monthPeriod}
        WHERE
            dr.id IS NULL
    </select>

    <insert id="insertGxtDeviceRuntime" parameterType="GxtDeviceRuntime" useGeneratedKeys="true" keyProperty="id">
        insert into gxt_device_runtime
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="deviceId != null">device_id,</if>
            <if test="workOrderProjectNo != null and workOrderProjectNo != ''">work_order_project_no,</if>
            <if test="faultTime != null">fault_time,</if>
            <if test="prevWbtime != null">prev_wbtime,</if>
            <if test="statTime != null">stat_time,</if>
            <if test="monthPeriod != null and monthPeriod != ''">month_period,</if>
            <if test="scroe != null">scroe,</if>
            <if test="status != null">status,</if>
            <if test="createBy != null and createBy != ''">create_by,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateBy != null and updateBy != ''">update_by,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="remark != null and remark != ''">remark,</if>
            <if test="deptId != null">dept_id,</if>
            <if test="scoreId != null">score_id,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="deviceId != null">#{deviceId},</if>
            <if test="workOrderProjectNo != null and workOrderProjectNo != ''">#{workOrderProjectNo},</if>
            <if test="faultTime != null">#{faultTime},</if>
            <if test="prevWbtime != null">#{prevWbtime},</if>
            <if test="statTime != null">#{statTime},</if>
            <if test="monthPeriod != null and monthPeriod != ''">#{monthPeriod},</if>
            <if test="scroe != null">#{scroe},</if>
            <if test="status != null">#{status},</if>
            <if test="createBy != null and createBy != ''">#{createBy},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateBy != null and updateBy != ''">#{updateBy},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="remark != null and remark != ''">#{remark},</if>
            <if test="deptId != null">#{deptId},</if>
            <if test="scoreId != null">#{scoreId},</if>
         </trim>
    </insert>

    <update id="updateGxtDeviceRuntime" parameterType="GxtDeviceRuntime">
        update gxt_device_runtime
        <trim prefix="SET" suffixOverrides=",">
            <if test="deviceId != null">device_id = #{deviceId},</if>
            <if test="workOrderProjectNo != null and workOrderProjectNo != ''">work_order_project_no = #{workOrderProjectNo},</if>
            <if test="faultTime != null">fault_time = #{faultTime},</if>
            <if test="prevWbtime != null">prev_wbtime = #{prevWbtime},</if>
            <if test="statTime != null">stat_time = #{statTime},</if>
            <if test="monthPeriod != null and monthPeriod != ''">month_period = #{monthPeriod},</if>
            <if test="scroe != null">scroe = #{scroe},</if>
            <if test="status != null">status = #{status},</if>
            <if test="createBy != null and createBy != ''">create_by = #{createBy},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateBy != null and updateBy != ''">update_by = #{updateBy},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="remark != null and remark != ''">remark = #{remark},</if>
            <if test="deptId != null">dept_id = #{deptId},</if>
            <if test="scoreId != null">score_id = #{scoreId},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteGxtDeviceRuntimeById" parameterType="Long">
        delete from gxt_device_runtime where id = #{id}
    </delete>

    <delete id="deleteGxtDeviceRuntimeByIds" parameterType="String">
        delete from gxt_device_runtime where id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>

</mapper>