70 lines
2.5 KiB
PHP
70 lines
2.5 KiB
PHP
<?php
|
|
/**
|
|
* This service file is part of item.
|
|
*
|
|
* @author ctexthuang
|
|
* @contact ctexthuang@qq.com
|
|
*/
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Service\Common\Account;
|
|
|
|
use App\Constants\Common\AccountCode;
|
|
use App\Exception\ErrException;
|
|
use App\Model\AccountDetail;
|
|
|
|
class PointOperateService extends AccountOperateInterface
|
|
{
|
|
public function inc(int $userId, string $value, int $businessCode, array $data, string $description = ''): bool
|
|
{
|
|
$this->checkNum($value);
|
|
|
|
$account = $this->userAccountModel->getAccountByUserId($userId);
|
|
|
|
if (!$account) throw new ErrException('请检查账户信息');
|
|
|
|
$insertModel = new AccountDetail();
|
|
|
|
$insertModel->user_id = $userId;
|
|
$insertModel->account_type = AccountCode::ACCOUNT_TYPE_POINT;
|
|
$insertModel->business_code = $businessCode;
|
|
$insertModel->before_num = $account->integral;
|
|
$insertModel->change_num = $value;
|
|
$insertModel->after_num = $account->integral + (float)$value;
|
|
$insertModel->track_user_id = 0;
|
|
$insertModel->track_param = json_encode($data);
|
|
$insertModel->remark = empty($description) ? AccountCode::getMessage($businessCode) : $description;
|
|
|
|
if (!$insertModel->save() || !$this->userAccountModel->incPointByUserId($userId, $value)) throw new ErrException('添加账户记录异常');
|
|
|
|
return true;
|
|
}
|
|
|
|
public function dec(int $userId, string $value, int $businessCode, array $data, string $description = ''): bool
|
|
{
|
|
$this->checkNum($value);
|
|
|
|
$account = $this->userAccountModel->getAccountByUserId($userId);
|
|
|
|
if (!$account) throw new ErrException('请检查账户信息');
|
|
|
|
if ($value > $account->integral) throw new ErrException('账户余额不足');
|
|
|
|
$insertModel = new AccountDetail();
|
|
|
|
$insertModel->user_id = $userId;
|
|
$insertModel->account_type = AccountCode::ACCOUNT_TYPE_POINT;
|
|
$insertModel->business_code = $businessCode;
|
|
$insertModel->before_num = $account->integral;
|
|
$insertModel->change_num = '-'.$value;
|
|
$insertModel->after_num = $account->integral - (float)$value;
|
|
$insertModel->track_user_id = 0;
|
|
$insertModel->track_param = json_encode($data);
|
|
$insertModel->remark = empty($description) ? AccountCode::getMessage($businessCode) : $description;
|
|
|
|
if (!$insertModel->save() || !$this->userAccountModel->decIntegralByUserId($userId, $value)) throw new ErrException('删除账户记录异常');
|
|
|
|
return true;
|
|
}
|
|
} |