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.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.bpm.controller.admin.oa.vo.workorder.*;
import cn.iocoder.yudao.module.bpm.dal.dataobject.oa.BpmOAWorkOrderDO;
import cn.iocoder.yudao.module.bpm.service.oa.BpmOAWorkOrderService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.annotation.security.PermitAll;
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;

/**
 * 管理后台 - BPM OA 工单
 *
 * @author 系统
 */
@Tag(name = "管理后台 - BPM OA 工单")
@RestController
@RequestMapping("/bpm/oa/work-order")
@Validated
@Slf4j
public class BpmOAWorkOrderController {

    @Resource
    private BpmOAWorkOrderService workOrderService;

    @PostMapping("/create")
    @Operation(summary = "创建工单")
    @PreAuthorize("@ss.hasPermission('bpm:oa-work-order:create')")
    public CommonResult<Long> createWorkOrder(@Valid @RequestBody BpmOAWorkOrderCreateReqVO createReqVO) {
        return success(workOrderService.createWorkOrder(getLoginUserId(), createReqVO));
    }

    @GetMapping("/page")
    @Operation(summary = "获得工单分页")
    @PreAuthorize("@ss.hasPermission('bpm:oa-work-order:query')")
    public CommonResult<PageResult<BpmOAWorkOrderRespVO>> getWorkOrderPage(@Valid BpmOAWorkOrderPageReqVO pageReqVO) {
        return success(workOrderService.getWorkOrderPage(getLoginUserId(), pageReqVO));
    }

    @GetMapping("/my-page")
    @Operation(summary = "获得我发起的工单分页")
    @PreAuthorize("@ss.hasPermission('bpm:oa-work-order:query')")
    public CommonResult<PageResult<BpmOAWorkOrderRespVO>> getMyWorkOrderPage(@Valid BpmOAWorkOrderPageReqVO pageReqVO) {
        return success(workOrderService.getMyWorkOrderPage(getLoginUserId(), pageReqVO));
    }

    @GetMapping("/assigned-page")
    @Operation(summary = "获得分配给我的工单分页")
    @PreAuthorize("@ss.hasPermission('bpm:oa-work-order:query')")
    public CommonResult<PageResult<BpmOAWorkOrderRespVO>> getAssignedWorkOrderPage(@Valid BpmOAWorkOrderPageReqVO pageReqVO) {
        return success(workOrderService.getAssignedWorkOrderPage(getLoginUserId(), pageReqVO));
    }

    @GetMapping("/get/{id}")
    @Operation(summary = "获得工单详情")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('bpm:oa-work-order:query')")
    public CommonResult<BpmOAWorkOrderRespVO> getWorkOrder(@PathVariable("id") Long id) {
        return success(workOrderService.getWorkOrderDetail(id));
    }

    @PostMapping("/track")
    @Operation(summary = "添加工单跟踪记录")
    @PreAuthorize("@ss.hasPermission('bpm:oa-work-order:update')")
    public CommonResult<Boolean> addTrackInfo(@Valid @RequestBody BpmOAWorkOrderTrackReqVO trackReqVO) {
        workOrderService.addTrackInfo(getLoginUserId(), trackReqVO);
        return success(true);
    }

    @GetMapping("/track-page")
    @Operation(summary = "获得工单跟踪记录分页")
    public CommonResult<PageResult<BpmOAWorkOrderTrackInfo>> getTrackPage(@Valid BpmOAWorkOrderTrackReqDTO dto) {
        return success(workOrderService.getTrackPage(dto));
    }

    @PutMapping("/update")
    @Operation(summary = "修改工单")
    @PreAuthorize("@ss.hasPermission('bpm:oa-work-order:update')")
    public CommonResult<Boolean> updateWorkOrder(@Valid @RequestBody BpmOAWorkOrderUpdateReqVO updateReqVO) {
        workOrderService.updateWorkOrder(updateReqVO);
        return success(true);
    }

    @GetMapping("/my-assigned-count")
    @Operation(summary = "获得我的待处理工单数量")
    @PermitAll
    public CommonResult<Long> getMyAssignedWorkOrderCount() {
        return success(workOrderService.getMyAssignedWorkOrderCount(getLoginUserId()));
    }

}
