package cn.iocoder.yudao.module.bpm.controller.admin.oa;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.module.bpm.controller.admin.oa.vo.worktask.BpmOAWorkTaskPageReqVO;
import cn.iocoder.yudao.module.bpm.controller.admin.oa.vo.worktask.BpmOAWorkTaskReqVO;
import cn.iocoder.yudao.module.bpm.controller.admin.oa.vo.worktask.BpmOAWorkTaskRespVO;
import cn.iocoder.yudao.module.bpm.convert.oa.BpmOAWorkTaskConvert;
import cn.iocoder.yudao.module.bpm.dal.dataobject.oa.BpmOAWorkTaskDO;
import cn.iocoder.yudao.module.bpm.service.oa.BpmOAWorkTaskService;
import cn.iocoder.yudao.module.bpm.service.task.BpmTaskService;
import cn.iocoder.yudao.module.system.api.dept.DeptApi;
import cn.iocoder.yudao.module.system.api.dept.dto.DeptRespDTO;
import cn.iocoder.yudao.module.system.api.user.AdminUserApi;
import cn.iocoder.yudao.module.system.api.user.dto.AdminUserRespDTO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId;

/**
 * OA 任务分配 Controller
 *
 * @author 姚君

 */
@Tag(name = "管理后台 - OA 任务分配")
@RestController
@RequestMapping("/bpm/oa/worktask")
@Validated
public class BpmOAWorkTaskController {

    @Resource
    private BpmOAWorkTaskService workTaskService;

    @Resource
    private AdminUserApi userApi;

    @Resource
    private DeptApi deptApi;

    @Resource
    private BpmTaskService taskService;

    @PostMapping("/create")
    @Operation(summary = "创建任务分配")
    public CommonResult<Long> createLeave(@Valid @RequestBody BpmOAWorkTaskReqVO createReqVO) {
        return success(workTaskService.createWorkTask(getLoginUserId(), createReqVO));
    }

    @GetMapping("/get")
    @Operation(summary = "获得任务分配")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    public CommonResult<BpmOAWorkTaskRespVO> getTask(@RequestParam("id") Long id) {

        BpmOAWorkTaskDO workTask = workTaskService.getWorkTask(id);
        //获得 责任人用户信息
        AdminUserRespDTO userRespDTO = userApi.getUser(workTask.getReceiverUserId()).getCheckedData();
        //获得 责任人部门信息
        DeptRespDTO deptRespDTO = deptApi.getDept(userRespDTO.getDeptId()).getCheckedData();

        String taskId = null;
        if (workTask.getWorkTaskStatus() != 3) {

            //获得TASK
            taskId = taskService.getCurrentTaskIdByProcessInstanceId(workTask.getProcessInstanceId());
        }

        return success(BpmOAWorkTaskConvert.INSTANCE.convert(workTask, userRespDTO, deptRespDTO, taskId));
    }

    @GetMapping("/getByProcessInstanceId")
    @Operation(summary = "获得任务分配")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    public CommonResult<BpmOAWorkTaskDO> getTask(@RequestParam("id") String processInstanceId) {

        BpmOAWorkTaskDO workTask = workTaskService.getWorkTaskByProcessInstanceId(processInstanceId);

        return success(workTask);
    }

    @PutMapping("/add")
    @Operation(summary = "添加任务跟踪数据【全量传】")
    public CommonResult<Boolean> addTrackInfo(@RequestBody BpmOAWorkTaskReqVO updateReqVO) {

        workTaskService.addTrackInfo(updateReqVO) ;
        return success(true);
    }

    @PostMapping("/update")
    @Operation(summary = "更新工作任务的状态和进度")
    public CommonResult<Boolean> updateWorkTask(@RequestBody BpmOAWorkTaskReqVO createReqVO) {

        workTaskService.updateWorkTask(createReqVO) ;
        return success(true);
    }

    @GetMapping("/page")
    @Operation(summary = "获得工作任务分页")
    public CommonResult<PageResult<BpmOAWorkTaskRespVO>> getPage(@Valid BpmOAWorkTaskPageReqVO pageVO) {

        PageResult<BpmOAWorkTaskRespVO> pageResult = workTaskService.getPage(getLoginUserId(), pageVO);
        return success(pageResult);
    }

    @GetMapping("/my-page")
    @Operation(summary = "获得我的任务分页")
    public CommonResult<PageResult<BpmOAWorkTaskRespVO>> getMyPage(@Valid BpmOAWorkTaskPageReqVO pageVO) {

        PageResult<BpmOAWorkTaskRespVO> pageResult = workTaskService.getMyPage(getLoginUserId(), pageVO);
        return success(pageResult);
    }

}
