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

    <resultMap type="GxtRuntimeScore" id="GxtRuntimeScoreResult">
        <result property="id" column="id"/>
        <result property="minHours" column="min_hours"/>
        <result property="maxHours" column="max_hours"/>
        <result property="scoreRange" column="score_range"/>
        <result property="description" column="description"/>
        <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"/>
    </resultMap>

    <sql id="selectGxtRuntimeScoreVo">
        select id, min_hours, max_hours, score_range, description, create_by, create_time, update_by, update_time, remark from gxt_score_runtime
    </sql>

    <select id="selectGxtRuntimeScoreList" parameterType="GxtRuntimeScore" resultMap="GxtRuntimeScoreResult">
        <include refid="selectGxtRuntimeScoreVo"/>
        <where>  
            <if test="minHours != null "> and min_hours = #{minHours}</if>
            <if test="maxHours != null "> and max_hours = #{maxHours}</if>
            <if test="scoreRange != null  and scoreRange != ''"> and score_range = #{scoreRange}</if>
            <if test="description != null  and description != ''"> and description = #{description}</if>
        </where>
        order by id asc
    </select>
    
    <select id="selectGxtRuntimeScoreById" parameterType="Long" resultMap="GxtRuntimeScoreResult">
        <include refid="selectGxtRuntimeScoreVo"/>
        where id = #{id}
    </select>
        
    <insert id="insertGxtRuntimeScore" parameterType="GxtRuntimeScore" useGeneratedKeys="true" keyProperty="id">
        insert into gxt_score_runtime
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="minHours != null">min_hours,</if>
            <if test="maxHours != null">max_hours,</if>
            <if test="scoreRange != null and scoreRange != ''">score_range,</if>
            <if test="description != null and description != ''">description,</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">remark,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="minHours != null">#{minHours},</if>
            <if test="maxHours != null">#{maxHours},</if>
            <if test="scoreRange != null and scoreRange != ''">#{scoreRange},</if>
            <if test="description != null and description != ''">#{description},</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">#{remark},</if>
         </trim>
    </insert>

    <update id="updateGxtRuntimeScore" parameterType="GxtRuntimeScore">
        update gxt_score_runtime
        <trim prefix="SET" suffixOverrides=",">
            <if test="minHours != null">min_hours = #{minHours},</if>
            <if test="maxHours != null">max_hours = #{maxHours},</if>
            <if test="scoreRange != null and scoreRange != ''">score_range = #{scoreRange},</if>
            <if test="description != null and description != ''">description = #{description},</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">remark = #{remark},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteGxtRuntimeScoreById" parameterType="Long">
        delete from gxt_score_runtime where id = #{id}
    </delete>

    <delete id="deleteGxtRuntimeScoreByIds" parameterType="Long">
        delete from gxt_score_runtime where id in 
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>
    
    <select id="selectAllRuntimeScores" resultMap="GxtRuntimeScoreResult">
        <include refid="selectGxtRuntimeScoreVo"/>
        order by min_hours asc
    </select>
    
    <!-- 维保运行统计SQL -->
    <select id="selectRuntimeScoreStatistics" resultType="java.util.HashMap">
        SELECT 
            <choose>
                <when test="params.groupFields != null and params.groupFields != ''">
                    <foreach collection="params.groupFields.split(',')" item="field" separator=",">
                        t.${field}
                    </foreach>,
                </when>
                <otherwise>
                    t.gxt_center,
                </otherwise>
            </choose>
            SUM(t.under_10_hours_score) as under_10_hours_score,
            SUM(t.range_10_240_score) as range_10_240_score,
            SUM(t.range_240_270_score) as range_240_270_score,
            SUM(t.range_270_300_score) as range_270_300_score,
            SUM(t.over_300_score) as over_300_score,
            SUM(t.total_score) as total_score,
            COUNT(t.device_id) as device_count
        FROM (
            SELECT 
                <choose>
                    <when test="params.groupFields != null and params.groupFields != ''">
                        <foreach collection="params.groupFields.split(',')" item="field" separator=",">
                            w.${field}
                        </foreach>,
                    </when>
                    <otherwise>
                        w.gxt_center,
                    </otherwise>
                </choose>
                w.pcs_device_id as device_id,
                w.pcs_device_name as device_name,
                MAX(r.restart_time) as last_restart_time,
                TIMESTAMPDIFF(HOUR, MAX(r.restart_time), NOW()) as downtime_hours,
                CASE 
                    WHEN TIMESTAMPDIFF(HOUR, MAX(r.restart_time), NOW()) &lt; 10 THEN COALESCE(SUM(w.score), 0)
                    ELSE 0
                END as under_10_hours_score,
                CASE 
                    WHEN TIMESTAMPDIFF(HOUR, MAX(r.restart_time), NOW()) &gt;= 10 AND TIMESTAMPDIFF(HOUR, MAX(r.restart_time), NOW()) &lt; 240 THEN COALESCE(SUM(w.score), 0)
                    ELSE 0
                END as range_10_240_score,
                CASE 
                    WHEN TIMESTAMPDIFF(HOUR, MAX(r.restart_time), NOW()) &gt;= 240 AND TIMESTAMPDIFF(HOUR, MAX(r.restart_time), NOW()) &lt; 270 THEN COALESCE(SUM(w.score), 0)
                    ELSE 0
                END as range_240_270_score,
                CASE 
                    WHEN TIMESTAMPDIFF(HOUR, MAX(r.restart_time), NOW()) &gt;= 270 AND TIMESTAMPDIFF(HOUR, MAX(r.restart_time), NOW()) &lt; 300 THEN COALESCE(SUM(w.score), 0)
                    ELSE 0
                END as range_270_300_score,
                CASE 
                    WHEN TIMESTAMPDIFF(HOUR, MAX(r.restart_time), NOW()) &gt;= 300 THEN COALESCE(SUM(w.score), 0)
                    ELSE 0
                END as over_300_score,
                COALESCE(SUM(w.score), 0) as total_score
            FROM gxt_work_order w
            LEFT JOIN gxt_repair_order r ON w.pcs_device_id = r.pcs_device_id AND r.restart_time IS NOT NULL
            WHERE w.create_time &gt;= DATE_SUB(NOW(), INTERVAL 1 MONTH)
                AND w.pcs_device_id IS NOT NULL
                <if test="params.gxtCenter != null and params.gxtCenter != ''">
                    AND w.gxt_center = #{params.gxtCenter}
                </if>
                <if test="params.pcsStationName != null and params.pcsStationName != ''">
                    AND w.pcs_station_name = #{params.pcsStationName}
                </if>
                <if test="params.brand != null and params.brand != ''">
                    AND w.brand = #{params.brand}
                </if>
                <if test="params.model != null and params.model != ''">
                    AND w.model = #{params.model}
                </if>
            GROUP BY 
                <choose>
                    <when test="params.groupFields != null and params.groupFields != ''">
                        <foreach collection="params.groupFields.split(',')" item="field" separator=",">
                            w.${field}
                        </foreach>,
                    </when>
                    <otherwise>
                        w.gxt_center,
                    </otherwise>
                </choose>
                w.pcs_device_id, w.pcs_device_name
        ) t
        GROUP BY
            <choose>
                <when test="params.groupFields != null and params.groupFields != ''">
                    <foreach collection="params.groupFields.split(',')" item="field" separator=",">
                        t.${field}
                    </foreach>
                </when>
                <otherwise>
                    t.gxt_center
                </otherwise>
            </choose>
        ORDER BY total_score DESC
    </select>
</mapper>