admin/addon/fenxiao/model/FenxiaoWithdraw.php

870 lines
32 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
/**
* B2B商城系统 - 团队十年电商经验汇集巨献!
* =========================================================
* Copy right 2023-2046 四川正今科技有限公司, 保留所有权利。
* ----------------------------------------------
* 官方网址: https://www.viewsphp.com
* =========================================================
*/
namespace addon\fenxiao\model;
use app\model\BaseModel;
use app\model\member\Member;
use app\model\member\MemberAccount;
use app\model\message\Message;
use app\model\message\Sms;
use app\model\shop\ShopAcceptMessage;
use addon\wechat\model\Message as WechatMessage;
use app\model\member\Member as MemberModel;
use addon\weapp\model\Message as WeappMessage;
use app\model\system\Pay;
use think\facade\Cache;
use think\facade\Db;
/**
* 分销商提现
*/
class FenxiaoWithdraw 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 $data
* @return array
*/
public function addFenxiaoWithdraw($data)
{
//获取分销商信息
$fenxiao_model = new Fenxiao();
$fenxiao_info = $fenxiao_model->getFenxiaoInfo([ [ 'member_id', '=', $data[ 'member_id' ] ] ], 'fenxiao_id,fenxiao_name,account');
if (empty($fenxiao_info[ 'data' ])) {
return $this->error('该分销商不存在');
}
if ($fenxiao_info[ 'data' ][ 'account' ] < $data[ 'money' ]) {
return $this->error('', '提现金额大于可提现金额');
}
//获取提现配置信息
$config_model = new Config();
$config = $config_model->getFenxiaoWithdrawConfig($data[ 'site_id' ]);
$config_info = $config[ 'data' ][ 'value' ];
if ($config_info[ 'withdraw' ] > $data[ 'money' ]) {
return $this->error('', '提现金额小于最低提现金额');
}
if ($data[ 'money' ] >= $config_info[ 'min_no_fee' ] && $data[ 'money' ] <= $config_info[ 'max_no_fee' ]) {
$data[ 'withdraw_rate' ] = 0;
$data[ 'withdraw_rate_money' ] = 0;
$data[ 'real_money' ] = $data[ 'money' ];
} else {
$data[ 'withdraw_rate' ] = $config_info[ 'withdraw_rate' ];
if ($config_info[ 'withdraw_rate' ] == 0) {
$data[ 'withdraw_rate' ] = 0;
$data[ 'withdraw_rate_money' ] = 0;
$data[ 'real_money' ] = $data[ 'money' ];
} else {
$data[ 'withdraw_rate' ] = $config_info[ 'withdraw_rate' ];
$data[ 'withdraw_rate_money' ] = round($data[ 'money' ] * $config_info[ 'withdraw_rate' ] / 100, 2);
$data[ 'real_money' ] = $data[ 'money' ] - $data[ 'withdraw_rate_money' ];
}
}
$data[ 'withdraw_no' ] = date('YmdHis') . rand(1000, 9999);
$data[ 'create_time' ] = time();
model('fenxiao_withdraw')->startTrans();
try {
$data[ 'fenxiao_id' ] = $fenxiao_info[ 'data' ][ 'fenxiao_id' ];
$data[ 'fenxiao_name' ] = $fenxiao_info[ 'data' ][ 'fenxiao_name' ];
$res = model('fenxiao_withdraw')->add($data);
//修改分销商提现中金额
model('fenxiao')->setInc([ [ 'member_id', '=', $data[ 'member_id' ] ] ], 'account_withdraw_apply', $data[ 'money' ]);
//修改分销商可提现金额
model('fenxiao')->setDec([ [ 'member_id', '=', $data[ 'member_id' ] ] ], 'account', $data[ 'money' ]);
//判断是否需要审核
if ($config_info[ 'withdraw_status' ] == 2) {//不需要
$result = $this->withdrawPass($res, $data[ 'site_id' ]);
if ($result[ 'code' ] < 0) {
model('fenxiao_withdraw')->rollback();
return $result;
}
}
model('fenxiao_withdraw')->commit();
//申请提现发送消息
$data[ 'keywords' ] = 'FENXIAO_WITHDRAWAL_APPLY';
$message_model = new Message();
$message_model->sendMessage($data);
return $this->success($res);
} catch (\Exception $e) {
model('fenxiao_withdraw')->rollback();
return $this->error('', $e->getMessage());
}
}
/**
* 提现审核通过
* @param $id
* @param $site_id
* @return array
*/
public function withdrawPass($ids, $site_id)
{
model('fenxiao_withdraw')->startTrans();
try {
$withdraw_list = $this->getFenxiaoWithdrawList([ [ 'id', 'in', $ids ] ], '*');
foreach ($withdraw_list[ 'data' ] as $k => $v) {
if ($v[ 'status' ] == 1) {
$this->agree([ 'id' => $v[ 'id' ], 'site_id' => $site_id ]);
}
}
model('fenxiao_withdraw')->commit();
return $this->success();
} catch (\Exception $e) {
model('fenxiao_withdraw')->rollback();
return $this->error('', $e->getMessage());
}
}
/**
* 获取提现详情
* @param array $condition
* @return array
*/
public function getFenxiaoWithdrawInfo($condition = [], $field = '*')
{
$res = model('fenxiao_withdraw')->getInfo($condition, $field);
return $this->success($res);
}
/**
* 提现详情
* @param $params
* @return array
*/
public function getFenxiaoWithdrawDetail($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('fenxiao_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 getFenxiaoWithdrawList($condition = [], $field = '*', $order = '', $limit = null)
{
$list = model('fenxiao_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 getFenxiaoWithdrawPageList($condition = [], $page = 1, $page_size = PAGE_LIST_ROWS, $order = '', $field = '*', $alias = '', $join = [])
{
$list = model('fenxiao_withdraw')->pageList($condition, $field, $order, $page, $page_size, $alias, $join);
return $this->success($list);
}
/**
* 分销佣金发放通知
* @param $data
*/
public function messageOrderCommissionGrant($data)
{
//发送短信
$sms_model = new Sms();
// 分销订单
$fenxiao_order_model = new FenxiaoOrder();
$fenxiao_order = $fenxiao_order_model->getFenxiaoOrderInfo([ [ 'fenxiao_order_id', '=', $data[ 'order_id' ] ] ]);
$fenxiao_order_info = $fenxiao_order[ 'data' ];
$commission = $fenxiao_order_info[ $data[ 'level' ] . '_commission' ];
$fenxiao_id = $fenxiao_order_info[ $data[ 'level' ] . '_fenxiao_id' ];
$fenxiao_model = new Fenxiao();
$fenxiao_info = $fenxiao_model->getFenxiaoInfo([ [ 'fenxiao_id', '=', $fenxiao_id ] ], 'fenxiao_id, member_id')[ 'data' ];
$member_model = new MemberModel();
$member_info_result = $member_model->getMemberInfo([ [ 'member_id', '=', $fenxiao_info[ 'member_id' ] ?? 0 ] ]);
$member_info = $member_info_result[ 'data' ];
//绑定微信公众号才发送
if (!empty($member_info) && !empty($member_info[ 'wx_openid' ])) {
$wechat_model = new WechatMessage();
$data[ 'openid' ] = $member_info[ 'wx_openid' ];
$data[ 'template_data' ] = [
'keyword1' => $commission,
'keyword2' => time_to_date($fenxiao_order_info[ 'create_time' ]),
];
$data[ 'page' ] = '';
$wechat_model->sendMessage($data);
}
//发送订阅消息
if (!empty($member_info) && !empty($member_info[ 'weapp_openid' ])) {
$weapp_model = new WeappMessage();
$data[ 'openid' ] = $member_info[ 'weapp_openid' ];
$data[ 'template_data' ] = [
'amount1' => [
'value' => $fenxiao_order_info[ 'real_goods_money' ]
],
'amount2' => [
'value' => $commission
],
'thing3' => [
'value' => $fenxiao_order_info[ 'sku_name' ]
],
'time4' => [
'value' => time_to_date($fenxiao_order_info[ 'create_time' ]),
],
];
$data[ 'page' ] = '';
$weapp_model->sendMessage($data);
}
$var_parse = [
'sitename' => replaceSpecialChar($data[ 'site_info' ][ 'site_name' ]),
'level' => $data[ 'level' ],
'username' => empty(replaceSpecialChar($fenxiao_order_info[ 'member_name' ])) ? $fenxiao_order_info[ 'member_mobile' ] : replaceSpecialChar($fenxiao_order_info[ 'member_name' ]),
];
$data[ 'sms_account' ] = $member_info[ 'mobile' ];
$data[ 'var_parse' ] = $var_parse;
$sms_model->sendMessage($data);
}
/**
* 分销提现成功通知
* @param $data
*/
public function messageFenxiaoWithdrawalSuccess($data)
{
//发送短信
$sms_model = new Sms();
$var_parse = array (
'fenxiaoname' => $data[ 'fenxiao_name' ],//会员名
'money' => $data[ 'money' ]
);
$data[ 'sms_account' ] = $data[ 'mobile' ];//手机号
$data[ 'var_parse' ] = $var_parse;
$sms_model->sendMessage($data);
$member_model = new MemberModel();
$member_info_result = $member_model->getMemberInfo([ [ 'member_id', '=', $data[ 'member_id' ] ] ]);
$member_info = $member_info_result[ 'data' ];
//绑定微信公众号才发送
if (!empty($member_info) && !empty($member_info[ 'wx_openid' ])) {
$wechat_model = new WechatMessage();
$data[ 'openid' ] = $member_info[ 'wx_openid' ];
$data[ 'template_data' ] = [
'keyword1' => $data[ 'money' ],
'keyword2' => time_to_date($data[ 'payment_time' ]),
];
$data[ 'page' ] = '';
$wechat_model->sendMessage($data);
}
//发送订阅消息
if (!empty($member_info) && !empty($member_info[ 'weapp_openid' ])) {
$weapp_model = new WeappMessage();
$data[ 'openid' ] = $member_info[ 'weapp_openid' ];
$data[ 'template_data' ] = [
'amount1' => [
'value' => $data[ 'money' ]
],
'time2' => [
'value' => time_to_date(time())
],
'thing3' => [
'value' => '提现成功'
]
];
$data[ 'page' ] = '';
$weapp_model->sendMessage($data);
}
}
/**
* 分销提现失败通知
* @param $data
*/
public function messageFenxiaoWithdrawalError($data)
{
//发送短信
$sms_model = new Sms();
$member_model = new MemberModel();
$member_info_result = $member_model->getMemberInfo([ [ 'member_id', '=', $data[ 'member_id' ] ] ]);
$member_info = $member_info_result[ 'data' ];
$var_parse = array (
'fenxiaoname' => str_replace(' ', '', $data[ 'fenxiao_name' ]),//会员名
'money' => $data[ 'money' ]
);
$data[ 'sms_account' ] = $member_info[ 'mobile' ];//手机号
$data[ 'var_parse' ] = $var_parse;
$sms_model->sendMessage($data);
//绑定微信公众号才发送
if (!empty($member_info) && !empty($member_info[ 'wx_openid' ])) {
$wechat_model = new WechatMessage();
$data[ 'openid' ] = $member_info[ 'wx_openid' ];
$data[ 'template_data' ] = [
'keyword1' => time_to_date($data[ 'create_time' ]),
'keyword2' => '审核失败',
'keyword3' => '会员申请提现',
'keyword4' => $data[ 'money' ],
];
$data[ 'page' ] = '';
$wechat_model->sendMessage($data);
}
//发送订阅消息
if (!empty($member_info) && !empty($member_info[ 'weapp_openid' ])) {
$weapp_model = new WeappMessage();
$data[ 'openid' ] = $member_info[ 'weapp_openid' ];
$data[ 'template_data' ] = [
'amount2' => [
'value' => $data[ 'money' ]
],
'thing4' => [
'value' => '提现审核失败'
]
];
$data[ 'page' ] = '';
$weapp_model->sendMessage($data);
}
}
/**
* 分销申请提现通知,卖家通知
* @param $data
*/
public function messageFenxiaoWithdrawalApply($data)
{
//发送短信
$sms_model = new Sms();
$var_parse = array (
'fenxiaoname' => replaceSpecialChar($data[ 'fenxiao_name' ]),//会员名
'money' => $data[ 'money' ],//退款申请金额
);
// $site_id = $data['site_id'];
// $shop_info = model('shop')->getInfo([['site_id', '=', $site_id]], 'mobile,email');
// $message_data['sms_account'] = $shop_info['mobile'];//手机号
$data[ 'var_parse' ] = $var_parse;
$shop_accept_message_model = new ShopAcceptMessage();
$result = $shop_accept_message_model->getShopAcceptMessageList();
$list = $result[ 'data' ];
if (!empty($list)) {
foreach ($list as $v) {
$message_data = $data;
$message_data[ 'sms_account' ] = $v[ 'mobile' ];//手机号
$sms_model->sendMessage($message_data);
if ($v[ 'wx_openid' ] != '') {
$wechat_model = new WechatMessage();
$data[ 'openid' ] = $v[ 'wx_openid' ];
$data[ 'template_data' ] = [
'keyword1' => replaceSpecialChar($data[ 'fenxiao_name' ]),
'keyword2' => time_to_date($data[ 'create_time' ]),
'keyword3' => $data[ 'money' ]
];
$data[ 'page' ] = '';
$wechat_model->sendMessage($data);
}
}
}
}
/**
* 获取提现数量
* @param array $condition
* @return array
*/
public function getFenxiaoWithdrawCount($condition = [], $field = '*')
{
$res = model('fenxiao_withdraw')->getCount($condition, $field);
return $this->success($res);
}
public function apply($data, $site_id = 0, $app_module = 'shop')
{
$config_model = new Config();
$config = $config_model->getFenxiaoWithdrawConfig($site_id)[ 'data' ][ 'value' ] ?? [];
$withdraw_no = $this->createWithdrawNo();
$apply_money = round($data[ 'apply_money' ], 2);
$withdraw_min_money = $config[ 'withdraw' ];
$withdraw_max_money = $config[ 'max' ];
if ($apply_money < $withdraw_min_money) return $this->error([], '申请提现金额不能小于最低提现额度' . $withdraw_min_money);
if ($apply_money > $withdraw_max_money) return $this->error([], '申请提现金额不能大于最高提现额度' . $withdraw_max_money);
$member_id = $data[ 'member_id' ];
$member_model = new Member();
$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');
$fenxiao_model = new Fenxiao();
$fenxiao_info = $fenxiao_model->getFenxiaoInfo($member_condition, 'fenxiao_id,fenxiao_name,account')[ 'data' ] ?? [];
if (empty($fenxiao_info)) {
return $this->error('该分销商不存在');
}
$fenxiao_account = $fenxiao_info[ 'account' ];//会员的分销佣金
if ($fenxiao_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('fenxiao_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,
'fenxiao_id' => $fenxiao_info[ 'fenxiao_id' ],
'fenxiao_name' => $fenxiao_info[ 'fenxiao_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('fenxiao_withdraw')->add($data);
//修改分销商提现中金额
model('fenxiao')->setInc($member_condition, 'account_withdraw_apply', $apply_money);
//修改分销商可提现金额
model('fenxiao')->setDec($member_condition, 'account', $apply_money);
//申请提现发送消息
//申请提现发送消息
$data[ 'keywords' ] = 'FENXIAO_WITHDRAWAL_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('fenxiao_withdraw')->rollback();
return $result;
}
}
model('fenxiao_withdraw')->commit();
return $this->success();
} catch (\Exception $e) {
model('fenxiao_withdraw')->rollback();
return $this->error('', $e->getMessage());
}
}
/**
* 提现流水号
*/
private function createWithdrawNo()
{
$cache = Cache::get('member_withdraw_no' . time());
if (empty($cache)) {
Cache::set('niutk' . 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;
}
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('fenxiao_withdraw')->getInfo($condition);
if (empty($info))
return $this->error();
$config_model = new Config();
$config = $config_model->getFenxiaoWithdrawConfig($site_id)[ 'data' ][ 'value' ] ?? [];
model('fenxiao_withdraw')->startTrans();
try {
$data = array (
'status' => 2,
// 'status_name' => '待转账',//已审核待转账
'audit_time' => time(),
);
$result = model('fenxiao_withdraw')->update($data, $condition);
//是否启用自动转账(必须是微信或支付宝)
if ($config[ 'is_auto_transfer' ] == 1) {
$this->transfer([ 'id' => $id ]);
}
model('fenxiao_withdraw')->commit();
return $this->success();
} catch (\Exception $e) {
model('fenxiao_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('fenxiao_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' ];
$fenxiao_id = $info[ 'fenxiao_id' ];
$fenxiao_name = $info[ 'fenxiao_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, 'fenxiao', '佣金提现', '分销佣金提现');
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 FenxiaoAccount();
$account_result = $account_model->addAccountLog($fenxiao_id, $fenxiao_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('fenxiao_withdraw')->getInfo($condition);
if (empty($info)) return $this->error();
$site_id = $info[ 'site_id' ];
$member_id = $info[ 'member_id' ];
$fenxiao_id = $info[ 'fenxiao_id' ];
$money = $info[ 'money' ];
$payment_time = time();
model('fenxiao_withdraw')->startTrans();
try {
$data = [
'status' => 3,
'status_name' => '已转账',
'payment_time' => $payment_time,
'document' => $param[ 'certificate' ] ?? '',
'transfer_remark' => $param[ 'certificate_remark' ] ?? ''
];
$result = model('fenxiao_withdraw')->update($data, $condition);
$fenxiao_condition = array (
[ 'fenxiao_id', '=', $fenxiao_id ]
);
//修改分销商提现中金额
model('fenxiao')->setDec($fenxiao_condition, 'account_withdraw_apply', $money);
//修改分销商已提现金额
model('fenxiao')->setInc($fenxiao_condition, 'account_withdraw', $money);
model('fenxiao_withdraw')->commit();
// $member_info = model('member')->getInfo([ [ 'member_id', '=', $member_id ] ], 'nickname');
// $stat_model = new Stat();
// $stat_model->switchStat(['type' => 'member_withdraw', 'data' => ['site_id' => $info['site_id'], 'id' => $info['id']]]);
$message_model = new Message();
$info[ 'keywords' ] = 'FENXIAO_WITHDRAWAL_SUCCESS';
$message_model->sendMessage($info);
return $this->success();
} catch (\Exception $e) {
model('fenxiao_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('fenxiao_withdraw')->getInfo($condition, '*');
if (empty($info)) return $this->error();
model('fenxiao_withdraw')->startTrans();
try {
$money = $info[ 'money' ];
$fenxiao_id = $info[ 'fenxiao_id' ];
if ($info[ 'status' ] == 1) {
model('fenxiao_withdraw')->update($data, $condition);
$fenxiao_condition = array (
[ 'fenxiao_id', '=', $fenxiao_id ]
);
//修改分销商提现中金额
model('fenxiao')->setDec($fenxiao_condition, 'account_withdraw_apply', $money);
//修改分销商可提现金额
model('fenxiao')->setInc($fenxiao_condition, 'account', $money);
//提现失败发送消息
$message_model = new Message();
$info[ 'keywords' ] = 'FENXIAO_WITHDRAWAL_ERROR';
$message_model->sendMessage($info);
}
model('fenxiao_withdraw')->commit();
return $this->success();
} catch (\Exception $e) {
model('fenxiao_withdraw')->rollback();
return $this->error('', $e->getMessage());
}
}
public function exportFenxiaoWithdraw($condition, $order, $site_id)
{
try {
$file_name = date('Y年m月d日-分销提现', time()) . '.csv';
// $file_name = date('YmdHis').'.csv';//csv文件名
//通过分批次执行数据导出(防止内存超出配置设置的)
set_time_limit(0);
ini_set('memory_limit', '256M');
//设置header头
header('Content-Description: File Transfer');
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment; filename="' . $file_name . '"');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
//打开php数据输入缓冲区
$fp = fopen('php://output', 'a');
// fwrite($fp, chr(0xEF).chr(0xBB).chr(0xBF)); // 添加 BOM
$heade = [ '分销商', '提现方式', '申请提现金额', '提现手续费', '实际转账金额', '提现状态', '申请时间', '收款账号', '真实姓名', '手机号', '银行名称', '银行账号' ];
//将数据编码转换成GBK格式
mb_convert_variables('GBK', 'UTF-8', $heade);
//将数据格式化为CSV格式并写入到output流中
fputcsv($fp, $heade);
$transfer_type_list = $this->getTransferType($site_id);
$status_name = [ 1 => "待审核", 2 => "待转账", 3 => "已转账", -1 => "已拒绝", -2 => "转账失败" ];
//写入第一行表头
Db::name('fenxiao_withdraw')->where($condition)->order($order)->chunk(500, function($item_list) use ($fp, $transfer_type_list, $status_name) {
//写入导出信息
foreach ($item_list as $k => $item_v) {
$temp_data = [
$item_v[ 'fenxiao_name' ] . "\t",
$transfer_type_list[ $item_v[ 'transfer_type' ] ] . "\t",
(float) $item_v[ 'money' ] . "\t",
(float) $item_v[ 'withdraw_rate_money' ] . "\t",
(float) $item_v[ 'real_money' ] . "\t",
$status_name[ $item_v[ 'status' ] ] . "\t",
time_to_date($item_v[ 'create_time' ]) . "\t",
$item_v[ 'account_number' ] . "\t",
$item_v[ 'realname' ] . "\t",
$item_v[ 'mobile' ] . "\t",
$item_v[ 'bank_name' ] . "\t",
$item_v[ 'transfer_account_no' ] . "\t",
];
mb_convert_variables('GBK', 'UTF-8', $temp_data);
fputcsv($fp, $temp_data);
//将已经存储到csv中的变量数据销毁释放内存
unset($item_v);
}
unset($item_list);
});
//关闭句柄
fclose($fp);
die;
} catch (\Exception $e) {
return $this->error([], $e->getMessage() . $e->getFile() . $e->getLine());
}
}
}