package com.storlead.message.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.storlead.framework.common.constant.CommonConstant;
import com.storlead.message.pojo.entity.UserMessageNoticeConfigEntity;
import com.storlead.message.mapper.UserMessageNoticeConfigMapper;
import com.storlead.message.service.UserMessageNoticeConfigService;
import com.storlead.framework.mybatis.service.impl.MyBaseServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * <p>
 * 消息开关配置 服务实现类
 * </p>
 *
 * @author chenkq
 * @since 2025-04-25
 */
@Service
public class UserMessageNoticeConfigServiceImpl extends MyBaseServiceImpl<UserMessageNoticeConfigMapper, UserMessageNoticeConfigEntity> implements UserMessageNoticeConfigService {

    @Override
    public Set<Long> checkUserNoticeConfig(Set<Long> userIds,Long templateId,String messageType) {
        Set<Long> newToUser = new HashSet<>(userIds);
        try {
            if (CollectionUtils.isEmpty(newToUser)) {
                return newToUser;
            }
            LambdaQueryWrapper<UserMessageNoticeConfigEntity> userMessageConfigW = new LambdaQueryWrapper<>();
            userMessageConfigW.eq(UserMessageNoticeConfigEntity::getTemplateEventId,templateId);
            userMessageConfigW.eq(UserMessageNoticeConfigEntity::getTemplateDetailType,messageType);
            userMessageConfigW.in(UserMessageNoticeConfigEntity::getOwnerBy,userIds);
            userMessageConfigW.in(UserMessageNoticeConfigEntity::getIsDelete, CommonConstant.DEL_FLAG_0);
            userMessageConfigW.in(UserMessageNoticeConfigEntity::getEnabled, Integer.valueOf(0));
            List<UserMessageNoticeConfigEntity> ls = this.list(userMessageConfigW);

            if (!CollectionUtils.isEmpty(ls)) {
                Set<Long> rmUserIds = ls.stream().map(UserMessageNoticeConfigEntity::getOwnerBy).collect(Collectors.toSet());
                if (!CollectionUtils.isEmpty(rmUserIds)) {
                    newToUser.removeAll(rmUserIds);
                }
            }
            return newToUser;
        } catch (Exception e) {
            log.error("checkUserNoticeConfig --- error -- ",e);
            return newToUser;
        }

    }
}
