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

216 lines
7.3 KiB
PHP
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
namespace app\models\integral;
use Yii;
use app\components\SysConst;
use app\models\Model;
/**
* This is the model class for table "{{%integral}}".
*
* @property int $id ID
* @property int $cx_mch_id 平台商户ID
* @property int $scene 场景类型
* @property int $user_id 用户ID
* @property int $account_integral 账户积分
* @property int $account_total_integral 累计积分
* @property int $created_at 添加时间
* @property int $reset_at 重置时间
* @property int $updated_at 更新时间
* @property int $deleted_at 删除时间
* @property int $is_delete 是否删除0=否1=是
*/
class Integral extends \yii\db\ActiveRecord
{
const TYPE_INCOME = 1; //收入
const TYPE_PAY = 2; //支出
/**
* {@inheritdoc}
*/
public static function tableName()
{
return '{{%integral}}';
}
/**
* {@inheritdoc}
*/
public function rules()
{
return [
[['cx_mch_id', 'scene', 'user_id', 'account_integral', 'account_total_integral', 'created_at', 'reset_at', 'updated_at', 'deleted_at', 'is_delete'], 'integer'],
[['user_id'], 'required'],
];
}
/**
* {@inheritdoc}
*/
public function attributeLabels()
{
return [
'id' => 'ID',
'cx_mch_id' => '平台商户ID',
'scene' => '场景类型',
'user_id' => '用户ID',
'account_integral' => '账户积分',
'account_total_integral' => '累计积分',
'created_at' => '添加时间',
'reset_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;
}
}
/** ----- INTEGRAL SCENE START ----- */
public static $cxIntegralSceneUserIntegralWallet = 0; //积分钱包
public static function getIntegralSceneLabels()
{
return [
'0' => '积分钱包'
];
}
public static function getIntegralScene($scene)
{
$labels = self::getIntegralSceneLabels();
return isset($labels[$scene]) ? $labels[$scene] : "未知";
}
/** ----- INTEGRAL SCENE END ----- */
/**
* 用户账户初始化
* @param integer $user_id 用户ID
* @param integer $scene 场景类型
* @param integer $cx_mch_id 平台商户ID
*/
public static function initIntegral($user_id, $scene = 0, $cx_mch_id = 0)
{
$model = Integral::findOne([
'cx_mch_id' => $cx_mch_id,
'scene' => $scene,
'user_id' => $user_id,
'is_delete' => 0
]);
if($model != null)
return Model::asReturnSuccess();
$model = new Integral();
$model->cx_mch_id = $cx_mch_id;
$model->scene = $scene;
$model->user_id = $user_id;
$model->account_integral = 0;
$model->account_total_integral = 0;
$model->reset_at = 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 integer $integral 积分
* @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, $integral, $desc, $order_type, $order_no, $scene = 0, $ext = "", $cx_mch_id = 0)
{
//账户积分变动
$integral_model = Integral::findOne([
'cx_mch_id' => $cx_mch_id,
'scene' => $scene,
'user_id' => $user_id,
'is_delete' => 0
]);
if($integral_model == null){
$res = self::initIntegral($user_id, $scene, $cx_mch_id);
if($res['code'] != 0)
return $res;
return self::logger($user_id, $type, $integral, $desc, $order_type, $order_no, $scene, $ext, $cx_mch_id);
}
$integral = intval($integral);
$before_account_integral = $integral_model->account_integral;
if($type == self::TYPE_INCOME){
$integral_model->account_integral += $integral;
$integral_model->account_total_integral += $integral;
$after_account_integral = $before_account_integral + $integral;
}
if($type == self::TYPE_PAY){
$integral_model->account_integral -= $integral;
$integral_model->account_total_integral -= $integral;
$after_account_integral = $before_account_integral - $integral;
//账户不够扣的情况
if($integral_model->account_integral < 0){
$scene_cn = self::getIntegralScene($scene);
return Model::asReturnError("{$scene_cn}积分不足");
}
}
if(!$integral_model->save())
return (new Model())->getModelError($integral_model);
//账户变动日志
$integral_log = new IntegralLog();
$integral_log->cx_mch_id = $cx_mch_id;
$integral_log->scene = $scene;
$integral_log->type = $type;
$integral_log->user_id = $user_id;
$integral_log->integral = $integral;
$integral_log->before_account_integral = $before_account_integral;
$integral_log->after_account_integral = $after_account_integral;
$integral_log->desc = $desc;
$integral_log->order_type = $order_type;
$integral_log->order_no = $order_no;
$integral_log->ext = $ext;
if(!$integral_log->save()){
return (new Model())->getModelError($integral_log);
}
return Model::asReturnSuccess();
}
/**
* 用户积分钱包积分变动
* @param integer $user_id 用户ID
* @param integer $type 类型1=收入2=支出
* @param integer $integral 积分
* @param string $desc 交易描述
* @param integer $order_type 订单类型
* @param string $order_no 订单号
* @param string $ext 扩展信息JSON
* @param integer $cx_mch_id 平台商户ID
*/
public static function userIntegralWalletLog($user_id, $type, $integral, $desc, $order_type, $order_no, $ext = "", $cx_mch_id = 0)
{
$scene = self::$cxIntegralSceneUserIntegralWallet;
return self::logger($user_id, $type, $integral, $desc, $order_type, $order_no, $scene, $ext, $cx_mch_id);
}
}