package com.ruoyi.xq.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.xq.domain.SysPush;
import com.ruoyi.xq.dto.admin.syspush.SysPushParams;
import com.ruoyi.xq.dto.app.common.IdReq;
import com.ruoyi.xq.service.SysPushService;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;

/**
 * 推送系统消息
 *
 * @author 77
 * @date 2024-03-20
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/xq/sysPush")
public class SysPushController extends BaseController {

    private final SysPushService sysPushService;

    /**
     * 查询推送系统消息列表
     */
    @SaCheckPermission("xq:sysPush:list")
    @GetMapping("/list")
    public TableDataInfo<SysPush> list(SysPush bo, PageQuery pageQuery) {
        Page<SysPush> page = sysPushService.page(pageQuery.build(), Wrappers.lambdaQuery(bo).orderByDesc(SysPush::getCreateTime));
        return TableDataInfo.build(page);
    }

    /**
     * 获取推送系统消息详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("xq:sysPush:query")
    @GetMapping("/{id}")
    public R<SysPush> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(sysPushService.getById(id));
    }

    @SaCheckPermission("xq:sysPush:run")
    @Log(title = "推送系统消息执行", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping("/run")
    public R<Void> run(@RequestBody IdReq idRes) {
        sysPushService.handStart(idRes.getId());
        return R.ok();
    }

    @SaCheckPermission("xq:sysPush:close")
    @Log(title = "取消定时系统消息执行", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping("/close")
    public R<Void> close(@RequestBody IdReq idRes) {
        sysPushService.closeSysPush(idRes.getId());
        return R.ok();
    }

    @SaCheckPermission("xq:sysPush:add")
    @Log(title = "推送系统消息", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@RequestBody SysPushParams params) {
        sysPushService.saveSysPush(params);
        return R.ok();
    }

    /**
     * 删除推送系统消息
     *
     * @param ids 主键串
     */
    @SaCheckPermission("xq:sysPush:remove")
    @Log(title = "推送系统消息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(sysPushService.removeBatchByIds(Arrays.asList(ids)));
    }
}
