-- 初始化证照实例缺失数据（组织/门店）
-- 规则：
-- 1) 组织（base_organize，category=company，未删除） -> businessLicense，subjectType=2（公司）
-- 2) 门店（ftb_store，未删除） -> hygieneLicense，subjectType=3（门店）
-- 3) status 固定为 1（缺失）
-- 4) 幂等：同一 (F_CertificateType, F_SubjectId) 已存在则不重复插入

INSERT INTO ftb_certificate_instance (
    F_Id,
    F_CertificateType,
    F_TemplateId,
    F_SubjectType,
    F_SubjectId,
    F_CertificateNo,
    F_IssueDate,
    F_ExpireDate,
    F_IsLongTerm,
    F_Status,
    F_CertificateImage,
    F_EnabledMark,
    F_CreatorUserId,
    F_CreatorTime,
    F_LastModifyUserId,
    F_LastModifyTime
)
SELECT
    REPLACE(UUID(), '-', '') AS F_Id,
    'businessLicense' AS F_CertificateType,
    '' AS F_TemplateId,
    2 AS F_SubjectType,
    bo.F_Id AS F_SubjectId,
    NULL AS F_CertificateNo,
    NULL AS F_IssueDate,
    NULL AS F_ExpireDate,
    0 AS F_IsLongTerm,
    1 AS F_Status,
    NULL AS F_CertificateImage,
    0 AS F_EnabledMark,
    NULL AS F_CreatorUserId,
    NOW() AS F_CreatorTime,
    NULL AS F_LastModifyUserId,
    NOW() AS F_LastModifyTime
FROM base_organize bo
LEFT JOIN ftb_certificate_instance ci
       ON ci.F_CertificateType = 'businessLicense'
      AND ci.F_SubjectId = bo.F_Id
WHERE bo.F_Category = 'company'
  AND (bo.F_DeleteMark = 0 OR bo.F_DeleteMark IS NULL)
  AND ci.F_Id IS NULL;


INSERT INTO ftb_certificate_instance (
    F_Id,
    F_CertificateType,
    F_TemplateId,
    F_SubjectType,
    F_SubjectId,
    F_StoreDisable,
    F_CertificateNo,
    F_IssueDate,
    F_ExpireDate,
    F_IsLongTerm,
    F_Status,
    F_CertificateImage,
    F_EnabledMark,
    F_CreatorUserId,
    F_CreatorTime,
    F_LastModifyUserId,
    F_LastModifyTime
)
SELECT
    REPLACE(UUID(), '-', '') AS F_Id,
    'hygieneLicense' AS F_CertificateType,
    NULL AS F_TemplateId,
    3 AS F_SubjectType,
    fs.F_Id AS F_SubjectId,
    fs.F_Disabled AS F_StoreDisable,
    NULL AS F_CertificateNo,
    NULL AS F_IssueDate,
    NULL AS F_ExpireDate,
    0 AS F_IsLongTerm,
    1 AS F_Status,
    NULL AS F_CertificateImage,
    0 AS F_EnabledMark,
    NULL AS F_CreatorUserId,
    NOW() AS F_CreatorTime,
    NULL AS F_LastModifyUserId,
    NOW() AS F_LastModifyTime
FROM ftb_store fs
LEFT JOIN ftb_certificate_instance ci
       ON ci.F_CertificateType = 'hygieneLicense'
      AND ci.F_SubjectId = fs.F_Id
      AND ci.F_TemplateId = ''
WHERE (fs.F_DeleteMark = 0 OR fs.F_DeleteMark IS NULL)
  AND ci.F_Id IS NULL;

INSERT INTO ftb_certificate_instance (
    F_Id,
    F_CertificateType,
    F_TemplateId,
    F_SubjectType,
    F_SubjectId,
    F_StoreDisable,
    F_CertificateNo,
    F_IssueDate,
    F_ExpireDate,
    F_IsLongTerm,
    F_Status,
    F_CertificateImage,
    F_EnabledMark,
    F_CreatorUserId,
    F_CreatorTime,
    F_LastModifyUserId,
    F_LastModifyTime
)
SELECT
    REPLACE(UUID(), '-', '') AS F_Id,
    'businessLicense' AS F_CertificateType,
    NULL AS F_TemplateId,
    3 AS F_SubjectType,
    fs.F_Id AS F_SubjectId,
    fs.F_Disabled AS F_StoreDisable,
    NULL AS F_CertificateNo,
    NULL AS F_IssueDate,
    NULL AS F_ExpireDate,
    0 AS F_IsLongTerm,
    1 AS F_Status,
    NULL AS F_CertificateImage,
    0 AS F_EnabledMark,
    NULL AS F_CreatorUserId,
    NOW() AS F_CreatorTime,
    NULL AS F_LastModifyUserId,
    NOW() AS F_LastModifyTime
FROM ftb_store fs
LEFT JOIN ftb_certificate_instance ci
       ON ci.F_CertificateType = 'businessLicense'
      AND ci.F_SubjectId = fs.F_Id
      AND ci.F_TemplateId = ''
WHERE (fs.F_DeleteMark = 0 OR fs.F_DeleteMark IS NULL)
  AND ci.F_Id IS NULL;
