cxgj/models/common/CommonCashSubmitForm.php
2023-11-27 09:45:13 +08:00

222 lines
7.9 KiB
PHP

<?php
/**
* @author Any
* @description KISS
* @date 2021年6月9日
* @version 1.0.0
*
* _____LOG_____
*
*/
namespace app\models\common;
use app\models\Model;
use app\models\Cash;
use app\models\Balance;
use app\models\CashSetting;
use app\models\UniqueOrderNo;
use app\components\SysConst;
class CommonCashSubmitForm extends Model
{
public $cx_mch_id;
public $scene;
public $user_id;
public $money;
public $cash_type;
public $wechat_account;
public $wechat_account_name;
public $alipay_account;
public $alipay_account_name;
public $bank_name;
public $bank_card_no;
public $bank_card_name;
public $bank_card_phone;
public $bank_card_bank;
public function rules()
{
return [
[['wechat_account', 'wechat_account_name', 'alipay_account', 'alipay_account_name', 'bank_name', 'bank_card_no', 'bank_card_name'] ,'trim'],
[['wechat_account', 'wechat_account_name', 'alipay_account', 'alipay_account_name', 'bank_name', 'bank_card_no', 'bank_card_name','bank_card_phone','bank_card_bank'] ,'string'],
[['cx_mch_id', 'scene', 'user_id', 'cash_type',], 'integer'],
[['money'], 'number'],
[['cx_mch_id', 'user_id', 'scene'], 'required'],
[['money', 'cash_type','bank_name','bank_card_no','bank_card_name','bank_card_phone','bank_card_bank'], 'required', 'on' => 'save'],
];
}
public function attributeLabels()
{
return [
'cx_mch_id' => '平台商户ID',
'scene' => '场景类型',
'user_id' => '用户ID',
'money' => '金额',
'wechat_account' => '微信账号',
'wechat_account_name' => '微信账号持有者姓名',
'alipay_account' => '支付宝账号',
'alipay_account_name' => '支付宝账号持有者姓名',
'bank_name' => '银行名称',
'bank_card_no' => '银行卡号',
'bank_card_name' => '银行卡持有者姓名',
'cash_type' => '提现类型',
'bank_card_phone' => '手机号',
'bank_card_bank' => '开户行',
];
}
public function save()
{
if(!$this->validate()){
return $this->getModelError();
}
$model = new Cash();
$this->money = sprintf("%.2f", $this->money);
//提现检查
$res = Cash::checkCashMoney($this->user_id, $this->money, $this->scene, $this->cx_mch_id);
if($res['code'] != 0)
return $res;
$service_charge = $res['data']['service_charge']; //提现费用
$cash_setting = $res['data']['cash_setting'];
//提现类型检查
$cash_types = Cash::getCashTypeLabels();
if(!isset($cash_types[$this->cash_type]) || !$cash_types[$this->cash_type]['show'] || $cash_types[$this->cash_type]['disabled'])
return Model::asReturnError("无效提现类型");
if($this->cash_type == Cash::$cxCashTypeAuto){
//检查是否符合自动打款
$res = Cash::checkAutoTransfer($this->user_id);
if($res['code'] != 0)
return $res;
}
if($this->cash_type == Cash::$cxCashTypeWx){
if(empty($this->wechat_account))
return Model::asReturnError("微信账号不能为空");
if(empty($this->wechat_account_name))
return Model::asReturnError("微信账号持有者姓名不能为空");
$model->wechat_account = $this->wechat_account;
$model->wechat_account_name = $this->wechat_account_name;
}
if($this->cash_type == Cash::$cxCashTypeAli){
if(empty($this->alipay_account))
return Model::asReturnError("支付宝账号不能为空");
if(empty($this->alipay_account_name))
return Model::asReturnError("支付宝账号持有者姓名不能为空");
$model->alipay_account = $this->alipay_account;
$model->alipay_account_name = $this->alipay_account_name;
}
if($this->cash_type == Cash::$cxCashTypeBank){
if(empty($this->bank_name))
return Model::asReturnError("请选择银行卡所属银行");
if(empty($this->bank_card_no))
return Model::asReturnError("银行卡号不能为空");
if(empty($this->bank_card_name))
return Model::asReturnError("银行卡持有者姓名不能为空");
$model->bank_name = $this->bank_name;
$model->bank_card_no = $this->bank_card_no;
$model->bank_card_name = $this->bank_card_name;
$model->bank_card_phone = $this->bank_card_phone;
$model->bank_card_bank = $this->bank_card_bank;
}
//账户余额
$res = Cash::checkUserBalance($this->user_id, $this->scene, $this->money, $this->cx_mch_id);
if($res['code'] != 0)
return $res;
$model->cx_mch_id = $this->cx_mch_id;
$model->user_id = $this->user_id;
$model->scene = $this->scene;
$model->money = $this->money;
$model->service_charge = $service_charge;
$order_no = $this->get_order_no();
$model->order_no = $order_no;
$model->status = 0;
$model->transfer_status = 0;
if($this->cash_type == Cash::$cxCashTypeAuto){
$model->status = 1;
$model->review_time = time();
$model->review_comment = "自动打款审核";
}
$model->cash_type = $this->cash_type;
$model->pay_type = 0;
$model->is_delete = 0;
if(!$model->save())
return $this->getModelError($model);
if($model->cash_type != Cash::$cxCashTypeAuto){
return $this->apiReturnSuccess("提现申请成功");
}
//自动打款提现
$form = new CommonCashActionForm();
$form->cash_id = $model->id;
$form->cx_mch_id = $this->cx_mch_id;
$data = $form->transfer();
return $data;
}
private function get_order_no()
{
switch ($this->scene){
case Balance::$cxBalanceSceneUserWallet:
$order_type = UniqueOrderNo::ORDER_TYPE_CASH_USER_WALLET;
break;
//@TODO 其他
default:
$order_type = UniqueOrderNo::ORDER_TYPE_CASH_USER_WALLET;
}
$order_no = UniqueOrderNo::generate($order_type, "\\app\\models\\Cash");
return $order_no;
}
public function search()
{
if(!$this->validate()){
return $this->getModelError();
}
$res = Cash::checkCashSetting($this->scene, $this->cx_mch_id);
if($res['code'] != 0)
return $res;
$cash_setting = $res['data'];
//账户余额
$balance = Balance::findOne([
'cx_mch_id' => $this->cx_mch_id,
'scene' => $this->scene,
'user_id' => $this->user_id,
'is_delete' => 0,
]);
$account_balance = 0;
if($balance == null){
$res = Balance::initBalance($this->user_id, $this->scene, $this->cx_mch_id);
if($res['code'] != 0)
return $res;
} else {
// $account_balance = $balance->account_balance;
$account_balance = $balance->account_balance_with;
}
$cash_types = [];
foreach($cash_setting['cash_types'] as $key => $val){
if(!$val['show'] || $val['disabled'] == true)
continue;
array_push($cash_types,$val);
}
$cash_setting['cash_types'] = $cash_types;
$data = [
'cash_setting' => $cash_setting,
'account_balance' => substr(sprintf("%.3f",$account_balance),0,-1),
];
return $this->apiReturnSuccess("ok", $data);
}
}