<?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.yeejoin.amos.supervision.business.dao.mapper.CheckReportMapper">

    <select id="queryByPlanTaskId" resultType="com.yeejoin.amos.supervision.business.dto.CheckReportMapperDto">
        SELECT
        count( DISTINCT ptd.id ) companyCount,
        ptd.task_no planTaskId,
        GROUP_CONCAT( DISTINCT executor_name ) checkUser,
        GROUP_CONCAT( DISTINCT c.check_time ) checkTime,
        pt.begin_time planBeginDate,
        pt.end_time planEndDate,
        GROUP_CONCAT( DISTINCT hd.latent_danger_id ) latentDangerId,
        GROUP_CONCAT( DISTINCT hd.point_id ) problemPointId,
        IF
        ( count( DISTINCT ptd.major_danger_num ) > 0, 1, 0 ) hasMajorDanger
        FROM
        `p_plan_task_detail` ptd
        LEFT JOIN p_check c ON c.plan_task_detail_id = ptd.id
        LEFT JOIN p_plan_task pt ON pt.id = ptd.task_no
        LEFT JOIN p_hidden_danger hd ON hd.check_id = c.id
        <where>
            <if test="planTaskId !=null and planTaskId != ''">ptd.task_no = #{planTaskId}</if>
        </where>
    </select>
    <select id="selectPageList" resultType="com.yeejoin.amos.supervision.core.common.dto.CheckReportDto">
        select cr.*,
        p.name planName,
        p.check_type_name planCheckType,
        p.check_level planCheckLevel
        from p_check_report cr
        LEFT JOIN p_plan_task pt ON pt.id = cr.plan_task_id
        LEFT JOIN p_plan p ON p.id = pt.plan_id
        <where>
            1=1
            <if test="queryParam != null and queryParam.name != null and queryParam.name != ''">
                and cr.name like concat('%', #{queryParam.name}, '%')
            </if>
            <if test="queryParam != null and queryParam.planName != null and queryParam.planName != ''">
                and p.name like concat('%', #{queryParam.planName}, '%')
            </if>
            <if test="queryParam != null and queryParam.planCheckType != null and queryParam.planCheckType != ''">
                and p.check_type_id = #{queryParam.planCheckType}
            </if>
            <if test="queryParam != null and queryParam.checkLevel != null and queryParam.checkLevel != ''">
                and p.check_level = #{queryParam.checkLevel}
            </if>
            <if test="queryParam != null and queryParam.startCheckTime != null">
                and cr.start_check_date <![CDATA[>=]]> #{queryParam.startCheckTime}
            </if>
            <if test="queryParam != null and queryParam.endCheckTime != null">
                and cr.end_check_date <![CDATA[<=]]> #{queryParam.endCheckTime}
            </if>
            <if test="queryParam != null and queryParam.checkUser != null and queryParam.checkUser != ''">
                and cr.check_user like concat('%', #{queryParam.checkUser}, '%')
            </if>
            <if test="queryParam != null and queryParam.orgCode != null and queryParam.orgCode != ''">
                and cr.org_code like concat(#{queryParam.orgCode}, '%')
            </if>
        </where>
        order by cr.create_date desc
    </select>
    <select id="selectDetailById" resultType="com.yeejoin.amos.supervision.core.common.dto.CheckReportDto">
        select cr.*,
               p.name                                 planName,
               p.check_type_name                      planCheckType,
               p.check_level                          planCheckLevel,
               DATE_FORMAT(pt.begin_time, '%Y-%m-%d') startPlanTaskDate,
               DATE_FORMAT(pt.end_time, '%Y-%m-%d')   endPlanTaskDate,
               p.plan_type                            planCheckFrequencyType
        from p_check_report cr
                 LEFT JOIN p_plan_task pt on pt.id = cr.plan_task_id
                 left join p_plan p on p.id = pt.plan_id
        where 1 = 1
          and cr.id = #{checkReportId}
    </select>
    <select id="getCheckReportCompanyList"
            resultType="com.yeejoin.amos.supervision.core.common.dto.CheckReportCompanyDto">
        SELECT c.point_name                        name,
               p.original_id                       id,
               GROUP_CONCAT(DISTINCT ci.user_name) escortUser,
               CASE
                   WHEN ci.safety_danger_num > 0 THEN
                       '存在隐患'
                   WHEN ci.is_ok = '1' THEN
                       '正常'
                   ELSE '异常'
                   END                             checkResult
        FROM p_check c
                 LEFT JOIN p_check_input ci ON ci.check_id = c.id
                 LEFT JOIN p_point p ON p.id = c.point_id
        WHERE c.plan_task_id = #{planTaskId}
        GROUP BY c.id
    </select>
</mapper>