package com.emoon.knowledge.controller;

import java.util.List;

import com.emoon.knowledge.domain.bo.ThinkModelBo;
import com.emoon.knowledge.domain.vo.ThinkModelVo;
import com.emoon.knowledge.service.IThinkModelService;
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 com.emoon.common.core.domain.R;
import com.emoon.common.core.validate.validate.AddGroup;
import com.emoon.common.core.validate.validate.EditGroup;
import com.emoon.common.excel.utils.ExcelUtil;
import com.emoon.common.idempotent.annotation.RepeatSubmit;
import com.emoon.common.log.annotation.Log;
import com.emoon.common.log.enums.BusinessType;
import com.emoon.common.mybatis.core.page.PageQuery;
import com.emoon.common.mybatis.core.page.TableDataInfo;
import com.emoon.common.web.core.BaseController;


/**
 * 模型
 *
 * @author wangkang
 * @date 2025-11-26
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/model")
public class ThinkModelController extends BaseController {

    private final IThinkModelService thinkModelService;

    /**
     * 查询模型列表
     */
    @SaCheckPermission("system:model:list")
    @GetMapping("/list")
    public TableDataInfo<ThinkModelVo> list(ThinkModelBo bo, PageQuery pageQuery) {
        return thinkModelService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出模型列表
     */
    @SaCheckPermission("system:model:export")
    @Log(title = "模型", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(ThinkModelBo bo, HttpServletResponse response) {
        List<ThinkModelVo> list = thinkModelService.queryList(bo);
        ExcelUtil.exportExcel(list, "模型", ThinkModelVo.class, response);
    }

    /**
     * 获取模型详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("system:model:query")
    @GetMapping("/{id}")
    public R<ThinkModelVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(thinkModelService.queryById(id));
    }

    /**
     * 新增模型
     */
    @SaCheckPermission("system:model:add")
    @Log(title = "模型", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody ThinkModelBo bo) {
        return toAjax(thinkModelService.insertByBo(bo));
    }

    /**
     * 修改模型
     */
    @SaCheckPermission("system:model:edit")
    @Log(title = "模型", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody ThinkModelBo bo) {
        return toAjax(thinkModelService.updateByBo(bo));
    }

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