package com.fs.qw.service.impl;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.fs.qw.domain.QwRoom;
import com.fs.qw.domain.QwUser;
import com.fs.qw.mapper.QwRoomMapper;
import com.fs.qw.mapper.QwRoomMemberMapper;
import com.fs.qw.service.IQwRoomService;
import com.fs.qw.vo.QwContactListVO;
import com.fs.qw.vo.QwRoomListVO;
import com.fs.qw.vo.QwRoomMember;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.fs.qw.utils.QwAllUtil.*;

@Service
public class QwRoomServiceImpl implements IQwRoomService {

    @Override
    public List<QwRoomListVO> list(Long seq,String deviceId) {
        List<QwRoomListVO> list = roomList(seq,deviceId).getRooms();
        return list;
    }

    @Override
    public QwRoomListVO detail(String deviceId, Long roomId) {
        Map<String,Object> map = new HashMap<>();
        map.put("roomId",roomId);
        return roomDetail(map,deviceId);
    }

    @Autowired
    private QwRoomMapper qwRoomMapper;

    @Autowired
    private QwRoomMemberMapper qwRoomMemberMapper;

    /**
     * 查询企微群
     *
     * @param roomId 企微群ID
     * @return 企微群
     */
    @Override
    public QwRoom selectQwRoomById(Long roomId)
    {
        return qwRoomMapper.selectQwRoomById(roomId);
    }

    /**
     * 查询企微群列表
     *
     * @param qwRoom 企微群
     * @return 企微群
     */
    @Override
    public List<QwRoom> selectQwRoomList(QwRoom qwRoom)
    {
        return qwRoomMapper.selectQwRoomList(qwRoom);
    }

    /**
     * 新增企微群
     *
     * @param qwRoom 企微群
     * @return 结果
     */
    @Override
    public int insertQwRoom(QwRoom qwRoom)
    {
        return qwRoomMapper.insertQwRoom(qwRoom);
    }

    /**
     * 修改企微群
     *
     * @param qwRoom 企微群
     * @return 结果
     */
    @Override
    public int updateQwRoom(QwRoom qwRoom)
    {

        return qwRoomMapper.updateQwRoom(qwRoom);
    }

    /**
     * 批量删除企微群
     *
     * @param roomIds 需要删除的企微群ID
     * @return 结果
     */
    @Override
    public int deleteQwRoomByIds(Long[] roomIds)
    {
        return qwRoomMapper.deleteQwRoomByIds(roomIds);
    }

    /**
     * 删除企微群信息
     *
     * @param roomId 企微群ID
     * @return 结果
     */
    @Override
    public int deleteQwRoomById(Long roomId)
    {
        return qwRoomMapper.deleteQwRoomById(roomId);
    }

    @Override
    public QwRoom selectByHomeUserId(Long roomId, String homeUserId) {
        return qwRoomMapper.selectByHomeUserId(roomId,homeUserId);
    }

    @Override
    public List<QwContactListVO> selectRoomMemberByRoomId(Long roomId) {
        QwRoomMember roomMember = new QwRoomMember();
        roomMember.setRoomId(roomId);
        List<QwRoomMember> roomMembers = qwRoomMemberMapper.selectQwRoomMemberList(roomMember);
        List<QwContactListVO> listVOS = new ArrayList<>();
        for (QwRoomMember qwRoomMember : roomMembers) {
            QwContactListVO listVO = new QwContactListVO();
            listVO.setId(qwRoomMember.getUserId());
            listVO.setDisplayName(qwRoomMember.getNickName());
            listVO.setUnread(0);
            listVO.setRoomId(qwRoomMember.getRoomId());
            listVO.setAvatar(qwRoomMember.getAvatar());
            listVO.setLastContent("");
            listVO.setLastSendTime(0L);
            listVOS.add(listVO);
        }
        return listVOS;
    }

    @Override
    public List<Long> selectRoomAdminList(Long roomId) {
        return JSON.parseObject(qwRoomMapper.selectRoomAdminList(roomId),  new TypeReference<List<Long>>(){});
    }

    @Override
    public QwRoom selectRoomByRoomId(Long roomId) {
        return qwRoomMapper.selectRoomByRoomId(roomId);
    }
}

