package org.dromara.project.controller;

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 org.dromara.common.core.constant.HttpStatus;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.exception.ServiceException;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
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.project.domain.req.project.ProjectCreateReq;
import org.dromara.project.domain.req.project.ProjectQueryReq;
import org.dromara.project.domain.req.project.ProjectUpdateReq;
import org.dromara.project.domain.vo.BusProjectVo;
import org.dromara.project.service.IBusProjectService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 项目
 *
 * @author lcj
 * @date 2025-03-04
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/project/project")
public class BusProjectController extends BaseController {

    private final IBusProjectService projectService;

    /**
     * 查询项目列表
     */
    @SaCheckPermission("project:project:list")
    @GetMapping("/list")
    public TableDataInfo<BusProjectVo> list(ProjectQueryReq req, PageQuery pageQuery) {
        return projectService.queryPageList(req, pageQuery);
    }

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

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

    /**
     * 新增项目
     */
    @SaCheckPermission("project:project:add")
    @Log(title = "项目", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Long> add(@Validated(AddGroup.class) @RequestBody ProjectCreateReq req) {
        if (req == null) {
            throw new ServiceException("参数不能为空", HttpStatus.BAD_REQUEST);
        }
        return R.ok(projectService.insertByBo(req));
    }

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

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