<?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.tzld.piaoquan.api.dao.mapper.wecom.thirdpart.ext.ThirdPartWeComRoomMapperExt">

    <select id="getRoomCount" resultType="java.lang.Integer">
        select count(1)
        from third_part_we_com_room room
        join third_part_we_com_staff staff on room.staff_id = staff.id
        join third_part_we_com_corp corp on staff.corp_id = corp.id
        left join third_part_we_com_room_config room_config on room.id = room_config.room_id and room_config.status = 1
        where room.is_delete = 0
        <if test="param.corpName != null">
            and corp.name like concat('%', #{param.corpName}, '%')
        </if>
        <if test="param.groupLeaderName != null">
            and staff.name like concat('%', #{param.groupLeaderName}, '%')
        </if>
        <if test="param.roomName != null">
            and room.name like concat('%', #{param.roomName}, '%')
        </if>
        <if test="pushAccountIds != null and pushAccountIds.size() > 0">
            and (room_config.primary_third_staff_id in
            <foreach collection="pushAccountIds" item="item" open="(" close=")" separator=",">#{item}</foreach>
            or room_config.second_third_staff_id in
            <foreach collection="pushAccountIds" item="item" open="(" close=")" separator=",">#{item}</foreach>)
        </if>
    </select>

    <select id="getRoomList"
            resultType="com.tzld.piaoquan.api.model.po.wecom.thirdpart.ThirdPartWeComRoom">
        select room.*
        from third_part_we_com_room room
        join third_part_we_com_staff staff on room.staff_id = staff.id
        join third_part_we_com_corp corp on staff.corp_id = corp.id
        left join third_part_we_com_room_config room_config on room.id = room_config.room_id and room_config.status = 1
        where room.is_delete = 0
        <if test="param.corpName != null">
            and corp.name like concat('%', #{param.corpName}, '%')
        </if>
        <if test="param.groupLeaderName!= null">
            and staff.name like concat('%', #{param.groupLeaderName}, '%')
        </if>
        <if test="param.roomName != null">
            and room.name like concat('%', #{param.roomName}, '%')
        </if>
        <if test="pushAccountIds != null and pushAccountIds.size() > 0">
            and (room_config.primary_third_staff_id in
            <foreach collection="pushAccountIds" item="item" open="(" close=")" separator=",">#{item}</foreach>
            or room_config.second_third_staff_id in
            <foreach collection="pushAccountIds" item="item" open="(" close=")" separator=",">#{item}</foreach>)
        </if>
        order by staff.id desc
        limit #{offset}, #{pageSize}
    </select>

    <select id="getNoConfigRoomList"
            resultType="com.tzld.piaoquan.api.model.po.wecom.thirdpart.ThirdPartWeComRoom">
        select room.*
        from third_part_we_com_room room
        join third_part_we_com_staff staff on room.staff_id = staff.id
        left join third_part_we_com_room_config room_config on room.id = room_config.room_id and room_config.status = 1
        where room.is_delete = 0 and staff.id = #{staffId} and room_config.id is null
    </select>

</mapper>