package org.springblade.common.utils;

import com.yeepay.yop.sdk.exception.YopServiceException;
import org.apache.commons.lang3.ObjectUtils;
import org.springblade.core.tool.api.R;
import org.springblade.ldt.bills.service.IBalanceBillsService;
import org.springblade.ldt.bills.service.IBillsService;
import org.springblade.ldt.bills.service.IFrozenRecService;
import org.springblade.ldt.bills.service.IPointBillsService;
import org.springblade.ldt.user.service.ILoginUserService;
import org.springblade.ldt.user.service.IUserChannelPointService;
import org.springblade.payment.handle.handler.CancelTradeHandle;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;


/**
 * @author July
 * @version 1.0.0
 * @ClassName GlobalExceptionHandler.java
 * @Description 全局异常处理类
 * @createTime 2021年10月08日 15:06:00
 */
@ControllerAdvice
@ResponseBody
public class GlobalExceptionHandler {
	@Autowired
	private CancelTradeHandle cancelTradeHandle;

	@ExceptionHandler(YopServiceException.class)
	public R defaultErrorHandler(YopServiceException e) {
		return R.fail(e.getStatusCode(), e.getErrorMessage() + "：" + e.getSubMessage());
	}

	@Transactional
	@ExceptionHandler(TradeException.class)
	public R tradeErrorHandler(TradeException e) {
		if (ObjectUtils.isNotEmpty(e.getSuccessParams())) {
			cancelTradeHandle.handle(e.getSuccessParams());
		}
		return R.fail(e.getResultCode(), e.getMessage());
	}
}
