jh-admin/addon/saas/model/ManageWithdraw.php

454 lines
16 KiB
PHP

<?php
/**
* SaaSMall商城系统 - 团队十年电商经验汇集巨献!
* =========================================================
* Copy right 2019-2029 成都SAAS云科技有限公司, 保留所有权利。
* ----------------------------------------------
* 官方网址: https://www.gobuysaas.com
* =========================================================
*/
namespace addon\saas\model;
use app\model\BaseModel;
use app\model\member\Member;
use app\model\member\MemberAccount;
use app\model\message\Message;
use app\model\member\Member as MemberModel;
use app\model\system\Pay;
use think\facade\Cache;
/**
* 分销商提现
*/
class ManageWithdraw extends BaseModel
{
//提现类型
public $withdraw_type = [
'balance' => '余额',
'weixin' => '微信',
'alipay' => '支付宝',
'bank' => '银行卡',
];
public $status = array(
1 => '待审核',
2 => '待转账',
3 => '已转账',
-1 => '已拒绝'
);
public function getTransferType($site_id)
{
$pay_model = new Pay();
$transfer_type_list = $pay_model->getTransferType($site_id);
$transfer_type_list['balance'] = '余额';
return $transfer_type_list;
}
/**
* 获取提现详情
* @param array $condition
* @return array
*/
public function getWithdrawInfo($condition = [], $field = '*')
{
$res = model('manage_withdraw')->getInfo($condition, $field);
return $this->success($res);
}
/**
* 提现详情
* @param $params
* @return array
*/
public function getWithdrawDetail($params)
{
$id = $params['id'];
$site_id = $params['site_id'] ?? 0;
$member_id = $params['member_id'] ?? 0;
$condition = array(
['id', '=', $id]
);
if ($site_id > 0) {
$condition[] = ['site_id', '=', $site_id];
}
if ($member_id > 0) {
$condition[] = ['member_id', '=', $member_id];
}
$info = model('manage_withdraw')->getInfo($condition, '*');
if (!empty($info)) {
$info = $this->tran($info);
}
return $this->success($info);
}
public function tran($data)
{
$status = $data['status'] ?? 0;
if ($status != 0) {
$data['status_name'] = $this->status[$status] ?? '';
}
return $data;
}
/**
* 获取列表
* @param array $condition
* @param string $field
* @param string $order
* @param string $limit
*/
public function getWithdrawList($condition = [], $field = '*', $order = '', $limit = null)
{
$list = model('manage_withdraw')->getList($condition, $field, $order, '', '', '', $limit);
return $this->success($list);
}
/**
* 获取提现分页列表
* @param array $condition
* @param number $page
* @param string $page_size
* @param string $order
* @param string $field
*/
public function getWithdrawPageList($condition = [], $page = 1, $page_size = PAGE_LIST_ROWS, $order = '', $field = '*', $alias = '', $join = [])
{
$list = model('manage_withdraw')->pageList($condition, $field, $order, $page, $page_size, $alias, $join);
return $this->success($list);
}
public function apply($data, $site_id = 0, $app_module = 'shop')
{
$config_model = new Config();
$config = $config_model->getWithdrawConfig($site_id)['data']['value'] ?? [];
$withdraw_no = $this->createWithdrawNo();
$apply_money = round(abs($data['apply_money']), 2);
$withdraw_min_money = $config['withdraw'];
$withdraw_max_money = $config['max'];
$time_week = $config['time_week']??[];
$member_id = $data['member_id'];
if ($apply_money < $withdraw_min_money) return $this->error([], '申请提现金额不能小于最低提现额度' . $withdraw_min_money);
if ($withdraw_max_money && $apply_money > $withdraw_max_money) return $this->error([], '申请提现金额不能大于最高提现额度' . $withdraw_max_money);
if(in_array(date('w'),$time_week)) return $this->error([], '每周提现时间为' . implode(',',$time_week));
if(isset($data['is_flexible_transfer'])&&$data['is_flexible_transfer']==1){
$status=model('member_bank_account')->getValue([['member_id','=',$member_id],['site_id','=',$site_id]],'status');
if($status!=4) return $this->error([], '银行卡未签约');
}
$member_model = new MemberModel();
$member_condition = array(
['member_id', '=', $member_id]
);
$member_info = $member_model->getMemberInfo($member_condition, 'balance_money,headimg,wx_openid,username,mobile,weapp_openid,nickname')['data'] ?? [];
if (empty($member_info))
return $this->error([], 'MEMBER_NOT_EXIST');
$manage_info = model('manage_user')->getInfo($member_condition, 'id,real_name,account');
if (empty($manage_info)) {
return $this->error('不存在的账号');
}
$account = $manage_info['account'];//会员的分销佣金
if ($account < $apply_money) {
return $this->error('', '提现金额大于可提现金额');
}
$transfer_type = $data['transfer_type'];
$transfer_type_list = $this->getTransferType($site_id);
$transfer_type_name = $transfer_type_list[$transfer_type] ?? '';
if (empty($transfer_type_name))
return $this->error([], '不支持的提现方式');
model('manage_withdraw')->startTrans();
try {
$withdraw_rate = $config['withdraw_rate'];
$bank_name = '';
$account_number = '';
$applet_type = 0;
switch ($transfer_type) {
case 'bank':
$bank_name = $data['bank_name'];
$account_number = $data['account_number'];
break;
case 'alipay':
$bank_name = '';
$account_number = $data['account_number'];
break;
case 'wechatpay':
$bank_name = '';
if (empty($member_info['wx_openid']) && empty($member_info['weapp_openid'])) {
return $this->error('', '请绑定微信或更换提现账户');
}
if (!empty($member_info['wx_openid'])) {
$account_number = $member_info['wx_openid'];
$applet_type = 0; // 公众号
} else {
$account_number = $member_info['weapp_openid'];
$applet_type = 1; // 小程序
}
break;
}
if ($transfer_type == 'balance') {
$withdraw_rate = 0;
}
$service_money = round($apply_money * $withdraw_rate / 100, 2);//手续费
$real_money = $apply_money - $service_money;
$data = array(
'site_id' => $site_id,
'withdraw_no' => $withdraw_no,
'member_id' => $member_id,
'manage_id' => $manage_info['id'],
'real_name' => $manage_info['real_name'],
'transfer_type' => $transfer_type,
'transfer_name' => $transfer_type_name,
'money' => $apply_money,
'withdraw_rate_money' => $service_money,
'withdraw_rate' => $withdraw_rate,
'real_money' => $real_money,
'create_time' => time(),
'status' => 1,
'member_headimg' => $member_info['headimg'],
'realname' => $data['realname'],
'bank_name' => $bank_name,
'account_number' => $account_number,
'mobile' => $data['mobile'],
'applet_type' => $applet_type
);
$result = model('manage_withdraw')->add($data);
//修改商提现中金额
model('manage_user')->setInc($member_condition, 'account_withdraw_apply', $apply_money);
//修改分销商可提现金额
model('manage_user')->setDec($member_condition, 'account', $apply_money);
//申请提现发送消息
$data['keywords'] = 'manage_withdraw_apply';
$message_model = new Message();
$message_model->sendMessage($data);
//判断是否需要审核
if ($config['withdraw_status'] == 2) {//不需要
$result = $this->agree(['id' => $result, 'site_id' => $site_id]);
if ($result['code'] < 0) {
model('manage_withdraw')->rollback();
return $result;
}
}
model('manage_withdraw')->commit();
return $this->success();
} catch (\Exception $e) {
model('manage_withdraw')->rollback();
return $this->error('', $e->getMessage());
}
}
/***
* 同意提现
* @param $params
* @return array
*/
public function agree($params)
{
$id = $params['id'];
$site_id = $params['site_id'];
if (empty($site_id)) {
return $this->error(-1, '参数错误');
}
$condition = array(
['id', '=', $id],
['site_id', '=', $site_id],
);
$info = model('manage_withdraw')->getInfo($condition);
if (empty($info))
return $this->error();
$config_model = new Config();
$config = $config_model->getWithdrawConfig($site_id)['data']['value'] ?? [];
model('manage_withdraw')->startTrans();
try {
$data = array(
'status' => 2,
'audit_time' => time(),
);
model('manage_withdraw')->update($data, $condition);
//是否启用自动转账(必须是微信或支付宝)
if ($config['is_auto_transfer'] == 1) {
$this->transfer(['id' => $id]);
}
model('manage_withdraw')->commit();
return $this->success();
} catch (\Exception $e) {
model('manage_withdraw')->rollback();
return $this->error('', $e->getMessage() . $e->getLine());
}
}
/**
* 转账
* @param $condition
*/
public function transfer($params)
{
$id = $params['id'];
$site_id = $params['site_id'] ?? 0;
$condition = array(
['id', '=', $id],
);
if ($site_id > 0) {
$condition[] = ['site_id', '=', $site_id];
}
$info = model('manage_withdraw')->getInfo($condition);
if (empty($info)) return $this->error();
$site_id = $info['site_id'];
$transfer_type = $info['transfer_type'];
$member_id = $info['member_id'];
$money = $info['money'];
$manage_id = $info['manage_id'];
$real_name = $info['real_name'];
$real_money = $info['real_money'];
if ($transfer_type == 'balance') {
//添加会员账户流水
$member_account = new MemberAccount();
$member_result = $member_account->addMemberAccount($site_id, $member_id, 'balance_money', $real_money, 'manage', '佣金提现', '业务佣金提现');
if ($member_result['code'] < 0) {
return $member_result;
}
} else {
if (!in_array($transfer_type, ['wechatpay', 'alipay']))
return $this->error('', '当前提现方式不支持在线转账');
$pay_data = array(
'id' => $id,
'out_trade_no' => $info['withdraw_no'],
'real_name' => $info['realname'],
'amount' => $info['real_money'],
'desc' => '会员提现',
'transfer_type' => $transfer_type,
'account_number' => $info['account_number'],
'site_id' => $info['site_id'],
'is_weapp' => $info['applet_type'],
'member_id' => $info['member_id']
);
//调用在线转账借口
$pay_result = event('PayTransfer', $pay_data, true);
if (empty($pay_result)) {
$pay_result = $this->error();
}
if ($pay_result['code'] < 0) {
return $pay_result;
}
}
$account_model = new ManageAccount();
$account_result = $account_model->addAccountLog($manage_id, $real_name, 'withdraw', '-' . $money, $id);
if ($account_result['code'] != 0) {
return $account_result;
}
//调用完成转账
$result = $this->transferFinish(['id' => $id, 'site_id' => $info['site_id']]);
return $result;
}
/**
* 提现转账完成
* @param $id
*/
public function transferFinish($param = [])
{
$condition = [
['id', '=', $param['id']],
['site_id', '=', $param['site_id']],
['status', '=', 2]
];
$info = model('manage_withdraw')->getInfo($condition);
if (empty($info)) return $this->error();
$site_id = $info['site_id'];
$manage_id = $info['manage_id'];
$money = $info['money'];
$payment_time = time();
model('manage_withdraw')->startTrans();
try {
$data = [
'status' => 3,
'status_name' => '已转账',
'payment_time' => $payment_time,
'document' => $param['certificate'] ?? '',
'transfer_remark' => $param['certificate_remark'] ?? ''
];
$result = model('manage_withdraw')->update($data, $condition);
$_condition = array(
['id', '=', $manage_id],
['site_id', '=', $site_id]
);
//修改分销商提现中金额
model('manage_user')->setDec($_condition, 'account_withdraw_apply', $money);
//修改分销商已提现金额
model('manage_user')->setInc($_condition, 'account_withdraw', $money);
model('manage_withdraw')->commit();
$message_model = new Message();
$info['keywords'] = 'manage_withdraw_success';
$message_model->sendMessage($info);
return $this->success();
} catch (\Exception $e) {
model('manage_withdraw')->rollback();
return $this->error('', $e->getMessage());
}
}
/**
* 拒绝提现申请
* @param $condition
*/
public function refuse($params)
{
$id = $params['id'];
$site_id = $params['site_id'];
$data = [
'status' => -1,
"refuse_reason" => $params["refuse_reason"],
"audit_time" => time(),
];
$condition = array(
['id', '=', $id],
['site_id', '=', $site_id]
);
$info = model('manage_withdraw')->getInfo($condition, '*');
if (empty($info)) return $this->error();
model('manage_withdraw')->startTrans();
try {
$money = abs($info['money']);
$manage_id = $info['id'];
if ($info['status'] == 1) {
model('manage_withdraw')->update($data, $condition);
$manage_condition = array(
['id', '=', $manage_id]
);
//修改分销商提现中金额
model('manage_user')->setDec($manage_condition, 'account_withdraw_apply', $money);
//修改分销商可提现金额
model('manage_user')->setInc($manage_condition, 'account', $money);
//提现失败发送消息
$message_model = new Message();
$info['keywords'] = 'manage_withdraw_error';
$message_model->sendMessage($info);
}
model('manage_withdraw')->commit();
return $this->success();
} catch (\Exception $e) {
model('manage_withdraw')->rollback();
return $this->error('', $e->getMessage());
}
}
/**
* 提现流水号
*/
private function createWithdrawNo()
{
$cache = Cache::get('member_withdraw_no' . time());
if (empty($cache)) {
Cache::set('saastka' . time(), 1000);
$cache = Cache::get('member_withdraw_no' . time());
} else {
$cache = $cache + 1;
Cache::set('member_withdraw_no' . time(), $cache);
}
$no = date('Ymdhis', time()) . rand(1000, 9999) . $cache;
return $no;
}
}