package cn.iocoder.yudao.module.crm.controller.admin.crmflow;

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.crm.controller.admin.crmflow.vo.*;
import cn.iocoder.yudao.module.crm.dal.dataobject.crmflow.CrmFlowDO;
import cn.iocoder.yudao.module.crm.dal.dataobject.crmflow.CrmFlowStepDO;
import cn.iocoder.yudao.module.crm.service.crmflow.CrmFlowService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;

import javax.annotation.Resource;
import javax.validation.Valid;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 审批流程")
@RestController
@RequestMapping("/crm/flow")
@Validated
public class CrmFlowController {

    @Resource
    private CrmFlowService flowService;

    @PostMapping("/create")
    @Operation(summary = "创建审批流程")
    // @PreAuthorize("@ss.hasPermission('crm:flow:create')")
    public CommonResult<Long> createFlow(@Valid @RequestBody CrmFlowSaveReqVO createReqVO) {
        return success(flowService.createFlow(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新审批流程")
    // @PreAuthorize("@ss.hasPermission('crm:flow:update')")
    public CommonResult<Boolean> updateFlow(@Valid @RequestBody CrmFlowSaveReqVO updateReqVO) {
        flowService.updateFlow(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除审批流程")
    @Parameter(name = "id", description = "编号", required = true)
    // @PreAuthorize("@ss.hasPermission('crm:flow:delete')")
    public CommonResult<Boolean> deleteFlow(@RequestParam("id") Long id) {
        flowService.deleteFlow(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得审批流程")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    // @PreAuthorize("@ss.hasPermission('crm:flow:query')")
    public CommonResult<CrmFlowRespVO> getFlow(@RequestParam("id") Long id) {
        CrmFlowDO flow = flowService.getFlow(id);
        return success(BeanUtils.toBean(flow, CrmFlowRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得审批流程分页")
    // @PreAuthorize("@ss.hasPermission('crm:flow:query')")
    public CommonResult<PageResult<CrmFlowRespVO>> getFlowPage(@Valid CrmFlowPageReqVO pageReqVO) {
        PageResult<CrmFlowDO> pageResult = flowService.getFlowPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, CrmFlowRespVO.class));
    }


    // ==================== 子表（审批步骤） ====================

    @GetMapping("/flow-step/list-by-flow-id")
    @Operation(summary = "获得审批步骤列表")
    @Parameter(name = "flowId", description = "审批流程ID")
    // @PreAuthorize("@ss.hasPermission('crm:flow:query')")
    public CommonResult<List<CrmFlowStepDO>> getFlowStepListByFlowId(@RequestParam("flowId") Long flowId) {
        return success(flowService.getFlowStepListByFlowId(flowId));
    }

    @GetMapping("/flow-users")
    @Operation(summary = "获得审批人列表")
    @Parameter(name = "flowType", description = "contract")
    public CommonResult<List<Long>> getFlowUserIds(@RequestParam("flowType") String flowType) {
        return success(flowService.getFlowUserIds(flowType));
    }


}
