<?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.zcloud.fire.check.persistence.mapper.FireCheckRecordMapper">
    <select id="getCheckRecordList" resultType="com.zcloud.fire.check.persistence.dataobject.FireCheckRecordDO">
        select cr.*,
        GROUP_CONCAT(DISTINCT u.name) AS userNameList,
         d.name AS departmentName,
        count(distinct cfp.id)                 as checkRecordPointCount
           from fire_check_record cr
                left join fire_check_record_user cru on cr.fire_record_id = cru.fire_record_id and cru.delete_enum = 'FALSE'
                left join user u on cru.user_id = u.id
                 left join department d on cru.department_id = d.id
                 left join fire_check_record_point cfp on cr.fire_record_id = cfp.fire_record_id and cfp.delete_enum = 'FALSE'
                 left join fire_check_record_device crd
                           on cr.fire_record_id = crd.fire_record_id and crd.fire_record_point_id = cfp.fire_record_point_id and
                              crd.delete_enum = 'FALSE'
        where cr.delete_enum = 'FALSE'
          and cr.corpinfo_id = #{params.corpinfoId}
          and cr.finished_flag = 1
          <if test="params.fireCheckId != null and params.fireCheckId != '' ">
              and cr.fire_check_id = #{params.fireCheckId}
          </if>
          <if test="params.checkedFlag != null ">
              and crd.checked_flag = #{params.checkedFlag}
          </if>

        <if test="params.startTime != null and params.startTime != ''">
            AND DATE(cr.check_time) <![CDATA[>=]]> #{params.startTime}
        </if>
        <if test="params.endTime != null and params.endTime != ''">
            AND DATE(cr.check_time) <![CDATA[<=]]> #{params.endTime}
        </if>

        group by cr.fire_record_id
        order by cr.create_time desc
    </select>
    <select id="statisticListByFireCheckIdList" resultType="com.zcloud.fire.check.persistence.dataobject.FireCheckRecordStatisticDO">
        select cr.fire_check_id as fireCheckId, count(*) as fireCheckCount
        from fire_check_record cr
        where cr.fire_check_id in
        <foreach collection="fireCheckIdList" item="fireCheckId" open="(" close=")" separator=",">
            #{fireCheckId}
        </foreach>
        and cr.finished_flag=1
        and cr.delete_enum = 'FALSE'
        group by cr.fire_check_id
        order by cr.create_time desc
    </select>
    <select id="DataStatisticDO" resultType="com.zcloud.fire.check.persistence.dataobject.DataStatisticDO">
        select cr.corpinfo_id as corpinfoId, count(*) as totalCount
        from fire_check_record cr
        where cr.corpinfo_id in
        <foreach collection="corpinfoIds" item="corpinfoId" open="(" close=")" separator=",">
            #{corpinfoId}
        </foreach>
        and cr.finished_flag=1
        and cr.delete_enum = 'FALSE'
        group by cr.corpinfo_id
        order by cr.create_time desc
    </select>
    <select id="getCheckRecordPageH5" resultType="com.zcloud.fire.check.persistence.dataobject.FireCheckRecordDO">
    select cr.*,
        GROUP_CONCAT(DISTINCT u.name) AS userNameList,
        GROUP_CONCAT(DISTINCT d.name) AS departmentName,
           GROUP_CONCAT(DISTINCT cfp.fire_point_name) AS firePointNameList,
           GROUP_CONCAT(DISTINCT cfp.fire_region_name) AS fireRegionNameList
        from fire_check_record cr
        left join fire_check_record_user cru on cr.fire_record_id = cru.fire_record_id and cru.delete_enum = 'FALSE'
        left join user u on cru.user_id = u.id
        left join department d on cru.department_id = d.id
        left join fire_check_record_point cfp on cr.fire_record_id = cfp.fire_record_id and cfp.delete_enum = 'FALSE'
        left join fire_check_record_device crd
        on cr.fire_record_id = crd.fire_record_id and crd.fire_record_point_id = cfp.fire_record_point_id and
        crd.delete_enum = 'FALSE'
        where cr.delete_enum = 'FALSE'
        and cr.finished_flag = 1
        and cfp.fire_point_id=#{params.firePointId}
        group by cr.fire_record_id
        order by cr.create_time desc
    </select>

    <select id="getCheckRecordPageH5New" resultType="com.zcloud.fire.check.persistence.dataobject.FireCheckRecordDO">
        select cr.*,
               GROUP_CONCAT(DISTINCT u.name) AS userNameList,
               GROUP_CONCAT(DISTINCT d.name) AS departmentName
        from fire_check_record cr
                 left join fire_check_record_user cru on cr.fire_record_id = cru.fire_record_id and cru.delete_enum = 'FALSE'
                 left join user u on cru.user_id = u.id
                 left join department d on cru.department_id = d.id
                 left join fire_check_record_point cfp on cr.fire_record_id = cfp.fire_record_id and cfp.delete_enum = 'FALSE'

        where cr.delete_enum = 'FALSE'
          and cr.finished_flag = 1
          and cfp.fire_point_id=#{params.firePointId}
        group by cr.fire_record_id
        order by cr.create_time desc
    </select>

    <select id="getCheckRecordInfo" resultType="com.zcloud.fire.check.persistence.dataobject.FireCheckRecordDO">
        select cr.*,
               GROUP_CONCAT(DISTINCT u.name) AS userNameList,
               GROUP_CONCAT(DISTINCT d.name) AS departmentName
        from fire_check_record cr
                 left join fire_check_record_user cru on cr.fire_record_id = cru.fire_record_id and cru.delete_enum = 'FALSE'
                 left join user u on cru.user_id = u.id
                 left join department d on cru.department_id = d.id
        where cr.delete_enum = 'FALSE'
          and cr.finished_flag = 1
          and cr.fire_record_id= #{fireRecordId}
        group by cr.fire_record_id
        order by cr.create_time desc
    </select>

</mapper>

