package org.springblade.payment.callback.trade;


import cn.hutool.core.lang.Assert;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springblade.common.enums.AppConstant;
import org.springblade.ldt.bills.entity.WithdrawRec;
import org.springblade.ldt.bills.service.IWithdrawRecService;
import org.springblade.ldt.shop.entity.Shop;
import org.springblade.ldt.shop.service.IShopService;
import org.springblade.ldt.user.entity.LoginUser;
import org.springblade.ldt.user.service.ILoginUserService;
import org.springblade.payment.entity.SuccessParams;
import org.springblade.payment.event.UserWithdrawEvent;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Objects;

/**
 * @author: lianghanqiang
 * @description: 用户付款回调
 * @since: 8/5/21 -- 3:01 PM
 */
@Component
@AllArgsConstructor
@Slf4j
public class UserWithdrawCallback {

	private IWithdrawRecService withdrawRecService;
	private IShopService shopService;
	private ILoginUserService loginUserService;


	@EventListener
	@Transactional
	public void PaySuccess(UserWithdrawEvent userWithdrawEvent) {
		SuccessParams successParams = userWithdrawEvent.getSuccessParams();
		//用户支付成功的回调数据：SuccessParams(billId=1425261064655839233, platformId=1013202108110000002503142099, orderId=null, status=success, orderType=USER_WITHDRAW)
		WithdrawRec withdrawRecord = withdrawRecService.getById(successParams.getWithdrawRecId());
		Assert.notNull(withdrawRecord, "提现记录无此订单");
		if (Objects.equals(withdrawRecord.getWithdrawStatus(), AppConstant.withdrawStatus.DONE.name())) {
			//已完成状态，有可能是前端状态传错，把平台订单id存起来
			withdrawRecord.setThirdOrderId(successParams.getThirdOrderId());
			return;
		}
		//结算异常
		if (successParams.getRes().getString("settleStatus").equals("SETTLE_FAIL")) {
			withdrawRecord.setWithdrawStatus(AppConstant.withdrawStatus.FAIL.name());
			withdrawRecord.setFailReason(successParams.getRes().getString("message"));
			withdrawRecService.updateById(withdrawRecord);
			return;
		}
		//提现成功
		withdrawRecord.setWithdrawStatus(AppConstant.withdrawStatus.DONE.name());
		withdrawRecord.setFee(new BigDecimal(successParams.getRes().getString("feeAmount")));
		withdrawRecord.setReceiveAmount(new BigDecimal(successParams.getRes().getString("settleAmount")));
		withdrawRecService.updateById(withdrawRecord);
		//修改用户的余额
		if (withdrawRecord.getOwnerType().equals(AppConstant.USER_TYPE.渠道代理.name())) {
			LoginUser loginUser = loginUserService.getById(withdrawRecord.getOrderId());
			Assert.notNull(loginUser, "找不到该用户");
			BigDecimal availableBalance = loginUser.getAvailableBalance().subtract(withdrawRecord.getPrice());
			if (availableBalance.compareTo(BigDecimal.ZERO) > 0) {
				loginUser.setAvailableBalance(availableBalance);
			} else {
				loginUser.setBalance(BigDecimal.ZERO);
			}
			loginUserService.updateById(loginUser);
		} else if (withdrawRecord.getOwnerType().equals(AppConstant.USER_TYPE.商户.name())) {
			Shop shop = shopService.getById(withdrawRecord.getOwnerId());
			Assert.notNull(shop, "找不到该用户");
			shop.setBalance(shop.getBalance().subtract(withdrawRecord.getPrice()));
			shopService.updateById(shop);
		}

	}
}



