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

    <resultMap type="GxtMonthRuntime" id="GxtMonthRuntimeResult">
        <result property="id" column="id"/>
        <result property="monthScoreId" column="month_score_id"/>
        <result property="deptId" column="dept_id"/>
        <result property="deptName" column="dept_name"/>
        <result property="runtimeScoreId" column="runtime_score_id"/>
        <result property="runtimeRange" column="runtime_range"/>
        <result property="deviceCount" column="device_count"/>
        <result property="runtimeScore" column="runtime_score"/>
        <result property="calculatedScore" column="calculated_score"/>
        <result property="monthPeriod" column="month_period"/>
    </resultMap>

    <sql id="selectGxtMonthRuntimeVo">
        select id, month_score_id, dept_id, dept_name, runtime_score_id,(select runtime_range from gxt_score_runtime where id=runtime_score_id) AS runtime_range, device_count, runtime_score, calculated_score, month_period from gxt_month_runtime
    </sql>

    <select id="selectGxtMonthRuntimeList" parameterType="GxtMonthRuntime" resultMap="GxtMonthRuntimeResult">
        <include refid="selectGxtMonthRuntimeVo"/>
        <where>
            <if test="monthScoreId != null "> and month_score_id = #{monthScoreId}</if>
            <if test="deptId != null "> and dept_id = #{deptId}</if>
            <if test="deptName != null  and deptName != ''"> and dept_name like concat('%', #{deptName}, '%')</if>
            <if test="runtimeScoreId != null "> and runtime_score_id = #{runtimeScoreId}</if>
            <if test="deviceCount != null "> and device_count = #{deviceCount}</if>
            <if test="runtimeScore != null "> and runtime_score = #{runtimeScore}</if>
            <if test="calculatedScore != null "> and calculated_score = #{calculatedScore}</if>
            <if test="monthPeriod != null  and monthPeriod != ''"> and month_period = #{monthPeriod}</if>
        </where>
    </select>

    <select id="selectGxtMonthRuntimeListByAdd" parameterType="GxtMonthRuntime" resultMap="GxtMonthRuntimeResult">
        SELECT
            dr.dept_id,
            d.dept_name,
            sr.id AS runtime_score_id,
            sr.runtime_range,
            COUNT(*) AS device_count,
            sr.score AS runtime_score,
            COUNT(*) * sr.score AS calculated_score
        FROM
            gxt_device_runtime dr
                LEFT JOIN gxt_score_runtime sr ON dr.score_id = sr.id
                LEFT JOIN sys_dept d ON d.dept_id=dr.dept_id
        WHERE
            dr.month_period = #{monthPeriod}
          AND dr.dept_id = #{deptId}
          AND sr.id IS NOT NULL
        GROUP BY
            sr.id,dr.dept_id
    </select>

    <select id="selectGxtMonthRuntimeById" parameterType="Long" resultMap="GxtMonthRuntimeResult">
        <include refid="selectGxtMonthRuntimeVo"/>
        where id = #{id}
    </select>

    <insert id="insertGxtMonthRuntime" parameterType="GxtMonthRuntime" useGeneratedKeys="true" keyProperty="id">
        insert into gxt_month_runtime
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="monthScoreId != null">month_score_id,</if>
            <if test="deptId != null">dept_id,</if>
            <if test="deptName != null and deptName != ''">dept_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>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="monthScoreId != null">#{monthScoreId},</if>
            <if test="deptId != null">#{deptId},</if>
            <if test="deptName != null and deptName != ''">#{deptName},</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>
         </trim>
    </insert>

    <update id="updateGxtMonthRuntime" parameterType="GxtMonthRuntime">
        update gxt_month_runtime
        <trim prefix="SET" suffixOverrides=",">
            <if test="monthScoreId != null">month_score_id = #{monthScoreId},</if>
            <if test="deptId != null">dept_id = #{deptId},</if>
            <if test="deptName != null and deptName != ''">dept_name = #{deptName},</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>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteGxtMonthRuntimeById" parameterType="Long">
        delete from gxt_month_runtime where id = #{id}
    </delete>

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

</mapper>