package cn.axzo.workflow.server.controller.web.bpmn;

import cn.axzo.workflow.client.feign.bpmn.ProcessActivityApi;
import cn.axzo.workflow.common.model.request.bpmn.task.BpmnActivitySetAssigneeDTO;
import cn.axzo.workflow.common.model.request.bpmn.task.BpmnTaskDelegateAssigner;
import cn.axzo.workflow.core.common.exception.WorkflowEngineException;
import cn.axzo.workflow.core.service.BpmnProcessActivityService;
import cn.axzo.workflow.server.common.annotation.ErrorReporter;
import cn.axzo.workflow.server.common.annotation.RepeatSubmit;
import cn.azxo.framework.common.model.CommonResponse;
import io.swagger.v3.oas.annotations.Operation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.constraints.NotBlank;
import java.util.ArrayList;
import java.util.List;

import static cn.axzo.workflow.core.common.code.BpmnTaskRespCode.ACTIVITY_BIZ_SET_ASSIGNEE_HAS_REPEAT;
import static cn.axzo.workflow.core.engine.cmd.helper.CustomTaskHelper.removeDuplicateByPersonId;

/**
 * 流程活动相关控制器
 *
 * @author wangli
 * @since 2023/11/17 16:33
 */
@Slf4j
@RequestMapping({"/web/v1/api/process/activity", "/api/process/activity"})
@RestController
@ErrorReporter
@Validated
public class BpmnProcessActivityController implements ProcessActivityApi {

    @Resource
    private BpmnProcessActivityService bpmnProcessActivityService;

    /**
     * 业务节点唤醒
     *
     * @param triggerId 触发 ID,数据来源于事件
     * @return
     */
    @Operation(summary = "业务节点唤醒")
    @GetMapping("/trigger")
    @Override
    @RepeatSubmit
    public CommonResponse<Boolean> trigger(@RequestParam @NotBlank(message = "触发 ID 不能为空") String triggerId, @RequestParam(required = false) Boolean async) {
        if (async != null && async) {
            bpmnProcessActivityService.triggerAsync(triggerId);
        } else {
            bpmnProcessActivityService.trigger(triggerId);
        }
        return CommonResponse.success(true);
    }

    /**
     * 业务节点设置审批人
     *
     * @param dto
     * @return
     */
    @Operation(summary = "业务节点设置审批人")
    @PostMapping("/assignee/set")
    @Override
    @RepeatSubmit
    public CommonResponse<Boolean> setAssignee(@Validated @RequestBody BpmnActivitySetAssigneeDTO dto) {
        if (!dto.getServerSideDeduplication()) {
            List<String> personIds = new ArrayList<>();
            for (BpmnTaskDelegateAssigner assigner : dto.getAssigners()) {
                if (personIds.contains(assigner.getPersonId())) {
                    throw new WorkflowEngineException(ACTIVITY_BIZ_SET_ASSIGNEE_HAS_REPEAT);
                }
                personIds.add(assigner.getPersonId());
            }
        } else {
            dto.setAssigners(removeDuplicateByPersonId(dto.getAssigners()));
        }
        if (dto.getAsync() != null && dto.getAsync()) {
            bpmnProcessActivityService.setAssigneeAsync(dto);
        } else {
            bpmnProcessActivityService.setAssignee(dto);
        }
        return CommonResponse.success(true);
    }
}
