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

    <select id="getList" parameterType="com.mortals.xhx.module.check.model.CheckAllRecordQuery" resultType="com.mortals.xhx.module.check.model.vo.CheckAllRecordVo">
        SELECT * FROM (
        SELECT id,recordId,staffId,staffName,workNum,deptId,deptName,ruleId,ruleName,subAddType,checkStatus,checkResult,score,subMethod,checkTime,deductTime,1 AS checkType ,'attend' AS performType FROM mortals_xhx_check_attend_record WHERE 1=1
        <if test="checkStatus != null and checkStatus!=''"> AND checkStatus = #{checkStatus} </if>
        <if test="subMethod != null and subMethod!=''"> AND subMethod = #{subMethod} </if>
        <if test="subAddType != null and subAddType!=''"> AND subAddType = #{subAddType} </if>
        <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>
        <if test="createUserId != null and createUserId!=''"> AND createUserId = #{createUserId} </if>
        UNION
        SELECT id,recordId,staffId,staffName,workNum,deptId,deptName,ruleId,ruleName,subAddType,checkStatus,checkResult,score,subMethod,checkTime,deductTime,4 AS checkType ,'effect' AS performType FROM mortals_xhx_check_effect_record WHERE 1=1
        <if test="checkStatus != null and checkStatus!=''"> AND checkStatus = #{checkStatus} </if>
        <if test="subMethod != null and subMethod!=''"> AND subMethod = #{subMethod} </if>
        <if test="subAddType != null and subAddType!=''"> AND subAddType = #{subAddType} </if>
        <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>
        <if test="createUserId != null and createUserId!=''"> AND createUserId = #{createUserId} </if>
        UNION
        SELECT id,recordId,staffId,staffName,workNum,deptId,deptName,ruleId,ruleName,subAddType,checkStatus,checkResult,score,subMethod,checkTime, deductTime,6 AS checkType ,'complain' AS performType FROM mortals_xhx_check_complain_record WHERE 1=1
        <if test="checkStatus != null and checkStatus!=''"> AND checkStatus = #{checkStatus} </if>
        <if test="subMethod != null and subMethod!=''"> AND subMethod = #{subMethod} </if>
        <if test="subAddType != null and subAddType!=''"> AND subAddType = #{subAddType} </if>
        <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>
        <if test="createUserId != null and createUserId!=''"> AND createUserId = #{createUserId} </if>
        UNION
        SELECT id,recordId,staffId,staffName,workNum,deptId,deptName,ruleId,ruleName, subAddType,checkStatus,checkResult,score,subMethod,checkTime, deductTime,3 AS checkType ,'gowork' AS performType FROM mortals_xhx_check_gowork_record WHERE 1=1
        <if test="checkStatus != null and checkStatus!=''"> AND checkStatus = #{checkStatus} </if>
        <if test="subMethod != null and subMethod!=''"> AND subMethod = #{subMethod} </if>
        <if test="subAddType != null and subAddType!=''"> AND subAddType = #{subAddType} </if>
        <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>
        <if test="createUserId != null and createUserId!=''"> AND createUserId = #{createUserId} </if>
        UNION
        SELECT id,recordId,staffId,staffName,workNum,deptId,deptName,ruleId,ruleName,subAddType,checkStatus,checkResult,score,subMethod,checkTime, deductTime,2 AS checkType ,'review' AS performType FROM mortals_xhx_check_review_record WHERE 1=1
        <if test="checkStatus != null and checkStatus!=''"> AND checkStatus = #{checkStatus} </if>
        <if test="subMethod != null and subMethod!=''"> AND subMethod = #{subMethod} </if>
        <if test="subAddType != null and subAddType!=''"> AND subAddType = #{subAddType} </if>
        <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>
        <if test="createUserId != null and createUserId!=''"> AND createUserId = #{createUserId} </if>
        UNION
        SELECT id,recordId,staffId,staffName,workNum,deptId,deptName,ruleId,ruleName,subAddType,checkStatus,checkResult,score,subMethod,checkTime, deductTime,5 AS checkType ,'other' AS performType FROM mortals_xhx_check_other_record WHERE 1=1
        <if test="checkStatus != null and checkStatus!=''"> AND checkStatus = #{checkStatus} </if>
        <if test="subMethod != null and subMethod!=''"> AND subMethod = #{subMethod} </if>
        <if test="subAddType != null and subAddType!=''"> AND subAddType = #{subAddType} </if>
        <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>
        <if test="createUserId != null and createUserId!=''"> AND createUserId = #{createUserId} </if>
        ) AS a
    </select>

    <select id="getListCount" parameterType="com.mortals.xhx.module.check.model.CheckAllRecordQuery" resultType="int">
        SELECT count(1) FROM (
        SELECT id,recordId,staffId,staffName,workNum,deptId,deptName,ruleId,ruleName,subAddType,score,subMethod,checkTime, deductTime,1 AS checkType ,'attend' AS performType FROM mortals_xhx_check_attend_record WHERE 1=1
        <if test="checkStatus != null and checkStatus!=''"> AND checkStatus = #{checkStatus} </if>
        <if test="subMethod != null and subMethod!=''"> AND subMethod = #{subMethod} </if>
        <if test="subAddType != null and subAddType!=''"> AND subAddType = #{subAddType} </if>
        <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>
        <if test="createUserId != null and createUserId!=''"> AND createUserId = #{createUserId} </if>
        UNION
        SELECT id,recordId,staffId,staffName,workNum,deptId,deptName,ruleId,ruleName,subAddType,score,subMethod,checkTime, deductTime,4 AS checkType ,'effect' AS performType FROM mortals_xhx_check_effect_record WHERE 1=1
        <if test="checkStatus != null and checkStatus!=''"> AND checkStatus = #{checkStatus} </if>
        <if test="subMethod != null and subMethod!=''"> AND subMethod = #{subMethod} </if>
        <if test="subAddType != null and subAddType!=''"> AND subAddType = #{subAddType} </if>
        <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>
        <if test="createUserId != null and createUserId!=''"> AND createUserId = #{createUserId} </if>
        UNION
        SELECT id,recordId,staffId,staffName,workNum,deptId,deptName,ruleId,ruleName,subAddType,score,subMethod,checkTime,deductTime,6 AS checkType ,'complain' AS performType FROM mortals_xhx_check_complain_record WHERE 1=1
        <if test="checkStatus != null and checkStatus!=''"> AND checkStatus = #{checkStatus} </if>
        <if test="subMethod != null and subMethod!=''"> AND subMethod = #{subMethod} </if>
        <if test="subAddType != null and subAddType!=''"> AND subAddType = #{subAddType} </if>
        <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>
        <if test="createUserId != null and createUserId!=''"> AND createUserId = #{createUserId} </if>
        UNION
        SELECT id,recordId,staffId,staffName,workNum,deptId,deptName,ruleId,ruleName,subAddType,score,subMethod,checkTime, deductTime,3 AS checkType ,'gowork' AS performType FROM mortals_xhx_check_gowork_record WHERE 1=1
        <if test="checkStatus != null and checkStatus!=''"> AND checkStatus = #{checkStatus} </if>
        <if test="subMethod != null and subMethod!=''"> AND subMethod = #{subMethod} </if>
        <if test="subAddType != null and subAddType!=''"> AND subAddType = #{subAddType} </if>
        <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>
        <if test="createUserId != null and createUserId!=''"> AND createUserId = #{createUserId} </if>
        UNION
        SELECT id,recordId,staffId,staffName,workNum,deptId,deptName,ruleId,ruleName, subAddType,score,subMethod,checkTime, deductTime,2 AS checkType ,'review' AS performType FROM mortals_xhx_check_review_record WHERE 1=1
        <if test="checkStatus != null and checkStatus!=''"> AND checkStatus = #{checkStatus} </if>
        <if test="subMethod != null and subMethod!=''"> AND subMethod = #{subMethod} </if>
        <if test="subAddType != null and subAddType!=''"> AND subAddType = #{subAddType} </if>
        <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>
        <if test="createUserId != null and createUserId!=''"> AND createUserId = #{createUserId} </if>
        UNION
        SELECT id,recordId,staffId,staffName,workNum,deptId,deptName,ruleId,ruleName,subAddType,score,subMethod,checkTime, deductTime,5 AS checkType ,'other' AS performType FROM mortals_xhx_check_other_record WHERE 1=1
        <if test="checkStatus != null and checkStatus!=''"> AND checkStatus = #{checkStatus} </if>
        <if test="subMethod != null and subMethod!=''"> AND subMethod = #{subMethod} </if>
        <if test="subAddType != null and subAddType!=''"> AND subAddType = #{subAddType} </if>
        <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>
        <if test="createUserId != null and createUserId!=''"> AND createUserId = #{createUserId} </if>
        ) AS a
    </select>

    <select id="getScoreSummary" parameterType="com.mortals.xhx.module.check.model.vo.StaffCheckSummaryQuery" resultType="com.mortals.xhx.module.check.model.vo.CheckAllScoreSummaryVo">
        SELECT sum(0-a.initScore) AS initScore,sum(0-a.checkScore) AS checkScore,sum(a.appealScore) AS appealScore,sum(0-a.checkScore-a.appealScore) AS total FROM (
        SELECT sum(CASE subMethod WHEN 4 THEN 0 ELSE (CASE subAddType WHEN 1 THEN score ELSE score*-1 END) END) as initScore,
        sum(CASE checkStatus WHEN 2 THEN (CASE subMethod WHEN 4 THEN 0  ELSE (CASE subAddType WHEN 1 THEN score ELSE score*-1 END) END) ELSE 0 END) checkScore,
        sum(CASE checkStatus WHEN 2 THEN (CASE subMethod WHEN 4 THEN (CASE subAddType WHEN 1 THEN score ELSE score*-1 END)  ELSE 0 END) ELSE 0 END) appealScore
        FROM mortals_xhx_check_attend_record WHERE 1=1
        <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>
        UNION

        SELECT sum(CASE subMethod WHEN 4 THEN 0 ELSE (CASE subAddType WHEN 1 THEN score ELSE score*-1 END) END) as initScore,
        sum(CASE checkStatus WHEN 2 THEN (CASE subMethod WHEN 4 THEN 0  ELSE (CASE subAddType WHEN 1 THEN score ELSE score*-1 END) END) ELSE 0 END) checkScore,
        sum(CASE checkStatus WHEN 2 THEN (CASE subMethod WHEN 4 THEN (CASE subAddType WHEN 1 THEN score ELSE score*-1 END)  ELSE 0 END) ELSE 0 END) appealScore
        FROM mortals_xhx_check_effect_record WHERE 1=1
        <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>
        UNION

        SELECT sum(CASE subMethod WHEN 4 THEN 0 ELSE (CASE subAddType WHEN 1 THEN score ELSE score*-1 END) END) as initScore,
        sum(CASE checkStatus WHEN 2 THEN (CASE subMethod WHEN 4 THEN 0  ELSE (CASE subAddType WHEN 1 THEN score ELSE score*-1 END) END) ELSE 0 END) checkScore,
        sum(CASE checkStatus WHEN 2 THEN (CASE subMethod WHEN 4 THEN (CASE subAddType WHEN 1 THEN score ELSE score*-1 END)  ELSE 0 END) ELSE 0 END) appealScore
        FROM mortals_xhx_check_complain_record WHERE 1=1
        <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>

        UNION
        SELECT sum(CASE subMethod WHEN 4 THEN 0 ELSE (CASE subAddType WHEN 1 THEN score ELSE score*-1 END) END) as initScore,
        sum(CASE checkStatus WHEN 2 THEN (CASE subMethod WHEN 4 THEN 0  ELSE (CASE subAddType WHEN 1 THEN score ELSE score*-1 END) END) ELSE 0 END) checkScore,
        sum(CASE checkStatus WHEN 2 THEN (CASE subMethod WHEN 4 THEN (CASE subAddType WHEN 1 THEN score ELSE score*-1 END)  ELSE 0 END) ELSE 0 END) appealScore
        FROM mortals_xhx_check_gowork_record WHERE 1=1
        <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>

        UNION
        SELECT sum(CASE subMethod WHEN 4 THEN 0 ELSE (CASE subAddType WHEN 1 THEN score ELSE score*-1 END) END) as initScore,
        sum(CASE checkStatus WHEN 2 THEN (CASE subMethod WHEN 4 THEN 0  ELSE (CASE subAddType WHEN 1 THEN score ELSE score*-1 END) END) ELSE 0 END) checkScore,
        sum(CASE checkStatus WHEN 2 THEN (CASE subMethod WHEN 4 THEN (CASE subAddType WHEN 1 THEN score ELSE score*-1 END)  ELSE 0 END) ELSE 0 END) appealScore
        FROM mortals_xhx_check_review_record WHERE 1=1
        <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>

        UNION
        SELECT sum(CASE subMethod WHEN 4 THEN 0 ELSE (CASE subAddType WHEN 1 THEN score ELSE score*-1 END) END) as initScore,
        sum(CASE checkStatus WHEN 2 THEN (CASE subMethod WHEN 4 THEN 0  ELSE (CASE subAddType WHEN 1 THEN score ELSE score*-1 END) END) ELSE 0 END) checkScore,
        sum(CASE checkStatus WHEN 2 THEN (CASE subMethod WHEN 4 THEN (CASE subAddType WHEN 1 THEN score ELSE score*-1 END)  ELSE 0 END) ELSE 0 END) appealScore
        FROM mortals_xhx_check_other_record WHERE 1=1
        <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>
        ) AS a
    </select>
</mapper>