package com.kingtom.shengtai.api.controller;

import java.util.List;

import com.kingtom.kirin.api.security.SaSecurityUtils;
import com.kingtom.kirin.core.common.base.PageInfo;
import com.kingtom.kirin.core.common.base.Result;
import com.kingtom.kirin.core.common.utils.CollectionUtils;
import com.kingtom.shengtai.api.mapper.MsgMapper;
import com.kingtom.shengtai.api.model.MsgSummaryDTO;
import com.kingtom.shengtai.app.message.model.MsgReceive;
import com.kingtom.shengtai.app.message.model.MsgSummary;
import com.kingtom.shengtai.app.message.service.IMsgReceiveService;
import com.kingtom.shengtai.app.message.service.IMsgSummaryService;
import com.mybatisflex.core.paginate.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 消息接收记录表 控制层。
 *
 * @author wany
 * @since 2023-12-27
 */
@RestController
@RequestMapping("/message")
@Tag(name = "MessageController", description = "消息接收记录表 控制层。")
public class MessageController{

    @Autowired
    private IMsgReceiveService receiveService;

    @Autowired
    private IMsgSummaryService msgSummaryService;

    /**
     * 分页查询消息内容表
     *
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Operation(description = "分页查询消息内容表")
    @GetMapping("page")
    public Result<PageInfo<MsgSummaryDTO>> page(Integer pageNum, Integer pageSize){
        String userId = SaSecurityUtils.findSessionData().getUserId();
        Page<MsgReceive> page = receiveService.page(userId, pageNum, pageSize);
        return Result.ok(CollectionUtils.convertPage(page, MsgSummaryDTO::convert));
    }

    @Operation(description = "根据id获取消息详情")
    @GetMapping("{id}/info")
    public Result<MsgSummaryDTO> findById(@PathVariable("id") String id){
        MsgSummary summary = msgSummaryService.getById(id);
        return Result.ok(MsgMapper.INSTANCE.convert(summary));
    }

    @Operation(description = "消息已读")
    @PostMapping("read")
    public Result<String> read(@RequestBody List<String> msgIds){
        String userId = SaSecurityUtils.findSessionData().getUserId();
        receiveService.updateMsgRead(userId, msgIds);
        return Result.ok(null);
    }

    @Operation(description = "消息全部已读")
    @PostMapping("readAll")
    public Result<String> readAll(){
        String userId = SaSecurityUtils.findSessionData().getUserId();
        receiveService.updateMsgRead(userId, null);
        return Result.ok(null);
    }

}
