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

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.processmapping.vo.BpmProcessMappingConfigCreateReqVO;
import cn.iocoder.yudao.module.bpm.controller.admin.processmapping.vo.BpmProcessMappingConfigPageReqVO;
import cn.iocoder.yudao.module.bpm.controller.admin.processmapping.vo.BpmProcessMappingConfigRespVO;
import cn.iocoder.yudao.module.bpm.controller.admin.processmapping.vo.BpmProcessMappingConfigUpdateReqVO;
import cn.iocoder.yudao.module.bpm.dal.dataobject.processmapping.BpmProcessMappingConfigDO;
import cn.iocoder.yudao.module.bpm.service.processmapping.ProcessMappingConfigService;
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.validation.Valid;

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

/**
 * 管理后台 - 流程映射配置
 *
 * @author 系统
 */
@Tag(name = "管理后台 - 流程映射配置")
@RestController
@RequestMapping("/bpm/process-mapping-config")
@Validated
@Slf4j
public class BpmProcessMappingConfigController {

    @Resource
    private ProcessMappingConfigService processMappingConfigService;

    @PostMapping("/create")
    @Operation(summary = "创建流程映射配置")
    @PreAuthorize("@ss.hasPermission('bpm:process-mapping-config:create')")
    public CommonResult<Long> createProcessMappingConfig(@Valid @RequestBody BpmProcessMappingConfigCreateReqVO createReqVO) {
        return success(processMappingConfigService.createProcessMappingConfig(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新流程映射配置")
    @PreAuthorize("@ss.hasPermission('bpm:process-mapping-config:update')")
    public CommonResult<Boolean> updateProcessMappingConfig(@Valid @RequestBody BpmProcessMappingConfigUpdateReqVO updateReqVO) {
        processMappingConfigService.updateProcessMappingConfig(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除流程映射配置")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('bpm:process-mapping-config:delete')")
    public CommonResult<Boolean> deleteProcessMappingConfig(@RequestParam("id") Long id) {
        processMappingConfigService.deleteProcessMappingConfig(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得流程映射配置")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('bpm:process-mapping-config:query')")
    public CommonResult<BpmProcessMappingConfigRespVO> getProcessMappingConfig(@RequestParam("id") Long id) {
        BpmProcessMappingConfigDO config = processMappingConfigService.getProcessMappingConfig(id);
        return success(BeanUtils.toBean(config, BpmProcessMappingConfigRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得流程映射配置分页")
    @PreAuthorize("@ss.hasPermission('bpm:process-mapping-config:query')")
    public CommonResult<PageResult<BpmProcessMappingConfigRespVO>> getProcessMappingConfigPage(@Valid BpmProcessMappingConfigPageReqVO pageReqVO) {
        PageResult<BpmProcessMappingConfigDO> pageResult = processMappingConfigService.getProcessMappingConfigPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, BpmProcessMappingConfigRespVO.class));
    }

    @PostMapping("/refresh-cache")
    @Operation(summary = "刷新流程映射配置缓存")
    @PreAuthorize("@ss.hasPermission('bpm:process-mapping-config:update')")
    public CommonResult<Boolean> refreshCache() {
        processMappingConfigService.refreshCache();
        return success(true);
    }

} 