package org.dromara.business.controller;

import java.util.List;

import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.dromara.common.satoken.utils.LoginHelper;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.system.domain.vo.CaseMessageVo;
import org.dromara.system.domain.bo.CaseMessageBo;
import org.dromara.system.service.ICaseMessageService;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 消息中心
 *
 * @author 吴志刚
 * @date 2023-12-06
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/business/message")
public class CaseMessageController extends BaseController {

    private final ICaseMessageService caseMessageService;

    /**
     * 查询消息中心列表
     */
    @GetMapping("/pageList")
    public TableDataInfo<CaseMessageVo> list(CaseMessageBo bo, PageQuery pageQuery) {
        return caseMessageService.queryPageList(bo, pageQuery);
    }


    /**
     * 查询用户消息中心列表
     */
    @GetMapping("/list")
    public R<List<CaseMessageVo>> list() {
        CaseMessageBo bo = new CaseMessageBo();
        // 当前登录用户
        bo.setUserId(LoginHelper.getUserId());
        return R.ok(caseMessageService.queryList(bo));
    }


    /**
     * 导出消息中心列表
     */
    @Log(title = "消息中心", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(CaseMessageBo bo, HttpServletResponse response) {
        List<CaseMessageVo> list = caseMessageService.queryList(bo);
        ExcelUtil.exportExcel(list, "消息中心", CaseMessageVo.class, response);
    }

    /**
     * 获取消息中心详细信息
     *
     * @param id 主键
     */
    @GetMapping("/{id}")
    public R<CaseMessageVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(caseMessageService.queryById(id));
    }

    /**
     * 新增消息中心
     */
    @Log(title = "消息中心", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody CaseMessageBo bo) {
        return toAjax(caseMessageService.insertByBo(bo));
    }

    /**
     * 修改消息中心
     */
    @Log(title = "消息中心", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody CaseMessageBo bo) {
        return toAjax(caseMessageService.updateByBo(bo));
    }

    /**
     * 删除消息中心
     *
     * @param ids 主键串
     */
    @Log(title = "消息中心", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(caseMessageService.deleteWithValidByIds(List.of(ids), true));
    }
}
