package com.cf.imes.module.system.service.notify;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.PageDTO;
import com.cf.imes.framework.common.pojo.PageResult;
import com.cf.imes.framework.common.util.date.LocalDateTimeUtils;
import com.cf.imes.module.system.controller.admin.notify.vo.message.NotifyMessagePageReqVO;
import com.cf.imes.module.system.dal.dataobject.notify.NotifyMessageDO;
import com.cf.imes.module.system.dal.dataobject.notify.NotifyMessageReadInfoDO;
import com.cf.imes.module.system.dal.dataobject.notify.NotifyTemplateDO;
import com.cf.imes.module.system.dal.mysql.notify.NotifyMessageMapper;
import com.cf.imes.module.system.dal.mysql.notify.NotifyMessageReadInfoMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import jakarta.annotation.Resource;
import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 站内信 Service 实现类
 *
 * @author xrcoder
 */
@Service
@Validated
@Slf4j
public class NotifyMessageServiceImpl implements NotifyMessageService {

    @Resource
    private NotifyMessageMapper notifyMessageMapper;

    @Resource
    private NotifyMessageReadInfoMapper notifyMessageReadInfoMapper;

    @Override
    public Long createNotifyMessage(Long userId, Integer userType,
                                    NotifyTemplateDO template, String templateContent, Map<String, Object> templateParams) {
        NotifyMessageDO message = new NotifyMessageDO().setUserId(userId).setUserType(userType)
                .setTemplateId(template.getId()).setTemplateCode(template.getCode())
                .setTemplateType(template.getType()).setTemplateNickname(template.getNickname())
                .setTemplateContent(templateContent).setTemplateParams(templateParams);
        notifyMessageMapper.insert(message);
        return message.getId();
    }

    @Override
    public PageResult<NotifyMessageDO> getMyMyNotifyMessagePage(NotifyMessagePageReqVO pageReqVO) {
        PageDTO page = new PageDTO(pageReqVO.getPageNo(), pageReqVO.getPageSize());
        pageReqVO.setCreateTime(LocalDateTimeUtils.generateTimeSection(pageReqVO.getCreateTime()));
        IPage<NotifyMessageDO> myMyNotifyMessage = notifyMessageMapper.getMyMyNotifyMessage(page, pageReqVO);
        return new PageResult<>(myMyNotifyMessage.getRecords(), myMyNotifyMessage.getTotal());
    }

    @Override
    public NotifyMessageDO getNotifyMessage(Long id) {
        return notifyMessageMapper.selectById(id);
    }

    @Override
    public Integer getUnreadNotifyMessageCount(Long userId, Integer userType) {
        return notifyMessageMapper.getUnreadNotifyMessageCount(userId, userType);
    }

    @Override
    public int updateNotifyMessageRead(Collection<Long> ids, Long userId, Integer userType) {
        List<NotifyMessageReadInfoDO> batchInsertReadInfoList = ids.stream().map(messageId -> NotifyMessageReadInfoDO.builder().messageId(messageId).userId(userId).build()).toList();
        Boolean insertBatch = notifyMessageReadInfoMapper.insertBatch(batchInsertReadInfoList);
        if (!insertBatch) {
            log.error("[updateNotifyMessageRead][新增站内信消息阅读信息失败]");
            return 0;
        }
        return 1;
    }

    @Override
    public int updateAllNotifyMessageRead(Long userId, Integer userType) {
        List<Long> messageIds = notifyMessageMapper.getUnreadNotifyMessageIds(userId, userType);
        if (CollUtil.isNotEmpty(messageIds)) {
            List<NotifyMessageReadInfoDO> batchInsertReadInfoList = messageIds.stream().map(messageId -> NotifyMessageReadInfoDO.builder().messageId(messageId).userId(userId).build()).toList();
            Boolean insertBatch = notifyMessageReadInfoMapper.insertBatch(batchInsertReadInfoList);
            if (!insertBatch) {
                log.error("[updateAllNotifyMessageRead][新增站内信消息阅读信息失败]");
                return 0;
            }
        }
        return 1;
    }
}
