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.domain.R;
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.projectteam.ProjectTeamCreateReq;
import org.dromara.project.domain.req.projectteam.ProjectTeamQueryReq;
import org.dromara.project.domain.req.projectteam.ProjectTeamUpdateReq;
import org.dromara.project.domain.resp.projectteam.ProjectTeamForemanResp;
import org.dromara.project.domain.vo.BusProjectTeamVo;
import org.dromara.project.service.IBusProjectTeamService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 项目班组
 *
 * @author lcj
 * @date 2025-03-07
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/project/projectTeam")
public class BusProjectTeamController extends BaseController {

    private final IBusProjectTeamService busProjectTeamService;

    /**
     * 查询项目班组列表
     */
    @SaCheckPermission("project:projectTeam:list")
    @GetMapping("/list")
    public TableDataInfo<BusProjectTeamVo> list(ProjectTeamQueryReq req, PageQuery pageQuery) {
        return busProjectTeamService.queryPageList(req, pageQuery);
    }

    /**
     * 根据项目id查询项目班组班组长信息列表
     */
    @SaCheckPermission("project:projectTeam:list")
    @GetMapping("/listForeman/{projectId}")
    public R<List<ProjectTeamForemanResp>> listForeman(@NotNull(message = "项目id不能为空")
                                                       @PathVariable Long projectId) {
        return R.ok(busProjectTeamService.queryForemanList(projectId));
    }

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

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

    /**
     * 新增项目班组
     */
    @SaCheckPermission("project:projectTeam:add")
    @Log(title = "项目班组", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Long> add(@Validated(AddGroup.class) @RequestBody ProjectTeamCreateReq req) {
        return R.ok(busProjectTeamService.insertByBo(req));
    }

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

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