package com.cyksj.service.user.impl;

import cn.hutool.core.util.StrUtil;
import com.alipay.api.AlipayApiException;
import com.alipay.api.response.AlipayTradeQueryResponse;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cyksj.common.exception.BusinessRuntimeException;
import com.cyksj.mapper.UserPayOrderBlockMapper;
import com.cyksj.model.entity.OrderDon;
import com.cyksj.model.entity.UserPayOrderBlock;
import com.cyksj.service.order.OrderDonService;
import com.cyksj.service.user.UserBindRelationService;
import com.cyksj.service.user.UserPayOrderBlockService;
import com.cyksj.service.wechat.WeChatService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

/**
 * 项目名: yhlxj
 * 文件名: UserPayOrderBlockServiceImpl
 * 创建者: JavaZou
 * 创建时间:2023/12/13 16:41
 */
@Service
@RequiredArgsConstructor
public class UserPayOrderBlockServiceImpl extends ServiceImpl<UserPayOrderBlockMapper, UserPayOrderBlock> implements UserPayOrderBlockService {
	private final OrderDonService orderDonService;

	private final WeChatService weChatService;

	private final UserBindRelationService userBindRelationService;

	@Override
	public void blockUserOrder(UserPayOrderBlock payOrderBlock) throws AlipayApiException {
		String orderNo = payOrderBlock.getOrderNo();

		OrderDon orderDon = orderDonService.getOne(Wrappers.lambdaQuery(OrderDon.class)
				.eq(OrderDon::getOrderNo, orderNo).last("limit 1"));
		if (orderDon == null) {
			throw BusinessRuntimeException.getInstance("该订单号不存在");
		}
		String buyerId = orderDon.getBuyerId();
		if (StrUtil.isEmpty(buyerId)) {
			Long userId = orderDon.getUserId();
			List<Long> relationUserIdList = userBindRelationService.getRelationUserIdList(userId, null);
			orderDon = orderDonService.getOne(Wrappers.lambdaQuery(OrderDon.class)
					.in(OrderDon::getUserId, relationUserIdList)
					.ne(OrderDon::getBuyerId, StrUtil.EMPTY)
					.last("limit 1"));
			if (orderDon != null) {
				buyerId = orderDon.getBuyerId();
			}
			if (StrUtil.isEmpty(buyerId)) {
				if (orderDon.getType() == OrderDon.Type.ALI_PAY) {
					AlipayTradeQueryResponse zfbOrderDetail = orderDonService.getZfbOrderDetail(orderDon.getShopId(), orderDon.getOrderNo());
					if (zfbOrderDetail != null) {
						buyerId = Optional.ofNullable(zfbOrderDetail.getBuyerUserId()).orElse(zfbOrderDetail.getBuyerOpenId());
					}
				}
			}
		}
		if (StrUtil.isEmpty(buyerId)) {
			throw BusinessRuntimeException.getInstance("未找到对应订单号对应用户支付账号id");
		}

		int count = count(Wrappers.lambdaQuery(UserPayOrderBlock.class)
				.eq(UserPayOrderBlock::getBuyerId, buyerId));
		if (count > 0) {
			throw BusinessRuntimeException.getInstance("已拉给该账号");
		}

		UserPayOrderBlock userPayOrderBlock = new UserPayOrderBlock();
		userPayOrderBlock.setUserId(orderDon.getUserId());
		userPayOrderBlock.setOrderNo(orderNo);
		userPayOrderBlock.setBuyerId(buyerId);
		save(userPayOrderBlock);
	}
}
