package com.ho.business.service.impl;

import com.ho.business.mapper.MessageInfoMapper;
import com.ho.business.service.MessageInfoService;
import com.ho.business.entity.MessageInfoVo;
import com.ho.business.entity.MessageReceiverVo;
import com.ho.business.vo.req.MessageInfoReqVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import java.util.stream.Collectors;

@Slf4j
@Service
public class MessageInfoServiceImpl implements MessageInfoService {

    @Autowired
    MessageInfoMapper messageInfoMapper;

    @Override
    @Transactional
    public MessageInfoVo modifyMessageInfo(MessageInfoReqVo messageInfoReqVo) {
        MessageInfoVo messageDetail = new MessageInfoVo();
        BeanUtils.copyProperties(messageInfoReqVo,messageDetail);
        String messageId = null;
        if(messageDetail.getMessageId() != null){
            //首先删除原消息相关数据
            List<String> messageIds = new ArrayList<>();
            messageIds.add(messageDetail.getMessageId());
            deleteMessage(messageIds);
        }
            //执行新增操作
            //生成36位消息id
            messageId = String.valueOf(UUID.randomUUID());
            if(messageInfoReqVo.getStartTime() == null){
                messageDetail.setCreateStatus(1);
            }else{
                if(messageInfoReqVo.getStartTime().equals(messageInfoReqVo.getCreateTime()) && 0 ==messageInfoReqVo.getSendType()){
                    messageDetail.setCreateStatus(3);
                }else{
                    messageDetail.setCreateStatus(2);
                }
            }
            messageDetail.setMessageId(messageId);
            List<MessageReceiverVo> receivers =  new ArrayList<>();
            for(String i:messageInfoReqVo.getReceivingUser()){
                MessageReceiverVo messageReceive =  new MessageReceiverVo();
                messageReceive.setMessageId(messageId);
                messageReceive.setReceivingType(messageInfoReqVo.getReceivingType());
                messageReceive.setReceivingUser(i);
                messageReceive.setReadStatus(0);
                messageReceive.setReceivingTime(messageInfoReqVo.getStartTime());
                receivers.add(messageReceive);
            }
            messageInfoMapper.addMessageInfo(messageDetail);
            messageInfoMapper.addMessageReceiver(receivers);

        return messageDetail;
    }

    @Override
    @Transactional
    public void deleteMessage(List<String> messageIds){
        //首先删除消息表
        messageInfoMapper.deleteMessageInfo(messageIds);
        //然后删除消息接收用户表
        messageInfoMapper.deleteMessageReceivers(messageIds);
    }

    @Override
    public List<MessageInfoReqVo> selectMessageInfo(Map<String,Object> condition){
        List<MessageInfoReqVo> result= new ArrayList<>();
        List<MessageInfoVo> messageInfo = messageInfoMapper.selectMessageInfo(condition);
        for(MessageInfoVo message:messageInfo){
            MessageInfoReqVo messageDetail = new MessageInfoReqVo();
            condition.put("messageId",message.getMessageId());
            List<MessageReceiverVo> receivers = messageInfoMapper.selectMessageReceivers(condition);
            BeanUtils.copyProperties(message,messageDetail);
            if(receivers.size()>0){
                messageDetail.setReceivingUser(receivers.stream().map(MessageReceiverVo::getReceivingUser).collect(Collectors.toList()));
                messageDetail.setReceivingUserName(receivers.stream().map(MessageReceiverVo::getReceivingUserName).collect(Collectors.toList()));
                messageDetail.setReceivingType(receivers.get(0).getReceivingType());
                messageDetail.setReceivingTime(receivers.get(0).getReceivingTime());
                messageDetail.setReadStatus(receivers.get(0).getReadStatus());
                result.add(messageDetail);
            }
        }
        if(condition.get("readStatus")!=null){
            result = result.stream().sorted(Comparator.comparing(MessageInfoReqVo::getReceivingTime)).collect(Collectors.toList());
        }else{
            result = result.stream().sorted(Comparator.comparing(MessageInfoReqVo::getStartTime)).collect(Collectors.toList());
        }
        return result;
    }

    @Override
    public void updateMessageReadStatus(Map<String,Object> condition) {
        //首先查询当前消息是否设置了播放频次和播放频次单位
        List<MessageInfoVo> messageInfo = messageInfoMapper.selectMessageInfo(condition);
        //只有设置了播放频次和播放频次单位的消息可以置为已读
        if(messageInfo.size()>0 && messageInfo.get(0).getMessageFrequency()!=null
                && messageInfo.get(0).getMessageFrequency()!= 0 && messageInfo.get(0).getFrequencyUnit() != null){
            messageInfoMapper.updateMessageReadStatus(condition);
        }
    }

    @Override
    @Transactional
    public void jobUpdateMessageInfo(){
        //获取消息开始时间在当前时间之前且消息创建状态是未发送的所有消息数据
        Map<String,Object> condition = new HashMap<>();
        condition.put("createStatus",2);
        condition.put("beginTime",new Date());
        List<MessageInfoVo> messageInfo = messageInfoMapper.selectMessageInfo(condition);
        List<String> messageId = messageInfo.stream().map(MessageInfoVo::getMessageId).collect(Collectors.toList());
        if(messageId.size()>0){
            //将消息开始时间在当前时间之前且消息创建状态是未发送的消息设置为已发送
            messageInfoMapper.updateCreateStatus(messageId);
            //将对应用户的消息接收时间修改为现在
            messageInfoMapper.updateReceivingTime(messageId);
        }

        //刷新播放频次已到消息的阅读状态及接收时间
        //首先获取消息创建状态是已发送的消息id
        condition.put("createStatus",3);
        condition.put("finishTime",new Date());
        condition.put("beginTime",null);
        messageInfo = messageInfoMapper.selectMessageInfo(condition);
        //未设置播放时间或者播放频次的消息不再刷新阅读状态
        messageInfo = messageInfo.stream().filter(e ->e.getFrequencyUnit()!=null && e.getMessageFrequency() != null).collect(Collectors.toList());
        for(MessageInfoVo message:messageInfo){
            messageInfoMapper.refreshMessageReadStatus(message);
        }

    }



}
