216 lines
6.9 KiB
PHP
216 lines
6.9 KiB
PHP
<?php
|
||
|
||
namespace app\models;
|
||
|
||
use Yii;
|
||
use app\components\SysConst;
|
||
|
||
/**
|
||
* This is the model class for table "{{%balance}}".
|
||
*
|
||
* @property int $id ID
|
||
* @property int $cx_mch_id 平台商户ID
|
||
* @property int $scene 场景类型
|
||
* @property int $user_id 用户ID
|
||
* @property float $account_balance 账户余额
|
||
* @property string|null $password 密码
|
||
* @property int $created_at 添加时间
|
||
* @property int $updated_at 更新时间
|
||
* @property int $deleted_at 删除时间
|
||
* @property int $is_delete 是否删除:0=否,1=是
|
||
*/
|
||
class Balance extends \yii\db\ActiveRecord
|
||
{
|
||
const TYPE_INCOME = 1; //收入
|
||
const TYPE_PAY = 2; //支出
|
||
|
||
/**
|
||
* {@inheritdoc}
|
||
*/
|
||
public static function tableName()
|
||
{
|
||
return '{{%balance}}';
|
||
}
|
||
|
||
/**
|
||
* {@inheritdoc}
|
||
*/
|
||
public function rules()
|
||
{
|
||
return [
|
||
[['password'], 'trim',],
|
||
[['cx_mch_id', 'scene', 'user_id', 'created_at', 'updated_at', 'deleted_at', 'is_delete'], 'integer'],
|
||
[['user_id'], 'required'],
|
||
[['account_balance'], 'number'],
|
||
[['password'], 'string', 'max' => 256],
|
||
];
|
||
}
|
||
|
||
/**
|
||
* {@inheritdoc}
|
||
*/
|
||
public function attributeLabels()
|
||
{
|
||
return [
|
||
'id' => 'ID',
|
||
'cx_mch_id' => '平台商户ID',
|
||
'scene' => '场景类型',
|
||
'user_id' => '用户ID',
|
||
'account_balance' => '账户余额',
|
||
'password' => '密码',
|
||
'created_at' => '添加时间',
|
||
'updated_at' => '更新时间',
|
||
'deleted_at' => '删除时间',
|
||
'is_delete' => '是否删除:0=否,1=是',
|
||
];
|
||
}
|
||
|
||
|
||
public function beforeSave($insert) {
|
||
if(parent::beforeSave($insert)){
|
||
if($this->isNewRecord){
|
||
$this->created_at = time();
|
||
}
|
||
$this->updated_at = time();
|
||
if($this->is_delete == 1)
|
||
$this->deleted_at = time();
|
||
return true;
|
||
} else {
|
||
return false;
|
||
}
|
||
}
|
||
|
||
/** ----- BALANCE SCENE START ----- */
|
||
|
||
public static $cxBalanceSceneUserWallet = 0; //用户钱包
|
||
|
||
|
||
public static function getBalanceSceneLabels()
|
||
{
|
||
return [
|
||
'0' => '钱包'
|
||
];
|
||
}
|
||
|
||
public static function getBalanceScene($scene)
|
||
{
|
||
$labels = self::getBalanceSceneLabels();
|
||
return isset($labels[$scene]) ? $labels[$scene] : "未知";
|
||
}
|
||
|
||
|
||
/** ----- BALANCE SCENE END ----- */
|
||
|
||
/**
|
||
* 用户账户初始化
|
||
* @param integer $user_id 用户ID
|
||
* @param integer $scene 场景类型
|
||
* @param integer $cx_mch_id 平台商户ID
|
||
*/
|
||
public static function initBalance($user_id, $scene = 0, $cx_mch_id = 0)
|
||
{
|
||
$model = Balance::findOne([
|
||
'cx_mch_id' => $cx_mch_id,
|
||
'scene' => $scene,
|
||
'user_id' => $user_id,
|
||
'is_delete' => 0
|
||
]);
|
||
if($model != null)
|
||
return Model::asReturnSuccess();
|
||
$model = new Balance();
|
||
$model->cx_mch_id = $cx_mch_id;
|
||
$model->scene = $scene;
|
||
$model->user_id = $user_id;
|
||
$model->account_balance = 0;
|
||
$model->is_delete = 0;
|
||
if(!$model->save())
|
||
return (new Model())->getModelError($model);
|
||
return Model::asReturnSuccess();
|
||
}
|
||
|
||
/**
|
||
* 用户账户余额变动
|
||
* @param integer $user_id 用户ID
|
||
* @param integer $type 类型,1=收入,2=支出
|
||
* @param number $money 金额
|
||
* @param string $desc 交易描述
|
||
* @param integer $order_type 订单类型
|
||
* @param string $order_no 订单号
|
||
* @param integer $scene 场景类型
|
||
* @param string $ext 扩展信息(JSON)
|
||
* @param integer $cx_mch_id 平台商户ID
|
||
*/
|
||
public static function logger($user_id, $type, $money, $desc, $order_type, $order_no, $scene = 0, $ext = "", $cx_mch_id = 0)
|
||
{
|
||
//账户余额变动
|
||
$balance = Balance::findOne([
|
||
'cx_mch_id' => $cx_mch_id,
|
||
'scene' => $scene,
|
||
'user_id' => $user_id,
|
||
'is_delete' => 0
|
||
]);
|
||
if($balance == null){
|
||
$res = self::initBalance($user_id, $scene, $cx_mch_id);
|
||
if($res['code'] != 0)
|
||
return $res;
|
||
return self::logger($user_id, $type, $money, $desc, $order_type, $order_no, $scene, $ext, $cx_mch_id);
|
||
}
|
||
$money = sprintf("%.2f",$money);
|
||
$before_account_balance = $balance->account_balance;
|
||
if($type == self::TYPE_INCOME){
|
||
$balance->account_balance += $money;
|
||
$after_account_balance = $before_account_balance + $money;
|
||
}
|
||
if($type == self::TYPE_PAY){
|
||
$balance->account_balance -= $money;
|
||
$after_account_balance = $before_account_balance - $money;
|
||
//账户余额不够情况
|
||
if($balance->account_balance < 0){
|
||
$scene_cn = self::getBalanceScene($scene);
|
||
return Model::asReturnError("{$scene_cn}余额不足");
|
||
}
|
||
}
|
||
if(!$balance->save())
|
||
return (new Model())->getModelError($balance);
|
||
|
||
//账户变动日志
|
||
$balance_log = new BalanceLog();
|
||
$balance_log->cx_mch_id = $cx_mch_id;
|
||
$balance_log->scene = $scene;
|
||
$balance_log->type = $type;
|
||
$balance_log->user_id = $user_id;
|
||
$balance_log->money = $money;
|
||
$balance_log->before_account_balance = $before_account_balance;
|
||
$balance_log->after_account_balance = $after_account_balance;
|
||
$balance_log->desc = $desc;
|
||
$balance_log->order_type = $order_type;
|
||
$balance_log->order_no = $order_no;
|
||
$balance_log->ext = $ext;
|
||
if(!$balance_log->save()){
|
||
return (new Model())->getModelError($balance_log);
|
||
}
|
||
return Model::asReturnSuccess();
|
||
}
|
||
|
||
|
||
/**
|
||
* 用户钱包余额变动
|
||
* @param integer $user_id 用户ID
|
||
* @param integer $type 类型,1=收入,2=支出
|
||
* @param number $money 金额
|
||
* @param string $desc 交易描述
|
||
* @param integer $order_type 订单类型
|
||
* @param string $order_no 订单号
|
||
* @param integer $scene 场景类型
|
||
* @param string $ext 扩展信息(JSON)
|
||
* @param integer $cx_mch_id 平台商户ID
|
||
*/
|
||
public static function userWalletLog($user_id, $type, $money, $desc, $order_type, $order_no, $ext = "", $cx_mch_id = 0)
|
||
{
|
||
$scene = self::$cxBalanceSceneUserWallet;
|
||
return self::logger($user_id, $type, $money, $desc, $order_type, $order_no, $scene, $ext, $cx_mch_id);
|
||
}
|
||
|
||
|
||
}
|