package com.njcn.rdms.module.system.api.websocket;

import cn.hutool.core.collection.CollUtil;
import com.njcn.rdms.framework.common.pojo.CommonResult;
import com.njcn.rdms.framework.common.enums.UserTypeEnum;
import com.njcn.rdms.framework.websocket.core.sender.WebSocketMessageSender;
import com.njcn.rdms.module.system.api.websocket.dto.WebSocketSendToUsersReqDTO;
import io.swagger.v3.oas.annotations.Hidden;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RestController;

import static com.njcn.rdms.framework.common.pojo.CommonResult.success;

@RestController // 提供 RESTful API 接口，给 Feign 调用
@Validated
@Hidden
public class WebSocketSenderApiImpl implements WebSocketSenderApi {

    private static final Integer SYSTEM_USER_TYPE = UserTypeEnum.ADMIN.getValue();

    @Resource
    private WebSocketMessageSender webSocketMessageSender;

    @Override
    public CommonResult<Boolean> sendToUsers(WebSocketSendToUsersReqDTO message) {
        if (CollUtil.isNotEmpty(message.getUserIds())) {
            message.getUserIds().stream().distinct().forEach(userId ->
                    webSocketMessageSender.send(SYSTEM_USER_TYPE, userId,
                            message.getMessageType(), message.getMessageContent()));
        }
        return success(true);
    }

}
