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.projectteammember.ProjectTeamMemberCreateReq;
import org.dromara.project.domain.req.projectteammember.ProjectTeamMemberQueryReq;
import org.dromara.project.domain.req.projectteammember.ProjectTeamMemberUpdateReq;
import org.dromara.project.domain.vo.BusProjectTeamMemberVo;
import org.dromara.project.service.IBusProjectTeamMemberService;
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/projectTeamMember")
public class BusProjectTeamMemberController extends BaseController {

    private final IBusProjectTeamMemberService busProjectTeamMemberService;

    /**
     * 查询项目班组下的成员列表
     */
    @SaCheckPermission("project:projectTeamMember:list")
    @GetMapping("/list")
    public TableDataInfo<BusProjectTeamMemberVo> list(ProjectTeamMemberQueryReq req, PageQuery pageQuery) {
        return busProjectTeamMemberService.queryPageList(req, pageQuery);
    }

    /**
     * 导出项目班组下的成员列表
     */
    @SaCheckPermission("project:projectTeamMember:export")
    @Log(title = "项目班组下的成员", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(ProjectTeamMemberQueryReq req, HttpServletResponse response) {
        List<BusProjectTeamMemberVo> list = busProjectTeamMemberService.queryList(req);
        ExcelUtil.exportExcel(list, "项目班组下的成员", BusProjectTeamMemberVo.class, response);
    }

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

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

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

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