/*
 *      Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 *  Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  Neither the name of the dreamlu.net developer nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *  Author: Chill 庄骞 (smallchill@163.com)
 */
package org.springblade.gateway.common_gateway.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.lang.Assert;
import lombok.AllArgsConstructor;
import org.springblade.common.enums.AppConstant;
import org.springblade.core.mp.base.BaseServiceImpl;
import org.springblade.gateway.common_gateway.entity.dto.WithdrawDTO;
import org.springblade.gateway.common_gateway.service.ICommonWithdrawRecService;
import org.springblade.ldt.bills.entity.WithdrawRec;
import org.springblade.ldt.bills.mapper.WithdrawRecMapper;
import org.springblade.ldt.platform.entity.WithdrawConfig;
import org.springblade.ldt.platform.service.IWithdrawConfigService;
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.entity.UserBank;
import org.springblade.ldt.user.service.ILoginUserService;
import org.springblade.ldt.user.service.IUserBankService;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;
import java.util.stream.Collectors;

/**
 *  服务实现类
 *
 * @author BladeX
 * @since 2021-09-08
 */
@Service
@AllArgsConstructor
public class CommonWithdrawRecServiceImpl extends BaseServiceImpl<WithdrawRecMapper, WithdrawRec> implements ICommonWithdrawRecService {

	private ILoginUserService loginUserService;
	private IShopService shopService;
	private IUserBankService userBankService;
	private IWithdrawConfigService withdrawConfigService;

	@Override
	public Long withdraw(WithdrawDTO withdrawDTO) {

		if (withdrawDTO.getOwnerType().equals(AppConstant.USER_TYPE.渠道代理.name())) {
			//代理提现
			LoginUser loginUser = loginUserService.getById(withdrawDTO.getOwnerId());
			Assert.notNull(loginUser, "找不到提现用户");
			BigDecimal availableBalance = loginUser.getAvailableBalance();
			Assert.isFalse(withdrawDTO.getPrice().compareTo(availableBalance) > 0, "账户余额不足");
		}

		if (withdrawDTO.getOwnerType().equals(AppConstant.USER_TYPE.商户.name())) {
			//商户提现
			Shop shop = shopService.getById(withdrawDTO.getOwnerId());
			Assert.notNull(shop, "找不到提现商户");
			BigDecimal availableBalance = shop.getBalance();
			Assert.isFalse(withdrawDTO.getPrice().compareTo(availableBalance) > 0, "账户余额不足");
		}

		UserBank userBank = userBankService.getById(withdrawDTO.getBankId());
		Assert.notNull(userBank, "找不到提现银行");


		WithdrawRec withdrawRec = new WithdrawRec();
		withdrawRec.setUserBankId(withdrawDTO.getBankId());
		withdrawRec.setOwnerType(withdrawDTO.getOwnerType());
		withdrawRec.setOwnerId(withdrawDTO.getOwnerId());
		withdrawRec.setPrice(withdrawDTO.getPrice());
		withdrawRec.setBankCode(userBank.getBankType());
		withdrawRec.setBankAccountType(userBank.getBankAccountType());
		withdrawRec.setReceiverAccountName(userBank.getRealName());
		withdrawRec.setReceiverAccountNo(userBank.getCardNo());
		//提现类型 商家提现或者是代理提现
		withdrawRec.setOwnerType(withdrawDTO.getOwnerType());
		//提现状态待确认
		withdrawRec.setWithdrawStatus(AppConstant.withdrawStatus.WAITING.name());

		//计算国信提现费率
		BigDecimal rate = BigDecimal.ZERO;
		List<WithdrawConfig> withdrawConfigList = withdrawConfigService.list();
		if (CollUtil.isNotEmpty(withdrawConfigList)) {
			List<WithdrawConfig> configList = withdrawConfigList.stream()
				.filter(item -> withdrawDTO.getPrice().compareTo(item.getMinBalance()) != -1
					&& withdrawDTO.getPrice().compareTo(item.getMaxBalance()) == -1).collect(Collectors.toList());
			if (CollUtil.isNotEmpty(configList)) {
				rate=configList.get(0).getRate().divide(BigDecimal.valueOf(100));
			}
		}
		withdrawRec.setPlatformFee(rate);
		//实际提现金额等于申请提现的金额减去手续费
		withdrawRec.setActualPrice(withdrawRec.getPrice().subtract(rate));
		boolean save = this.save(withdrawRec);
		Assert.isTrue(save,"提现失败");

		return withdrawRec.getId();


	}
}
