<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "mybatis-3-mapper.dtd">
<mapper namespace="com.mortals.xhx.module.check.dao.ibatis.CheckReviewRecordDaoImpl">
    <!-- 汇总已审核的核查记录 -->
    <select id="getSummaryCheckList" parameterType="com.mortals.xhx.module.check.model.vo.StaffCheckSummaryQuery" resultType="com.mortals.xhx.module.check.model.vo.StaffCheckSummaryVo">
        SELECT
            staffId,staffName,workNum,deptId,deptName,
            ANY_VALUE(DATE_FORMAT(checkTime,'%Y')) as `year`,
            ANY_VALUE(DATE_FORMAT(checkTime,'%m')) as `month`,
            SUM(CASE subAddType WHEN 1 THEN score ELSE score*-1 END) as sumScore
        FROM
            mortals_xhx_check_review_record
        WHERE checkStatus = 2 and auditStatus = 1
        <if test="staffId != null and staffId!=''"> AND staffId = #{staffId} </if>
        <if test="checkTimeStart != null and checkTimeStart!=''"> AND checkTime <![CDATA[ >= ]]> STR_TO_DATE(left(concat(#{checkTimeStart},' 00:00:00'),19),'%Y-%m-%d %k:%i:%s') </if>
        <if test="checkTimeEnd != null and checkTimeEnd!=''"> AND checkTime <![CDATA[ <= ]]> STR_TO_DATE(left(concat(#{checkTimeEnd},' 23:59:59'),19),'%Y-%m-%d %k:%i:%s') </if>
        GROUP BY
            staffId,staffName,workNum,deptId,deptName,DATE_FORMAT(checkTime,'%Y-%m')
    </select>

    <!-- 个人绩效统计 -->
    <select id="getStaffPerformStat" parameterType="com.mortals.xhx.module.check.model.vo.StaffCheckSummaryQuery" resultType="com.mortals.xhx.module.staff.model.StaffPerformStatEntity">
        SELECT
        staffId,staffName,workNum,deptId,deptName,
        ANY_VALUE(DATE_FORMAT(checkTime,'%Y')) as `year`,
        ANY_VALUE(DATE_FORMAT(checkTime,'%m')) as `month`,
        ANY_VALUE(DATE_FORMAT(checkTime,'%d')) as `day`,
        SUM(CASE subAddType WHEN 1 THEN score ELSE 0 END) as reviewScoreAdd,
        SUM(CASE subAddType WHEN 2 THEN score ELSE 0 END) as reviewScoreSub
        FROM
        mortals_xhx_check_review_record
        WHERE checkStatus = 2 and auditStatus = 1
        <if test="staffId != null and staffId!=''"> AND staffId = #{staffId} </if>
        <if test="checkTimeStart != null and checkTimeStart!=''"> AND checkTime <![CDATA[ >= ]]> STR_TO_DATE(left(concat(#{checkTimeStart},' 00:00:00'),19),'%Y-%m-%d %k:%i:%s') </if>
        <if test="checkTimeEnd != null and checkTimeEnd!=''"> AND checkTime <![CDATA[ <= ]]> STR_TO_DATE(left(concat(#{checkTimeEnd},' 23:59:59'),19),'%Y-%m-%d %k:%i:%s') </if>
        GROUP BY
        staffId,staffName,workNum,deptId,deptName,DATE_FORMAT(checkTime,'%Y-%m-%d');
    </select>

    <!-- 部门绩效统计 -->
    <select id="getDeptPerformStat" parameterType="com.mortals.xhx.module.check.model.vo.StaffCheckSummaryQuery" resultType="com.mortals.xhx.module.dept.model.DeptPerformStatEntity">
        SELECT
        deptId,deptName,
        ANY_VALUE(DATE_FORMAT(checkTime,'%Y')) as `year`,
        ANY_VALUE(DATE_FORMAT(checkTime,'%m')) as `month`,
        ANY_VALUE(DATE_FORMAT(checkTime,'%d')) as `day`,
        SUM(CASE subAddType WHEN 1 THEN score ELSE 0 END) as reviewScoreAdd,
        SUM(CASE subAddType WHEN 2 THEN score ELSE 0 END) as reviewScoreSub
        FROM
        mortals_xhx_check_review_record
        WHERE checkStatus = 2 and auditStatus = 1
        <if test="staffId != null and staffId!=''"> AND staffId = #{staffId} </if>
        <if test="checkTimeStart != null and checkTimeStart!=''"> AND checkTime <![CDATA[ >= ]]> STR_TO_DATE(left(concat(#{checkTimeStart},' 00:00:00'),19),'%Y-%m-%d %k:%i:%s') </if>
        <if test="checkTimeEnd != null and checkTimeEnd!=''"> AND checkTime <![CDATA[ <= ]]> STR_TO_DATE(left(concat(#{checkTimeEnd},' 23:59:59'),19),'%Y-%m-%d %k:%i:%s') </if>
        GROUP BY
        deptId,deptName,DATE_FORMAT(checkTime,'%Y-%m-%d');
    </select>
</mapper>