<?php
|
|
namespace app\api\model\plus\bonus;
|
|
use app\common\exception\BaseException;
|
use app\common\model\plus\bonus\Cash as CashModel;
|
use app\common\model\user\User as UserModel;
|
use app\common\model\plus\yms\Setting as YmsSettingModel;
|
use app\common\model\plus\yms\User as YmsUserModel;
|
|
/**
|
* 分销商提现明细模型
|
*/
|
class Cash extends CashModel
|
{
|
/**
|
* 隐藏字段
|
*/
|
protected $hidden = [
|
'update_time',
|
];
|
|
/**
|
* 完税待完成提现申请详情
|
*/
|
public static function getPayDetail($cashNo, $apply_status)
|
{
|
$model = new static();
|
$model = $model->where('cash_no', '=', $cashNo);
|
if($apply_status > 0){
|
$model = $model->where('apply_status', '=', 50);
|
}
|
return $model->find();
|
}
|
|
/**
|
* 获取分销商提现明细
|
*/
|
public function getList($user_id, $apply_status = -1,$limit=15)
|
{
|
$model = $this;
|
$apply_status > -1 && $model = $model->where('apply_status', '=', $apply_status);
|
return $model->where('user_id', '=', $user_id)->order(['create_time' => 'desc'])
|
->paginate($limit);
|
}
|
|
/**
|
* 提交申请
|
*/
|
public function submit($user, $data)
|
{
|
$this->startTrans();
|
try {
|
// 数据验证
|
$this->validation($user, $data,['is_open'=>0]);
|
// 新增申请记录
|
$this->save(array_merge($data, [
|
'user_id' => $user['user_id'],
|
'apply_status' => 10,
|
'app_id' => self::$app_id,
|
'cash_no' => $this->createCashNo(),
|
'is_yms' => 0, // 是否使用完税系统打款 by lyzflash
|
]));
|
// 将支付宝或银行卡信息保存到用户表 by lyzflash
|
$this->saveAccount($user['user_id'], $data, ['is_open'=>0]);
|
// 冻结用户资金
|
$user->freezeMoney($data['money']);
|
$this->commit();
|
return true;
|
} catch (\Exception $e) {
|
$this->error = $e->getMessage();
|
$this->rollback();
|
return false;
|
}
|
}
|
|
/**
|
* 数据验证
|
*/
|
private function validation($user, $data, $ymsSetting)
|
{
|
// 结算设置
|
$settlement = Setting::getItem('settlement');
|
// 最低提现分红
|
if ($data['money'] <= 0) {
|
throw new BaseException(['msg' => '提现金额不正确']);
|
}
|
if ($user['money'] <= 0) {
|
throw new BaseException(['msg' => '当前用户没有可提现分红']);
|
}
|
if ($data['money'] > $user['money']) {
|
throw new BaseException(['msg' => '提现金额不能大于可提现分红']);
|
}
|
if ($data['money'] < $settlement['min_money']) {
|
throw new BaseException(['msg' => '最低提现金额为' . $settlement['min_money']]);
|
}
|
if (!in_array($data['pay_type'], $settlement['pay_type'])) {
|
throw new BaseException(['msg' => '提现方式不正确']);
|
}
|
if ($data['pay_type'] == '20') {
|
if (empty($data['alipay_name']) || empty($data['alipay_account'])) {
|
throw new BaseException(['msg' => '请补全提现信息']);
|
}
|
} elseif ($data['pay_type'] == '30' && $ymsSetting['is_open'] == '0') {
|
if (empty($data['bank_name']) || empty($data['bank_account']) || empty($data['bank_card'])) {
|
throw new BaseException(['msg' => '请补全提现信息']);
|
}
|
}
|
}
|
|
/**
|
* 数据验证
|
*/
|
private function saveAccount($user_id, $formData, $ymsSetting)
|
{
|
if ($formData['pay_type'] == 20) {
|
$data = [
|
'alipay_name' => $formData['alipay_name'],
|
'alipay_account' => $formData['alipay_account'],
|
];
|
} elseif ($formData['pay_type'] == 30 && $ymsSetting['is_open'] == '0') {
|
$data = [
|
'bank_name' => $formData['bank_name'],
|
'bank_account' => $formData['bank_account'],
|
'bank_card' => $formData['bank_card'],
|
];
|
} else {
|
return false;
|
}
|
$user = UserModel::detail($user_id);
|
$user->save($data);
|
}
|
|
private function createCashNo()
|
{
|
return date('Ymd') . substr(implode(NULL, array_map('ord', str_split(substr(uniqid(), 7, 13), 1))), 0, 8);
|
}
|
|
}
|