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

300 lines
10 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\User;
use app\models\Balance;
use app\models\Cash;
use app\models\CashSetting;
use app\components\SysConst;
use app\components\FlashStorage;
use app\models\common\payment\Payment;
use app\models\common\payment\PaymentTransfer;
class CommonCashActionForm extends Model
{
public $cash_id;
public $cx_mch_id;
public $scene;
public $status;
public $review_comment;
public $user_id;
public function rules()
{
return [
[['review_comment'], 'trim'],
[['review_comment'], 'string'],
[['cx_mch_id', 'scene', 'status', 'user_id'], 'integer'],
[['cash_id'], 'safe'],
[['status'], 'in', 'range' => [1,2]],
[['cx_mch_id', 'cash_id'], 'required'],
[['status', ], 'required', 'on' => 'review'],
[['user_id', 'scene', ], 'required', 'on' => 'user'],
[['user_id', 'scene', 'cash_id'], 'required', 'on' => 'detail'],
];
}
public function review()
{
if(!$this->validate()){
return $this->getModelError();
}
$cache_key = "_c_c_a_f_r_{$this->cash_id}";
$cache_val = FlashStorage::getCache($cache_key);
if($cache_val){
return $this->apiReturnError("提现审核处理中");
}
FlashStorage::setCache($cache_key, $cache_key);
$model = Cash::findOne([
'cx_mch_id' => $this->cx_mch_id,
'is_delete' => 0,
'status' => 0,
'id' => $this->cash_id
]);
if($model == null){
FlashStorage::deleteCache($cache_key);
return $this->apiReturnError("提现不存在或已经删除");
}
$model->status = $this->status;
if($this->status == 2){
$model->transfer_status = 2;
}
$model->review_comment = $this->review_comment;
$model->review_time = time();
//账户余额
$res = Cash::checkUserBalance($model->user_id, $model->scene, $model->money, $model->cx_mch_id);
if($res['code'] != 0){
$model->status = 2;
$model->transfer_status = 2;
$model->review_comment = $res['msg'];
}
if(!$model->save()){
FlashStorage::deleteCache($cache_key);
return $this->getModelError($model);
}
FlashStorage::deleteCache($cache_key);
return $this->apiReturnSuccess("审核成功");
}
public function transfer()
{
if(!$this->validate()){
return $this->getModelError();
}
$cache_key = "_c_c_a_f_t_{$this->cash_id}";
$cache_val = FlashStorage::getCache($cache_key);
if($cache_val){
return $this->apiReturnError("提现转账处理中");
}
FlashStorage::setCache($cache_key, $cache_key);
$model = Cash::findOne([
'cx_mch_id' => $this->cx_mch_id,
'is_delete' => 0,
'status' => 1,
'transfer_status' => 0,
'id' => $this->cash_id
]);
if($model == null){
FlashStorage::deleteCache($cache_key);
return $this->apiReturnError("提现不存在或已经打款");
}
//账户余额
$res = Cash::checkUserBalance($model->user_id, $model->scene, $model->money, $model->cx_mch_id);
if($res['code'] != 0){
$model->status = 2;
$model->transfer_status = 2;
$model->review_time = time();
$model->review_comment = $res['msg'];
$model->save();
FlashStorage::deleteCache($cache_key);
return $res;
}
if($model->cash_type == Cash::$cxCashTypeAuto){
//自动打款
$res = $this->transfer_by_auto($model);
if($res['code'] == 0){
$model->pay_time = time();
$model->pay_type = $res['data']['transfer_type'];
}
}else if($model->cash_type == Cash::$cxCashTypeWx){
//微信线下
$res = $this->transfer_by_wx();
$model->pay_time = time();
$model->pay_type = Cash::$cxPayTypeWxpay;
}else if($model->cash_type == Cash::$cxCashTypeAli){
//支付宝线下
$res = $this->transfer_by_ali();
$model->pay_time = time();
$model->pay_type = Cash::$cxPayTypeAlipay;
}else if($model->cash_type == Cash::$cxCashTypeBank){
//银行转账线下
$res = $this->transfer_by_bank();
$model->pay_time = time();
$model->pay_type = Cash::$cxPayTypeBank;
} else {
FlashStorage::deleteCache($cache_key);
return $this->apiReturnError("无效提现类型");
}
if($res['code'] != 0){
FlashStorage::deleteCache($cache_key);
return $res;
}
$model->transfer_status = 1;
$model->save();
//转账成功后续操作
$cash_warn_form = new CommonCashWarnForm([
'cash' => $model
]);
$res = $cash_warn_form->afterTransfer();
FlashStorage::deleteCache($cache_key);
return $this->apiReturnSuccess("打款成功");
}
private function transfer_by_wx()
{
return $this->apiReturnSuccess();
}
private function transfer_by_ali()
{
return $this->apiReturnSuccess();
}
private function transfer_by_bank()
{
return $this->apiReturnSuccess();
}
private function transfer_by_auto($cash)
{
$amount = $cash->money - $cash->service_charge;
$scene_cn = Balance::getBalanceScene($cash->scene);
$title = "{$scene_cn}提现";
$user = User::findOne([
'id' => $cash->user_id,
'is_delete' => 0,
'status' => User::STATUS_NORMAL,
]);
$transfer_type = 0;
if($user->bindWxmp)
$transfer_type = PaymentTransfer::TRANSFER_TYPE_WXPAY;
//@TODO 其他转账方式
$paymentTransfer = new PaymentTransfer([
'cx_mch_id' => $cash->cx_mch_id,
'amount' => $amount,
'title' => $title,
'user' => $user,
'order_no' => $cash->order_no,
'transfer_type' => $transfer_type
]);
if(!$paymentTransfer->validate()){
return $this->getModelError($paymentTransfer);
}
try{
$payment = new Payment();
$res = $payment->transfer($paymentTransfer);
if(!isset($res['data']))
$res['data'] = [];
$res['data']['transfer_type'] = $transfer_type;
return $res;
} catch (\Exception $ex){
return $this->apiReturnError($ex->getMessage());
}
}
public function delete()
{
if(!$this->validate()){
return $this->getModelError();
}
if(is_array($this->cash_id)){
foreach($this->cash_id as $cash_id){
$model = Cash::find()
->where([
'cx_mch_id' => $this->cx_mch_id,
'is_delete' => 0,
'status' => [1,2],
'id' => $cash_id
])
->andFilterWhere([
'scene' => $this->scene,
'user_id' => $this->user_id
])
->one();
if($model == null)
continue;
if($model->status == 1 && ($model->transfer_status != 0 || $model->cash_type != 0)){
$this->apiReturnError("提现记录不可删除");
}
$model->is_delete = 1;
if(!$model->save())
return $this->getModelError($model);
}
} else {
$model = Cash::find()
->where([
'cx_mch_id' => $this->cx_mch_id,
'is_delete' => 0,
'status' => [1,2],
'id' => $this->cash_id
])
->andFilterWhere([
'scene' => $this->scene,
'user_id' => $this->user_id
])
->one();
if($model == null)
$this->apiReturnError("提现不存在或已经删除");
if($model->status == 1 && ($model->transfer_status != 0 || $model->cash_type != 0)){
$this->apiReturnError("提现记录不可删除");
}
$model->is_delete = 1;
if(!$model->save())
return $this->getModelError($model);
}
return $this->apiReturnSuccess("操作成功");
}
public function detail()
{
if(!$this->validate()){
return $this->getModelError();
}
$form = new CommonCashListForm();
$form->scenario = 'user';
$form->scene = Balance::$cxBalanceSceneUserWallet;
$form->cx_mch_id = $this->cx_mch_id;
$form->user_id = $this->user_id;
$form->cash_id = $this->cash_id;
$res = $form->search();
if($res['code'] != 0)
return $res;
if(count($res['data']) == 0)
return $this->apiReturnError('提现详情不存在');
$data = $res['data'][0];
return $this->apiReturnSuccess('ok',$data);
}
}