package org.dromara.ai.controller.admin;

import cn.dev33.satoken.annotation.SaCheckPermission;
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.Valid;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.domain.R;
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.dromara.ai.domain.bo.AiPaintingTemplateBo;
import org.dromara.ai.domain.vo.AiPaintingTemplateVo;
import org.dromara.ai.service.AiPaintingTemplateService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * AI绘画参数模板管理
 *
 * @author pengles
 */
@Tag(name = "管理端-AI绘画参数模板管理", description = "AI绘画参数模板管理接口")
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/ai/painting/template")
public class AdminAiPaintingTemplateController extends BaseController {

    private final AiPaintingTemplateService aiPaintingTemplateService;

    /**
     * 查询AI绘画参数模板列表
     */
    @Operation(summary = "查询AI绘画参数模板列表", description = "分页查询AI绘画参数模板列表，支持多条件搜索")
    @SaCheckPermission("ai:painting:template:list")
    @GetMapping("/list")
    public TableDataInfo<AiPaintingTemplateVo> list(@Parameter(description = "模板查询条件") AiPaintingTemplateBo bo,
                                                      @Parameter(description = "分页参数") PageQuery pageQuery) {
        return aiPaintingTemplateService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出AI绘画参数模板列表
     */
    @Operation(summary = "导出AI绘画参数模板列表", description = "导出AI绘画参数模板到Excel")
    @SaCheckPermission("ai:painting:template:export")
    @Log(title = "AI绘画参数模板", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(@Parameter(description = "模板查询条件") AiPaintingTemplateBo bo,
                       @Parameter(description = "响应对象") HttpServletResponse response) {
        List<AiPaintingTemplateVo> list = aiPaintingTemplateService.queryList(bo);
        ExcelUtil.exportExcel(list, "AI绘画参数模板", AiPaintingTemplateVo.class, response);
    }

    /**
     * 获取AI绘画参数模板详细信息
     */
    @Operation(summary = "获取AI绘画参数模板详情", description = "根据模板ID获取详细信息")
    @SaCheckPermission("ai:painting:template:query")
    @GetMapping("/{id}")
    public R<AiPaintingTemplateVo> getInfo(@Parameter(description = "模板ID", required = true)
                                            @NotNull(message = "主键不能为空")
                                            @PathVariable Long id) {
        return R.ok(aiPaintingTemplateService.queryById(id));
    }

    /**
     * 新增AI绘画参数模板
     */
    @Operation(summary = "新增AI绘画参数模板", description = "新增AI绘画参数模板")
    @SaCheckPermission("ai:painting:template:add")
    @Log(title = "AI绘画参数模板", businessType = BusinessType.INSERT)
    @PostMapping
    public R<Void> add(@Valid @RequestBody AiPaintingTemplateBo bo) {
        return toAjax(aiPaintingTemplateService.insertByBo(bo));
    }

    /**
     * 修改AI绘画参数模板
     */
    @Operation(summary = "修改AI绘画参数模板", description = "修改AI绘画参数模板")
    @SaCheckPermission("ai:painting:template:edit")
    @Log(title = "AI绘画参数模板", businessType = BusinessType.UPDATE)
    @PutMapping
    public R<Void> edit(@Valid @RequestBody AiPaintingTemplateBo bo) {
        return toAjax(aiPaintingTemplateService.updateByBo(bo));
    }

    /**
     * 批量删除AI绘画参数模板
     */
    @Operation(summary = "批量删除AI绘画参数模板", description = "根据ID数组批量删除AI绘画参数模板")
    @SaCheckPermission("ai:painting:template:remove")
    @Log(title = "AI绘画参数模板", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@Parameter(description = "模板ID数组", required = true)
                           @NotNull(message = "主键不能为空")
                           @PathVariable Long[] ids) {
        return toAjax(aiPaintingTemplateService.deleteWithValidByIds(Arrays.asList(ids), true));
    }

}