package com.cico.nest.lcdp.boot.core.system.service.notify;

import com.cico.nest.lcdp.boot.core.system.controller.admin.notify.vo.message.NotifyMessageMyPageReqVO;
import com.cico.nest.lcdp.boot.core.system.controller.admin.notify.vo.message.NotifyMessagePageReqVO;
import com.cico.nest.lcdp.boot.core.system.dal.dataobject.notify.NotifyMessageDO;
import com.cico.nest.lcdp.boot.core.system.dal.dataobject.notify.NotifyTemplateDO;
import com.cico.nest.lcdp.boot.core.system.dal.mysql.notify.NotifyMessageMapper;
import com.cico.nest.lcdp.framework.common.pojo.PageResult;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;

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

    @Resource
    private NotifyMessageMapper notifyMessageMapper;

    @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).setReadStatus(false);
        notifyMessageMapper.insert(message);
        return message.getId();
    }

    @Override
    public void createNotifyMessage(List<Long> userIds, Integer userType, NotifyTemplateDO template, String templateContent, Map<String, Object> templateParams) {
        List<NotifyMessageDO> messageDOList = new ArrayList<>();
        for(Long userId : userIds){
            NotifyMessageDO message = new NotifyMessageDO().setUserId(userId).setUserType(userType)
                    .setTemplateId(template.getId()).setTemplateCode(template.getCode())
                    .setTemplateType(template.getType()).setTemplateNickname(template.getNickname())
                    .setTemplateContent(templateContent).setTemplateParams(templateParams).setReadStatus(false);
            messageDOList.add(message);
        }
        notifyMessageMapper.insertBatch(messageDOList);
    }

    @Override
    public PageResult<NotifyMessageDO> getNotifyMessagePage(NotifyMessagePageReqVO pageReqVO) {
        return notifyMessageMapper.selectPage(pageReqVO);
    }

    @Override
    public PageResult<NotifyMessageDO> getMyMyNotifyMessagePage(NotifyMessageMyPageReqVO pageReqVO, Long userId, Integer userType) {
        return notifyMessageMapper.selectPage(pageReqVO, userId, userType);
    }

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

    @Override
    public List<NotifyMessageDO> getUnreadNotifyMessageList(Long userId, Integer userType, Integer size) {
        return notifyMessageMapper.selectUnreadListByUserIdAndUserType(userId, userType, size);
    }

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

    @Override
    public int updateNotifyMessageRead(Collection<Long> ids, Long userId, Integer userType) {
        return notifyMessageMapper.updateListRead(ids, userId, userType);
    }

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

}
