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

    <select id="getAccountCount" resultType="java.lang.Integer">
        select count(1)
        from third_part_we_com_staff staff
        join third_part_we_com_corp corp on staff.corp_id = corp.id
        <where>
            <trim prefixOverrides="and">
                <if test="param.corpName != null">
                    and corp.name like concat('%', #{param.corpName}, '%')
                </if>
                <if test="param.accountName != null">
                    and staff.name like concat('%', #{param.accountName}, '%')
                </if>
                <if test="param.mobile != null">
                    and staff.mobile = #{param.mobile}
                </if>
            </trim>
        </where>
    </select>

    <select id="getAccountList"
            resultType="com.tzld.piaoquan.api.model.po.wecom.thirdpart.ThirdPartWeComStaff">
        select staff.*
        from third_part_we_com_staff staff
        join third_part_we_com_corp corp on staff.corp_id = corp.id
        <where>
            <trim prefixOverrides="and">
                <if test="param.corpName != null">
                    and corp.name like concat('%', #{param.corpName}, '%')
                </if>
                <if test="param.accountName != null">
                    and staff.name like concat('%', #{param.accountName}, '%')
                </if>
                <if test="param.mobile != null">
                    and staff.mobile = #{param.mobile}
                </if>
            </trim>
        </where>
        order by staff.id desc
        limit #{offset}, #{pageSize}
    </select>

    <select id="getPushStaffList"
            resultType="com.tzld.piaoquan.api.model.po.wecom.thirdpart.ThirdPartWeComStaff">
        select staff.*
        from third_part_we_com_staff staff
        join (
            select distinct staff.name, staff.third_staff_id, staff.id
            from third_part_we_com_room room
             join third_part_we_com_room_user room_user on room.third_room_id = room_user.third_room_id
             join third_part_we_com_staff staff on staff.third_staff_id = room_user.uin
            where room.staff_id = #{accountId}
            <if test="roomId != null and roomId != ''">
                and room.id = #{roomId}
            </if>
        ) t2 on staff.id = t2.id
        where staff.status = 0
        <if test="name != null and name != ''">
            and staff.name like concat('%', #{name}, '%')
        </if>
    </select>

    <select id="getStaffIdByName" resultType="java.lang.Long">
        select third_staff_id
        from third_part_we_com_staff
        where name like concat('%', #{name}, '%')
    </select>

</mapper>