package com.yonge.cooleshow.biz.dal.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yonge.cooleshow.auth.api.client.SysUserFeignService;
import com.yonge.cooleshow.auth.api.entity.SysUser;
import com.yonge.cooleshow.biz.dal.dao.ImUserFriendDao;
import com.yonge.cooleshow.biz.dal.dao.TeacherDao;
import com.yonge.cooleshow.biz.dal.dto.BasicUserInfo;
import com.yonge.cooleshow.biz.dal.entity.ImUserFriend;
import com.yonge.cooleshow.biz.dal.service.ImUserFriendService;
import com.yonge.cooleshow.biz.dal.support.PageUtil;
import com.yonge.toolset.mybatis.page.PageInfo;
import com.yonge.toolset.mybatis.page.QueryInfo;
import com.yonge.toolset.utils.collection.MapUtil;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 用户通讯录表(ImUserFriend)表服务实现类
 *
 * @author zx
 * @since 2022-03-22 10:46:00
 */
@Service("imUserFriendService")
public class ImUserFriendServiceImpl extends ServiceImpl<ImUserFriendDao, ImUserFriend> implements ImUserFriendService {

    private final static Logger log = LoggerFactory.getLogger(ImUserFriendServiceImpl.class);

    @Resource
    private TeacherDao teacherDao;
    @Resource
    private SysUserFeignService sysUserFeignService;
    @Override
    public ImUserFriendDao getDao() {
        return this.baseMapper;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveUserFriend(Long teacherId, Set<Long> studentIds) {
        Date now = new Date();
        //老师添加学员联系人
        List<ImUserFriend> imUserFriends = new ArrayList<>();
        BasicUserInfo basicUserInfo = teacherDao.getBasicUserInfo(teacherId);
        List<BasicUserInfo> basicUserInfoList = teacherDao.findBasicUserInfo(studentIds);
        basicUserInfoList.add(basicUserInfo);
        Map<Long, List<BasicUserInfo>> basicUserInfoMap = basicUserInfoList.stream().collect(Collectors.groupingBy(BasicUserInfo::getUserId));
        for(Long studentId : studentIds){
            //老师关联学员
            ImUserFriend teacherFriend = new ImUserFriend();
            //学员关联老师
            ImUserFriend studentFriend = new ImUserFriend();
            List<BasicUserInfo> basicUserInfos = basicUserInfoMap.get(studentId);
            if(CollectionUtils.isNotEmpty(basicUserInfos)){
                BasicUserInfo info = basicUserInfos.get(0);
                teacherFriend.setFriendAvatar(info.getAvatar());
                teacherFriend.setFriendNickname(info.getUsername());
                studentFriend.setFriendAvatar(info.getAvatar());
                studentFriend.setFriendNickname(info.getRealName());
            }
            teacherFriend.setFriendId(studentId);
            teacherFriend.setUserId(teacherId);
            teacherFriend.setCreateTime(now);
            teacherFriend.setUpdateTime(now);

            studentFriend.setFriendId(teacherId);
            studentFriend.setUserId(studentId);
            studentFriend.setCreateTime(now);
            studentFriend.setUpdateTime(now);
            imUserFriends.add(teacherFriend);
            imUserFriends.add(studentFriend);
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ImUserFriend getDetail(Long userId) {
        SysUser sysUser = sysUserFeignService.queryUserInfo();
        BasicUserInfo basicUserInfo = teacherDao.getBasicUserInfo(userId);
        if(sysUser != null && sysUser.getId() != null){
            ImUserFriend imUserFriend = baseMapper.findByUserIdAndFriendId(sysUser.getId(),userId);
            if(Objects.nonNull(imUserFriend)){
                //更新当前用户关联的该用户的详情信息
                imUserFriend.setFriendAvatar(basicUserInfo.getAvatar());
                imUserFriend.setFriendNickname(StringUtils.isEmpty(basicUserInfo.getRealName())?basicUserInfo.getUsername():basicUserInfo.getRealName());
                imUserFriend.setUpdateTime(new Date());
                baseMapper.updateById(imUserFriend);
                return imUserFriend;
            }
        }
        ImUserFriend imUserFriend = new ImUserFriend();
        imUserFriend.setFriendAvatar(basicUserInfo.getAvatar());
        imUserFriend.setFriendNickname(StringUtils.isEmpty(basicUserInfo.getRealName())?basicUserInfo.getUsername():basicUserInfo.getRealName());
        imUserFriend.setFriendId(userId);
        return imUserFriend;
    }

}

