package com.dayou.service.impl;

import com.alibaba.fastjson.JSON;
import com.dayou.common.BaseEntity;
import com.dayou.entity.BusinessReply;
import com.dayou.entity.Message;
import com.dayou.mapper.MessageMapper;
import com.dayou.message.MessageTypeEnum;
import com.dayou.message.bo.BusinessMessageBO;
import com.dayou.message.bo.SystemMessageBO;
import com.dayou.message.handler.BusinessMessageHandler;
import com.dayou.message.handler.SystemMessageHandler;
import com.dayou.service.IMessageService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dayou.utils.LoginContext;
import com.dayou.vo.ItemVO;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.dayou.utils.ExcelUtil;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.web.multipart.MultipartFile;
import java.util.List;
import java.util.ArrayList;
import java.util.Set;
import java.util.stream.Collectors;

import org.springframework.transaction.annotation.Transactional;
import com.dayou.enums.BatchTaskTypeEnum;

import static com.dayou.enums.BusinessReplyEnum.MARKET_LOG_REPLY;
import static com.dayou.enums.BusinessReplyEnum.MARKET_VISIT_REPLY;
import static com.dayou.message.MessageTitle.*;
import static com.dayou.message.MessageTypeEnum.*;

/**
 * <p>
 * 消息 服务实现类
 * </p>
 *
 * @author wucl
 * @since 2023-06-30
 */
@Service
public class MessageServiceImpl extends ServiceImpl<MessageMapper, Message> implements IMessageService {

    @Autowired
    private BusinessMessageHandler businessMessageHandler;

    @Autowired
    private SystemMessageHandler systemMessageHandler;

    @Override
    public Boolean packagingBusinessMessage(BusinessReply originalBO) {
        BusinessMessageBO messageBO = new BusinessMessageBO();
        messageBO.setReceiver(originalBO.getReceiverId());
        String bizType = originalBO.getBizType().equals(MARKET_VISIT_REPLY.name())?MARKET_VISIT_REPLY.getName(): MARKET_LOG_REPLY.getName();
        messageBO.setTitle(BUSINESS_MESSAGE_TITLE.replace("x", bizType));
        messageBO.setMessage(originalBO.getContent());
        messageBO.setParams(JSON.toJSONString(originalBO));
        messageBO.setSender(originalBO.getReplierId());
        messageBO.setType(BUSINESS.name());
        return businessMessageHandler.sendMessage(messageBO);
    }

    @Override
    public Boolean packagingSettleMessage(Set<Long> userIds) {
        Set<SystemMessageBO> settleMessages = userIds.stream().map(x -> {
            SystemMessageBO systemMessageBO = new SystemMessageBO();
            systemMessageBO.setTitle(SYSTEM_MESSAGE_TITLE.replace("x", SETTLE.getName()));
            systemMessageBO.setMessage(SETTLE_MESSAGE_CONTENT);
            systemMessageBO.setReceiver(x);
            systemMessageBO.setType(SETTLE.name());
            return systemMessageBO;
        }).collect(Collectors.toSet());
        if (CollectionUtils.isNotEmpty(settleMessages)){
            return systemMessageHandler.sendMessage(settleMessages);
        }
        return Boolean.FALSE;
    }

    @Override
    public List<Message> messageRedPoint() {
        Long currentUserId = LoginContext.getCurrentUserId();
        List<Message> list = this.list(new LambdaQueryWrapper<Message>().eq(Message::getReceiver, currentUserId)
                .eq(Message::getIsRead, Boolean.FALSE).orderByDesc(BaseEntity::getCreated));
        return list;
    }

    @Override
    public Boolean updateRead(Long id) {
        return this.update(new LambdaUpdateWrapper<Message>().eq(BaseEntity::getId,id).set(Message::getIsRead,Boolean.TRUE));
    }

    @Override
    public void packagingEvaluateRemind(List<ItemVO> completedNotEvaluateItem) {
        Set<SystemMessageBO> collect = completedNotEvaluateItem.stream().map(x -> {
            SystemMessageBO systemMessageBO = new SystemMessageBO();
            systemMessageBO.setType(EVALUATE_REMIND.name());
            systemMessageBO.setTitle(SYSTEM_MESSAGE_TITLE.replace("x", EVALUATE_REMIND.getName()));
            systemMessageBO.setMessage(EVALUATE_REMIND_CONTENT.replace("x", x.getName()));
            systemMessageBO.setParams(JSON.toJSONString(x));
            systemMessageBO.setReceiver(x.getUserId());
            return systemMessageBO;
        }).collect(Collectors.toSet());
        systemMessageHandler.sendMessage(collect);
    }

    @Override
    public Boolean allRead() {
        Long currentUserId = LoginContext.getCurrentUserId();
        return this.update(new LambdaUpdateWrapper<Message>().eq(Message::getReceiver,currentUserId).set(Message::getIsRead,true));
    }
}
