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

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.module.bpm.controller.admin.business.vo.ModelConfigPageReqVO;
import cn.iocoder.yudao.module.bpm.controller.admin.business.vo.ModelConfigRespVO;
import cn.iocoder.yudao.module.bpm.controller.admin.business.vo.ModelConfigSaveReqVO;
import cn.iocoder.yudao.module.bpm.dal.dataobject.business.ModelConfigDO;
import cn.iocoder.yudao.module.bpm.service.business.ModelConfigService;
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.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

import java.io.IOException;
import java.util.List;

import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

/**
 * @Author: jinyd
 * @CreateTime: 2024/8/15  11:52
 * @Description:
 * @Version: 4.0
 */


@Tag(name = "管理后台 - 流程模型配置")
@RestController
@RequestMapping("/bpm/model-config")
@Validated
public class BpmModelConfigController {

    @Resource
    private ModelConfigService modelConfigService;

    @PostMapping("/create")
    @Operation(summary = "创建流程模型配置")
    @PreAuthorize("@ss.hasPermission('bpm:model-config:create')")
    public CommonResult<Long> createModelConfig(@Valid @RequestBody ModelConfigSaveReqVO createReqVO) {
        return success(modelConfigService.createModelConfig(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新流程模型配置")
    @PreAuthorize("@ss.hasPermission('bpm:model-config:update')")
    public CommonResult<Boolean> updateModelConfig(@Valid @RequestBody ModelConfigSaveReqVO updateReqVO) {
        modelConfigService.updateModelConfig(updateReqVO);
        return success(true);
    }

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

    @GetMapping("/get")
    @Operation(summary = "获得流程模型配置")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('bpm:model-config:query')")
    public CommonResult<ModelConfigRespVO> getModelConfig(@RequestParam("id") Long id) {
        ModelConfigDO modelConfig = modelConfigService.getModelConfig(id);
        return success(BeanUtils.toBean(modelConfig, ModelConfigRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得流程模型配置分页")
    @PreAuthorize("@ss.hasPermission('bpm:model-config:query')")
    public CommonResult<PageResult<ModelConfigRespVO>> getModelConfigPage(@Valid ModelConfigPageReqVO pageReqVO) {
        PageResult<ModelConfigDO> pageResult = modelConfigService.getModelConfigPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ModelConfigRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出流程模型配置 Excel")
    @PreAuthorize("@ss.hasPermission('bpm:model-config:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportModelConfigExcel(@Valid ModelConfigPageReqVO pageReqVO,
                                       HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ModelConfigDO> list = modelConfigService.getModelConfigPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "流程模型配置.xls", "数据", ModelConfigRespVO.class,
                BeanUtils.toBean(list, ModelConfigRespVO.class));
    }

}
