package com.sckw.message.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sckw.core.annotation.Log;
import com.sckw.core.model.page.PageHelperUtil;
import com.sckw.core.model.page.PageResult;
import com.sckw.core.web.response.HttpResult;
import com.sckw.message.model.KwmMessage;
import com.sckw.message.model.vo.req.*;
import com.sckw.message.model.vo.res.MessagesStatisticsResVO;
import com.sckw.message.service.MessageService;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @param
 * @desc: 消息相关接口
 * @author: yzc
 * @date: 2023-06-09 15:13
 * @return
 */
@RestController
@RequestMapping(value = "/kwmMessage")
@RequiredArgsConstructor
public class MessageController {

    private final MessageService messageService;

    /**
     * @param id
     * @return com.sckw.core.web.response.HttpResult
     * @desc: 获取消息详情
     * @author: yzc
     * @date: 2023-06-13 16:17
     */
    @GetMapping("/detail")
    @Log(description = "获取消息详情")
    public HttpResult detail(@RequestParam Long id) {
        KwmMessage message = messageService.detail(id);
        return HttpResult.ok(message);
    }

    /**
     * @param params
     * @return com.sckw.core.web.response.HttpResult
     * @desc: 分页查询消息
     * @author: yzc
     * @date: 2023-06-09 15:12
     */
    @PostMapping(value = "/select", produces = MediaType.APPLICATION_JSON_VALUE)
    @Log(description = "分页查询消息列表")
    public HttpResult select(@RequestBody @Valid SelectMessagesReqVO params) {
        PageHelper.startPage(params.getPage(), params.getPageSize());
        List<KwmMessage> list = messageService.select(params);
        PageResult result = PageHelperUtil.getPageResult(new PageInfo<>(list));
        return HttpResult.ok(result);
    }

    /**
     * @param readMessagesReqVO
     * @return com.sckw.core.web.response.HttpResult
     * @desc: 消息已读
     * @author: yzc
     * @date: 2023-06-09 14:21
     */
    @PostMapping(value = "/read", produces = MediaType.APPLICATION_JSON_VALUE)
    @Log(description = "消息已读")
    public HttpResult read(@RequestBody ReadMessagesReqVO readMessagesReqVO) {
        messageService.read(readMessagesReqVO);
        return HttpResult.ok();
    }

    /**
     * @param deleteMessagesReqVO
     * @return com.sckw.core.web.response.HttpResult
     * @desc: 删除消息
     * @author: yzc
     * @date: 2023-06-09 14:21
     */
    @PostMapping(value = "/delete", produces = MediaType.APPLICATION_JSON_VALUE)
    @Log(description = "删除消息")
    public HttpResult delete(@RequestBody DeleteMessagesReqVO deleteMessagesReqVO) {
        messageService.delete(deleteMessagesReqVO);
        return HttpResult.ok();
    }

    /**
     * @desc: 消息统计查询
     * @author: yzc
     * @date: 2023-06-14 10:54
     * @param category
     * @return com.sckw.core.web.response.HttpResult
     */
    @GetMapping(value = "/statistics")
    @Log(description = "消息统计查询")
    public HttpResult statistics(@RequestParam(required = false) String category) {
        List<MessagesStatisticsResVO> result = messageService.statistics(category);
        return HttpResult.ok(result);
    }

    /**
     * @param findMessagesReqVO
     * @return com.sckw.core.web.response.HttpResult
     * @desc: 查找消息集合
     * @author: yzc
     * @date: 2023-06-09 15:12
     */
    @PostMapping(value = "/findList", produces = MediaType.APPLICATION_JSON_VALUE)
    @Log(description = "查找消息列表")
    public HttpResult findList(@RequestBody FindMessagesReqVO findMessagesReqVO) {
        return HttpResult.ok(messageService.selectMessages(findMessagesReqVO));
    }

}
