/*
 * Copyright (c) 2023 codvision.com All Rights Reserved.
 */

package com.codvision.workflow.controller;

import com.codvision.commoncore.common.ResponseEntity;
import com.codvision.commoncore.exception.BusinessException;
import com.codvision.workflow.entity.WorkFlowFormLink;
import com.codvision.workflow.entity.dto.WorkFlowFormLinkDTO;
import com.codvision.workflow.entity.vo.WorkFlowFormLinkVO;
import com.codvision.workflow.service.WorkFlowFormLinkService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author hxl
 */
@Slf4j
@Api(tags = "工作流业务模块表单关联管理")
@RestController
@RequiredArgsConstructor
@RequestMapping("/flowFormLink")
public class WorkFlowFormLinkController {

    private final WorkFlowFormLinkService service;

    @PostMapping("/add")
    @ApiOperation("添加关联信息")
    public ResponseEntity<String> addFormLink(@Validated @RequestBody WorkFlowFormLinkDTO workFlowFormLinkDTO) {
        ResponseEntity<String> responseEntity = new ResponseEntity<>();
        if (CollectionUtils.isEmpty(workFlowFormLinkDTO.getMenuIds())) {
            throw new BusinessException(30002);
        }

        service.addFormLink(workFlowFormLinkDTO);
        return responseEntity.ok();
    }

    @PostMapping("/update")
    @ApiOperation("更新关联信息")
    public ResponseEntity<String> updateFormLink(@Validated @RequestBody WorkFlowFormLinkDTO workFlowFormLinkDTO) {
        ResponseEntity<String> responseEntity = new ResponseEntity<>();
        service.updateFormLink(workFlowFormLinkDTO);
        return responseEntity.ok();
    }

    @PostMapping("/getFormInfoByMenuId")
    @ApiOperation("根据菜单ID获取表单信息")
    public ResponseEntity<WorkFlowFormLinkVO> getFormInfoByMenuId(@RequestParam(value = "menuId") Integer menuId) {
        ResponseEntity<WorkFlowFormLinkVO> responseEntity = new ResponseEntity<>();
        if (menuId == null) {
            throw new BusinessException(30002);
        }

        return responseEntity.data(service.getFormInfoByMenuId(menuId));
    }

    @PostMapping("/getByDefinitionId")
    @ApiOperation("根据流程定义ID获取关联信息")
    public ResponseEntity<List<WorkFlowFormLink>> getByDefinitionId(@RequestParam(value = "definitionId") String definitionId) {
        ResponseEntity<List<WorkFlowFormLink>> responseEntity = new ResponseEntity<>();
        if (StringUtils.isBlank(definitionId)) {
            throw new BusinessException(30002);
        }

        return responseEntity.data(service.getByDefinitionId(definitionId));
    }
}
