package com.njcn.rdms.module.project.controller.admin.project;

import com.njcn.rdms.framework.common.pojo.CommonResult;
import com.njcn.rdms.module.project.controller.admin.project.vo.member.ProjectMemberBatchCreateReqVO;
import com.njcn.rdms.module.project.controller.admin.project.vo.member.ProjectMemberBatchInactiveReqVO;
import com.njcn.rdms.module.project.controller.admin.project.vo.member.ProjectMemberInactiveReqVO;
import com.njcn.rdms.module.project.controller.admin.project.vo.member.ProjectMemberRespVO;
import com.njcn.rdms.module.project.controller.admin.project.vo.member.ProjectMemberSaveReqVO;
import com.njcn.rdms.module.project.controller.admin.project.vo.member.ProjectMemberUpdateReqVO;
import com.njcn.rdms.module.project.service.project.ProjectMemberService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import static com.njcn.rdms.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 项目成员")
@RestController
@RequestMapping("/project/project")
@Validated
public class ProjectMemberController {

    @Resource
    private ProjectMemberService projectMemberService;

    @GetMapping("/{id}/members")
    @Operation(summary = "获取项目成员列表")
    @Parameter(name = "id", description = "项目编号", required = true, example = "1024")
    public CommonResult<List<ProjectMemberRespVO>> getProjectMemberList(@PathVariable("id") Long projectId) {
        return success(projectMemberService.getProjectMemberList(projectId));
    }

    @PostMapping("/{id}/members")
    @Operation(summary = "新增项目成员")
    @Parameter(name = "id", description = "项目编号", required = true, example = "1024")
    public CommonResult<Long> createProjectMember(@PathVariable("id") Long projectId,
                                                  @Valid @RequestBody ProjectMemberSaveReqVO reqVO) {
        return success(projectMemberService.createProjectMember(projectId, reqVO));
    }

    @PostMapping("/{id}/members/batch")
    @Operation(summary = "批量新增项目成员")
    @Parameter(name = "id", description = "项目编号", required = true, example = "1024")
    public CommonResult<List<Long>> batchCreateProjectMembers(@PathVariable("id") Long projectId,
                                                              @Valid @RequestBody ProjectMemberBatchCreateReqVO reqVO) {
        return success(projectMemberService.batchCreateProjectMembers(projectId, reqVO));
    }

    @PutMapping("/{id}/members/{memberId}")
    @Operation(summary = "调整项目成员角色")
    public CommonResult<Boolean> updateProjectMember(@PathVariable("id") Long projectId,
                                                     @PathVariable("memberId") Long memberId,
                                                     @Valid @RequestBody ProjectMemberUpdateReqVO reqVO) {
        projectMemberService.updateProjectMember(projectId, memberId, reqVO);
        return success(true);
    }

    @PostMapping("/{id}/members/{memberId}/inactive")
    @Operation(summary = "移出项目成员")
    public CommonResult<Boolean> inactiveProjectMember(@PathVariable("id") Long projectId,
                                                       @PathVariable("memberId") Long memberId,
                                                       @Valid @RequestBody ProjectMemberInactiveReqVO reqVO) {
        projectMemberService.inactiveProjectMember(projectId, memberId, reqVO);
        return success(true);
    }

    @PostMapping("/{id}/members/batch/inactive")
    @Operation(summary = "批量移出项目成员")
    @Parameter(name = "id", description = "项目编号", required = true, example = "1024")
    public CommonResult<Boolean> batchInactiveProjectMembers(@PathVariable("id") Long projectId,
                                                             @Valid @RequestBody ProjectMemberBatchInactiveReqVO reqVO) {
        projectMemberService.batchInactiveProjectMembers(projectId, reqVO);
        return success(true);
    }

}
