package io.wdd.server.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
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.po.ProjectInfoPO;
import io.wdd.server.beans.request.ProjectQueryEntity;
import io.wdd.server.beans.vo.ProjectInfoVO;
import io.wdd.server.coreService.CoreProjectService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

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

@RestController
@Api("Octopus Server - 项目信息")
@RequestMapping("/project")
public class ProjectController {

    @Resource
    CoreProjectService projectService;

    @GetMapping("/all")
    @ApiOperation("项目-获取全部项目信息")
    public R<List<ProjectInfoPO>> projectGetAll(
    ) {
        return R.ok(projectService.projectGetAll());
    }

    @PostMapping("/one")
    @ApiOperation("项目- 根据项目名称模糊查询")
    public R<Page<ProjectInfoPO>> projectQueryOne(
            @Validated @RequestBody ProjectQueryEntity projectQueryEntity
    ) {

        Page<ProjectInfoPO> poPage = projectService.projectQueryByEntity(projectQueryEntity);

        return R.ok(poPage);
    }

    @PostMapping("/create")
    @ApiOperation("项目-创建一个项目")
    public R<String> projectCreate(
            @RequestBody ProjectInfoVO projectInfoVO
    ) {

        String result = "创建项目成功！";
        if (!projectService.projectCreate(projectInfoVO)) {
            result = "创建项目失败！";
        }
        return R.ok(result);
    }

    @PostMapping("/update")
    @ApiOperation("项目-更新一个项目")
    public R<String> projectUpdate(
            @RequestBody ProjectInfoPO projectInfoPO
    ) {

        String result = "更新项目成功！";
        if (!projectService.projectUpdate(projectInfoPO)) {
            result = "更新项目失败！";
        }
        return R.ok(result);
    }

    @PostMapping("/delete")
    @ApiOperation("项目-删除一个项目")
    public R<String> projectDelete(
            @RequestParam(value = "projectId", name = "projectId") @ApiParam(name = "projectId", value = "projectId") Long projectId
    ) {

        String result = "删除项目成功！";
        if (!projectService.projectDelete(projectId)) {
            result = "删除项目失败！";
        }
        return R.ok(result);
    }


}
