Initial commit
This commit is contained in:
@@ -0,0 +1,131 @@
|
||||
<?php
|
||||
|
||||
namespace app\common\service;
|
||||
|
||||
use app\common\model\account\Withdraws;
|
||||
use app\common\traits\ServiceTrait;
|
||||
use think\Db;
|
||||
use think\Log;
|
||||
|
||||
class AccountWithdrawService
|
||||
{
|
||||
use ServiceTrait;
|
||||
|
||||
/** @var Withdraws */
|
||||
public static $Model = Withdraws::class;
|
||||
|
||||
//转账类型:0=默认,1=微信,2=支付宝,3=线下转款
|
||||
const TYPE_WECHAT = 1;
|
||||
const TYPE_ALIPAY = 2;
|
||||
const TYPE_OFFLINE = 3;
|
||||
|
||||
const TYPE_MAP = [
|
||||
self::TYPE_WECHAT => '微信',
|
||||
self::TYPE_ALIPAY => '支付宝',
|
||||
self::TYPE_OFFLINE => '线下转款',
|
||||
];
|
||||
|
||||
const STATUS_MAP = [
|
||||
CommonService::ONGOING => '审核中',
|
||||
CommonService::SUCCESS => '审核通过',
|
||||
CommonService::FAILED => '审核驳回',
|
||||
];
|
||||
|
||||
|
||||
/**
|
||||
* 用户申请提现
|
||||
* @param int|string $userId 用户id
|
||||
* @param int|float $amount 余额
|
||||
* @param string $name 名字
|
||||
* @return void
|
||||
* @throws \Exception
|
||||
*/
|
||||
public static function applyWithdraw($userId, $amount, string $name = '')
|
||||
{
|
||||
//开启事务 抛出异常时自动回滚
|
||||
Db::transaction(function () use ($userId, $amount, $name) {
|
||||
if($name){
|
||||
$user = UserService::getOne(['id' => $userId]);
|
||||
if ($user->username != $name) {
|
||||
$user->username = $name;
|
||||
$user->save();
|
||||
}
|
||||
}
|
||||
//添加提现记录
|
||||
$data = [
|
||||
'apply_sn' => generate_order_sn('TX'),
|
||||
'user_id' => $userId,
|
||||
'amount' => $amount,
|
||||
'status' => AccountWithdrawService::$Common::ONGOING,
|
||||
];
|
||||
$recordRes = self::create($data);
|
||||
if (!($recordRes)) {
|
||||
throw new \Exception('提现申请失败');
|
||||
}
|
||||
//记录提现日志
|
||||
$title = '用户发起提现';
|
||||
$log_res = AccountLogService::addLog(AccountLogService::ACCOUNT_TYPE_AMOUNT, $userId, $amount, $title . '-余额扣除', AccountLogService::TYPE_WITHDRAW, 0, AccountLogService::INC_EXP_EXPEND, $recordRes->id);
|
||||
$log_res2 = AccountLogService::addLog(AccountLogService::ACCOUNT_TYPE_FREEZE, $userId, $amount, $title . '-冻结金额增加', AccountLogService::TYPE_WITHDRAW, 0, AccountLogService::INC_EXP_INCREASE, $recordRes->id);
|
||||
//减去余额,加到冻结里面
|
||||
AccountService::updateData($userId, -$amount, $amount, 0,0, $log_res->id);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 审核提现
|
||||
* @param $withdraw_record_id int 提现申请id
|
||||
* @param $approval bool 是否同意
|
||||
* @param $remark string 驳回意见
|
||||
* @param $type int 转款方式
|
||||
* @throws \Exception
|
||||
*/
|
||||
public static function approvalWithdraw($withdraw_record_id, $approval = false, $remark = '', $type = 0)
|
||||
{
|
||||
$withdraw_record = AccountWithdrawService::get($withdraw_record_id);
|
||||
if (!$withdraw_record) {
|
||||
throw new \Exception('提现申请不存在');
|
||||
}
|
||||
if ($withdraw_record->status == AccountWithdrawService::$Common::SUCCESS) {
|
||||
throw new \Exception('已经提现成功,请勿重复操作');
|
||||
}
|
||||
$userId = $withdraw_record->user_id;
|
||||
$amount = $withdraw_record->amount;
|
||||
$user = UserService::get($userId);
|
||||
|
||||
//检测收支是否相同
|
||||
// if(!AccountService::checkBreakEven($userId, $user->is_merchant)){
|
||||
// throw new \Exception('该用户余额收支异常');
|
||||
// }
|
||||
//提现通过
|
||||
if ($approval) {
|
||||
//记录提现日志
|
||||
$title = '用户提现成功(' . self::TYPE_MAP[$type] .')';
|
||||
$log_res = AccountLogService::addLog(AccountLogService::ACCOUNT_TYPE_FREEZE, $userId, $amount, $title . '-冻结金额扣除', AccountLogService::TYPE_WITHDRAW, 0, AccountLogService::INC_EXP_EXPEND, $withdraw_record->id);
|
||||
//减去冻结
|
||||
AccountService::updateData($userId, 0, -$amount, 0,0, $log_res->id);
|
||||
|
||||
if($type == AccountWithdrawService::TYPE_WECHAT){
|
||||
//发起微信付款到用户微信余额
|
||||
$payRes = (new EasyWeChatService())->mchPay($withdraw_record->apply_sn, $user->openid, $user->username, $amount);
|
||||
if (!(isset($payRes['result_code']) && in_array($payRes['result_code'], ['SUCCESS']))) {
|
||||
throw new \Exception(($payRes['return_msg'] ?? '企业付款给用户失败') . ':' . ($payRes['err_code_des'] ?? ''));
|
||||
}
|
||||
$withdraw_record->result_status = $payRes['result_code'];
|
||||
$withdraw_record->save();
|
||||
}
|
||||
} else {
|
||||
if (!$remark) {
|
||||
throw new \Exception('驳回原因必填');
|
||||
}
|
||||
if ($withdraw_record->status == AccountWithdrawService::$Common::FAILED) {
|
||||
throw new \Exception('已经审核驳回,请勿重复操作');
|
||||
}
|
||||
//记录提现日志
|
||||
$title = '提现失败(' . $remark .')';
|
||||
$log_res = AccountLogService::addLog(AccountLogService::ACCOUNT_TYPE_FREEZE, $userId, $amount, $title . '-冻结金额扣除', AccountLogService::TYPE_WITHDRAW, 0, AccountLogService::INC_EXP_EXPEND, $withdraw_record->id);
|
||||
$log_res2 = AccountLogService::addLog(AccountLogService::ACCOUNT_TYPE_AMOUNT, $userId, $amount, $title . '-余额增加', AccountLogService::TYPE_WITHDRAW, 0, AccountLogService::INC_EXP_INCREASE, $withdraw_record->id);
|
||||
//减去冻结,返回到余额
|
||||
AccountService::updateData($userId, $amount, -$amount, 0,0, $log_res->id);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user