<?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.GxtUserRepairProjectScoreMapper">
    
    <resultMap type="GxtUserRepairProjectScore" id="GxtUserRepairProjectScoreResult">
        <result property="id" column="id"/>
        <result property="userId" column="user_id"/>
        <result property="userName" column="user_name"/>
        <result property="maintenanceType" column="maintenance_type"/>
        <result property="orderCount" column="order_count"/>
        <result property="projectScore" column="project_score"/>
        <result property="calculatedScore" column="calculated_score"/>
        <result property="totalScore" column="total_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="permission" column="permission"/>
    </resultMap>
    
    <sql id="selectGxtUserRepairProjectScoreVo">
        select id, user_id, user_name, maintenance_type, order_count, project_score, calculated_score, month_period from gxt_user_repair_project_score
    </sql>
    
    <select id="selectGxtUserRepairProjectScoreById" parameterType="Long" resultMap="GxtUserRepairProjectScoreResult">
        <include refid="selectGxtUserRepairProjectScoreVo"/>
        where id = #{id}
    </select>
    
    <select id="selectGxtUserRepairProjectScoreList" parameterType="GxtUserRepairProjectScore" resultMap="GxtUserRepairProjectScoreResult">
        <include refid="selectGxtUserRepairProjectScoreVo"/>
        <where>  
            <if test="userId != null "> and user_id = #{userId}</if>
            <if test="deptId != null "> and user_id in (SELECT user_id FROM sys_user WHERE dept_id = #{deptId})</if>
            <if test="permission != null and permission != ''">
                AND 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 user_name like concat('%', #{userName}, '%')</if>
            <if test="maintenanceType != null and maintenanceType != '' "> and maintenance_type = #{maintenanceType}</if>
            <if test="orderCount != null "> and order_count = #{orderCount}</if>
            <if test="projectScore != null "> and project_score = #{projectScore}</if>
            <if test="calculatedScore != null "> and calculated_score = #{calculatedScore}</if>
            <if test="monthPeriod != null  and monthPeriod != ''"> and month_period = #{monthPeriod}</if>
        </where>
        order by user_name asc
    </select>
    
    <insert id="insertGxtUserRepairProjectScore" parameterType="GxtUserRepairProjectScore" useGeneratedKeys="true" keyProperty="id">
        insert into gxt_user_repair_project_score
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="userId != null">user_id,</if>
            <if test="userName != null and userName != ''">user_name,</if>
            <if test="maintenanceType != null and maintenanceType != ''">maintenance_type,</if>
            <if test="orderCount != null">order_count,</if>
            <if test="projectScore != null">project_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="maintenanceType != null and maintenanceType != ''">#{maintenanceType},</if>
            <if test="orderCount != null">#{orderCount},</if>
            <if test="projectScore != null">#{projectScore},</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="updateGxtUserRepairProjectScore" parameterType="GxtUserRepairProjectScore">
        update gxt_user_repair_project_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="maintenanceType != null and maintenanceType != ''">maintenance_type = #{maintenanceType},</if>
            <if test="orderCount != null">order_count = #{orderCount},</if>
            <if test="projectScore != null">project_score = #{projectScore},</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="deleteGxtUserRepairProjectScoreById" parameterType="Long">
        delete from gxt_user_repair_project_score where id = #{id}
    </delete>
    
    <delete id="deleteGxtUserRepairProjectScoreByIds" parameterType="String">
        delete from gxt_user_repair_project_score where id in 
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>
    
    <!-- 自定义查询SQL语句 -->
    <select id="selectUserRepairProjectScoreStatistics" parameterType="GxtUserRepairProjectScore" resultMap="GxtUserRepairProjectScoreResult">
        SELECT 
            rop.user_id,
            rop.nick_name AS user_name,
            ro.maintenance_type,
            COUNT(*) AS order_count,
            ps.score_per_completion AS project_score,
            COUNT(*)*ps.score_per_completion AS total_score
        FROM gxt_repair_order_person rop 
        LEFT JOIN gxt_repair_order ro ON ro.id=rop.order_id 
        LEFT JOIN gxt_score_project ps ON ro.maintenance_type=ps.maintenance_type AND ps.status=0
        WHERE rop.user_id IN (SELECT user_id FROM sys_user WHERE dept_id = #{deptId})
            <if test="permission != null and permission != ''">
                AND rop.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>
            AND DATE_FORMAT(ro.accept_time, '%Y-%m') = #{monthPeriod}
        GROUP BY rop.user_id,rop.nick_name,ro.maintenance_type,ps.score_per_completion
    </select>

    <select id="selectRepairProjectScoreByDeptId" parameterType="GxtUserRepairProjectScore" resultMap="GxtUserRepairProjectScoreResult">
        SELECT
            sp.project_name AS maintenance_type,
            COUNT(*) AS order_count,
            sp.score_per_completion AS project_score
        FROM
            gxt_repair_order ro
                LEFT JOIN gxt_score_project sp ON ro.maintenance_type = sp.maintenance_type
                LEFT JOIN sys_user u ON ro.accept_user_id = u.user_id
        WHERE
            u.dept_id = #{deptId}
            AND DATE_FORMAT(ro.real_start_time, '%Y-%m') = #{monthPeriod}
        GROUP BY
            ro.maintenance_type,sp.score_per_completion
    </select>
</mapper>