package com.emoon.system.controller.system;

import cn.dev33.satoken.annotation.SaCheckPermission;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
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;
import com.emoon.system.domain.bo.SysProjectBo;
import com.emoon.system.domain.vo.SysProjectVo;
import com.emoon.system.service.ISysProjectService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author destiny
 * @description:
 * @date 2025/8/24 18:01
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/project")
public class SysProjectController extends BaseController {

    private final ISysProjectService sysProjectService;

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

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

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

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

    /**
     * 修改项目
     */
    @SaCheckPermission("system:project:edit")
    @Log(title = "项目", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody SysProjectBo bo) {
        return toAjax(sysProjectService.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(sysProjectService.deleteWithValidByIds(List.of(ids), true));
    }

}
