<?php
namespace app\store\model\dealer;
use app\store\model\User as UserModel;
use app\store\model\Wxapp as WxappModel;
use app\common\model\dealer\User as dealerUserModel;
use app\common\model\dealer\Withdraw as WithdrawModel;
use app\common\service\Order as OrderService;
use app\common\service\Message as MessageService;
use app\common\enum\dealer\withdraw\PayType as PayTypeEnum;
use app\common\enum\dealer\withdraw\ApplyStatus as ApplyStatusEnum;
use app\common\library\wechat\WxPay;
class Withdraw extends WithdrawModel
{
public function getAuditTimeAttr($value)
{
return $value > 0 ? date('Y-m-d H:i:s', $value) : 0;
}
public function getPayTypeAttr($value)
{
return ['text' => PayTypeEnum::data()[$value]['name'], 'value' => $value];
}
public function getList($userId = null, $apply_status = -1, $pay_type = -1, $search = '')
{
$this->alias('withdraw')
->with(['user'])
->field('withdraw.*, dealer.real_name, dealer.mobile, user.nickName, user.avatarUrl')
->join('user', 'user.user_id = withdraw.user_id')
->join('dealer_user dealer', 'dealer.user_id = withdraw.user_id')
->order(['withdraw.create_time' => 'desc']);
$userId > 0 && $this->where('withdraw.user_id', '=', $userId);
!empty($search) && $this->where('dealer.real_name|dealer.mobile', 'like', "%$search%");
$apply_status > 0 && $this->where('withdraw.apply_status', '=', $apply_status);
$pay_type > 0 && $this->where('withdraw.pay_type', '=', $pay_type);
return $this->paginate(15, false, [
'query' => \request()->request()
]);
}
public function submit($data)
{
if (
$data['apply_status'] == ApplyStatusEnum::AUDIT_REJECT
&& empty($data['reject_reason'])
) {
$this->error = '请填写驳回原因';
return false;
}
$this->transaction(function () use ($data) {
$data['audit_time'] = time();
$this->allowField(true)->save($data);
if ($data['apply_status'] == ApplyStatusEnum::AUDIT_REJECT) {
User::backFreezeMoney($this['user_id'], $this['money']);
}
MessageService::send('dealer.withdraw', [
'withdraw' => $this,
'user' => UserModel::detail($this['user_id']),
]);
});
return true;
}
public function money($verifyUserFreezeMoney = true)
{
if ($verifyUserFreezeMoney && !$this->verifyUserFreezeMoney($this['user_id'], $this['money'])) {
return false;
}
return $this->transaction(function () {
$this->allowField(true)->save([
'apply_status' => 40,
'audit_time' => time(),
]);
User::totalMoney($this['user_id'], $this['money']);
Capital::add([
'user_id' => $this['user_id'],
'flow_type' => 20,
'money' => -$this['money'],
'describe' => '申请提现',
]);
return true;
});
}
public function wechatPay()
{
if (!$this->verifyUserFreezeMoney($this['user_id'], $this['money'])) {
return false;
}
$user = $this['user']['user'];
$orderNO = OrderService::createOrderNo();
$desc = '分销商提现付款';
$wxConfig = WxappModel::getWxappCache();
$WxPay = new WxPay($wxConfig);
if ($WxPay->transfers($orderNO, $user['open_id'], $this['money'], $desc)) {
$this->money(false);
return true;
}
return false;
}
public function verifyUserFreezeMoney($userId, $money)
{
$dealerUserInfo = dealerUserModel::detail($userId);
if ($dealerUserInfo['freeze_money'] < $money) {
$this->error = '数据错误:已冻结的佣金不能小于提现的金额';
return false;
}
return true;
}
}