bztang-admin/app/frontend/modules/withdraw/services/DataValidatorService.php

163 lines
5.0 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
/**
* Created by PhpStorm.
*
* User: king/QQ995265288
* Date: 2018/6/11 下午3:30
* Email: livsyitian@163.com
*/
namespace app\frontend\modules\withdraw\services;
use app\common\exceptions\AppException;
use app\frontend\modules\withdraw\models\Income;
use app\frontend\modules\withdraw\models\Withdraw;
class DataValidatorService
{
const WITHDRAW_TYPE_WECHAT = 'wechat';
const WITHDRAW_TYPE_ALIPAY = 'alipay';
const WITHDRAW_TYPE_CONVERGE = 'converge_pay';
/**
* @var Withdraw
*/
private $withdrawModel;
/**
* @var array
*/
private $income_set;
public function __construct(Withdraw $withdrawModel)
{
$this->withdrawModel = $withdrawModel;
$this->income_set = $this->incomeSet();
}
/**
* @throws AppException
*/
public function validator()
{
if($this->withdrawModel->is_auto) {
return true;
}
$type_name = $this->withdrawModel->type_name;
// 12月20号修改 原提现金额不能小于1
$amount = $this->withdrawModel->amounts;
$type = $this->withdrawModel->pay_way;
//微信支付宝提现限制
$this->cashLimitation($type,$type_name);
if (bccomp($amount, 0, 2) == -1) {
throw new AppException("{$type_name}提现金额不能小于0元");
}
$real_amount = $this->getIncomeAmount();
if (bccomp(bcadd($amount,$this->withdrawModel->freeze_money,2), $real_amount, 2) != 0) {
throw new AppException("{$type_name}提现金额错误!");
}
$roll_out_limit = $this->getRollOutLimit();
if (bccomp($amount, $roll_out_limit, 2) == -1 && $type != self::WITHDRAW_TYPE_WECHAT && $type != self::WITHDRAW_TYPE_ALIPAY ) {
throw new AppException("{$type_name}提现金额不能小于{$roll_out_limit}");
}
// 12月20号修改 原扣除手续费、劳务税金额不能小于1元
$outlay = bcadd($this->withdrawModel->poundage, $this->withdrawModel->servicetax, 2);
$result_amount = bcsub($amount, $outlay, 2);
if (bccomp($result_amount, 0, 2) == -1) {
throw new AppException("{$type_name}扣除手续费、劳务税金额不能小于0元");
}
}
/**
* @return float
*/
private function getIncomeAmount()
{
$type_ids = $this->withdrawModel->type_id;
//->whereStatus(Income::STATUS_INITIAL)
return Income::whereIn('id', explode(',', $type_ids))->whereStatus(Income::STATUS_INITIAL)->sum('amount');
}
/**
* @return float
*/
private function getRollOutLimit()
{
return $this->getIncomeSet('roll_out_limit');
}
/**
* @param $key
* @return float
*/
private function getIncomeSet($key)
{
$result = array_get($this->income_set, $key, '0');
return empty($result) ? '0' : $result;
}
/**
* @return array
*/
private function incomeSet()
{
return $this->withdrawModel->income_set;
}
private function withdrawIncomeSet()
{
return \Setting::get('withdraw.income');
}
private function cashLimitation($type,$type_name){
$set = $this->withdrawIncomeSet();
if( $type == self::WITHDRAW_TYPE_WECHAT){
$wechat_min = $set['wechat_min'] ;
$wechat_max = $set['wechat_max'] ;
if( $this->withdrawModel->amounts < $wechat_min && !empty($wechat_min)){
throw new AppException("{$type_name}提现到微信单笔提现额度最低{$wechat_min}",['status'=>0]);
}elseif( $this->withdrawModel->amounts > $wechat_max && !empty($wechat_max)){
throw new AppException("{$type_name}提现到微信单笔提现额度最高{$wechat_max}",['status'=>0]);
}
}elseif($type == self::WITHDRAW_TYPE_ALIPAY){
$alipay_min = $set['alipay_min'] ;
$alipay_max = $set['alipay_max'] ;
if( $this->withdrawModel->amounts < $alipay_min && !empty($alipay_min)){
throw new AppException("{$type_name}提现到支付宝单笔提现额度最低{$alipay_min}",['status'=>0]);
}elseif( $this->withdrawModel->amounts > $alipay_max && !empty($alipay_max)){
throw new AppException("{$type_name}提现到支付宝单笔提现额度最高{$alipay_max}",['status'=>0]);
}
} elseif ($type == self::WITHDRAW_TYPE_CONVERGE) {
$converge_min = $set['converge_pay_min'] ;
$converge_max = $set['converge_pay_max'] ;
if( $this->withdrawModel->amounts < $converge_min && !empty($converge_min)){
throw new AppException("{$type_name}提现到汇聚单笔提现额度最低{$converge_min}",['status'=>0]);
}elseif( $this->withdrawModel->amounts > $converge_max && !empty($converge_max)){
throw new AppException("{$type_name}提现到汇聚单笔提现额度最高{$converge_max}",['status'=>0]);
}
}
}
}