package com.yingpaipay.setting.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 com.yingpaipay.setting.domain.vo.KeywordSettingVo;
import com.yingpaipay.setting.domain.bo.KeywordSettingBo;
import com.yingpaipay.setting.service.IKeywordSettingService;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 文件夹关键词设置
 *
 * @author Huanyi
 * @date 2026-01-20
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/setting/keyword")
public class KeywordSettingController extends BaseController {

    private final IKeywordSettingService keywordSettingService;

    /**
     * 查询文件夹关键词设置列表
     */
    @SaCheckPermission("setting:keyword:list")
    @GetMapping("/list")
    public TableDataInfo<KeywordSettingVo> list(KeywordSettingBo bo, PageQuery pageQuery) {
        return keywordSettingService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出文件夹关键词设置列表
     */
    @SaCheckPermission("setting:keyword:export")
    @Log(title = "文件夹关键词设置", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(KeywordSettingBo bo, HttpServletResponse response) {
        List<KeywordSettingVo> list = keywordSettingService.queryList(bo);
        ExcelUtil.exportExcel(list, "文件夹关键词设置", KeywordSettingVo.class, response);
    }

    /**
     * 获取文件夹关键词设置详细信息
     *
     * @param id 主键
     */
    @GetMapping("/{id}")
    public R<KeywordSettingVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(keywordSettingService.queryById(id));
    }

    /**
     * 新增文件夹关键词设置
     */
    @SaCheckPermission("setting:keyword:add")
    @Log(title = "文件夹关键词设置", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody KeywordSettingBo bo) {
        return toAjax(keywordSettingService.insertByBo(bo));
    }

    /**
     * 修改文件夹关键词设置
     */
    @SaCheckPermission("setting:keyword:edit")
    @Log(title = "文件夹关键词设置", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody KeywordSettingBo bo) {
        return toAjax(keywordSettingService.updateByBo(bo));
    }

    /**
     * 删除文件夹关键词设置
     *
     * @param ids 主键串
     */
    @SaCheckPermission("setting:keyword:remove")
    @Log(title = "文件夹关键词设置", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(keywordSettingService.deleteWithValidByIds(List.of(ids), true));
    }

    @GetMapping("/listOnFolder")
    public R<List<KeywordSettingVo>> listOnFolder() {
        return R.ok(keywordSettingService.list());
    }
}
