<?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.GxtUserScoreMapper">
    
    <resultMap type="GxtUserScore" id="GxtUserScoreResult">
        <result property="id" column="id"/>
        <result property="userName" column="user_name"/>
        <result property="userId" column="user_id"/>
        <result property="monthPeriod" column="month_period"/>
        <result property="monthScoreId" column="month_score_id"/>
        <result property="maintenanceTotalScore" column="maintenance_total_score"/>
        <result property="repairTotalScore" column="repair_total_score"/>
        <result property="finalScore" column="final_score"/>
        <result property="firstCheckStatus" column="first_check_status"/>
        <result property="firstChecker" column="first_checker"/>
        <result property="firstCheckTime" column="first_check_time"/>
        <result property="secondCheckStatus" column="second_check_status"/>
        <result property="secondChecker" column="second_checker"/>
        <result property="secondCheckTime" column="second_check_time"/>
        <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"/>
        <result property="rankPermission" column="rankPermission"/>
        <result property="nickName" column="nick_name"/>
    </resultMap>
    
    <sql id="selectGxtUserScoreVo">
        select id, user_name, nick_name, (SELECT u1.user_id FROM sys_user u1 WHERE u1.user_name = gxt_user_score.user_name) AS user_id, month_period, month_score_id, maintenance_total_score, repair_total_score, final_score, first_check_status,
               first_checker, first_check_time, second_check_status, second_checker, second_check_time, status,
               create_by, create_time, update_by, update_time, remark from gxt_user_score
    </sql>
    
    <select id="selectGxtUserScoreById" parameterType="Long" resultMap="GxtUserScoreResult">
        <include refid="selectGxtUserScoreVo"/>
        where id = #{id}
    </select>
    
    <select id="selectGxtUserScoreList" parameterType="GxtUserScore" resultMap="GxtUserScoreResult">
        <include refid="selectGxtUserScoreVo"/>
        <where>  
            <if test="userName != null  and userName != ''"> and user_name = #{userName}</if>
            <if test="nickName != null  and nickName != ''"> and nick_name like concat('%', #{nickName}, '%')</if>
            <if test="monthPeriod != null  and monthPeriod != ''"> and month_period = #{monthPeriod}</if>
            <if test="monthScoreId != null"> and month_score_id = #{monthScoreId}</if>
            <if test="maintenanceTotalScore != null "> and maintenance_total_score = #{maintenanceTotalScore}</if>
            <if test="repairTotalScore != null "> and repair_total_score = #{repairTotalScore}</if>
            <if test="finalScore != null "> and final_score = #{finalScore}</if>
            <if test="firstCheckStatus != null  and firstCheckStatus != ''"> and first_check_status = #{firstCheckStatus}</if>
            <if test="firstChecker != null "> and first_checker = #{firstChecker}</if>
            <if test="firstCheckTime != null "> and first_check_time = #{firstCheckTime}</if>
            <if test="secondCheckStatus != null  and secondCheckStatus != ''"> and second_check_status = #{secondCheckStatus}</if>
            <if test="secondChecker != null "> and second_checker = #{secondChecker}</if>
            <if test="secondCheckTime != null "> and second_check_time = #{secondCheckTime}</if>
            <if test="status != null "> and status = #{status}</if>
            <if test="userId != null "> and user_name = (select nick_name from sys_user where user_id = #{userId})</if>
            <if test="deptId != null "> 
                and user_name in (select nick_name from sys_user where dept_id = #{deptId})
            </if>
            <if test="permission != null and permission != ''">
                AND user_name IN (
                SELECT u.nick_name
                FROM sys_user u
                LEFT JOIN sys_user_role ur ON u.user_id = ur.user_id
                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="rankPermission != null and rankPermission != ''">
                AND user_name IN (
                SELECT user_name
                FROM sys_user
                WHERE user_id IN
                <foreach item="rankItem" collection="rankPermission.split(',')" open="(" separator="," close=")">
                    #{rankItem}
                </foreach>
                )
            </if>
        </where>
        order by user_name asc
    </select>
    
    <insert id="insertGxtUserScore" parameterType="GxtUserScore" useGeneratedKeys="true" keyProperty="id">
        insert into gxt_user_score
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="userName != null and userName != ''">user_name,</if>
            <if test="nickName != null and nickName != ''">nick_name,</if>
            <if test="monthPeriod != null and monthPeriod != ''">month_period,</if>
            <if test="monthScoreId != null">month_score_id,</if>
            <if test="maintenanceTotalScore != null">maintenance_total_score,</if>
            <if test="repairTotalScore != null">repair_total_score,</if>
            <if test="finalScore != null">final_score,</if>
            <if test="firstCheckStatus != null">first_check_status,</if>
            <if test="firstChecker != null">first_checker,</if>
            <if test="firstCheckTime != null">first_check_time,</if>
            <if test="secondCheckStatus != null">second_check_status,</if>
            <if test="secondChecker != null">second_checker,</if>
            <if test="secondCheckTime != null">second_check_time,</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="userName != null and userName != ''">#{userName},</if>
            <if test="nickName != null and nickName != ''">#{nickName},</if>
            <if test="monthPeriod != null and monthPeriod != ''">#{monthPeriod},</if>
            <if test="monthScoreId != null">#{monthScoreId},</if>
            <if test="maintenanceTotalScore != null">#{maintenanceTotalScore},</if>
            <if test="repairTotalScore != null">#{repairTotalScore},</if>
            <if test="finalScore != null">#{finalScore},</if>
            <if test="firstCheckStatus != null">#{firstCheckStatus},</if>
            <if test="firstChecker != null">#{firstChecker},</if>
            <if test="firstCheckTime != null">#{firstCheckTime},</if>
            <if test="secondCheckStatus != null">#{secondCheckStatus},</if>
            <if test="secondChecker != null">#{secondChecker},</if>
            <if test="secondCheckTime != null">#{secondCheckTime},</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="updateGxtUserScore" parameterType="GxtUserScore">
        update gxt_user_score
        <trim prefix="SET" suffixOverrides=",">
            <if test="userName != null and userName != ''">user_name = #{userName},</if>
            <if test="nickName != null and nickName != ''">nick_name = #{nickName},</if>
            <if test="monthPeriod != null and monthPeriod != ''">month_period = #{monthPeriod},</if>
            <if test="monthScoreId != null">month_score_id = #{monthScoreId},</if>
            <if test="maintenanceTotalScore != null">maintenance_total_score = #{maintenanceTotalScore},</if>
            <if test="repairTotalScore != null">repair_total_score = #{repairTotalScore},</if>
            <if test="finalScore != null">final_score = #{finalScore},</if>
            <if test="firstCheckStatus != null">first_check_status = #{firstCheckStatus},</if>
            <if test="firstChecker != null">first_checker = #{firstChecker},</if>
            <if test="firstCheckTime != null">first_check_time = #{firstCheckTime},</if>
            <if test="secondCheckStatus != null">second_check_status = #{secondCheckStatus},</if>
            <if test="secondChecker != null">second_checker = #{secondChecker},</if>
            <if test="secondCheckTime != null">second_check_time = #{secondCheckTime},</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="deleteGxtUserScoreById" parameterType="Long">
        delete from gxt_user_score where id = #{id}
    </delete>
    
    <delete id="deleteGxtUserScoreByIds" parameterType="String">
        delete from gxt_user_score where id in 
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>
</mapper>