package io.wdd.server.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.wdd.common.response.R;
import io.wdd.server.beans.request.ProjectQueryEntity;
import io.wdd.server.beans.vo.ProjectServerVO;
import io.wdd.server.coreService.CoreProjectServerService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/project-server")
@Api("Project-Server绑定")
public class ProjectServerController {

    @Resource
    CoreProjectServerService coreProjectServerService;

    @GetMapping("/all")
    @ApiOperation("Project-Server查询所有项目的服务器信息")
    public R<List<ProjectServerVO>> projectServerAll() {

        List<ProjectServerVO> projectServerVOS = coreProjectServerService.projectServerAll();
        return R.ok(projectServerVOS);
    }

    @PostMapping("/one")
    @ApiOperation("Project-Server查询单个项目的服务器信息")
    public R<ProjectServerVO> projectServerOne(
            @RequestParam(value = "projectId", name = "projectId")
            @ApiParam(value = "projectId") Long projectId
    ) {

        ProjectServerVO projectServerVO = coreProjectServerService.projectServerOne(projectId);
        return R.ok(projectServerVO);
    }

    @PostMapping("/query/one")
    @ApiOperation("Project-Server查询特定项目的服务器信息")
    public R<ProjectServerVO> projectServerOne(
            @RequestBody @Validated ProjectQueryEntity projectQueryEntity
    ) {

        ProjectServerVO projectServerVO = coreProjectServerService.projectServerQuery(projectQueryEntity);
        return R.ok(projectServerVO);
    }


    @PostMapping("/create")
    @ApiOperation("Project-Server-创建一个Project-Server绑定关系")
    public R<String> projectServerCreate(
            @RequestParam(value = "projectId", name = "projectId")
            @ApiParam(value = "projectId") Long projectId,
            @RequestParam(value = "serverId", name = "serverId")
            @ApiParam(value = "serverId") Long serverId
    ) {

        String result = "创建Project-Server成功！";


        if (!coreProjectServerService.projectServerCreate(
                projectId,
                serverId
        )) {
            result = "创建Project-Server失败！";
        }
        return R.ok(result);
    }

    @PostMapping("/create/multi")
    @ApiOperation("Project-Server-创建一对多的Project-Server绑定关系")
    public R<String> projectServerCreateMulti(
            @RequestParam(value = "projectId", name = "projectId")
            @ApiParam(value = "projectId") Long projectId,
            @RequestParam(value = "serverIdList", name = "serverIdList")
            @ApiParam(value = "serverIdList") List<Long> serverIdList
    ) {

        String result = "创建Project-Server成功！";

        if (!coreProjectServerService.projectServerCreateMulti(
                projectId,
                serverIdList
        )) {
            result = "创建Project-Server失败！";
        }
        return R.ok(result);
    }

    @PostMapping("/update")
    @ApiOperation("Project-Server-更新一个Project-Server")
    public R<String> projectServerUpdate(
            @RequestParam(value = "projectId", name = "projectId")
            @ApiParam(value = "projectId") Long projectId,
            @RequestParam(value = "serverId", name = "serverId")
            @ApiParam(value = "serverId") Long serverId
    ) {

        String result = "更新Project-Server成功！";
        if (!coreProjectServerService.projectServerUpdate(
                projectId,
                serverId
        )) {
            result = "更新Project-Server失败！";
        }
        return R.ok(result);
    }

    @PostMapping("/delete")
    @ApiOperation("Project-Server-删除一个Project-Server")
    public R<String> projectServerDelete(
            @RequestParam(value = "projectId", name = "projectId")
            @ApiParam(value = "projectId") Long projectId,
            @RequestParam(value = "serverId", name = "serverId")
            @ApiParam(value = "serverId") Long serverId
    ) {

        String result = "删除Project-Server成功！";
        if (!coreProjectServerService.projectServerDelete(
                projectId,
                serverId
        )) {
            result = "删除Project-Server失败！";
        }
        return R.ok(result);
    }


}
