cxfoot/models/common/integral/CommonRechargeIntegralActionForm.php
2023-10-27 14:25:12 +08:00

101 lines
3.1 KiB
PHP

<?php
/**
* @author Any
* @description KISS
* @date 2021年10月4日
* @version 1.0.0
*
* _____LOG_____
*
*/
namespace app\models\common\integral;
use app\models\Model;
use app\models\integral\Integral;
use app\models\UniqueOrderNo;
class CommonRechargeIntegralActionForm extends Model
{
public $type;
public $integral;
public $remark;
public $receipt;
public $operator_name;
public $rule_id;
public $pay_type;
public $user_id;
public $scene;
public $cx_mch_id;
public function rules()
{
return [
[['remark', 'receipt', 'operator_name'], 'trim'],
[['remark', 'receipt', 'operator_name'], 'string'],
[['type', 'cx_mch_id', 'user_id', 'rule_id'], 'integer'],
[['integral'], 'integer'],
[['type'], 'in', 'range' => [1,2]],
[['cx_mch_id', 'user_id', 'scene'], 'required'],
[['integral', 'type', 'remark', 'operator_name'], 'required', 'on' => 'recharge'],
[['integral', 'pay_type',], 'required', 'on' => 'auto_recharge'],
];
}
public function attributeLabels(){
return [
'operator_name' => '操作用户',
'type' => '类型',
'integral' => '积分',
'remark' => '备注',
'receipt' => '回执',
];
}
//后台充值
public function recharge()
{
if(!$this->validate()){
return $this->getModelError();
}
if($this->type == 1 && $this->integral <= 0){
return $this->apiReturnError('充值金额不能小于0');
}
$res = Integral::initIntegral($this->user_id, $this->scene, $this->cx_mch_id);
if($res['code'] != 0)
return $res;
$integral_model = Integral::findOne(['user_id' => $this->user_id, 'scene' => $this->scene, 'cx_mch_id' => $this->cx_mch_id, 'is_delete' => 0]);
if($integral_model == null){
return $this->apiReturnError('系统内部错误');
}
if($this->type == 2 && $this->integral > $integral_model->account_integral){
return $this->apiReturnError('账户积分不足');
}
$type_cn = $this->type == 1 ? '充值' : '扣除';
$desc = $this->operator_name . "操作后台" . $type_cn . $this->integral."积分";
$order_no = UniqueOrderNo::generate(UniqueOrderNo::ORDER_TYPE_MANUAL_RECHARGE_INTEGRAL_ORDER);
$order_type = UniqueOrderNo::getOrderTypeByOrderNo($order_no);
$ext = [
'remark' => $this->remark,
'receipt' => $this->receipt
];
$ext = json_encode($ext,JSON_UNESCAPED_UNICODE);
$t = \Yii::$app->db->beginTransaction();
$res = Integral::logger($this->user_id, $this->type, $this->integral, $desc, $order_type, $order_no, $this->scene, $ext, $this->cx_mch_id);
if($res['code'] != 0){
$t->rollBack();
return $res;
}
$t->commit();
return $this->apiReturnSuccess("操作成功");
}
}