package com.hotwj.platform.reportStatistics.controller;

import com.hotwj.platform.reportStatistics.domain.bo.SysReportTemplateBo;
import com.hotwj.platform.reportStatistics.domain.vo.SysReportTemplateVo;
import com.hotwj.platform.reportStatistics.service.ISysReportTemplateService;
import com.hotwj.platform.reportStatistics.service.ReportRenderService;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
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.excel.utils.ExcelUtil;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.web.core.BaseController;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 报表模板
 *
 * @author shihongwei
 * @date 2024-03-15
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/report/template")
public class SysReportTemplateController extends BaseController {

    private final ISysReportTemplateService sysReportTemplateService;
    private final ReportRenderService reportRenderService;

    /**
     * 查询报表模板列表
     */
    //@SaCheckPermission("report:template:list")
    @GetMapping("/list")
    public TableDataInfo<SysReportTemplateVo> list(SysReportTemplateBo bo, PageQuery pageQuery) {
        return sysReportTemplateService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出报表模板列表
     */
    //@SaCheckPermission("report:template:export")
    @Log(title = "报表模板", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(SysReportTemplateBo bo, HttpServletResponse response) {
        List<SysReportTemplateVo> list = sysReportTemplateService.queryList(bo);
        ExcelUtil.exportExcel(list, "报表模板", SysReportTemplateVo.class, response);
    }

    /**
     * 获取报表模板详细信息
     */
    //@SaCheckPermission("report:template:query")
    @GetMapping("/{id}")
    public R<SysReportTemplateVo> getInfo(@NotNull(message = "主键不能为空") @PathVariable Long id) {
        return R.ok(sysReportTemplateService.queryById(id));
    }

    /**
     * 新增报表模板
     */
    //@SaCheckPermission("report:template:add")
    @Log(title = "报表模板", businessType = BusinessType.INSERT)
    @PostMapping
    public R<Void> add(@Validated(AddGroup.class) @RequestBody SysReportTemplateBo bo) {
        return toAjax(sysReportTemplateService.insertByBo(bo));
    }

    /**
     * 修改报表模板
     */
    //@SaCheckPermission("report:template:edit")
    @Log(title = "报表模板", businessType = BusinessType.UPDATE)
    @PutMapping
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody SysReportTemplateBo bo) {
        return toAjax(sysReportTemplateService.updateByBo(bo));
    }

    /**
     * 同步模版
     */
    //@SaCheckPermission("report:template:sync")
    @Log(title = "同步模版", businessType = BusinessType.UPDATE)
    @PostMapping("/sync")
    public R<Void> sync() {
        sysReportTemplateService.syncTemplatesFromClasspath();
        return R.ok();
    }

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