
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 com.pavis.admin.common.context.UserContextHolder;
import com.pavis.admin.system.model.query.MessageQuery;
import com.pavis.admin.system.model.resp.message.MessageResp;
import com.pavis.admin.system.model.resp.message.MessageUnreadResp;
import com.pavis.admin.system.service.MessageService;
import com.pavis.admin.system.service.MessageUserService;
import org.springframework.web.bind.annotation.*;
import top.continew.starter.extension.crud.model.query.PageQuery;
import top.continew.starter.extension.crud.model.req.IdsReq;
import top.continew.starter.extension.crud.model.resp.PageResp;
import top.continew.starter.log.annotation.Log;

/**
 * 消息管理 API
 *
 * 
 * @since 2023/10/15 19:05
 */
@Tag(name = "消息管理 API")
@RestController
@RequiredArgsConstructor
@RequestMapping("/system/message")
public class MessageController {

    private final MessageService baseService;
    private final MessageUserService messageUserService;

    @Operation(summary = "分页查询列表", description = "分页查询列表")
    @GetMapping
    public PageResp<MessageResp> page(MessageQuery query, @Validated PageQuery pageQuery) {
        query.setUserId(UserContextHolder.getUserId());
        return baseService.page(query, pageQuery);
    }

    @Operation(summary = "删除数据", description = "删除数据")
    @DeleteMapping
    public void delete(@Validated @RequestBody IdsReq req) {
        baseService.delete(req.getIds());
    }

    @Operation(summary = "标记已读", description = "将消息标记为已读状态")
    @PatchMapping("/read")
    public void read(@Validated @RequestBody IdsReq req) {
        messageUserService.readMessage(req.getIds());
    }

    @Operation(summary = "全部已读", description = "将所有消息标记为已读状态")
    @PatchMapping("/readAll")
    public void readAll() {
        messageUserService.readMessage(null);
    }

    @Log(ignore = true)
    @Operation(summary = "查询未读消息数量", description = "查询当前用户的未读消息数量")
    @Parameter(name = "isDetail", description = "是否查询详情", example = "true", in = ParameterIn.QUERY)
    @GetMapping("/unread")
    public MessageUnreadResp countUnread(@RequestParam(required = false) Boolean detail) {
        return messageUserService.countUnreadMessageByUserId(UserContextHolder.getUserId(), detail);
    }
}