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

    <insert id="batchInsertThirdPartWeComRoomUser">
        insert into third_part_we_com_room_user (id, third_room_id, unionid, sex, mobile, acctid, join_scene, avatar,
        english_name, realname, room_notes, join_time, quit_time, nickname,
        room_nickname, `position`, uin, invite_user_id, corp_id, create_time, update_time)
        values
        <foreach collection="records" item="item" separator=",">
            (#{item.id}, #{item.thirdRoomId}, #{item.unionid}, #{item.sex}, #{item.mobile}, #{item.acctid},
            #{item.joinScene}, #{item.avatar}, #{item.englishName}, #{item.realname}, #{item.roomNotes},
            #{item.joinTime}, #{item.quitTime}, #{item.nickname}, #{item.roomNickname}, #{item.position},
            #{item.uin}, #{item.inviteUserId}, #{item.corpId}, #{item.createTime}, #{item.updateTime})
        </foreach>
    </insert>

    <update id="batchUpdateThirdPartWeComRoomUser">
        update third_part_we_com_room_user
        <trim prefix="set" suffixOverrides=",">
            <trim prefix="sex = case" suffix="end,">
                <foreach collection="records" item="item">
                    when id = #{item.id} then #{item.sex}
                </foreach>
            </trim>
            <trim prefix="mobile = case" suffix="end,">
                <foreach collection="records" item="item">
                    when id = #{item.id} then #{item.mobile}
                </foreach>
            </trim>
            <trim prefix="acctid = case" suffix="end,">
                <foreach collection="records" item="item">
                    when id = #{item.id} then #{item.acctid}
                </foreach>
            </trim>
            <trim prefix="avatar = case" suffix="end,">
                <foreach collection="records" item="item">
                    when id = #{item.id} then #{item.avatar}
                </foreach>
            </trim>
            <trim prefix="english_name = case" suffix="end,">
                <foreach collection="records" item="item">
                    when id = #{item.id} then #{item.englishName}
                </foreach>
            </trim>
            <trim prefix="realname = case" suffix="end,">
                <foreach collection="records" item="item">
                    when id = #{item.id} then #{item.realname}
                </foreach>
            </trim>
            <trim prefix="room_notes = case" suffix="end,">
                <foreach collection="records" item="item">
                    when id = #{item.id} then #{item.roomNotes}
                </foreach>
            </trim>
            <trim prefix="quit_time = case" suffix="end,">
                <foreach collection="records" item="item">
                    when id = #{item.id} then #{item.quitTime}
                </foreach>
            </trim>
            <trim prefix="nickname = case" suffix="end,">
                <foreach collection="records" item="item">
                    when id = #{item.id} then #{item.nickname}
                </foreach>
            </trim>
            <trim prefix="room_nickname = case" suffix="end,">
                <foreach collection="records" item="item">
                    when id = #{item.id} then #{item.roomNickname}
                </foreach>
            </trim>
            <trim prefix="position = case" suffix="end,">
                <foreach collection="records" item="item">
                    when id = #{item.id} then #{item.position}
                </foreach>
            </trim>
            <trim prefix="update_time = case" suffix="end">
                <foreach collection="records" item="item">
                    when id = #{item.id} then #{item.updateTime}
                </foreach>
            </trim>
        </trim>
        where id in
        <foreach collection="records" item="item" open="(" close=")" separator=",">
            #{item.id}
        </foreach>
    </update>

    <insert id="batchInsertThirdPartWeComStaffUser">
        insert into third_part_we_com_staff_user (id, third_staff_id, add_customer_time, remove_customer_time, unionid,
        sex, mobile, company_remark, acctid, avatar, `source`, english_name,
        remark_phone, realname, real_remarks, labelid, user_id, nickname,
        `position`, corp_id, remarks, seq, `status`, create_time, update_time)
        values
        <foreach collection="records" item="item" separator=",">
            (#{item.id}, #{item.thirdStaffId}, #{item.addCustomerTime}, #{item.removeCustomerTime}, #{item.unionid},
            #{item.sex}, #{item.mobile}, #{item.companyRemark}, #{item.acctid}, #{item.avatar}, #{item.source},
            #{item.englishName}, #{item.remarkPhone}, #{item.realname}, #{item.realRemarks}, #{item.labelid},
            #{item.userId}, #{item.nickname}, #{item.position}, #{item.corpId}, #{item.remarks}, #{item.seq},
            #{item.status}, #{item.createTime}, #{item.updateTime})
        </foreach>
    </insert>

    <update id="batchUpdateThirdPartWeComStaffUser">
        update third_part_we_com_staff_user
        <trim prefix="set" suffixOverrides=",">
            <trim prefix="add_customer_time = case" suffix="end,">
                <foreach collection="records" item="item">
                    when id = #{item.id} then #{item.addCustomerTime}
                </foreach>
            </trim>
            <trim prefix="remove_customer_time = case" suffix="end,">
                <foreach collection="records" item="item">
                    when id = #{item.id} then #{item.removeCustomerTime}
                </foreach>
            </trim>
            <trim prefix="unionid = case" suffix="end,">
                <foreach collection="records" item="item">
                    when id = #{item.id} then #{item.unionid}
                </foreach>
            </trim>
            <trim prefix="sex = case" suffix="end,">
                <foreach collection="records" item="item">
                    when id = #{item.id} then #{item.sex}
                </foreach>
            </trim>
            <trim prefix="mobile = case" suffix="end,">
                <foreach collection="records" item="item">
                    when id = #{item.id} then #{item.mobile}
                </foreach>
            </trim>
            <trim prefix="company_remark = case" suffix="end,">
                <foreach collection="records" item="item">
                    when id = #{item.id} then #{item.companyRemark}
                </foreach>
            </trim>
            <trim prefix="acctid = case" suffix="end,">
                <foreach collection="records" item="item">
                    when id = #{item.id} then #{item.acctid}
                </foreach>
            </trim>
            <trim prefix="avatar = case" suffix="end,">
                <foreach collection="records" item="item">
                    when id = #{item.id} then #{item.avatar}
                </foreach>
            </trim>
            <trim prefix="source = case" suffix="end,">
                <foreach collection="records" item="item">
                    when id = #{item.id} then #{item.source}
                </foreach>
            </trim>
            <trim prefix="english_name = case" suffix="end,">
                <foreach collection="records" item="item">
                    when id = #{item.id} then #{item.englishName}
                </foreach>
            </trim>
            <trim prefix="remark_phone = case" suffix="end,">
                <foreach collection="records" item="item">
                    when id = #{item.id} then #{item.remarkPhone}
                </foreach>
            </trim>
            <trim prefix="realname = case" suffix="end,">
                <foreach collection="records" item="item">
                    when id = #{item.id} then #{item.realname}
                </foreach>
            </trim>
            <trim prefix="real_remarks = case" suffix="end,">
                <foreach collection="records" item="item">
                    when id = #{item.id} then #{item.realRemarks}
                </foreach>
            </trim>
            <trim prefix="labelid = case" suffix="end,">
                <foreach collection="records" item="item">
                    when id = #{item.id} then #{item.labelid}
                </foreach>
            </trim>
            <trim prefix="user_id = case" suffix="end,">
                <foreach collection="records" item="item">
                    when id = #{item.id} then #{item.userId}
                </foreach>
            </trim>
            <trim prefix="nickname = case" suffix="end,">
                <foreach collection="records" item="item">
                    when id = #{item.id} then #{item.nickname}
                </foreach>
            </trim>
            <trim prefix="`position` = case" suffix="end,">
                <foreach collection="records" item="item">
                    when id = #{item.id} then #{item.position}
                </foreach>
            </trim>
            <trim prefix="corp_id = case" suffix="end,">
                <foreach collection="records" item="item">
                    when id = #{item.id} then #{item.corpId}
                </foreach>
            </trim>
            <trim prefix="remarks = case" suffix="end,">
                <foreach collection="records" item="item">
                    when id = #{item.id} then #{item.remarks}
                </foreach>
            </trim>
            <trim prefix="seq = case" suffix="end,">
                <foreach collection="records" item="item">
                    when id = #{item.id} then #{item.seq}
                </foreach>
            </trim>
            <trim prefix="status = case" suffix="end,">
                <foreach collection="records" item="item">
                    when id = #{item.id} then #{item.status}
                </foreach>
            </trim>
            <trim prefix="update_time = case" suffix="end">
                <foreach collection="records" item="item">
                    when id = #{item.id} then #{item.updateTime}
                </foreach>
            </trim>
        </trim>
        where id in
        <foreach collection="records" item="item" open="(" close=")" separator=",">
            #{item.id}
        </foreach>
    </update>

    <select id="getNoGroupStaffUserList"
            resultType="com.tzld.piaoquan.api.model.po.wecom.thirdpart.ThirdPartWeComStaffUser">
        select staff_user.*
        from third_part_we_com_staff staff
        join third_part_we_com_staff_user staff_user on staff.third_staff_id = staff_user.third_staff_id
        left join (select distinct room_user.uin
            from third_part_we_com_staff staff
                     join third_part_we_com_room room on staff.id = room.staff_id
                     join third_part_we_com_room_user room_user
                      on room.third_room_id = room_user.third_room_id
            where staff.third_staff_id = #{thirdStaffId}) room_user on staff_user.user_id = room_user.uin
        where staff.third_staff_id = #{thirdStaffId}
          and staff_user.status not in (0, 8, 2049)
          and room_user.uin is null
    </select>

    <update id="setRoomUserQuitTime">
        update third_part_we_com_room_user
        set quit_time = #{quitTime}
        where third_room_id = #{thirdRoomId}
        and uin in
        <foreach collection="deleteList" item="item" open="(" close=")" separator=",">
            #{item}
        </foreach>
    </update>

    <insert id="batchInsertThirdPartWeComRoom">
        insert into third_part_we_com_room (id, corp_id, staff_id, third_room_id, third_create_user_id, member_count,
        `name`, room_url, room_create_time, add_user_status, send_status, create_time, update_time)
        values
        <foreach collection="records" item="item" separator=",">
            (#{item.id}, #{item.corpId}, #{item.staffId}, #{item.thirdRoomId}, #{item.thirdCreateUserId},
             #{item.memberCount}, #{item.name}, #{item.roomUrl}, #{item.roomCreateTime}, #{item.addUserStatus},
             #{item.sendStatus}, #{item.createTime}, #{item.updateTime})
        </foreach>
    </insert>

    <update id="batchUpdateThirdPartWeComRoom">
        update third_part_we_com_room
        <trim prefix="set" suffixOverrides=",">
            <trim prefix="corp_id = case" suffix="end,">
                <foreach collection="records" item="item">
                    when id = #{item.id} then #{item.corpId}
                </foreach>
            </trim>
            <trim prefix="staff_id = case" suffix="end,">
                <foreach collection="records" item="item">
                    when id = #{item.id} then #{item.staffId}
                </foreach>
            </trim>
            <trim prefix="third_room_id = case" suffix="end,">
                <foreach collection="records" item="item">
                    when id = #{item.id} then #{item.thirdRoomId}
                </foreach>
            </trim>
            <trim prefix="third_create_user_id = case" suffix="end,">
                <foreach collection="records" item="item">
                    when id = #{item.id} then #{item.thirdCreateUserId}
                </foreach>
            </trim>
            <trim prefix="member_count = case" suffix="end,">
                <foreach collection="records" item="item">
                    when id = #{item.id} then #{item.memberCount}
                </foreach>
            </trim>
            <trim prefix="`name` = case" suffix="end,">
                <foreach collection="records" item="item">
                    when id = #{item.id} then #{item.name}
                </foreach>
            </trim>
            <trim prefix="room_url = case" suffix="end,">
                <foreach collection="records" item="item">
                    when id = #{item.id} then #{item.roomUrl}
                </foreach>
            </trim>
            <trim prefix="room_create_time = case" suffix="end,">
                <foreach collection="records" item="item">
                    when id = #{item.id} then #{item.roomCreateTime}
                </foreach>
            </trim>
            <trim prefix="add_user_status = case" suffix="end,">
                <foreach collection="records" item="item">
                    when id = #{item.id} then #{item.addUserStatus}
                </foreach>
            </trim>
            <trim prefix="send_status = case" suffix="end,">
                <foreach collection="records" item="item">
                    when id = #{item.id} then #{item.sendStatus}
                </foreach>
            </trim>
            <trim prefix="send_time = case" suffix="end,">
                <foreach collection="records" item="item">
                    when id = #{item.id} then #{item.sendTime}
                </foreach>
            </trim>
            <trim prefix="is_delete = case" suffix="end,">
                <foreach collection="records" item="item">
                    when id = #{item.id} then #{item.isDelete}
                </foreach>
            </trim>
            <trim prefix="update_time = case" suffix="end">
                <foreach collection="records" item="item">
                    when id = #{item.id} then #{item.updateTime}
                </foreach>
            </trim>
        </trim>
        where id in
        <foreach collection="records" item="item" open="(" close=")" separator=",">
            #{item.id}
        </foreach>
    </update>

    <update id="updateStaffQrImageByUuid">
        update third_part_we_com_staff
        set qr_image = #{qrImage}
        where third_uuid = #{uuid}
    </update>

</mapper>