package com.sonic.pigeon.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.sonic.common.rpc.Page;
import com.sonic.daosupport.converter.PageConverter;
import com.sonic.pigeon.dao.MessageDao;
import com.sonic.pigeon.domain.entity.Message;
import com.sonic.pigeon.domain.input.MessageListInput;
import com.sonic.pigeon.domain.input.SendMessageInput;
import com.sonic.pigeon.domain.output.MessageListOutput;
import com.sonic.pigeon.enums.MessageStatusEnum;
import com.sonic.pigeon.service.MessageService;
import com.sonic.pigeon.service.MessageStatService;
import com.sonic.pigeon.utils.BeanConver;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class MessageServiceImpl extends ServiceImpl<MessageDao, Message> implements MessageService {

    @Autowired
    private MessageStatService messageStatService;

    @Override
    public void sendMessage(List<SendMessageInput> reqList) {
        reqList.forEach(req -> {
            Message message = BeanConver.copeBean(req, Message.class);
            message.setStatus(MessageStatusEnum.NO_READ.getIndex());
            message.setIsDelete(false);
            message.setCreateTime(LocalDateTime.now());
            message.setEditTime(LocalDateTime.now());
            //保存
            save(message);
            //更新统计表未读数,最近未读消息内容及时间
            messageStatService.updateUnread(req.getUserId(), 1, req.getContent());
        });
    }

    @Override
    public Page<MessageListOutput> messageList(Long userId, MessageListInput input) {
        //分页获取
        com.baomidou.mybatisplus.extension.plugins.pagination.Page<Message> page = new com.baomidou.mybatisplus.extension.plugins.pagination.Page<>(input.getPage().getPn(), input.getPage().getPs());
        IPage<Message> messagePage = page(page, Wrappers.<Message>lambdaQuery()
                .eq(Message::getUserId, userId)
                .eq(Message::getIsDelete, false)
                .orderByDesc(Message::getId)
        );
        List<Message> records = messagePage.getRecords();
        //没有数据直接返回
        if (CollectionUtils.isEmpty(records)) {
            return new Page<MessageListOutput>();
        }
        //未读消息列表
        List<Long> unreadIdList = records.stream().filter(e->e.getStatus() == 0).map(Message::getId).collect(Collectors.toList());
        if (CollectionUtils.isNotEmpty(unreadIdList)) {
            //更新消息为已读
            update(Wrappers.<Message>lambdaUpdate().set(Message::getStatus, MessageStatusEnum.READ.getIndex()).in(Message::getId, unreadIdList));
            //统计表未读数要减去本次拉取的数量
            messageStatService.updateUnread(userId, -unreadIdList.size(), null);
        }
        return PageConverter.convert(messagePage, message -> {
            return BeanConver.copeBean(message, MessageListOutput.class);
        });
    }
}