package org.dromara.zhishu.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.zhishu.domain.vo.PrintTemplateVo;
import org.dromara.zhishu.domain.bo.PrintTemplateBo;
import org.dromara.zhishu.service.IPrintTemplateService;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 打印模板
 *
 * @author yxy
 * @date 2026-03-07
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/zhishu/printTemplate")
public class PrintTemplateController extends BaseController {

    private final IPrintTemplateService printTemplateService;

    /**
     * 查询打印模板列表
     */
    @SaCheckPermission("zhishu:printTemplate:list")
    @GetMapping("/list")
    public TableDataInfo<PrintTemplateVo> list(PrintTemplateBo bo, PageQuery pageQuery) {
        return printTemplateService.queryPageList(bo, pageQuery);
    }

    /**
     * 查询打印模板列表
     * @param bo
     * @return
     */
    @SaCheckPermission("zhishu:printTemplate:list")
    @GetMapping("/listNoPage")
    public List<PrintTemplateVo> listNoPage(PrintTemplateBo bo){
        return printTemplateService.queryList(bo);
    }

    /**
     * 导出打印模板列表
     */
    @SaCheckPermission("zhishu:printTemplate:export")
    @Log(title = "打印模板", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(PrintTemplateBo bo, HttpServletResponse response) {
        List<PrintTemplateVo> list = printTemplateService.queryList(bo);
        ExcelUtil.exportExcel(list, "打印模板", PrintTemplateVo.class, response);
    }

    /**
     * 获取打印模板详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("zhishu:printTemplate:query")
    @GetMapping("/{id}")
    public R<PrintTemplateVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(printTemplateService.queryById(id));
    }

    /**
     * 新增打印模板
     */
    @SaCheckPermission("zhishu:printTemplate:add")
    @Log(title = "打印模板", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody PrintTemplateBo bo) {
        return toAjax(printTemplateService.insertByBo(bo));
    }

    /**
     * 修改打印模板
     */
    @SaCheckPermission("zhishu:printTemplate:edit")
    @Log(title = "打印模板", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody PrintTemplateBo bo) {
        return toAjax(printTemplateService.updateByBo(bo));
    }

    /**
     * 删除打印模板
     *
     * @param ids 主键串
     */
    @SaCheckPermission("zhishu:printTemplate:remove")
    @Log(title = "打印模板", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(printTemplateService.deleteWithValidByIds(List.of(ids), true));
    }
}
