<?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.certificate.mapper.CertificateAppRiskMapper">

    <select id="queryHealthRiskStatusCount" resultType="jnpf.model.certificate.vo.app.CertificateAppStatusCountVO">
        SELECT
            ci.F_Status AS status,
            COUNT(1) AS count
        FROM ftb_certificate_instance ci
        <where>
            ci.F_EnabledMark = 0
            AND ci.F_CertificateType = 'healthCertificate'
            AND ci.F_SubjectType = 1
            <if test="req.status != null and req.status != ''" >
                AND ci.F_Status = #{req.status}
            </if>
            <if test="req.userIds != null and req.userIds.size > 0">
                AND ci.F_SubjectId in
                <foreach collection="req.userIds" item="userId" open="(" separator="," close=")">
                    #{userId}
                </foreach>
            </if>
        </where>
        GROUP by ci.F_Status
    </select>

    <select id="queryStoreRiskStatusCount" resultType="jnpf.model.certificate.vo.app.CertificateAppStatusCountVO">
        SELECT
            ci.F_Status AS status,
            COUNT(1) AS count
        FROM ftb_certificate_instance ci
        <where>
            ci.F_EnabledMark = 0
            <if test="req.status != null and req.status != ''" >
                AND ci.F_Status = #{req.status}
            </if>
            AND ci.F_SubjectType IN (2, 3)
            <if test="req.certificateType != null and req.certificateType != ''">
                AND ci.F_CertificateType = #{req.certificateType}
            </if>
            <if test="req.orgId != null and req.orgId != ''">
                AND ci.F_SubjectId = #{req.orgId}
            </if>
        </where>
        GROUP BY ci.F_Status
    </select>

    <select id="queryEmployeeRiskPageByUserIds" resultType="jnpf.model.certificate.vo.app.CertificateAppEmployeeRiskVO">
        SELECT
            ci.F_SubjectId AS userId,
            ci.F_Id AS certificateInstanceId,
            ci.F_Status AS status,
            ci.F_IssueDate AS issueDate,
            ci.F_ExpireDate AS expireDate
        FROM ftb_certificate_instance ci
        <where>
            ci.F_EnabledMark = 0
            AND ci.F_Status IN (1, 2, 3)
            AND ci.F_CertificateType = 'healthCertificate'
            AND ci.F_SubjectType = 1
            <if test="userIds != null and userIds.size > 0">
                AND ci.F_SubjectId IN
                <foreach collection="userIds" item="userId" open="(" separator="," close=")">
                    #{userId}
                </foreach>
            </if>
        </where>
        ORDER BY ci.F_Status ASC,
                 ci.F_ExpireDate ASC
    </select>

    <select id="queryStoreRiskPage" resultType="jnpf.model.certificate.vo.app.CertificateAppStoreRiskVO">
        SELECT
            ci.F_SubjectId as subjectId,
            MAX(ci.F_SubjectType) AS subjectType
        FROM ftb_certificate_instance ci
        <where>
            ci.F_EnabledMark = 0
            AND ci.F_SubjectType IN (2, 3)
            AND ci.F_Status IN (1, 2, 3)
            AND ci.F_TemplateStatus = 1
            <if test="req.certificateType != null and req.certificateType != ''">
                AND ci.F_CertificateType = #{req.certificateType}
            </if>
            <if test="req.orgId != null and req.orgId != ''">
                AND ci.F_SubjectId = #{req.orgId}
            </if>
        </where>
        GROUP BY ci.F_SubjectId
        ORDER BY MIN(ci.F_Status) ASC,
                 MIN(ci.F_ExpireDate) ASC
    </select>

    <select id="queryStoreRiskDetailList" resultType="jnpf.model.certificate.vo.app.CertificateAppStoreRiskItemVO">
        SELECT
            ci.F_SubjectId AS subjectId,
            ci.F_Id AS certificateInstanceId,
            ci.F_CertificateType AS certificateType,
            ci.F_Status AS status,
            ci.F_IssueDate AS issueDate,
            ci.F_ExpireDate AS expireDate,
            ci.F_TemplateId AS templateId
        FROM ftb_certificate_instance ci
        <where>
            ci.F_EnabledMark = 0
            AND ci.F_Status IN (1, 2, 3)
            AND ci.F_SubjectId IN
            <foreach collection="subjectIds" item="subjectId" open="(" separator="," close=")">
                #{subjectId}
            </foreach>
        </where>
    </select>

    <select id="queryEmployeeRiskTotalCount" resultType="java.lang.Long">
        SELECT COUNT(DISTINCT ci.F_SubjectId)
        FROM ftb_certificate_instance ci
        <where>
            ci.F_EnabledMark = 0
            AND ci.F_Status IN (1, 2, 3)
            AND ci.F_CertificateType = 'healthCertificate'
            AND ci.F_SubjectType = 1
            <if test="userIds != null and userIds.size() > 0">
                AND ci.F_SubjectId IN
                <foreach collection="userIds" item="userId" open="(" separator="," close=")">
                    #{userId}
                </foreach>
            </if>
        </where>
    </select>

    <select id="queryOrganizationRiskTotalCount" resultType="java.lang.Long">
        SELECT COUNT(DISTINCT ci.F_SubjectId,ci.F_CertificateType,ci.F_TemplateId)
        FROM ftb_certificate_instance ci
        <where>
            ci.F_EnabledMark = 0
            AND ci.F_SubjectType IN (2, 3)
            AND ci.F_Status IN (1, 2, 3)
            and ci.F_TemplateStatus = 1
            <if test="orgIds != null and orgIds.size() > 0">
                AND ci.F_SubjectId in
                <foreach collection="orgIds" item="orgId" open="(" separator="," close=")">
                    #{orgId}
                </foreach>
            </if>
        </where>
    </select>

</mapper>
