package cn.axzo.workflow.server.advice;

import cn.axzo.framework.autoconfigure.web.exception.RespErrorCodeMappingProperties;
import cn.axzo.framework.autoconfigure.web.exception.handler.AbstractExceptionApiResultHandler;
import cn.axzo.framework.domain.web.code.BaseCode;
import cn.axzo.framework.domain.web.code.IRespCode;
import cn.axzo.framework.domain.web.code.RespCode;
import cn.axzo.workflow.client.feign.bpmn.ProcessActivityApi;
import cn.axzo.workflow.client.feign.bpmn.ProcessInstanceApi;
import cn.axzo.workflow.common.model.request.bpmn.process.BpmnProcessInstanceAbortDTO;
import cn.axzo.workflow.common.model.request.bpmn.task.BpmnActivitySetAssigneeDTO;
import org.springframework.stereotype.Component;

import java.sql.SQLIntegrityConstraintViolationException;


/**
 * 处理多个接口同时操作一个流程实例时，可能会抛出数据库的异常，这种异常是可以被忽略的
 * <p>
 * 例如： {@link ProcessActivityApi#setAssignee(BpmnActivitySetAssigneeDTO)} 于 {@link ProcessInstanceApi#abortProcessInstance(BpmnProcessInstanceAbortDTO)}
 * 两个接口并发访问时，由于 abort 先执行完，并提交事务后，setAssignee 方法内虽然有判断实例状态，但最后事务提交时突然发现实例状态被中止了就会抛出异常。
 *
 * @author wangli
 * @since 2024/6/20 09:42
 */
@Component
public class SQLIntegrityConstraintViolationExceptionHandlerAdvice extends AbstractExceptionApiResultHandler<SQLIntegrityConstraintViolationException> {
    public SQLIntegrityConstraintViolationExceptionHandlerAdvice(RespErrorCodeMappingProperties properties) {
        super(properties);
    }

    @Override
    protected IRespCode decode(SQLIntegrityConstraintViolationException ex, IRespCode fallbackCode) {
        return new RespCode(BaseCode.SUCCESS.getCode(), ex.getMessage());
    }
}
