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

    <resultMap type="GxtUserFanInspectionScore" id="GxtUserFanInspectionScoreResult">
        <result property="id" column="id"/>
        <result property="userId" column="user_id"/>
        <result property="userName" column="user_name"/>
        <result property="fanInspectionId" column="fan_inspection_id"/>
        <result property="deviceCount" column="device_count"/>
        <result property="fanInspectionScore" column="fan_inspection_score"/>
        <result property="totalScore" column="total_score"/>
        <result property="calculatedScore" column="calculated_score"/>
        <result property="model" column="model"/>
        <result property="inspectionTypeId" column="inspection_type_id"/>
        <result property="monthPeriod" column="month_period"/>
        <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="permission" column="permission"/>
    </resultMap>

    <sql id="selectGxtUserFanInspectionScoreVo">
        SELECT
            tt.id,
            tt.user_id,
            tt.user_name,
            tt.fan_inspection_id,
            tt.device_count,
            tt.fan_inspection_score,
            tt.device_count * tt.fan_inspection_score AS total_score,
            tt.calculated_score,
            tt.month_period,
            tt.status,
            tt.remark,
            fis.fan_type AS model,
            fis.inspection_type_id
        FROM gxt_user_fan_inspection_score tt
        LEFT JOIN gxt_score_fan_inspection fis ON tt.fan_inspection_id = fis.id AND fis.status = 0
    </sql>

    <select id="selectGxtUserFanInspectionScoreById" parameterType="Long" resultMap="GxtUserFanInspectionScoreResult">
        <include refid="selectGxtUserFanInspectionScoreVo"/>
        where tt.id = #{id}
    </select>

    <select id="selectGxtUserFanInspectionScoreList" parameterType="GxtUserFanInspectionScore" resultMap="GxtUserFanInspectionScoreResult">
        <include refid="selectGxtUserFanInspectionScoreVo"/>
        <where>
            <if test="userId != null "> and tt.user_id = #{userId}</if>
            <if test="userName != null  and userName != ''"> and tt.user_name like concat('%', #{userName}, '%')</if>
            <if test="fanInspectionId != null "> and tt.fan_inspection_id = #{fanInspectionId}</if>
            <if test="deviceCount != null "> and tt.device_count = #{deviceCount}</if>
            <if test="fanInspectionScore != null "> and tt.fan_inspection_score = #{fanInspectionScore}</if>
            <if test="calculatedScore != null "> and tt.calculated_score = #{calculatedScore}</if>
            <if test="model != null  and model != ''"> and fis.fan_type = #{model}</if>
            <if test="inspectionTypeId != null "> and fis.inspection_type_id = #{inspectionTypeId}</if>
            <if test="monthPeriod != null  and monthPeriod != ''"> and tt.month_period = #{monthPeriod}</if>
            <if test="status != null "> and tt.status = #{status}</if>
        </where>
        order by tt.create_time desc
    </select>

    <!-- 新增：查询维保机组类型评分 -->
    <select id="selectFanInspectionScoreForCurrentMonth" parameterType="GxtUserFanInspectionScore" resultMap="GxtUserFanInspectionScoreResult">
        SELECT
            tt.user_id,
            tt.user_name,
            fist.fan_type AS model,
            fist.inspection_type_id,
            tt.device_count,
            fist.score AS fan_inspection_score,
            tt.device_count * fist.score AS total_score,
            tt.fan_inspection_id
        FROM (
            SELECT
                wop.user_id,
                wop.nick_name AS user_name,
                e.model,
                fis.id AS fan_inspection_id,
                COUNT(*) AS device_count
            FROM gxt_work_order_person wop
            LEFT JOIN gxt_work_order wo ON wop.order_id = wo.id
            LEFT JOIN gxt_equipment e ON wo.pcs_device_id = e.equipment_id
            LEFT JOIN gxt_score_fan_inspection fis ON wo.inspection_type = fis.inspection_type_id AND fis.fan_type = e.model AND fis.status = 0
            WHERE DATE_FORMAT(wo.real_start_time, '%Y-%m') = #{monthPeriod}
            GROUP BY wop.user_id, wop.nick_name, e.model, fis.id
        ) tt
        LEFT JOIN gxt_score_fan_inspection fist ON tt.fan_inspection_id = fist.id
        WHERE tt.user_id IN (SELECT user_id FROM sys_user WHERE dept_id = #{deptId})
        <if test="permission != null and permission != ''">
            AND tt.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>
    </select>

    <select id="selectFanInspectionScoreByDeptId" parameterType="GxtUserFanInspectionScore" resultMap="GxtUserFanInspectionScoreResult">
        SELECT
            wo.inspection_type AS inspection_type_id,
            wo.model,
            COUNT(*) AS device_count,
            sfi.score AS fan_inspection_score
        FROM
            gxt_work_order wo
                LEFT JOIN gxt_score_fan_inspection sfi ON sfi.inspection_type_id = wo.inspection_type
                AND sfi.fan_type = wo.model
                LEFT JOIN sys_user u ON wo.accept_user_id = u.user_id
        WHERE
            u.dept_id = #{deptId}
            AND DATE_FORMAT(wo.real_start_time, '%Y-%m') = #{monthPeriod}
        GROUP BY
            wo.inspection_type,
            wo.model,
            sfi.score
    </select>

    <!-- 新增：根据部门/用户查询维保机组类型评分 -->
    <select id="selectFanInspectionScoreBySP" parameterType="GxtUserFanInspectionScore" resultMap="GxtUserFanInspectionScoreResult">
        <include refid="selectGxtUserFanInspectionScoreVo"/>
        WHERE tt.month_period = #{monthPeriod}
        <if test="userId != null "> and tt.user_id = #{userId}</if>
        <if test="deptId != null ">
            AND tt.user_id IN (
                SELECT user_id
                FROM sys_user
                WHERE dept_id = #{deptId}
            )
        </if>
        <if test="permission != null and permission != ''">
            AND tt.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>
    </select>

    <insert id="insertGxtUserFanInspectionScore" parameterType="GxtUserFanInspectionScore" useGeneratedKeys="true" keyProperty="id">
        insert into gxt_user_fan_inspection_score
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="userId != null">user_id,</if>
            <if test="userName != null and userName != ''">user_name,</if>
            <if test="fanInspectionId != null">fan_inspection_id,</if>
            <if test="deviceCount != null">device_count,</if>
            <if test="fanInspectionScore != null">fan_inspection_score,</if>
            <if test="calculatedScore != null">calculated_score,</if>
            <if test="model != null and model != ''">model,</if>
            <if test="inspectionTypeId != null">inspection_type_id,</if>
            <if test="monthPeriod != null and monthPeriod != ''">month_period,</if>
            <if test="status != null">status,</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="fanInspectionId != null">#{fanInspectionId},</if>
            <if test="deviceCount != null">#{deviceCount},</if>
            <if test="fanInspectionScore != null">#{fanInspectionScore},</if>
            <if test="calculatedScore != null">#{calculatedScore},</if>
            <if test="model != null and model != ''">#{model},</if>
            <if test="inspectionTypeId != null">#{inspectionTypeId},</if>
            <if test="monthPeriod != null and monthPeriod != ''">#{monthPeriod},</if>
            <if test="status != null">#{status},</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="updateGxtUserFanInspectionScore" parameterType="GxtUserFanInspectionScore">
        update gxt_user_fan_inspection_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="fanInspectionId != null">fan_inspection_id = #{fanInspectionId},</if>
            <if test="deviceCount != null">device_count = #{deviceCount},</if>
            <if test="fanInspectionScore != null">fan_inspection_score = #{fanInspectionScore},</if>
            <if test="calculatedScore != null">calculated_score = #{calculatedScore},</if>
            <if test="model != null and model != ''">model = #{model},</if>
            <if test="inspectionTypeId != null">inspection_type_id = #{inspectionTypeId},</if>
            <if test="monthPeriod != null and monthPeriod != ''">month_period = #{monthPeriod},</if>
            <if test="status != null">status = #{status},</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="deleteGxtUserFanInspectionScoreById" parameterType="Long">
        delete from gxt_user_fan_inspection_score where id = #{id}
    </delete>

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