<?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.FireCheckListUserMapper">

    <select id="getListUserNameByFireCheckIdList" resultType="com.zcloud.fire.check.persistence.dataobject.FireCheckListUserDO">
        select cu.*,
        GROUP_CONCAT(DISTINCT u.name) AS userName
        from fire_check_list_user cu
        left join user u on cu.user_id = u.id
        where cu.delete_enum = 'FALSE' and cu.fire_check_id in
        <foreach collection="fireCheckIdList" item="fireCheckId" open="(" close=")" separator=",">
            #{fireCheckId}
        </foreach>
        group by cu.fire_check_id
    </select>

    <select id="getListUserStatisticByFireCheckIdList" resultType="com.zcloud.fire.check.persistence.dataobject.FireCheckListStatisticDO">
        select cu.fire_check_list_user_id as fireCheckListUserId,
               cu.fire_check_id as fireCheckId,
               GROUP_CONCAT(DISTINCT u.name) AS userName,
               count(DISTINCT cr.id) as checkCount
        from fire_check_list_user cu
        left join user u on cu.user_id = u.id
                 left join fire_check_record cr on cu.fire_check_id = cr.fire_check_id and cr.delete_enum = 'FALSE' and cr.finished_flag=1
        where cu.delete_enum = 'FALSE' and cu.fire_check_id in
        <foreach collection="fireCheckIdList" item="fireCheckId" open="(" close=")" separator=",">
            #{fireCheckId}
        </foreach>
        group by cu.fire_check_id
    </select>
    <select id="getListUserStatisticByFireCheckIdListTime" resultType="com.zcloud.fire.check.persistence.dataobject.FireCheckListStatisticDO">
        select cu.fire_check_list_user_id as fireCheckListUserId,
        cu.fire_check_id as fireCheckId,
        GROUP_CONCAT(DISTINCT u.name) AS userName,
        count(DISTINCT cr.id) as checkCount
        from fire_check_list_user cu
        left join (
        select DISTINCT user_id, name
        from user
        ) u on cu.user_id = u.user_id
        left join fire_check_record cr on cu.fire_check_id = cr.fire_check_id and cr.delete_enum = 'FALSE' and cr.finished_flag=1
        where cu.delete_enum = 'FALSE' and cu.fire_check_id in
        <foreach collection="fireCheckIdList" item="fireCheckId" open="(" close=")" separator=",">
            #{fireCheckId}
        </foreach>
        <if test="params != null">
            <if test="params.startTime != null and params.startTime != ''">
                and DATE(cr.check_time) >= #{params.startTime}
            </if>
            <if test="params.endTime != null and params.endTime != ''">
                and DATE(cr.check_time) <![CDATA[<=]]> #{params.endTime}
            </if>
        </if>
        group by cu.fire_check_id
    </select>

    <select id="getListUserByFireCheckIdList" resultType="com.zcloud.fire.check.persistence.dataobject.FireCheckListUserDO">
        select cu.fire_check_list_user_id as fireCheckListUserId,
        cu.fire_check_id as fireCheckId,
        GROUP_CONCAT(DISTINCT u.name) AS userName,
        GROUP_CONCAT(DISTINCT p.post_name) AS postName
        from fire_check_list_user cu
        left join user u on cu.user_id = u.id
        left join post p on u.post_id = p.id
        where cu.delete_enum = 'FALSE' and cu.fire_check_id in
        <foreach collection="fireCheckIdList" item="fireCheckId" open="(" close=")" separator=",">
            #{fireCheckId}
        </foreach>
        group by cu.fire_check_id

    </select>
    <select id="getListByFireCheckId" resultType="com.zcloud.fire.check.persistence.dataobject.FireCheckListUserDO">
        select cu.*,
               u.name AS userName,
               d.name AS departmentName
        from fire_check_list_user cu
                 left join user u on cu.user_id = u.id
                 left join department  d on  cu.department_id = d.id
        where cu.delete_enum = 'FALSE' and cu.fire_check_id=#{fireCheckId}

    </select>

    <select id="getListUserIdByFireCheckIdList"
            resultType="com.zcloud.fire.check.persistence.dataobject.FireCheckListUserDO">
        select cu.*,
        GROUP_CONCAT(DISTINCT u.name) AS userName
        from fire_check_list_user cu
        left join user u on cu.user_id = u.id
        where cu.delete_enum = 'FALSE' and cu.fire_check_id in
        <foreach collection="fireCheckIdList" item="fireCheckId" open="(" close=")" separator=",">
            #{fireCheckId}
        </foreach>
        group by cu.id
    </select>
</mapper>

