package org.dromara.biz.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.biz.domain.vo.BizTaskLogVo;
import org.dromara.biz.domain.bo.BizTaskLogBo;
import org.dromara.biz.service.IBizTaskLogService;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 任务日志
 *
 * @author chenle
 * @date 2025-05-19
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/biz/taskLog")
public class BizTaskLogController extends BaseController {

    private final IBizTaskLogService bizTaskLogService;

    /**
     * 查询任务日志列表
     */
    @SaCheckPermission("biz:taskLog:list")
    @GetMapping("/list")
    public TableDataInfo<BizTaskLogVo> list(BizTaskLogBo bo, PageQuery pageQuery) {
        return bizTaskLogService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出任务日志列表
     */
    @SaCheckPermission("biz:taskLog:export")
    @Log(title = "任务日志", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(BizTaskLogBo bo, HttpServletResponse response) {
        List<BizTaskLogVo> list = bizTaskLogService.queryList(bo);
        ExcelUtil.exportExcel(list, "任务日志", BizTaskLogVo.class, response);
    }

    /**
     * 获取任务日志详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("biz:taskLog:query")
    @GetMapping("/{id}")
    public R<BizTaskLogVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(bizTaskLogService.queryById(id));
    }

    /**
     * 新增任务日志
     */
    @SaCheckPermission("biz:taskLog:add")
    @Log(title = "任务日志", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody BizTaskLogBo bo) {
        return toAjax(bizTaskLogService.insertByBo(bo));
    }

    /**
     * 修改任务日志
     */
    @SaCheckPermission("biz:taskLog:edit")
    @Log(title = "任务日志", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody BizTaskLogBo bo) {
        return toAjax(bizTaskLogService.updateByBo(bo));
    }

    /**
     * 删除任务日志
     *
     * @param ids 主键串
     */
    @SaCheckPermission("biz:taskLog:remove")
    @Log(title = "任务日志", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(bizTaskLogService.deleteWithValidByIds(List.of(ids), true));
    }
}
