-- 存量隐患排查数据迁移为“审核不通过”状态，并补写驳回历史
-- 使用前请先确认：
-- 1. 已执行 update_hidden_danger_inspection_reject_history.sql
-- 2. 首次执行前建议备份 hot_hidden_danger_inspection 与 hot_hidden_danger_inspection_audit_history
-- 3. 本脚本默认将所有未删除的老数据统一初始化为第 1 次驳回

START TRANSACTION;

-- 1. 将主表老数据统一调整为驳回状态
UPDATE `hot_hidden_danger_inspection`
SET `submit_version` = IFNULL(NULLIF(`submit_version`, 0), 1),
    `audit_result` = 'REJECT',
    `reject_count` = CASE
        WHEN `reject_count` IS NULL OR `reject_count` = 0 THEN 1
        ELSE `reject_count`
    END,
    `last_reject_reason` = CASE
        WHEN `last_reject_reason` IS NULL OR `last_reject_reason` = '' THEN '历史数据初始化为审核不通过，请驾驶员重新编辑后提交'
        ELSE `last_reject_reason`
    END,
    `last_reject_time` = COALESCE(`last_reject_time`, `audit_date`, `update_time`, `create_time`, NOW()),
    `last_reject_by_id` = COALESCE(`last_reject_by_id`, `approver_id`),
    `last_reject_by_name` = COALESCE(NULLIF(`last_reject_by_name`, ''), `approver_name`),
    `flow_status` = 'REJECTED',
    `instance_id` = NULL,
    `status` = 10
WHERE `is_deleted` = 0;

-- 2. 将老数据写入驳回历史表
-- 说明：
-- - 每条主单补 1 条“第 1 次驳回”历史
-- - 已存在 inspection_id + audit_round=1 的记录时不会重复插入
SET @history_max_id := (SELECT IFNULL(MAX(`id`), 0) FROM `hot_hidden_danger_inspection_audit_history`);

INSERT INTO `hot_hidden_danger_inspection_audit_history` (
    `id`,
    `inspection_id`,
    `company_id`,
    `plan_id`,
    `project_name`,
    `project_id`,
    `project_type`,
    `submit_version`,
    `audit_round`,
    `audit_result`,
    `reject_reason`,
    `audit_date`,
    `approver_id`,
    `approver_name`,
    `approver_sign_img_url`,
    `audit_has_danger`,
    `submit_snapshot_json`,
    `check_content_json`,
    `danger_desc`,
    `attachment_url`,
    `sign_img_url`,
    `checker_id`,
    `checker_name`,
    `check_date`,
    `create_dept`,
    `create_by`,
    `create_by_name`,
    `create_time`,
    `update_by`,
    `update_by_name`,
    `update_time`,
    `is_deleted`,
    `remark`
)
SELECT
    (@history_max_id := @history_max_id + 1) AS `id`,
    i.`id` AS `inspection_id`,
    i.`company_id`,
    i.`plan_id`,
    i.`project_name`,
    i.`project_id`,
    i.`project_type`,
    IFNULL(NULLIF(i.`submit_version`, 0), 1) AS `submit_version`,
    1 AS `audit_round`,
    'REJECT' AS `audit_result`,
    COALESCE(NULLIF(i.`last_reject_reason`, ''), '历史数据初始化为审核不通过，请驾驶员重新编辑后提交') AS `reject_reason`,
    COALESCE(i.`last_reject_time`, i.`audit_date`, i.`update_time`, i.`create_time`, NOW()) AS `audit_date`,
    COALESCE(i.`last_reject_by_id`, i.`approver_id`) AS `approver_id`,
    COALESCE(NULLIF(i.`last_reject_by_name`, ''), i.`approver_name`) AS `approver_name`,
    i.`approver_sign_img_url`,
    i.`audit_has_danger`,
    JSON_OBJECT(
        'inspectionId', i.`id`,
        'companyId', i.`company_id`,
        'planId', i.`plan_id`,
        'projectName', i.`project_name`,
        'projectId', i.`project_id`,
        'projectType', i.`project_type`,
        'submitVersion', IFNULL(NULLIF(i.`submit_version`, 0), 1),
        'checkContentJson', i.`check_content_json`,
        'dangerDesc', i.`danger_desc`,
        'attachmentUrl', i.`attachment_url`,
        'signImgUrl', i.`sign_img_url`,
        'checkerId', i.`checker_id`,
        'checkerName', i.`checker_name`,
        'checkDate', i.`check_date`
    ) AS `submit_snapshot_json`,
    i.`check_content_json`,
    i.`danger_desc`,
    i.`attachment_url`,
    i.`sign_img_url`,
    i.`checker_id`,
    i.`checker_name`,
    i.`check_date`,
    i.`create_dept`,
    COALESCE(i.`update_by`, i.`create_by`) AS `create_by`,
    NULL AS `create_by_name`,
    COALESCE(i.`update_time`, i.`create_time`, NOW()) AS `create_time`,
    i.`update_by`,
    NULL AS `update_by_name`,
    i.`update_time`,
    0 AS `is_deleted`,
    '历史数据初始化补录' AS `remark`
FROM `hot_hidden_danger_inspection` i
WHERE i.`is_deleted` = 0
  AND NOT EXISTS (
      SELECT 1
      FROM `hot_hidden_danger_inspection_audit_history` h
      WHERE h.`inspection_id` = i.`id`
        AND h.`audit_round` = 1
        AND h.`is_deleted` = 0
  );

COMMIT;

-- 校验SQL
-- SELECT COUNT(*) FROM hot_hidden_danger_inspection WHERE is_deleted = 0 AND audit_result = 'REJECT' AND status = 10;
-- SELECT COUNT(*) FROM hot_hidden_danger_inspection_audit_history WHERE is_deleted = 0;



update hot_hidden_danger_inspection set status = 1
update hot_hidden_danger_inspection set status = 10 where approver_id is not null
