<?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="jnpf.cultivate.mapper.CultivateIdentifyTableMapper">
    <!--web列表筛选条件查询-->
    <sql id="PAGE_LIST_WHERE">
        <where>
            <if test="groupIds!= null and groupIds.size()>0">
                fag.F_Id in
                <foreach collection="groupIds" item="item" open="(" separator="," close=")">
                    #{item}
                </foreach>
            </if>
            <if test="userIds!= null and userIds.size()>0">
                and fagu.F_UserId in
                <foreach collection="userIds" item="item" open="(" separator="," close=")">
                    #{item}
                </foreach>
            </if>
        </where>
    </sql>
    <!-- 鉴定表/列表-->
    <select id="getPageList" resultType="jnpf.model.cultivate.vo.identify.IdentifyTableListVo">
        SELECT fcit.F_Id id,
        fcit.F_Name `name`,
        fcit.F_RuleId ruleId,
        fcit.F_RuleDesc ruleDesc,
        count(fcii.F_Id) identifyCount,
        fcit.F_LastModifyTime lastModifyTime
        FROM ftb_cultivate_identify_table fcit
        LEFT JOIN ftb_cultivate_identify_items fcii on fcii.F_TableId = fcit.F_Id
        <where>
            fcit.F_DeleteMark = 0
            <if test="req.keyword != null and req.keyword != ''">
                AND (fcit.F_Name like concat('%', #{req.keyword}, '%') or fcit.F_RuleId like concat('%', #{req.keyword},
                '%') )
            </if>
        </where>
        GROUP BY fcit.F_Id
        ORDER BY fcit.F_CreatorTime DESC,fcit.F_RuleId ASC
    </select>
    <!-- 组织列表统计-->
    <select id="organizationListStatistics"
            resultType="jnpf.model.cultivate.vo.identify.FtbCultivateIdentityOrgWisdomStatisticVO">
        SELECT
        a.F_BeIdentifyOrgList organizationID ,
        a.numberOfPeopleBeingIdentified,
        a.F_TableId as tableId,
        a.F_Name as appraisalTableName,
        IFNULL( b.excellentNumber, 0 ) AS excellentNumber,
        ROUND( IFNULL(( b.excellentNumber / a.numberOfPeopleBeingIdentified ) * 100, 0 ), 2 ) excellentRate,
        IFNULL( c.qualifiedNumberOfPeople, 0 ) AS qualifiedNumberOfPeople,
        ROUND( IFNULL(( c.qualifiedNumberOfPeople / a.numberOfPeopleBeingIdentified ) * 100, 0 ), 2 ) passRate,
        IFNULL( d.numberOfUnqualifiedPeople, 0 ) AS numberOfUnqualifiedPeople,
        ROUND( IFNULL(( d.numberOfUnqualifiedPeople / a.numberOfPeopleBeingIdentified ) * 100, 0 ), 2 ) failureRate
        FROM
        (
        SELECT
        app.F_BeIdentifyOrgList,
        COUNT( ba.F_Id ) AS numberOfPeopleBeingIdentified,
        ba.F_TableId,
        ta.F_Name
        FROM
        ftb_cultivate_identify_apply AS app
        INNER JOIN ftb_cultivate_identify_apply_table_backups AS ba ON app.F_TableId = ba.F_Id
        INNER JOIN ftb_cultivate_identify_table AS ta ON ba.F_TableId = ta.F_Id
        <where>
                                                             app.F_DeleteMark=0
            <if test="dto.orgId != null and dto.orgId.size()>0">
                and  app.F_BeIdentifyOrgList in
                <foreach collection="dto.orgId" item="item" open="(" separator="," close=")">
                    #{item}
                </foreach>
            </if>
            <if test="dto.startTime != null and dto.endTime != null">
                and app.F_IdentifyTime BETWEEN #{dto.startTime} AND #{dto.endTime}
            </if>
            <if test="dto.chooseATypes!= null and dto.chooseATypes.size()>0">
                and ta.F_Id in
                <foreach collection="dto.chooseATypes" item="item" open="(" separator="," close=")">
                    #{item}
                </foreach>
            </if>
        </where>
        GROUP BY
        app.F_BeIdentifyOrgList,
        ba.F_TableId
        ) a
        LEFT JOIN (
        SELECT
        app.F_BeIdentifyOrgList,
        COUNT( app.F_Result ) AS excellentNumber,
        ba.F_TableId
        FROM
        ftb_cultivate_identify_apply AS app
        INNER JOIN ftb_cultivate_identify_apply_table_backups AS ba ON app.F_TableId = ba.F_Id
        WHERE
        app.F_DeleteMark=0 and
        app.F_Result = "1"
        GROUP BY
        app.F_BeIdentifyOrgList,
        ba.F_TableId
        ) b ON a.F_TableId = b.F_TableId
        AND a.F_BeIdentifyOrgList = b.F_BeIdentifyOrgList
        LEFT JOIN (
        SELECT
        app.F_BeIdentifyOrgList,
        COUNT( app.F_Result ) AS qualifiedNumberOfPeople,
        ba.F_TableId
        FROM
        ftb_cultivate_identify_apply AS app
        INNER JOIN ftb_cultivate_identify_apply_table_backups AS ba ON app.F_TableId = ba.F_Id
        WHERE
        app.F_Result = "0" and app.F_DeleteMark=0
        GROUP BY
        app.F_BeIdentifyOrgList,
        ba.F_TableId
        ) c ON a.F_TableId = c.F_TableId
        AND a.F_BeIdentifyOrgList = c.F_BeIdentifyOrgList
        LEFT JOIN (
        SELECT
        app.F_BeIdentifyOrgList,
        COUNT( app.F_Result ) AS numberOfUnqualifiedPeople,
        ba.F_TableId
        FROM
        ftb_cultivate_identify_apply AS app
        INNER JOIN ftb_cultivate_identify_apply_table_backups AS ba ON app.F_TableId = ba.F_Id
        WHERE
        app.F_Result = "2" and  app.F_DeleteMark=0
        GROUP BY
        app.F_BeIdentifyOrgList,
        ba.F_TableId
        ) d ON a.F_TableId = d.F_TableId
        AND a.F_BeIdentifyOrgList = d.F_BeIdentifyOrgList
        ORDER BY
        a.F_BeIdentifyOrgList DESC

    </select>
    <!-- 个人列表统计-->
    <select id="personListStatistics"
            resultType="jnpf.model.cultivate.vo.identify.FtbCultivateIdentityPersonWisdomStatisticVO">
        SELECT
        apy.F_Id as id,
        apy.F_TableId as tableId,
        ta.F_Name  as appraisalTableName,
        apy.F_BeIdentifyUserId as employeeID,
        apy.F_BeIdentifyOrgList as organizationID,
        apy.F_BeOfficialRankInfoJson as beIdentifyOrgList,
        apy.F_IdentifyUserId as examinerId,
        apy.F_IdentifyTime as identificationTime,
        apy.F_Status as authenticationStatus,
        apy.F_Result as identificationResults,
        apy.F_Source as authenticationType
        FROM
        ftb_cultivate_identify_apply AS apy
        INNER JOIN ftb_cultivate_identify_apply_table_backups AS ba ON apy.F_TableId = ba.F_Id
        INNER JOIN ftb_cultivate_identify_table AS ta ON ba.F_TableId = ta.F_Id
        <where>
            apy.F_DeleteMark = 0   and apy.F_IsVisible = 0
            <if test="dto.chooseATypes!= null and dto.chooseATypes.size()>0">
                AND  ta.F_Id in
                <foreach collection="dto.chooseATypes" item="item" open="(" separator="," close=")">
                    #{item}
                </foreach>
            </if>
            <if test="dto.selectPeoples != null and dto.selectPeoples.size()>0 and  (dto.appraiserOrAppraisee == '' or dto.appraiserOrAppraisee == null)  ">
                AND  apy.F_BeIdentifyUserId in
                <foreach collection="dto.selectPeoples" item="item" open="(" separator="," close=")">
                    #{item}
                </foreach>
            </if>
            <if test="dto.selectPeoples != null and dto.selectPeoples.size()>0 and  dto.appraiserOrAppraisee == 1">
                AND  apy.F_BeIdentifyUserId in
                <foreach collection="dto.selectPeoples" item="item" open="(" separator="," close=")">
                    #{item}
                </foreach>
            </if>
            <if test="dto.selectPeoples != null and dto.selectPeoples.size()>0 and  dto.appraiserOrAppraisee == 0">
                AND apy.F_IdentifyUserId in
                <foreach collection="dto.selectPeoples" item="item" open="(" separator="," close=")">
                    #{item}
                </foreach>
            </if>
            <if test="dto.leaverUserIds != null and dto.leaverUserIds.size()>0">
                AND apy.F_IdentifyUserId not in
                <foreach collection="dto.leaverUserIds" item="item" open="(" separator="," close=")">
                    #{item}
                </foreach>
            </if>

            <if test="dto.tableId != null and dto.tableId != ''">
                and  apy.F_TableId =#{dto.tableId}
            </if>
            <if test="dto.tableId != null and dto.tableId != ''">
                AND apy.F_Id = #{dto.tableId}
            </if>
            <if test="dto.startTime != null and dto.endTime != null">
                AND apy.F_IdentifyTime BETWEEN #{dto.startTime} AND #{dto.endTime}
            </if>
            <if test="dto.authenticationType != null and dto.authenticationType != ''">
                AND apy.F_Source = #{dto.authenticationType}
            </if>
            <if test="dto.identificationResults != null and dto.identificationResults != ''">
                AND apy.F_Result = #{dto.identificationResults}
            </if>
            <if test="dto.authenticationStatus != null and dto.authenticationStatus != ''">
                AND apy.F_Status = #{dto.authenticationStatus}
            </if>

        </where>


    </select>
    <!-- 获取评估人数量-->
    <select id="numberOfAppraisers" resultType="java.lang.String">
        SELECT F_PostRankId FROM ftb_cultivate_position_exam_identify
        WHERE F_EnabledMark = 0 and F_IdentifyId = #{tableId} AND F_PostRankId IN
        <foreach collection="postionIds" item="item" open="(" separator="," close=")">
            #{item}
        </foreach>
    </select>
    <!-- 查询当前人员的评估信息-->
    <select id="organizationListStatisticsForUser"
            resultType="jnpf.model.cultivate.vo.identify.FtbCultivateIdentityOrgWisdomStatisticVO">
        SELECT
        a.numberOfPeopleBeingIdentified,
        a.F_TableId as tableId,
        a.F_Name as appraisalTableName,
        IFNULL( b.excellentNumber, 0 ) AS excellentNumber,
        ROUND( IFNULL(( b.excellentNumber / a.numberOfPeopleBeingIdentified ) * 100, 0 ), 2 ) excellentRate,
        IFNULL( c.qualifiedNumberOfPeople, 0 ) AS qualifiedNumberOfPeople,
        ROUND( IFNULL(( c.qualifiedNumberOfPeople / a.numberOfPeopleBeingIdentified ) * 100, 0 ), 2 ) passRate,
        IFNULL( d.numberOfUnqualifiedPeople, 0 ) AS numberOfUnqualifiedPeople,
        ROUND( IFNULL(( d.numberOfUnqualifiedPeople / a.numberOfPeopleBeingIdentified ) * 100, 0 ), 2 ) failureRate
        FROM
        (
        SELECT
        COUNT( ba.F_Id ) AS numberOfPeopleBeingIdentified,
        ba.F_TableId,
        ta.F_Name
        FROM
        ftb_cultivate_identify_apply AS app
        INNER JOIN ftb_cultivate_identify_apply_table_backups AS ba ON app.F_TableId = ba.F_Id
        INNER JOIN ftb_cultivate_identify_table AS ta ON ba.F_TableId = ta.F_Id
        <where>
            app.F_DeleteMark = 0
            and app.F_IsVisible = 0
            <if test="dto.userIds != null and dto.userIds.size()>0">
                and  app.F_BeIdentifyUserId in
                <foreach collection="dto.userIds" item="item" open="(" separator="," close=")">
                    #{item}
                </foreach>
            </if>
            <if test="dto.startTime != null and dto.endTime != null">
                and app.F_IdentifyTime BETWEEN #{dto.startTime} AND #{dto.endTime}
            </if>
            <if test="dto.chooseATypes!= null and dto.chooseATypes.size()>0">
                and ta.F_Id in
                <foreach collection="dto.chooseATypes" item="item" open="(" separator="," close=")">
                    #{item}
                </foreach>
            </if>
        </where>
        GROUP BY
        ba.F_TableId
        ) a
        LEFT JOIN (
        SELECT
        COUNT( app.F_Result ) AS excellentNumber,
        ba.F_TableId
        FROM
        ftb_cultivate_identify_apply AS app
        INNER JOIN ftb_cultivate_identify_apply_table_backups AS ba ON app.F_TableId = ba.F_Id
        WHERE
        app.F_Result = "1"
        and app.F_DeleteMark = 0
        and app.F_IsVisible = 0
        <if test="dto.userIds != null and dto.userIds.size()>0">
            and  app.F_BeIdentifyUserId in
            <foreach collection="dto.userIds" item="item" open="(" separator="," close=")">
                #{item}
            </foreach>
        </if>
        <if test="dto.startTime != null and dto.endTime != null">
            and app.F_IdentifyTime BETWEEN #{dto.startTime} AND #{dto.endTime}
        </if>
        GROUP BY
        ba.F_TableId
        ) b ON a.F_TableId = b.F_TableId
        LEFT JOIN (
        SELECT
        COUNT( app.F_Result ) AS qualifiedNumberOfPeople,
        ba.F_TableId
        FROM
        ftb_cultivate_identify_apply AS app
        INNER JOIN ftb_cultivate_identify_apply_table_backups AS ba ON app.F_TableId = ba.F_Id
        WHERE
        app.F_Result = "0"
        and   app.F_DeleteMark = 0
        and app.F_IsVisible = 0
        <if test="dto.userIds != null and dto.userIds.size()>0">
            and  app.F_BeIdentifyUserId in
            <foreach collection="dto.userIds" item="item" open="(" separator="," close=")">
                #{item}
            </foreach>
        </if>
        <if test="dto.startTime != null and dto.endTime != null">
            and app.F_IdentifyTime BETWEEN #{dto.startTime} AND #{dto.endTime}
        </if>
        GROUP BY
        ba.F_TableId
        ) c ON a.F_TableId = c.F_TableId
        LEFT JOIN (
        SELECT
        COUNT( app.F_Result ) AS numberOfUnqualifiedPeople,
        ba.F_TableId
        FROM
        ftb_cultivate_identify_apply AS app
        INNER JOIN ftb_cultivate_identify_apply_table_backups AS ba ON app.F_TableId = ba.F_Id
        WHERE
        app.F_Result = "2"
        and   app.F_DeleteMark = 0
        and app.F_IsVisible = 0
        <if test="dto.userIds != null and dto.userIds.size()>0">
            and  app.F_BeIdentifyUserId in
            <foreach collection="dto.userIds" item="item" open="(" separator="," close=")">
                #{item}
            </foreach>
        </if>
        <if test="dto.startTime != null and dto.endTime != null">
            and app.F_IdentifyTime BETWEEN #{dto.startTime} AND #{dto.endTime}
        </if>
        GROUP BY
        ba.F_TableId
        ) d ON a.F_TableId = d.F_TableId
    </select>
    <!-- 查询资格信息-->
    <select id="queryQualificationInformation"
            resultType="jnpf.model.cultivate.vo.position.web.FtbCultivatePositionIdentifyVO">
        SELECT
        a.numberOfPeopleBeingIdentified,
        IFNULL( c.numberAppraisal, 0 ) AS numberAppraisal,
        ROUND( IFNULL(( c.numberAppraisal / a.numberOfPeopleBeingIdentified ) * 100, 0 ), 2 ) appraisalPassRate
        FROM
        (
        SELECT
        COUNT( app.F_Id ) AS numberOfPeopleBeingIdentified
        FROM
        ftb_cultivate_identify_apply AS app
        WHERE
        app.F_Source = 2
        AND app.F_DeleteMark = 0
        AND app.F_IsVisible = 0
        <if test="userIdsByGradesId != null and userIdsByGradesId.size()>0">
            and  app.F_BeIdentifyUserId in
            <foreach collection="userIdsByGradesId" item="item" open="(" separator="," close=")">
                #{item}
            </foreach>
        </if>
        AND app.F_Id = (
                SELECT * FROM (
                    SELECT
                    DISTINCT a.F_IdentifyRecordId
                    FROM
                    ftb_cultivate_position_identify_result a
                    JOIN ftb_cultivate_position b ON a.F_PostLearnId = b.F_Id
                    WHERE
                    a.F_EnabledMark = 0
                    AND b.F_EnabledMark = 0
                    AND a.F_PostRankId = #{postId}
                    AND a.F_UserId in
                    <foreach collection="userIdsByGradesId" item="item" open="(" separator="," close=")">
                        #{item}
                    </foreach>
                    ORDER BY a.F_CreatorTime DESC
                    limit 1
                    ) as abFIRI
                )
        ) a,(
        SELECT
        COUNT( app.F_Result ) AS numberAppraisal
        FROM
        ftb_cultivate_identify_apply AS app
        WHERE
        app.F_Result IN ( 0, 1 )
        AND app.F_Source = 2
        AND app.F_DeleteMark = 0
        AND app.F_IsVisible = 0
        <if test="userIdsByGradesId != null and userIdsByGradesId.size()>0">
            and  app.F_BeIdentifyUserId in
            <foreach collection="userIdsByGradesId" item="item" open="(" separator="," close=")">
                #{item}
            </foreach>
        </if>
        AND app.F_Id = (
            SELECT * FROM (
                SELECT
                DISTINCT a.F_IdentifyRecordId
                FROM
                ftb_cultivate_position_identify_result a
                JOIN ftb_cultivate_position b ON a.F_PostLearnId = b.F_Id
                WHERE
                a.F_EnabledMark = 0
                AND b.F_EnabledMark = 0
                AND a.F_PostRankId = #{postId}
                AND a.F_UserId in
                <foreach collection="userIdsByGradesId" item="item" open="(" separator="," close=")">
                    #{item}
                </foreach>
                ORDER BY a.F_CreatorTime DESC
                limit 1
            ) as abFIRI
        )
        ) c
    </select>
    <!-- 查询合格人员列表-->
    <select id="queryTheListOfQualifiedPersons"
            resultType="jnpf.model.cultivate.vo.position.web.FtbCultivatePositionIdentifyPersonVO">
        SELECT
        re.F_UserId userId,
        apy.F_Result isPassTheIdentification,
        SUM( de.F_Score ) identificationScore
        FROM
        ftb_cultivate_position_identify_result AS re
        INNER JOIN ftb_cultivate_identify_apply AS apy ON re.F_IdentifyRecordId = apy.F_Id
        INNER JOIN ftb_cultivate_identify_apply_details AS de ON de.F_ApplyId = apy.F_Id
        WHERE
        apy.F_BeIdentifyUserId IN
        <foreach collection="userIds" item="item" open="(" separator="," close=")">
            #{item}
        </foreach>
        AND re.F_PostRankId = #{positionId}
        AND apy.F_Status = 1
        AND apy.F_Source = 2
        AND apy.F_DeleteMark = 0
        AND apy.F_IsVisible = 0
        AND re.F_IdentifyId = (
        SELECT
        a.F_IdentifyId
        FROM
        ftb_cultivate_position_exam_identify a
        JOIN ftb_cultivate_position b ON a.F_PostLearnId = b.F_Id
        WHERE
        a.F_EnabledMark = 0
        AND b.F_EnabledMark = 0
        AND a.F_PostRankId = #{positionId}
        )
        GROUP BY
        re.F_UserId
    </select>
    <!-- 查询当前人员的评估信息-->
    <select id="currentPersonSAssessmentInformation"
            resultType="jnpf.model.cultivate.vo.position.web.FtbCultivatePositionIdentifyPersonVO">
        SELECT
        apy.F_BeIdentifyUserId userId,
        apy.F_Result isPassTheIdentification,
        SUM( de.F_Score ) identificationScore
        FROM
        ftb_cultivate_identify_apply AS apy
        INNER JOIN ftb_cultivate_identify_apply_details AS de ON de.F_ApplyId = apy.F_Id
        WHERE
        apy.F_BeIdentifyUserId  = #{userId}
        AND apy.F_Status = 1
        AND apy.F_Source = 2
        AND apy.F_DeleteMark = 0
        AND apy.F_IsVisible = 0
        AND apy.F_Id  in
        <foreach collection="applyIds" item="item" open="(" separator="," close=")">
            #{item}
        </foreach>

    </select>

    <select id="getPageListV2" resultType="jnpf.model.cultivate.v2.identify.vo.V2IdentifyTableListVo">
        SELECT fcit.F_Id id,
        fcit.F_Name `name`,
        fcit.F_RuleId ruleId,
        fcit.F_RuleDesc ruleDesc,
        fcit.F_LastModifyTime lastModifyTime,
        fcit.F_LabelId labelId,
        fl.F_Name labelName,
        fcit.F_CoverId coverId,
        fcit.F_CoverUrl coverUrl,
        fcit.F_IsAbnormal as isAbnormal
        FROM ftb_cultivate_identify_table fcit left join ftb_cultivate_label fl on fcit.F_LabelId = fl.F_Id
        <where>
            fcit.F_DeleteMark = 0
            <if test="params.keyWord != null and params.keyWord != ''">
                AND (fcit.F_Name like concat('%', #{params.keyWord}, '%') or fcit.F_RuleId like concat('%', #{params.keyWord},
                '%') )
            </if>
            <if test="params.isAbnormal != null">
                and  fcit.F_IsAbnormal = #{params.isAbnormal}
            </if>
            <if test="params.labelId != null and params.labelId != '' and '1'.toString() != params.labelId">
             and  fcit.F_LabelId = #{params.labelId}
            </if>
            <if test="params.labelId != null and params.labelId != '' and '1'.toString() == params.labelId">
                and  (fcit.F_LabelId = '' or fcit.F_LabelId is null)
            </if>
        </where>
        ORDER BY fcit.F_CreatorTime DESC,fcit.F_RuleId ASC
    </select>
</mapper>