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

import com.njcn.rdms.framework.common.pojo.CommonResult;
import com.njcn.rdms.framework.common.pojo.PageResult;
import com.njcn.rdms.module.project.controller.admin.project.task.vo.worklog.TaskWorklogPageReqVO;
import com.njcn.rdms.module.project.controller.admin.project.task.vo.worklog.TaskWorklogRespVO;
import com.njcn.rdms.module.project.controller.admin.project.task.vo.worklog.TaskWorklogSaveReqVO;
import com.njcn.rdms.module.project.service.project.task.worklog.TaskWorklogService;
import io.swagger.v3.oas.annotations.Operation;
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.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

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

@Tag(name = "管理后台 - 任务工时")
@RestController
@RequestMapping("/project/project/{projectId}/executions/{executionId}/tasks/{taskId}")
@Validated
public class TaskWorklogController {

    @Resource
    private TaskWorklogService taskWorklogService;

    @GetMapping("/worklogs")
    @Operation(summary = "获取任务工时分页")
    public CommonResult<PageResult<TaskWorklogRespVO>> getWorklogPage(@PathVariable("projectId") Long projectId,
                                                                      @PathVariable("executionId") Long executionId,
                                                                      @PathVariable("taskId") Long taskId,
                                                                      @Valid TaskWorklogPageReqVO reqVO) {
        return success(taskWorklogService.getWorklogPage(projectId, executionId, taskId, reqVO));
    }

    @PostMapping("/worklogs")
    @Operation(summary = "新增任务工时")
    public CommonResult<Long> createWorklog(@PathVariable("projectId") Long projectId,
                                            @PathVariable("executionId") Long executionId,
                                            @PathVariable("taskId") Long taskId,
                                            @Valid @RequestBody TaskWorklogSaveReqVO reqVO) {
        return success(taskWorklogService.createWorklog(projectId, executionId, taskId, reqVO));
    }

    @PutMapping("/worklogs/{worklogId}")
    @Operation(summary = "修改任务工时（仅自己）")
    public CommonResult<Boolean> updateWorklog(@PathVariable("projectId") Long projectId,
                                               @PathVariable("executionId") Long executionId,
                                               @PathVariable("taskId") Long taskId,
                                               @PathVariable("worklogId") Long worklogId,
                                               @Valid @RequestBody TaskWorklogSaveReqVO reqVO) {
        taskWorklogService.updateWorklog(projectId, executionId, taskId, worklogId, reqVO);
        return success(true);
    }

    @DeleteMapping("/worklogs/{worklogId}")
    @Operation(summary = "删除任务工时（自己或任务负责人）")
    public CommonResult<Boolean> deleteWorklog(@PathVariable("projectId") Long projectId,
                                               @PathVariable("executionId") Long executionId,
                                               @PathVariable("taskId") Long taskId,
                                               @PathVariable("worklogId") Long worklogId) {
        taskWorklogService.deleteWorklog(projectId, executionId, taskId, worklogId);
        return success(true);
    }

}
