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

    <select id="queryHealthManagePage" resultType="jnpf.model.certificate.vo.CertificateHealthManageVO">
        SELECT
            fci.F_SubjectId AS userId,
            fci.F_Id AS certificateInstanceId,
            fci.F_IssueDate AS issueDate,
            fci.F_ExpireDate AS expireDate,
            CASE
                WHEN fci.F_IsLongTerm = 1 OR fci.F_ExpireDate IS NULL THEN NULL
                ELSE DATEDIFF(fci.F_ExpireDate, CURDATE())
            END AS validDays,
            fci.F_Status AS certificateStatus,
            fci.F_CertificateImage AS certificateImage
        FROM ftb_certificate_instance fci
        <where>
            fci.F_EnabledMark = 0
            AND fci.F_SubjectType = 1
            AND fci.F_CertificateType = 'healthCertificate'
            <if test="req.userIds != null and req.userIds.size > 0">
                AND fci.F_SubjectId in
                <foreach collection="req.userIds" item="userId" open="(" separator="," close=")">
                    #{userId}
                </foreach>
            </if>

            <if test="req.issueStart != null and req.issueStart != ''">
                AND DATE(fci.F_IssueDate) <![CDATA[>=]]> #{req.issueStart}
            </if>
            <if test="req.issueEnd != null and req.issueEnd != ''">
                AND DATE(fci.F_IssueDate) <![CDATA[<=]]> #{req.issueEnd}
            </if>
            <if test="req.expireStart != null and req.expireStart != ''">
                AND DATE(fci.F_ExpireDate) <![CDATA[>=]]> #{req.expireStart}
            </if>
            <if test="req.expireEnd != null and req.expireEnd != ''">
                AND DATE(fci.F_ExpireDate) <![CDATA[<=]]> #{req.expireEnd}
            </if>
            <if test="req.certificateStatus != null">
                AND fci.F_Status = #{req.certificateStatus}
            </if>
        </where>
        ORDER BY fci.F_ExpireDate
    </select>

    <select id="queryHealthCertificateStatus" resultType="jnpf.model.certificate.dto.HealthCertificateStatusDTO">
        SELECT
            fci.F_Status AS status,
            fci.F_SubjectId as userId
        FROM ftb_certificate_instance fci
        <where>
            fci.F_EnabledMark = 0
            AND fci.F_SubjectType = 1
            AND fci.F_CertificateType = 'healthCertificate'
            <if test="userIds != null and userIds.size() > 0">
                AND fci.F_SubjectId in
                <foreach collection="userIds" item="userId" open="(" separator="," close=")">
                    #{userId}
                </foreach>
            </if>
        </where>
    </select>

    <select id="queryStoreDashboardStatusStats" resultType="jnpf.model.certificate.vo.CertificateHealthDashboardStatusStatVO">
        SELECT
            fci.F_SubjectId AS storeId,
            fci.F_Status AS status,
            COUNT(1) AS totalCount
        FROM ftb_certificate_instance fci
        <if test="storeType != null and storeType != ''">
            JOIN ftb_store fs ON fs.F_Id = fci.F_SubjectId
        </if>
        <where>
            fci.F_EnabledMark = 0
            AND fic.F_StoreDisable = 0
            AND fic.F_CertificateType = #{certificateType}
            <if test="templateId != null and templateId != ''">
                AND fci.F_TemplateId = #{templateId}
            </if>
            <if test="storeIds != null and storeIds.size() > 0">
                AND fci.F_SubjectId in
                <foreach collection="storeIds" item="sid" open="(" separator="," close=")">
                    #{sid}
                </foreach>
            </if>
            <if test="storeType != null and storeType != ''">
                AND fs.F_StoreTag = #{storeType}
            </if>
        </where>
        GROUP BY fci.F_Status
    </select>

    <select id="queryStoreDashboardTablePage" resultType="jnpf.model.certificate.vo.CertificateStoreCustomStatusTableVO">
        SELECT
            fci.F_SubjectId AS storeId,
            SUM(CASE WHEN fci.F_Status = 1 THEN 1 ELSE 0 END) AS missingCount,
            SUM(CASE WHEN fci.F_Status = 3 THEN 1 ELSE 0 END) AS nearExpireCount,
            SUM(CASE WHEN fci.F_Status = 2 THEN 1 ELSE 0 END) AS expiredCount,
            SUM(CASE WHEN fci.F_Status = 4 THEN 1 ELSE 0 END) AS normalCount,
            COUNT(1) AS totalCount
        FROM ftb_certificate_instance fci
        LEFT JOIN ftb_store fs ON fs.F_Id = fci.F_SubjectId
        <where>
            fci.F_EnabledMark = 0
            AND fci.F_StoreDisable = 0
            AND fci.F_CertificateType = #{certificateType}
            <if test="templateId != null and templateId != ''">
                AND fci.F_TemplateId = #{templateId}
            </if>
            <if test="storeIds != null and storeIds.size() > 0">
                AND fci.F_SubjectId in
                <foreach collection="storeIds" item="sid" open="(" separator="," close=")">
                    #{sid}
                </foreach>
            </if>
            <if test="storeType != null and storeType != ''">
                AND fs.F_StoreTag = #{storeType}
            </if>
        </where>
        GROUP BY fci.F_SubjectId
    </select>

    <insert id="batchInitHealthCertificate">
        insert into ftb_certificate_instance (
            F_Id,
            F_CertificateType,
            F_SubjectType,
            F_SubjectId,
            F_Status,
            F_EnabledMark
        )
        values
        <foreach collection="entities" item="entity" separator=",">
            (
                #{entity.id},
                #{entity.certificateType},
                #{entity.subjectType},
                #{entity.subjectId},
                #{entity.status},
                #{entity.enabledMark}
            )
        </foreach>
    </insert>

    <select id="selectInstanceById" resultType="jnpf.model.certificate.po.CertificateInstanceEntity">
        SELECT
            fci.F_Id AS id,
            fci.F_CertificateType AS certificateType,
            fci.F_TemplateId AS templateId,
            fci.F_TemplateStatus AS templateStatus,
            fci.F_SubjectType AS subjectType,
            fci.F_SubjectId AS subjectId,
            fci.F_CertificateNo AS certificateNo,
            fci.F_IssueDate AS issueDate,
            fci.F_ExpireDate AS expireDate,
            fci.F_ExpireTimestamp AS expireTimestamp,
            fci.F_IsLongTerm AS isLongTerm,
            fci.F_Status AS status,
            fci.F_CertificateImage AS certificateImage,
            fci.F_EnabledMark AS enabledMark
        FROM ftb_certificate_instance fci
        WHERE fci.F_Id = #{id}
          AND fci.F_EnabledMark = 0
    </select>

    <select id="selectBySubjectTypesAndStatusesFilterUser" resultType="jnpf.model.certificate.po.CertificateInstanceEntity">
        SELECT
            fci.F_Id AS id,
            fci.F_CertificateType AS certificateType,
            fci.F_TemplateId AS templateId,
            fci.F_TemplateStatus AS templateStatus,
            fci.F_SubjectType AS subjectType,
            fci.F_SubjectId AS subjectId,
            fci.F_CertificateNo AS certificateNo,
            fci.F_IssueDate AS issueDate,
            fci.F_ExpireDate AS expireDate,
            fci.F_ExpireTimestamp AS expireTimestamp,
            fci.F_IsLongTerm AS isLongTerm,
            fci.F_Status AS status,
            fci.F_CertificateImage AS certificateImage,
            fci.F_EnabledMark AS enabledMark
        FROM ftb_certificate_instance fci
        <where>
            fci.F_EnabledMark = 0
            <if test="subjectTypes != null and subjectTypes.size() > 0">
                AND fci.F_SubjectType IN
                <foreach collection="subjectTypes" item="subjectType" open="(" separator="," close=")">
                    #{subjectType}
                </foreach>
            </if>
            <if test="statuses != null and statuses.size() > 0">
                AND fci.F_Status IN
                <foreach collection="statuses" item="status" open="(" separator="," close=")">
                    #{status}
                </foreach>
            </if>
            <if test="userIds != null and userIds.size() > 0">
                AND fci.F_SubjectId in
                <foreach collection="userIds" item="userId" open="(" separator="," close=")">
                    #{userId}
                </foreach>
            </if>
        </where>
    </select>

    <select id="selectBySubjectTypesAndStatusesFilterOrganization" resultType="jnpf.model.certificate.po.CertificateInstanceEntity">
        SELECT
            fci.F_Id AS id,
            fci.F_CertificateType AS certificateType,
            fci.F_TemplateId AS templateId,
            fci.F_TemplateStatus AS templateStatus,
            fci.F_SubjectType AS subjectType,
            fci.F_SubjectId AS subjectId,
            fci.F_CertificateNo AS certificateNo,
            fci.F_IssueDate AS issueDate,
            fci.F_ExpireDate AS expireDate,
            fci.F_ExpireTimestamp AS expireTimestamp,
            fci.F_IsLongTerm AS isLongTerm,
            fci.F_Status AS status,
            fci.F_CertificateImage AS certificateImage,
            fci.F_EnabledMark AS enabledMark
        FROM ftb_certificate_instance fci
        <where>
            fci.F_EnabledMark = 0
            <if test="subjectTypes != null and subjectTypes.size() > 0">
                AND fci.F_SubjectType IN
                <foreach collection="subjectTypes" item="subjectType" open="(" separator="," close=")">
                    #{subjectType}
                </foreach>
            </if>
            <if test="statuses != null and statuses.size() > 0">
                AND fci.F_Status IN
                <foreach collection="statuses" item="status" open="(" separator="," close=")">
                    #{status}
                </foreach>
            </if>
            <if test="orgId != null and orgId != ''">
                and fci.F_SubjectId = #{orgId}
            </if>
        </where>
    </select>
    <select id="selectCertificateStoreManageVOPage"
            resultType="jnpf.model.certificate.vo.CertificateStoreManageVO">
        SELECT
        fci.F_Id as certificateInstanceId,
        fci.F_CertificateType AS certificateType,
        fci.F_TemplateId AS templateId,
        fci.F_TemplateStatus AS templateStatus,
        fci.F_SubjectType AS subjectType,
        fci.F_SubjectId AS orgId,
        fci.F_CertificateNo AS certificateNo,
        fci.F_IssueDate AS issueDate,
        fci.F_ExpireDate AS expireDate,
        fci.F_IsLongTerm AS isLongTerm,
        fci.F_Status AS certificateStatus,
        fci.F_CertificateImage AS thumbnail,
        fci.F_EnabledMark AS enabledMark
        FROM ftb_certificate_instance fci
        <where>
            fci.F_EnabledMark = 0
            <if test="queryReq.orgIds != null and queryReq.orgIds.size() > 0">
                AND fci.F_SubjectId IN
                <foreach collection="queryReq.orgIds" item="orgId" open="(" separator="," close=")">
                    #{orgId}
                </foreach>
            </if>

            <if test="queryReq.certificateType != null and queryReq.certificateType != ''">
                and fci.F_CertificateType = #{queryReq.certificateType}
            </if>

            <if test="queryReq.templateId != null and queryReq.templateId != ''">
                and fci.F_TemplateId = #{queryReq.templateId}
            </if>

            <if test="queryReq.certificateStatus != null">
                and fci.F_Status = #{queryReq.certificateStatus}
            </if>
        </where>
    </select>

</mapper>
