package cn.gov.customs.wxjy.ibmmq.async;

import cn.gov.customs.wxjy.ibmmq.config.DataMessageBuilder;
import cn.gov.customs.wxjy.ibmmq.config.PushContext;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 异步推送数据、流程数据、附件
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class AsyncPushService {
	static Logger logger = LoggerFactory.getLogger(AsyncPushService.class);
    /**
     * 异步推送数据、流程数据、附件
     * @param context 构建推送日志记录信息
     * @param data 要执行方法的表实体
     * @param mapperClassName 操作mapper类名 如UserMapper.class.getName()
     * @param method 执行方法名称  如：insert、updata、delete
     * @param propertyName 指定参数实体名称  如：isDel  。当参数为实体时，传入null
     * @param methodParamTypes  指定参数类型，如：new Class[]{SysUser.class} / new Class[]{String.class}
     * @param attachments   推送附件信息，没有则传入null
     */
    @Async("asyncTaskExecutor")  // 指定使用配置的线程池
    public void sendAsync(PushContext context, Object data, String mapperClassName, String method, String propertyName,
                          Class<?>[] methodParamTypes, List<String> attachments) {
        try {
            DataMessageBuilder.create(context)
                    .withData(data)
                    .withMapperClassName(mapperClassName)
                    .withMethod(method)
                    .withPropertyName(propertyName)
                    .withMethodParamTypes(methodParamTypes)
                    .withAttachments(attachments)
                    .send();
        } catch (Exception e) {
			logger.error("【异步推送失败】业务ID: {}, 类型: {}", context.getBusId(), context.getBusType(), e);
            // 这里可以添加失败重试或告警逻辑
        }
    }
}
