package cn.iocoder.yudao.server.controller.admin;

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.server.controller.admin.vo.FlowPageReqVO;
import cn.iocoder.yudao.server.controller.admin.vo.FlowRespVO;
import cn.iocoder.yudao.server.controller.admin.vo.FlowSaveReqVO;
import cn.iocoder.yudao.server.dal.dataobject.FlowDO;
import cn.iocoder.yudao.server.service.FlowService;
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;

@Tag(name = "管理后台 - 门户流程配置")
@RestController
@RequestMapping("/portal/flow")
@Validated
public class FlowController {

    @Resource
    private FlowService flowService;

    @PostMapping("/create")
    @Operation(summary = "创建门户流程配置")
    @PreAuthorize("@ss.hasPermission('portal:flow:create')")
    public CommonResult<Long> createFlow(@Valid @RequestBody FlowSaveReqVO createReqVO) {
        return success(flowService.createFlow(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新门户流程配置")
    @PreAuthorize("@ss.hasPermission('portal:flow:update')")
    public CommonResult<Boolean> updateFlow(@Valid @RequestBody FlowSaveReqVO updateReqVO) {
        flowService.updateFlow(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除门户流程配置")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('portal: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('portal:flow:query')")
    public CommonResult<FlowRespVO> getFlow(@RequestParam("id") Long id) {
        FlowDO flow = flowService.getFlow(id);
        return success(BeanUtils.toBean(flow, FlowRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得门户流程配置分页")
    @PreAuthorize("@ss.hasPermission('portal:flow:query')")
    public CommonResult<PageResult<FlowRespVO>> getFlowPage(@Valid FlowPageReqVO pageReqVO) {
        PageResult<FlowDO> pageResult = flowService.getFlowPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, FlowRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出门户流程配置 Excel")
    @PreAuthorize("@ss.hasPermission('portal:flow:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportFlowExcel(@Valid FlowPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<FlowDO> list = flowService.getFlowPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "门户流程配置.xls", "数据", FlowRespVO.class,
                        BeanUtils.toBean(list, FlowRespVO.class));
    }

}