<?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.sundot.airport.ledger.mapper.ScoreEmployeeAdditionalMapper">
    
    <resultMap type="ScoreEmployeeAdditional" id="ScoreEmployeeAdditionalResult">
        <id     property="id"              column="id"              />
        <result property="deptId"          column="dept_id"         />
        <result property="deptName"        column="dept_name"       />
        <result property="teamId"          column="team_id"         />
        <result property="teamName"        column="team_name"       />
        <result property="groupId"         column="group_id"        />
        <result property="groupName"       column="group_name"      />
        <result property="userId"          column="user_id"         />
        <result property="userName"        column="user_name"       />
        <result property="monthOfRecord"   column="month_of_record" />
        <result property="type"            column="type"            />
        <result property="score"           column="score"           />
        <result property="remark"          column="remark"          />
        <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="delFlag"         column="del_flag"        />
    </resultMap>

    <sql id="selectScoreEmployeeAdditionalVo">
        select id, dept_id, dept_name, team_id, team_name, group_id, group_name, 
               user_id, user_name, month_of_record, type, score, remark, 
               create_by, create_time, update_by, update_time, del_flag
        from score_employee_additional
    </sql>

    <select id="selectScoreEmployeeAdditionalList" parameterType="ScoreEmployeeAdditional" resultMap="ScoreEmployeeAdditionalResult">
        <include refid="selectScoreEmployeeAdditionalVo"/>
        <where>  
            del_flag = '0'
            <if test="deptId != null "> and dept_id = #{deptId}</if>
            <if test="deptName != null  and deptName != ''"> and dept_name like concat('%', #{deptName}, '%')</if>
            <if test="teamId != null "> and team_id = #{teamId}</if>
            <if test="teamName != null  and teamName != ''"> and team_name like concat('%', #{teamName}, '%')</if>
            <if test="groupId != null "> and group_id = #{groupId}</if>
            <if test="groupName != null  and groupName != ''"> and group_name like concat('%', #{groupName}, '%')</if>
            <if test="userId != null "> and user_id = #{userId}</if>
            <if test="userName != null  and userName != ''"> and user_name like concat('%', #{userName}, '%')</if>
            <if test="monthOfRecord != null  and monthOfRecord != ''"> and month_of_record = #{monthOfRecord}</if>
            <if test="type != null "> and type = #{type}</if>
        </where>
        order by create_time desc
    </select>

    <select id="selectTotalScoreByEmployee" parameterType="ScoreEmployeeAdditional" resultType="com.sundot.airport.ledger.domain.vo.EmployeeAdditionalScoreVO">
        select 
            user_id as userId,
            user_name as userName,
            dept_id as deptId,
            dept_name as deptName,
            team_id as teamId,
            team_name as teamName,
            group_id as groupId,
            group_name as groupName,
            sum(
                case 
                    when type = 0 then score
                    when type = 1 then -score
                    else 0 
                end
            ) as totalScore
        from score_employee_additional
        <where>
            del_flag = '0'
            <if test="deptId != null "> and dept_id = #{deptId}</if>
            <if test="deptName != null  and deptName != ''"> and dept_name like concat('%', #{deptName}, '%')</if>
            <if test="teamId != null "> and team_id = #{teamId}</if>
            <if test="teamName != null  and teamName != ''"> and team_name like concat('%', #{teamName}, '%')</if>
            <if test="groupId != null "> and group_id = #{groupId}</if>
            <if test="groupName != null  and groupName != ''"> and group_name like concat('%', #{groupName}, '%')</if>
            <if test="userId != null "> and user_id = #{userId}</if>
            <if test="userName != null  and userName != ''"> and user_name like concat('%', #{userName}, '%')</if>
            <if test="monthOfRecord != null  and monthOfRecord != ''"> and month_of_record = #{monthOfRecord}</if>
            <if test="type != null "> and type = #{type}</if>
        </where>
        group by user_id, user_name, dept_id, dept_name, team_id, team_name, group_id, group_name
        order by totalScore desc
    </select>
</mapper>