<?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="com.hotwj.platform.securityManagement.hiddenDangerPlan.mapper.HotHiddenDangerPlanMapper">
    <sql id="baseSelectWithStats">
        SELECT
            p.id,
            p.company_id,
            p.inspect_id,
            p.inspect_name,
            p.plan_name,
            p.start_date,
            p.end_date,
            p.is_recheck,
            p.recheck_start_time,
            p.recheck_end_time,
            p.progress,
            p.remark,
            p.is_deleted,
            p.is_auto_generated_by_backend,
            p.plan_date_key,
            COALESCE(stats.completed_count, 0) AS completedCount,
            COALESCE(stats.pending_count, 0) AS pendingCount
        FROM hot_hidden_danger_plan p
        LEFT JOIN (
            SELECT
                i.plan_id,
                SUM(CASE WHEN i.status IN (3, 9) THEN 1 ELSE 0 END) AS completed_count,
                SUM(CASE WHEN i.status IS NULL OR i.status NOT IN (3, 9) THEN 1 ELSE 0 END) AS pending_count
            FROM hot_hidden_danger_inspection i
            WHERE i.is_deleted = 0
            GROUP BY i.plan_id
        ) stats ON stats.plan_id = p.id
    </sql>

    <select id="queryPageList" resultType="com.hotwj.platform.securityManagement.hiddenDangerPlan.domain.vo.HotHiddenDangerPlanVo">
        <include refid="baseSelectWithStats"/>
        ${ew.customSqlSegment}
    </select>

    <select id="queryList" resultType="com.hotwj.platform.securityManagement.hiddenDangerPlan.domain.vo.HotHiddenDangerPlanVo">
        <include refid="baseSelectWithStats"/>
        ${ew.customSqlSegment}
    </select>
</mapper>
