135 lines
4.8 KiB
PHP
Executable File
135 lines
4.8 KiB
PHP
Executable File
<?php
|
||
// +----------------------------------------------------------------------
|
||
// | likeadmin快速开发前后端分离管理后台(PHP版)
|
||
// +----------------------------------------------------------------------
|
||
// | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
|
||
// | 开源版本可自由商用,可去除界面版权logo
|
||
// | gitee下载:https://gitee.com/likeshop_gitee/likeadmin
|
||
// | github下载:https://github.com/likeshop-github/likeadmin
|
||
// | 访问官网:https://www.likeadmin.cn
|
||
// | likeadmin团队 版权所有 拥有最终解释权
|
||
// +----------------------------------------------------------------------
|
||
// | author: likeadminTeam
|
||
// +----------------------------------------------------------------------
|
||
|
||
namespace app\common\logic;
|
||
|
||
use app\common\enum\user\AccountLogEnum;
|
||
use app\common\model\account\AccountLog;
|
||
use app\common\model\user\UserAccountLog;
|
||
use app\common\model\user\User;
|
||
|
||
/**
|
||
* 账户流水记录逻辑层
|
||
* Class AccountLogLogic
|
||
* @package app\common\logic
|
||
*/
|
||
class AccountLogLogic extends BaseLogic
|
||
{
|
||
|
||
/**
|
||
* @notes 账户流水记录
|
||
* @param $userId
|
||
* @param $changeType
|
||
* @param $action
|
||
* @param $changeAmount
|
||
* @param string $sourceSn
|
||
* @param string $remark
|
||
* @param array $extra
|
||
* @return UserAccountLog|false|\think\Model
|
||
* @author 段誉
|
||
* @date 2023/2/23 12:03
|
||
*/
|
||
public static function add($userId, $changeType, $action, $changeAmount, string $sourceSn = '', string $remark = '', array $extra = [])
|
||
{
|
||
$user = User::findOrEmpty($userId);
|
||
if($user->isEmpty()) {
|
||
return false;
|
||
}
|
||
|
||
$changeObject = AccountLogEnum::getChangeObject($changeType);
|
||
if(!$changeObject) {
|
||
return false;
|
||
}
|
||
|
||
switch ($changeObject) {
|
||
// 用户余额
|
||
case AccountLogEnum::UM:
|
||
$left_amount = $user->user_money;
|
||
break;
|
||
// 其他
|
||
}
|
||
|
||
$data = [
|
||
'sn' => generate_sn(UserAccountLog::class, 'sn', 20),
|
||
'user_id' => $userId,
|
||
'change_object' => $changeObject,
|
||
'change_type' => $changeType,
|
||
'action' => $action,
|
||
'left_amount' => $left_amount,
|
||
'change_amount' => $changeAmount,
|
||
'source_sn' => $sourceSn,
|
||
'remark' => $remark,
|
||
'extra' => $extra ? json_encode($extra, JSON_UNESCAPED_UNICODE) : '',
|
||
];
|
||
return UserAccountLog::create($data);
|
||
}
|
||
|
||
/**
|
||
* Notes:记录会员账户流水,如果变动类型是成长值,且是增加的,则调用更新会员等级方法。该方法应在添加用户账户后调用
|
||
* @author: cjh 2020/12/15 11:49
|
||
* @param int $user_id 用户id
|
||
* @param float $amount 变动数量
|
||
* @param int $change_type 变动类型:1-增加;2-减少
|
||
* @param int $source_type 来源类型
|
||
* @param string $remark 说明
|
||
* @param string $source_id 来源id
|
||
* @param string $source_sn 来源单号
|
||
* @param string $extra 额外字段说明
|
||
* @return bool
|
||
* @throws \think\db\exception\DataNotFoundException
|
||
* @throws \think\db\exception\ModelNotFoundException
|
||
* @throws \think\exception\DbException
|
||
*/
|
||
public static function AccountRecord($user_id,$amount,$change_type,$source_type,$remark ='',$source_id ='',$source_sn='',$extra=''){
|
||
$user = User::get($user_id);
|
||
if(empty($user)){
|
||
return false;
|
||
}
|
||
$type = AccountLog::getChangeType($source_type);
|
||
$left_amount = '';
|
||
switch ($type){
|
||
case 'money':
|
||
$left_amount = $user->user_money;
|
||
break;
|
||
case 'integral':
|
||
$left_amount = $user->user_integral;
|
||
break;
|
||
case 'growth':
|
||
$left_amount = $user->user_growth;
|
||
break;
|
||
case 'earnings':
|
||
$left_amount = $user->earnings;
|
||
}
|
||
$account_log = new AccountLog();
|
||
$account_log->log_sn = createSn('account_log','log_sn','',4);
|
||
$account_log->user_id = $user_id;
|
||
$account_log->source_type = $source_type;
|
||
$account_log->source_id = $source_id;
|
||
$account_log->source_sn = $source_sn;
|
||
$account_log->change_amount = $amount;
|
||
$account_log->left_amount = $left_amount;
|
||
$account_log->remark = AccountLog::getRemarkDesc($source_type,$source_sn,$remark);
|
||
$account_log->extra = $extra;
|
||
$account_log->change_type = $change_type;
|
||
$account_log->create_time = time();
|
||
$account_log->save();
|
||
|
||
//更新会员等级
|
||
if($type === 'growth' && $change_type == 1){
|
||
UserLevelLogic::updateUserLevel($user_id);
|
||
}
|
||
return true;
|
||
|
||
}
|
||
} |