package org.dromara.zhishu.controller;

import java.util.List;
import java.util.Map;

import cn.dev33.satoken.annotation.SaIgnore;
import com.pdd.pop.sdk.common.util.JsonUtil;
import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.dromara.zhishu.domain.bo.ViolationBo;
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.zhishu.domain.vo.RunningLogFileVo;
import org.dromara.zhishu.domain.bo.RunningLogFileBo;
import org.dromara.zhishu.service.IRunningLogFileService;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 程序运行日志表
 *
 * @author yxy
 * @date 2025-06-12
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/zhishu/runningLogFile")
public class RunningLogFileController extends BaseController {

    private final IRunningLogFileService runningLogFileService;

    /**
     * 查询程序运行日志表列表
     */
    @SaCheckPermission("zhishu:runningLogFile:list")
    @GetMapping("/list")
    public TableDataInfo<RunningLogFileVo> list(RunningLogFileBo bo, PageQuery pageQuery) {
        return runningLogFileService.queryPageList(bo, pageQuery);
    }

    @SaIgnore
    @PostMapping("/newList")
    public String newList(@RequestBody Map map){
        Integer pageSize = Integer.parseInt(map.get("pageSize").toString());
        Integer pageNum = Integer.parseInt(map.get("pageNum").toString());

        PageQuery pageQuery = new PageQuery(pageSize,pageNum);

        RunningLogFileBo bo = new RunningLogFileBo();

        return JsonUtil.transferToJson(runningLogFileService.queryPageList(bo, pageQuery));
    }

    /**
     * 导出程序运行日志表列表
     */
    @SaCheckPermission("zhishu:runningLogFile:export")
    @Log(title = "程序运行日志表", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(RunningLogFileBo bo, HttpServletResponse response) {
        List<RunningLogFileVo> list = runningLogFileService.queryList(bo);
        ExcelUtil.exportExcel(list, "程序运行日志表", RunningLogFileVo.class, response);
    }

    /**
     * 获取程序运行日志表详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("zhishu:runningLogFile:query")
    @GetMapping("/{id}")
    public R<RunningLogFileVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(runningLogFileService.queryById(id));
    }

    /**
     * 新增程序运行日志表
     */
    @SaCheckPermission("zhishu:runningLogFile:add")
    @Log(title = "程序运行日志表", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody RunningLogFileBo bo) {
        return toAjax(runningLogFileService.insertByBo(bo));
    }

    /**
     * 修改程序运行日志表
     */
    @SaCheckPermission("zhishu:runningLogFile:edit")
    @Log(title = "程序运行日志表", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody RunningLogFileBo bo) {
        return toAjax(runningLogFileService.updateByBo(bo));
    }

    /**
     * 删除程序运行日志表
     *
     * @param ids 主键串
     */
    @SaCheckPermission("zhishu:runningLogFile:remove")
    @Log(title = "程序运行日志表", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(runningLogFileService.deleteWithValidByIds(List.of(ids), true));
    }
}
