
package com.pavis.admin.controller.system;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.pavis.admin.common.context.UserContextHolder;
import com.pavis.admin.system.enums.NoticeScopeEnum;
import com.pavis.admin.system.model.query.NoticeQuery;
import com.pavis.admin.system.model.resp.NoticeDetailResp;
import com.pavis.admin.system.model.resp.NoticeResp;
import com.pavis.admin.system.service.NoticeService;
import top.continew.starter.core.validation.CheckUtils;
import top.continew.starter.extension.crud.model.query.PageQuery;
import top.continew.starter.extension.crud.model.resp.BasePageResp;

/**
 * 个人消息 API
 *
 * 
 * @since 2025/4/5 21:30
 */
@Tag(name = "个人消息 API")
@Validated
@RestController
@RequiredArgsConstructor
@RequestMapping("/user/message")
public class UserMessageController {

    private final NoticeService noticeService;

    @Operation(summary = "分页查询公告列表", description = "分页查询公告列表")
    @GetMapping("/notice")
    public BasePageResp<NoticeResp> pageNotice(@Validated NoticeQuery query, @Validated PageQuery pageQuery) {
        query.setUserId(UserContextHolder.getUserId());
        return noticeService.page(query, pageQuery);
    }

    @Operation(summary = "查询公告详情", description = "查询公告")
    @Parameter(name = "id", description = "ID", example = "1", in = ParameterIn.PATH)
    @GetMapping("/notice/{id}")
    public NoticeDetailResp getNotice(@PathVariable Long id) {
        NoticeDetailResp detail = noticeService.get(id);
        CheckUtils.throwIf(detail == null || (NoticeScopeEnum.USER.equals(detail.getNoticeScope()) && !detail
            .getNoticeUsers()
            .contains(UserContextHolder.getUserId().toString())), "公告不存在或无权限访问");
        return detail;
    }
}
