<?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.FtbCultivateCertificateMapper">
    <!--统计组织列表信息-->
    <select id="organizationListStatistics"
            resultType="jnpf.model.cultivate.vo.certificate.FtbCertificateOrgWisdomStatisticVO">
        SELECT
        a.*
        FROM
        (
        SELECT
        ca.F_Id certificateId,
        ca.F_Name certificateName,
        us.F_OrganizeId AS orgID,
        ca.F_Status AS onShelfStatus,
        COUNT( DISTINCT us.F_UserId ) AS numberOfPeopleIssued,
        ca.F_CreatorTime,
        ca.F_LastModifyTime
        FROM
        ftb_cultivate_certificate_user AS us
        RIGHT JOIN ftb_cultivate_certificate ca ON us.F_CertificateId = ca.F_Id
        <where>
             ca.F_EnabledMark =  1 and us.F_EnabledMark=1
            <if test="statisticDTO.onShelfStatus != null and statisticDTO.onShelfStatus != ''">
                AND ca.F_Status = #{statisticDTO.onShelfStatus}
            </if>
            <if test="statisticDTO.startTime != null and statisticDTO.endTime != null">
                and us.F_CreatorTime BETWEEN #{statisticDTO.startTime} AND #{statisticDTO.endTime}
            </if>
            <if test="statisticDTO.innerPowerUserIds != null  and statisticDTO.innerPowerUserIds.size() > 0">
                and  us.F_UserId in
                <foreach collection="statisticDTO.innerPowerUserIds" item="userId" open="(" separator="," close=")">
                    #{userId}
                </foreach>
            </if>
            <if test="statisticDTO.chooseATypes != null  and statisticDTO.chooseATypes.size() > 0">
                and ca.F_Id in
                <foreach collection="statisticDTO.chooseATypes" item="chooseAType" open="(" separator="," close=")">
                    #{chooseAType}
                </foreach>
            </if>
        </where>
        GROUP BY
        us.F_OrganizeId,ca.F_Id
        ) a
        <where>
            a.numberOfPeopleIssued != 0
        </where>
        ORDER BY
        a.F_LastModifyTime DESC,
        a.F_CreatorTime DESC,
        a.certificateName DESC
    </select>
    <!--统计个人维度信息-->
    <select id="personalDimensionStatistics"
            resultType="jnpf.model.cultivate.vo.certificate.FtbCertificatePersonWisdomStatisticVO">
        SELECT
        user.F_Id,
        user.F_CertificateId,
        user.F_CertificateName certificateName,
        user.F_ExpireTime expirationTime,
        user.F_UserId employeeID,
        user.F_UserName employeeSName,
        user.F_OrganizeId orgID,
        user.F_PositionId sysPostId,
        user.F_EffectTime issueTime,
        user.F_Status effectiveStatus
        FROM
        ftb_cultivate_certificate_user as user inner join ftb_cultivate_certificate as ce
        on  user.F_CertificateId = ce.F_Id
        <where>
            ce.F_EnabledMark =  1 and user.F_EnabledMark=1
            <if test="dto.certificateId != null and dto.certificateId !=''">
            and  user.F_CertificateId =#{dto.certificateId}
            </if>

            <if test="dto.chooseATypes != null and dto.chooseATypes.size() > 0">
                and user.F_CertificateId in
                <foreach collection="dto.chooseATypes" item="item" open="(" separator="," close=")">
                    #{item}
                </foreach>
            </if>
            <if test="dto.effectiveStatus != null and    dto.effectiveStatus != 3 and dto.effectiveStatus != '' ">
                and  user.F_Status =#{dto.effectiveStatus}
            </if>
            <if test="dto.effectiveStatus != null and    dto.effectiveStatus == 3 and dto.effectiveStatus != ''  ">
                and  ce.F_ExpireType = 0
            </if>
            <if test="dto.startTime != null and dto.endTime != null">
                and user.F_CreatorTime BETWEEN #{dto.startTime} AND #{dto.endTime}
            </if>
            <if test="dto.realUserIds != null and dto.realUserIds.size() > 0">
                and user.F_UserId in
                <foreach collection="dto.realUserIds" item="userId" open="(" separator="," close=")">
                    #{userId}
                </foreach>
            </if>
        </where>
        GROUP BY
        user.F_UserId,user.F_CertificateId
        order by user.F_CreatorTime desc
    </select>
    <select id="organizationListStatisticsAll"
            resultType="jnpf.model.cultivate.vo.certificate.FtbCertificateQueryStatisticAllVO">

        SELECT
        us.F_Id certificateId,
        us.F_UserId userId,
        ca.F_Status AS onShelfStatus,
        ca.F_Name certificateName,
        ca.F_Id id
        FROM
        ftb_cultivate_certificate_user AS us
        RIGHT JOIN ftb_cultivate_certificate ca ON us.F_CertificateId = ca.F_Id
        <where>
            ca.F_EnabledMark =  1 and us.F_EnabledMark=1
            <if test="statisticDTO.onShelfStatus != null and statisticDTO.onShelfStatus != ''">
                AND ca.F_Status = #{statisticDTO.onShelfStatus}
            </if>
            <if test="statisticDTO.startTime != null and statisticDTO.endTime != null">
                and us.F_CreatorTime BETWEEN #{statisticDTO.startTime} AND #{statisticDTO.endTime}
            </if>

            <if test="statisticDTO.chooseATypes != null  and statisticDTO.chooseATypes.size() > 0">
                and ca.F_Id in
                <foreach collection="statisticDTO.chooseATypes" item="chooseAType" open="(" separator="," close=")">
                    #{chooseAType}
                </foreach>
            </if>
        </where>

    </select>
</mapper>