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

import com.hotwj.platform.config.systemTemplate.domain.bo.HotSystemTemplateBo;
import com.hotwj.platform.config.systemTemplate.domain.vo.HotSystemTemplateVo;
import com.hotwj.platform.config.systemTemplate.service.IHotSystemTemplateService;
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-14
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/config/systemTemplate")
@Tag(name = "体系模板", description = "体系模板管理")
public class HotSystemTemplateController extends BaseController {

    private final IHotSystemTemplateService hotSystemTemplateService;

    /**
     * 查询体系模板列表
     */
    //@SaCheckPermission("config:systemTemplate:list")
    @GetMapping("/list")
    @Operation(summary = "分页查询体系模板列表")
    public TableDataInfo<HotSystemTemplateVo> list(HotSystemTemplateBo bo, PageQuery pageQuery) {
        return hotSystemTemplateService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出体系模板列表
     */
    //@SaCheckPermission("config:systemTemplate:export")
    @Log(title = "体系模板", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @Operation(summary = "导出体系模板列表")
    public void export(HotSystemTemplateBo bo, HttpServletResponse response) {
        List<HotSystemTemplateVo> list = hotSystemTemplateService.queryList(bo);
        ExcelUtil.exportExcel(list, "体系模板", HotSystemTemplateVo.class, response);
    }

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

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

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

    /**
     * 删除体系模板
     *
     * @param ids 主键串
     */
    //@SaCheckPermission("config:systemTemplate: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(hotSystemTemplateService.deleteWithValidByIds(List.of(ids), true));
    }

    //@SaCheckPermission("config:systemTemplate:add")
    @PostMapping("/sync")
    @Operation(summary = "同步总部体系模板到企业")
    public R<Void> sync(@NotNull @RequestParam("companyId") Long companyId,
                        @NotNull @RequestParam("type") Long type) {
        return toAjax(hotSystemTemplateService.syncFromHeadquarters(companyId, type));
    }
}
