package com.hotwj.platform.config.dispatchTemplate.controller;

import com.hotwj.platform.config.dispatchTemplate.domain.bo.HotDispatchTemplateBo;
import com.hotwj.platform.config.dispatchTemplate.domain.vo.HotDispatchTemplateVo;
import com.hotwj.platform.config.dispatchTemplate.service.IHotDispatchTemplateService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
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.idempotent.annotation.RepeatSubmit;
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 2026-02-15
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/config/dispatchTemplate")
@Tag(name = "下发模板", description = "下发模板管理")
public class HotDispatchTemplateController extends BaseController {

    private final IHotDispatchTemplateService hotDispatchTemplateService;

    /**
     * 查询下发模板列表
     */
    //@SaCheckPermission("config:dispatchTemplate:list")
    @GetMapping("/list")
    @Operation(summary = "分页查询下发模板列表")
    public TableDataInfo<HotDispatchTemplateVo> list(HotDispatchTemplateBo bo, PageQuery pageQuery) {
        return hotDispatchTemplateService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出下发模板列表
     */
    //@SaCheckPermission("config:dispatchTemplate:export")
    @Log(title = "下发模板", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @Operation(summary = "导出下发模板列表")
    public void export(HotDispatchTemplateBo bo, HttpServletResponse response) {
        List<HotDispatchTemplateVo> list = hotDispatchTemplateService.queryList(bo);
        ExcelUtil.exportExcel(list, "下发模板", HotDispatchTemplateVo.class, response);
    }

    /**
     * 获取下发模板详细信息
     *
     * @param id 主键
     */
    //@SaCheckPermission("config:dispatchTemplate:query")
    @GetMapping("/{id}")
    @Operation(summary = "获取下发模板详细信息")
    public R<HotDispatchTemplateVo> getInfo(@NotNull(message = "主键不能为空")
                                            @Parameter(name = "id", description = "主键", required = true, example = "1")
                                            @PathVariable Long id) {
        return R.ok(hotDispatchTemplateService.queryById(id));
    }

    /**
     * 新增下发模板
     */
    //@SaCheckPermission("config:dispatchTemplate:add")
    @Log(title = "下发模板", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    @Operation(summary = "新增下发模板")
    public R<Void> add(@Validated(AddGroup.class) @RequestBody HotDispatchTemplateBo bo) {
        return toAjax(hotDispatchTemplateService.insertByBo(bo));
    }

    /**
     * 修改下发模板
     */
    //@SaCheckPermission("config:dispatchTemplate:edit")
    @Log(title = "下发模板", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    @Operation(summary = "修改下发模板")
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody HotDispatchTemplateBo bo) {
        return toAjax(hotDispatchTemplateService.updateByBo(bo));
    }

    /**
     * 删除下发模板
     *
     * @param ids 主键串
     */
    //@SaCheckPermission("config:dispatchTemplate:remove")
    @Log(title = "下发模板", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    @Operation(summary = "删除下发模板")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @Parameter(name = "ids", description = "主键串", required = true, example = "1,2,3")
                          @PathVariable Long[] ids) {
        return toAjax(hotDispatchTemplateService.deleteWithValidByIds(List.of(ids), true));
    }

    @Log(title = "下发模板", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping("/useDefault")
    @Operation(summary = "采用默认配置")
    public R<Void> useDefault() {
        return toAjax(hotDispatchTemplateService.useDefault());
    }
}
