package org.dromara.payment.sys.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.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.payment.sys.domain.vo.BusDownloadsVo;
import org.dromara.payment.sys.domain.bo.BusDownloadsBo;
import org.dromara.payment.sys.service.IBusDownloadsService;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 异步下载
 * 前端访问路由地址为:/sys/downloads
 *
 * @author LionLi
 * @date 2024-06-21
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/downloads")
public class BusDownloadsController extends BaseController {

    private final IBusDownloadsService busDownloadsService;

    /**
     * 查询异步下载列表
     */
//    @SaCheckPermission("sys:downloads:list")
    @GetMapping("/list")
    public TableDataInfo<BusDownloadsVo> list(BusDownloadsBo bo, PageQuery pageQuery) {
        return busDownloadsService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出异步下载列表
     */
    @SaCheckPermission("sys:downloads:export")
    @Log(title = "异步下载", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(BusDownloadsBo bo, HttpServletResponse response) {
        List<BusDownloadsVo> list = busDownloadsService.queryList(bo);
        ExcelUtil.exportExcel(list, "异步下载", BusDownloadsVo.class, response);
    }

    /**
     * 获取异步下载详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("sys:downloads:query")
    @GetMapping("/{id}")
    public R<BusDownloadsVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(busDownloadsService.queryById(id));
    }

    /**
     * 新增异步下载
     */
    @SaCheckPermission("sys:downloads:add")
    @Log(title = "异步下载", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody BusDownloadsBo bo) {
        return toAjax(busDownloadsService.insertByBo(bo));
    }

    /**
     * 修改异步下载
     */
    @SaCheckPermission("sys:downloads:edit")
    @Log(title = "异步下载", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody BusDownloadsBo bo) {
        return toAjax(busDownloadsService.updateByBo(bo));
    }

    /**
     * 删除异步下载
     *
     * @param ids 主键串
     */
//    @SaCheckPermission("sys:downloads:remove")
    @Log(title = "异步下载", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(busDownloadsService.deleteWithValidByIds(List.of(ids), true));
    }
}
