<?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.GxtUserRuntimeScoreMapper">
    
    <resultMap type="GxtUserRuntimeScore" id="GxtUserRuntimeScoreResult">
        <result property="id" column="id"/>
        <result property="userId" column="user_id"/>
        <result property="userName" column="user_name"/>
        <result property="runtimeScoreId" column="runtime_score_id"/>
        <result property="deviceCount" column="device_count"/>
        <result property="runtimeScore" column="runtime_score"/>
        <result property="calculatedScore" column="calculated_score"/>
        <result property="monthPeriod" column="month_period"/>
        <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="runtimeRange" column="runtime_range"/>
        <result property="score" column="rs_score"/>
        <result property="permission"    column="permission"      />
    </resultMap>
    
    <!-- 本月部门用户运行天数统计结果映射 -->
    <resultMap type="GxtUserRuntimeScore" id="CurrentMonthDeptUserRuntimeScoreResult">
        <result property="userId" column="user_id"/>
        <result property="userName" column="nick_name"/>
        <result property="runtimeScoreId" column="runtime_score_id"/>
        <result property="deviceCount" column="device_count"/>
        <result property="runtimeScore" column="runtime_score"/>
        <result property="score" column="score"/>
        <result property="runtimeRange" column="runtime_range"/>
    </resultMap>
    
    <sql id="selectGxtUserRuntimeScoreVo">
        select 
            gurs.id, 
            gurs.user_id, 
            gurs.user_name, 
            gurs.runtime_score_id, 
            gurs.device_count, 
            gurs.runtime_score, 
            gurs.calculated_score, 
            gurs.month_period,
            grs.runtime_range,
            grs.score as rs_score
        from gxt_user_runtime_score gurs
        left join gxt_score_runtime grs on grs.id = gurs.runtime_score_id
    </sql>
    
    <select id="selectGxtUserRuntimeScoreById" parameterType="Long" resultMap="GxtUserRuntimeScoreResult">
        <include refid="selectGxtUserRuntimeScoreVo"/>
        where gurs.id = #{id}
    </select>
    
    <select id="selectGxtUserRuntimeScoreList" parameterType="GxtUserRuntimeScore" resultMap="GxtUserRuntimeScoreResult">
        <include refid="selectGxtUserRuntimeScoreVo"/>
        <where>  
            <if test="userId != null "> and gurs.user_id = #{userId}</if>
            <if test="deptId != null "> and gurs.user_id in (SELECT user_id FROM sys_user WHERE dept_id = #{deptId})</if>
            <if test="permission != null and permission != ''">
                AND gurs.user_id IN (
                SELECT ur.user_id
                FROM sys_user_role ur
                LEFT JOIN sys_role r ON ur.role_id = r.role_id
                WHERE r.role_key IN
                <foreach item="item" collection="permission.split(',')" open="(" separator="," close=")">
                    #{item}
                </foreach>
                )
            </if>
            <if test="userName != null  and userName != ''"> and gurs.user_name like concat('%', #{userName}, '%')</if>
            <if test="runtimeScoreId != null "> and gurs.runtime_score_id = #{runtimeScoreId}</if>
            <if test="deviceCount != null "> and gurs.device_count = #{deviceCount}</if>
            <if test="runtimeScore != null "> and gurs.runtime_score = #{runtimeScore}</if>
            <if test="calculatedScore != null "> and gurs.calculated_score = #{calculatedScore}</if>
            <if test="monthPeriod != null  and monthPeriod != ''"> and gurs.month_period = #{monthPeriod}</if>
        </where>
        order by gurs.user_name asc
    </select>
    
    <!-- 查询本月部门用户运行天数统计 -->
    <select id="selectCurrentMonthDeptUserRuntimeScore" parameterType="GxtUserRuntimeScore" resultMap="CurrentMonthDeptUserRuntimeScoreResult">
        SELECT
            tt.user_id,
            tt.nick_name,
            tt.runtime_id AS runtime_score_id,
            tt.runtime_range,
            count(*) AS device_count,
            MIN(tt.score) AS score,
            SUM( tt.score ) AS runtime_score
        FROM
            (
            SELECT
                wop.user_id,
                wop.nick_name,
                wop.order_id,
                wrt.running_days,
                rs.id AS runtime_id,
                rs.runtime_range,
                rs.score 
            FROM
                gxt_work_order_person wop
                LEFT JOIN ( SELECT id, TIMESTAMPDIFF( HOUR, IFNULL( restart_time, real_start_time ), #{endTime} ) / 24 AS running_days FROM gxt_work_order ) wrt ON wop.order_id = wrt.id
                LEFT JOIN gxt_score_runtime rs ON (wrt.running_days <![CDATA[<]]> rs.max_hours OR rs.max_hours IS NULL) AND wrt.running_days >= rs.min_hours
            WHERE
                rs.STATUS = '0' 
                AND wop.user_id IN (SELECT user_id FROM sys_user WHERE dept_id = #{deptId})
                <if test="permission != null and permission != ''">
                    AND wop.user_id IN (
                    SELECT ur.user_id
                    FROM sys_user_role ur
                    LEFT JOIN sys_role r ON ur.role_id = r.role_id
                    WHERE r.role_key IN
                    <foreach item="item" collection="permission.split(',')" open="(" separator="," close=")">
                        #{item}
                    </foreach>
                    )
                </if>
            ) tt 
        GROUP BY
            tt.user_id,
            tt.nick_name,
            tt.runtime_id
    </select>

    <select id="selectMonthDeptUserRuntimeScore" parameterType="GxtUserRuntimeScore" resultMap="CurrentMonthDeptUserRuntimeScoreResult">
        SELECT
        rs.runtime_range,
        COUNT(*) AS device_count,
        rs.score
        FROM
        (
        SELECT
        o.id,
        TIMESTAMPDIFF( HOUR, IFNULL( o.restart_time, o.real_start_time ), #{endTime} ) / 24 AS running_days
        FROM
        gxt_work_order o
        LEFT JOIN sys_user u ON o.accept_user_id = u.user_id
        WHERE
        u.dept_id = #{deptId}
        ) wo
        LEFT JOIN gxt_score_runtime rs ON (wo.running_days <![CDATA[<]]> rs.max_hours OR rs.max_hours IS NULL) AND wo.running_days >= rs.min_hours
        AND rs.STATUS = 0
        GROUP BY
        rs.runtime_range,
        rs.score
    </select>
    
    <insert id="insertGxtUserRuntimeScore" parameterType="GxtUserRuntimeScore" useGeneratedKeys="true" keyProperty="id">
        insert into gxt_user_runtime_score
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="userId != null">user_id,</if>
            <if test="userName != null and userName != ''">user_name,</if>
            <if test="runtimeScoreId != null">runtime_score_id,</if>
            <if test="deviceCount != null">device_count,</if>
            <if test="runtimeScore != null">runtime_score,</if>
            <if test="calculatedScore != null">calculated_score,</if>
            <if test="monthPeriod != null and monthPeriod != ''">month_period,</if>
            <if test="createBy != null">create_by,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateBy != null">update_by,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="remark != null">remark,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="userId != null">#{userId},</if>
            <if test="userName != null and userName != ''">#{userName},</if>
            <if test="runtimeScoreId != null">#{runtimeScoreId},</if>
            <if test="deviceCount != null">#{deviceCount},</if>
            <if test="runtimeScore != null">#{runtimeScore},</if>
            <if test="calculatedScore != null">#{calculatedScore},</if>
            <if test="monthPeriod != null and monthPeriod != ''">#{monthPeriod},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateBy != null">#{updateBy},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="remark != null">#{remark},</if>
         </trim>
    </insert>
    
    <update id="updateGxtUserRuntimeScore" parameterType="GxtUserRuntimeScore">
        update gxt_user_runtime_score
        <trim prefix="SET" suffixOverrides=",">
            <if test="userId != null">user_id = #{userId},</if>
            <if test="userName != null and userName != ''">user_name = #{userName},</if>
            <if test="runtimeScoreId != null">runtime_score_id = #{runtimeScoreId},</if>
            <if test="deviceCount != null">device_count = #{deviceCount},</if>
            <if test="runtimeScore != null">runtime_score = #{runtimeScore},</if>
            <if test="calculatedScore != null">calculated_score = #{calculatedScore},</if>
            <if test="monthPeriod != null and monthPeriod != ''">month_period = #{monthPeriod},</if>
            <if test="createBy != null">create_by = #{createBy},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateBy != null">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="deleteGxtUserRuntimeScoreById" parameterType="Long">
        delete from gxt_user_runtime_score where id = #{id}
    </delete>
    
    <delete id="deleteGxtUserRuntimeScoreByIds" parameterType="String">
        delete from gxt_user_runtime_score where id in 
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>
</mapper>