DataValidatorService.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. *
  5. * User: king/QQ:995265288
  6. * Date: 2018/6/11 下午3:30
  7. * Email: livsyitian@163.com
  8. */
  9. namespace app\frontend\modules\withdraw\services;
  10. use app\common\exceptions\AppException;
  11. use app\frontend\modules\withdraw\models\Income;
  12. use app\frontend\modules\withdraw\models\Withdraw;
  13. class DataValidatorService
  14. {
  15. const WITHDRAW_TYPE_WECHAT = 'wechat';
  16. const WITHDRAW_TYPE_ALIPAY = 'alipay';
  17. /**
  18. * @var Withdraw
  19. */
  20. private $withdrawModel;
  21. /**
  22. * @var array
  23. */
  24. private $income_set;
  25. public function __construct(Withdraw $withdrawModel)
  26. {
  27. $this->withdrawModel = $withdrawModel;
  28. $this->income_set = $this->incomeSet();
  29. }
  30. /**
  31. * @throws AppException
  32. */
  33. public function validator()
  34. {
  35. if($this->withdrawModel->is_auto) {
  36. return true;
  37. }
  38. $type_name = $this->withdrawModel->type_name;
  39. // 12月20号修改 原:提现金额不能小于1
  40. $amount = $this->withdrawModel->amounts;
  41. $type = $this->withdrawModel->pay_way;
  42. //微信支付宝提现限制
  43. $this->cashLimitation($type,$type_name);
  44. if (bccomp($amount, 0, 2) == -1) {
  45. throw new AppException("{$type_name}提现金额不能小于0元");
  46. }
  47. $real_amount = $this->getIncomeAmount();
  48. if (bccomp($amount, $real_amount, 2) != 0) {
  49. throw new AppException("{$type_name}提现金额错误!");
  50. }
  51. $roll_out_limit = $this->getRollOutLimit();
  52. if (bccomp($amount, $roll_out_limit, 2) == -1 && $type != self::WITHDRAW_TYPE_WECHAT && $type != self::WITHDRAW_TYPE_ALIPAY ) {
  53. throw new AppException("{$type_name}提现金额不能小于{$roll_out_limit}元");
  54. }
  55. // 12月20号修改 原:扣除手续费、劳务税金额不能小于1元
  56. $outlay = bcadd($this->withdrawModel->poundage, $this->withdrawModel->servicetax, 2);
  57. $result_amount = bcsub($amount, $outlay, 2);
  58. if (bccomp($result_amount, 0, 2) == -1) {
  59. throw new AppException("{$type_name}扣除手续费、劳务税金额不能小于0元");
  60. }
  61. }
  62. /**
  63. * @return float
  64. */
  65. private function getIncomeAmount()
  66. {
  67. $type_ids = $this->withdrawModel->type_id;
  68. //->whereStatus(Income::STATUS_INITIAL)
  69. return Income::whereIn('id', explode(',', $type_ids))->whereStatus(Income::STATUS_INITIAL)->sum('amount');
  70. }
  71. /**
  72. * @return float
  73. */
  74. private function getRollOutLimit()
  75. {
  76. return $this->getIncomeSet('roll_out_limit');
  77. }
  78. /**
  79. * @param $key
  80. * @return float
  81. */
  82. private function getIncomeSet($key)
  83. {
  84. $result = array_get($this->income_set, $key, '0');
  85. return empty($result) ? '0' : $result;
  86. }
  87. /**
  88. * @return array
  89. */
  90. private function incomeSet()
  91. {
  92. return $this->withdrawModel->income_set;
  93. }
  94. private function withdrawIncomeSet()
  95. {
  96. return \Setting::get('withdraw.income');
  97. }
  98. private function cashLimitation($type,$type_name){
  99. $set = $this->withdrawIncomeSet();
  100. if( $type == self::WITHDRAW_TYPE_WECHAT){
  101. $wechat_min = $set['wechat_min'] ;
  102. $wechat_max = $set['wechat_max'] ;
  103. if( $this->withdrawModel->amounts < $wechat_min && !empty($wechat_min)){
  104. throw new AppException("{$type_name}提现到微信单笔提现额度最低{$wechat_min}元",['status'=>0]);
  105. }elseif( $this->withdrawModel->amounts > $wechat_max && !empty($wechat_max)){
  106. throw new AppException("{$type_name}提现到微信单笔提现额度最高{$wechat_max}元",['status'=>0]);
  107. }
  108. }elseif($type == self::WITHDRAW_TYPE_ALIPAY){
  109. $alipay_min = $set['alipay_min'] ;
  110. $alipay_max = $set['alipay_max'] ;
  111. if( $this->withdrawModel->amounts < $alipay_min && !empty($alipay_min)){
  112. throw new AppException("{$type_name}提现到支付宝单笔提现额度最低{$alipay_min}元",['status'=>0]);
  113. }elseif( $this->withdrawModel->amounts > $alipay_max && !empty($alipay_max)){
  114. throw new AppException("{$type_name}提现到支付宝单笔提现额度最高{$alipay_max}元",['status'=>0]);
  115. }
  116. }
  117. }
  118. }