package org.springblade.payment.handle.handler;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.ObjectUtils;
import org.springblade.common.enums.AppConstant;
import org.springblade.common.enums.FrozenSatus;
import org.springblade.common.enums.FrozenType;
import org.springblade.common.enums.GoodsConstant;
import org.springblade.core.log.exception.ServiceException;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.tool.utils.ObjectUtil;
import org.springblade.ldt.agent.entity.AgentLeagueRecord;
import org.springblade.ldt.agent.service.IAgentLeagueRecordService;
import org.springblade.ldt.bills.entity.*;
import org.springblade.ldt.bills.service.*;
import org.springblade.ldt.user.entity.LoginUser;
import org.springblade.ldt.user.entity.UserChannelPoint;
import org.springblade.ldt.user.service.ILoginUserService;
import org.springblade.ldt.user.service.IUserChannelPointService;
import org.springblade.payment.entity.SuccessParams;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Objects;

/**
 * @author July
 * @version 1.0.0
 * @ClassName CancelTradeHandle.java
 * @Description 取消交易处理
 * @createTime 2021年11月01日 15:14:00
 */
@Component
@AllArgsConstructor
public class CancelTradeHandle {

	private ILoginUserService loginUserService;
	private IBillsService billsService;
	private IPointBillsService pointBillsService;
	private IBalanceBillsService balanceBillsService;
	private IFrozenRecService frozenRecService;
	private IUserChannelPointService userChannelPointService;
	private IGoodsBillsService goodsBillsService;
	private IAgentLeagueRecordService agentLeagueRecordService;

	public void handle(SuccessParams successParams) throws ServiceException {
		Bills successParamsBills = successParams.getBills();
		synchronized (successParams.getBills().getId().toString().intern()) {

			Bills bills = billsService.getById(successParamsBills.getId());
			if (ObjectUtils.isNotEmpty(bills) && !bills.getPayStatus().equals(AppConstant.BillPayStatus.取消付款.name())) {

				List<FrozenRec> frozenRecs = frozenRecService.list(Wrappers.<FrozenRec>lambdaQuery().eq(FrozenRec::getTradeNo, bills.getId()));

				UserChannelPoint channelPoint = userChannelPointService.getById(successParams.getChannelId());

				//修改用户冻结余额或积分
				LoginUser user = loginUserService.getById(bills.getPayId());
				if (ObjectUtils.isNotEmpty(user)) {
					frozenRecs.forEach(frozenRec -> {
						frozenRec.setFrozenSatus(FrozenSatus.RETURNED_FROZEN);
						if (Objects.equals(frozenRec.getFrozenType(), FrozenType.POINT)) {
							user.setChannelPoint(user.getChannelPoint().add(frozenRec.getFrozenNum()));
							user.setFrozenPoint(user.getFrozenPoint().subtract(frozenRec.getFrozenNum()));
							if (ObjectUtils.isNotEmpty(channelPoint)) {
								channelPoint.setAvailable(channelPoint.getAvailable().add(frozenRec.getFrozenNum()));
								userChannelPointService.saveOrUpdate(channelPoint);
							}
						} else if (Objects.equals(frozenRec.getFrozenType(), FrozenType.BALANCE)) {
							user.setBalance(user.getBalance().add(frozenRec.getFrozenNum()));
							user.setFrozenBalance(user.getFrozenBalance().subtract(frozenRec.getFrozenNum()));
						}
					});
				}

				if (ObjectUtils.isNotEmpty(frozenRecs)) {
					frozenRecService.updateBatchById(frozenRecs);
				}
				loginUserService.saveOrUpdate(user);

				bills.setPayStatus(AppConstant.BillPayStatus.取消付款.name());
				billsService.saveOrUpdate(bills);

				PointBills pointBills = pointBillsService.getById(successParams.getPointBillsId());
				if (ObjectUtils.isNotEmpty(pointBills)) {
					pointBills.setPayStatus(AppConstant.BillPayStatus.取消付款.name());
					pointBillsService.saveOrUpdate(pointBills);
				}

				BalanceBills balanceBills = balanceBillsService.getById(successParams.getBalanceBillsId());
				if (ObjectUtils.isNotEmpty(balanceBills)) {
					balanceBills.setPayStatus(AppConstant.BillPayStatus.取消付款.name());
					balanceBillsService.saveOrUpdate(balanceBills);
				}

				AgentLeagueRecord agentLeagueRecord = agentLeagueRecordService.getOne(Wrappers.<AgentLeagueRecord>lambdaQuery()
					.eq(AgentLeagueRecord::getTradeNo, successParams.getBills().getId()));
				if (ObjectUtils.isNotEmpty(agentLeagueRecord)) {
					agentLeagueRecord.setPayStatus(AppConstant.BillPayStatus.取消付款.name());
					agentLeagueRecordService.saveOrUpdate(agentLeagueRecord);
				}

				//取消商品订单
				GoodsBills goodsBills = goodsBillsService.getOne(Condition.getQueryWrapper(new GoodsBills()).lambda().eq(GoodsBills::getBillsId, bills.getId()));
				if (ObjectUtil.isNotEmpty(goodsBills)) {
					goodsBills.setPayStatus(AppConstant.BillPayStatus.取消付款.name());
					goodsBills.setOrderStatus(GoodsConstant.GOODS_STATUS.取消付款.name());
					goodsBillsService.updateById(goodsBills);
				}
			}
		}
	}
}
