package com.wzj.soopin.im.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wzj.soopin.im.domain.SysMessage;
import com.wzj.soopin.im.domain.bo.SysMessageBo;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.satoken.utils.LoginHelper;
import org.dromara.common.web.core.BaseController;
import com.wzj.soopin.im.domain.vo.SysMessageVo;
import com.wzj.soopin.im.service.ISysMessageService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * 消息管理
 *
 * @author ruoyi
 */
@Validated
@RestController
@RequiredArgsConstructor
@RequestMapping("/system/message")
public class IMMessageController extends BaseController {

    private final ISysMessageService messageService;

    /**
     * 获取当前用户ID
     */
    private Long getUserId() {
        return LoginHelper.getUserId();
    }

    /**
     * 查询消息列表
     */
//    @SaCheckPermission("system:message:list")
    @Tag(name = "查询消息列表")
    @PostMapping("/page")
    public R<Page<SysMessageVo>> list(@RequestBody SysMessageBo bo, Page page) {
        Page<SysMessage> messagePage = messageService.page(page, bo.toWrapper());
        Page<SysMessageVo> voPage = new Page<>(messagePage.getCurrent(), messagePage.getSize(), messagePage.getTotal());
        voPage.setRecords(MapstructUtils.convert(messagePage.getRecords(), SysMessageVo.class));
        return R.ok(voPage);
    }

    /**
     * 获取消息详细信息
     *
     * @param id 消息ID
     */
//    @SaCheckPermission("system:message:query")
    @GetMapping("/{id}")
    public R<SysMessageVo> getInfo(@NotNull(message = "消息ID不能为空") @PathVariable Long id) {
        return R.ok(messageService.selectMessageById(id));
    }


    /**
     * 发送消息
     */
//    @SaCheckPermission("system:message:send")
    @Log(title = "消息管理", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping("/send")
    public R<Void> send(@RequestBody SysMessageBo bo) {
        // 如果提供了模板ID，则从模板中获取标题和内容
        Long templateId = bo.getTemplateId();
        if(templateId == null){
            return R.fail("模板不能为空");
        }
        messageService.send(bo);
        return R.ok();
    }





}
