package com.emoon.knowledge.controller;

import java.util.List;

import com.emoon.knowledge.domain.bo.ThinkProjectBo;
import com.emoon.knowledge.domain.vo.ThinkProjectVo;
import com.emoon.knowledge.service.IThinkProjectService;
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/project")
public class ThinkProjectController extends BaseController {

    private final IThinkProjectService thinkProjectService;

    /**
     * 查询项目列表
     */
    @SaCheckPermission("system:project:list")
    @GetMapping("/list")
    public TableDataInfo<ThinkProjectVo> list(ThinkProjectBo bo, PageQuery pageQuery) {
        return thinkProjectService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出项目列表
     */
    @SaCheckPermission("system:project:export")
    @Log(title = "项目", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(ThinkProjectBo bo, HttpServletResponse response) {
        List<ThinkProjectVo> list = thinkProjectService.queryList(bo);
        ExcelUtil.exportExcel(list, "项目", ThinkProjectVo.class, response);
    }

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

    /**
     * 新增项目
     */
    @SaCheckPermission("system:project:add")
    @Log(title = "项目", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody ThinkProjectBo bo) {
        return toAjax(thinkProjectService.insertByBo(bo));
    }

    /**
     * 修改项目
     */
    @SaCheckPermission("system:project:edit")
    @Log(title = "项目", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody ThinkProjectBo bo) {
        return toAjax(thinkProjectService.updateByBo(bo));
    }

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