feat : finish order
This commit is contained in:
67
app/Service/Common/Account/AccountOperateInterface.php
Normal file
67
app/Service/Common/Account/AccountOperateInterface.php
Normal file
@@ -0,0 +1,67 @@
|
||||
<?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\Exception\ErrException;
|
||||
use App\Model\UserAccount;
|
||||
use Hyperf\Di\Annotation\Inject;
|
||||
|
||||
abstract class AccountOperateInterface
|
||||
{
|
||||
/**
|
||||
* @var UserAccount
|
||||
*/
|
||||
#[Inject]
|
||||
protected UserAccount $userAccountModel;
|
||||
|
||||
|
||||
/**
|
||||
* 检查num是否正确
|
||||
* @param $num
|
||||
* @return void
|
||||
*/
|
||||
protected function checkNum($num): void
|
||||
{
|
||||
if ($num < 0) throw new ErrException('数值错误');
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $userId
|
||||
* @param string $value
|
||||
* @param int $businessCode
|
||||
* @param array $data
|
||||
* @param string $description
|
||||
* @return bool
|
||||
*/
|
||||
abstract public function inc(
|
||||
int $userId,
|
||||
string $value,
|
||||
int $businessCode,
|
||||
array $data,
|
||||
string $description = ''
|
||||
): bool;
|
||||
|
||||
/**
|
||||
* @param int $userId
|
||||
* @param string $value
|
||||
* @param int $businessCode
|
||||
* @param array $data
|
||||
* @param string $description
|
||||
* @return bool
|
||||
*/
|
||||
abstract public function dec(
|
||||
int $userId,
|
||||
string $value,
|
||||
int $businessCode,
|
||||
array $data,
|
||||
string $description = ''
|
||||
): bool;
|
||||
}
|
||||
24
app/Service/Common/Account/BalanceOperateService.php
Normal file
24
app/Service/Common/Account/BalanceOperateService.php
Normal file
@@ -0,0 +1,24 @@
|
||||
<?php
|
||||
/**
|
||||
* This service file is part of item.
|
||||
*
|
||||
* @author ctexthuang
|
||||
* @contact ctexthuang@qq.com
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Service\Common\Account;
|
||||
|
||||
class BalanceOperateService extends AccountOperateInterface
|
||||
{
|
||||
public function inc(int $userId, string $value, int $businessCode, array $data, string $description = '',): bool
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
public function dec(int $userId, string $value, int $businessCode, array $data, string $description = '',): bool
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
70
app/Service/Common/Account/PointOperateService.php
Normal file
70
app/Service/Common/Account/PointOperateService.php
Normal file
@@ -0,0 +1,70 @@
|
||||
<?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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user