|
|
@@ -4,47 +4,58 @@ namespace App\Module\Mex\Logic;
|
|
|
|
|
|
use App\Module\Fund\Services\FundService;
|
|
|
use App\Module\Fund\Enums\FUND_TYPE;
|
|
|
+use App\Module\Fund\Enums\FUND_CURRENCY_TYPE;
|
|
|
use App\Module\GameItems\Services\ItemService;
|
|
|
use App\Module\Mex\Logic\MexTransactionLogic;
|
|
|
use App\Module\Mex\Logic\MexWarehouseLogic;
|
|
|
+use App\Module\Mex\Logic\FundLogic;
|
|
|
use App\Module\Mex\Enums\TransactionType;
|
|
|
use Illuminate\Support\Facades\DB;
|
|
|
use Illuminate\Support\Facades\Log;
|
|
|
|
|
|
/**
|
|
|
* 农贸市场账户流转逻辑
|
|
|
- *
|
|
|
+ *
|
|
|
* 处理用户与仓库账户之间的资金和物品流转
|
|
|
* 仓库账户USER_ID为15,调控账户USER_ID为16
|
|
|
+ * 支持多币种交易,默认使用钻石币种
|
|
|
*/
|
|
|
class MexAccountLogic
|
|
|
{
|
|
|
// 仓库账户ID
|
|
|
const WAREHOUSE_USER_ID = 15;
|
|
|
-
|
|
|
- // 调控账户ID
|
|
|
+
|
|
|
+ // 调控账户ID
|
|
|
const REGULATION_USER_ID = 16;
|
|
|
-
|
|
|
- // 默认资金类型(金币)
|
|
|
- const DEFAULT_FUND_TYPE = FUND_TYPE::FUND1;
|
|
|
|
|
|
/**
|
|
|
* 处理用户卖出订单的账户流转
|
|
|
* 用户卖出:资金从仓库转出到用户、物品从用户转入仓库
|
|
|
- *
|
|
|
+ *
|
|
|
* @param int $userId 用户ID
|
|
|
* @param int $itemId 商品ID
|
|
|
* @param int $quantity 数量
|
|
|
* @param string $price 单价
|
|
|
* @param string $totalAmount 总金额
|
|
|
* @param int $orderId 订单ID
|
|
|
+ * @param FUND_CURRENCY_TYPE|null $currencyType 币种类型,默认使用钻石
|
|
|
* @return array 操作结果
|
|
|
*/
|
|
|
- public static function processSellOrder(int $userId, int $itemId, int $quantity, string $price, string $totalAmount, int $orderId): array
|
|
|
+ public static function processSellOrder(int $userId, int $itemId, int $quantity, string $price, string $totalAmount, int $orderId, ?FUND_CURRENCY_TYPE $currencyType = null): array
|
|
|
{
|
|
|
try {
|
|
|
DB::beginTransaction();
|
|
|
-
|
|
|
+
|
|
|
+ // 获取币种类型,默认使用钻石
|
|
|
+ $currencyType = $currencyType ?? FundLogic::getDefaultCurrency();
|
|
|
+
|
|
|
+ // 获取对应的账户类型
|
|
|
+ $availableAccountType = FundLogic::getAvailableAccountType($currencyType);
|
|
|
+ if (!$availableAccountType) {
|
|
|
+ DB::rollBack();
|
|
|
+ return ['success' => false, 'message' => '不支持的币种类型'];
|
|
|
+ }
|
|
|
+
|
|
|
// 1. 验证用户是否有足够的物品
|
|
|
$checkResult = ItemService::checkItemQuantity($userId, $itemId, $quantity);
|
|
|
if (!$checkResult->success) {
|
|
|
@@ -77,8 +88,9 @@ class MexAccountLogic
|
|
|
}
|
|
|
|
|
|
// 4. 从仓库账户转出资金给用户
|
|
|
- $warehouseFundService = new FundService(self::WAREHOUSE_USER_ID, self::DEFAULT_FUND_TYPE);
|
|
|
- $fundAmount = (int)bcmul($totalAmount, '100', 0); // 转换为整数(分)
|
|
|
+ $warehouseFundService = new FundService(self::WAREHOUSE_USER_ID, $availableAccountType->value);
|
|
|
+ $precision = $currencyType->getPrecision();
|
|
|
+ $fundAmount = (int)bcmul($totalAmount, bcpow('10', $precision), 0); // 根据币种精度转换
|
|
|
|
|
|
$transferResult = $warehouseFundService->trade(
|
|
|
$userId,
|
|
|
@@ -106,6 +118,7 @@ class MexAccountLogic
|
|
|
'buyer_id' => self::WAREHOUSE_USER_ID,
|
|
|
'seller_id' => $userId,
|
|
|
'item_id' => $itemId,
|
|
|
+ 'currency_type' => $currencyType->value,
|
|
|
'quantity' => $quantity,
|
|
|
'price' => $price,
|
|
|
'total_amount' => $totalAmount,
|
|
|
@@ -147,29 +160,41 @@ class MexAccountLogic
|
|
|
/**
|
|
|
* 处理用户买入订单的账户流转
|
|
|
* 用户买入:资金从用户转入仓库、物品从仓库转出到用户
|
|
|
- *
|
|
|
+ *
|
|
|
* @param int $userId 用户ID
|
|
|
* @param int $itemId 商品ID
|
|
|
* @param int $quantity 数量
|
|
|
* @param string $price 单价
|
|
|
* @param string $totalAmount 总金额
|
|
|
* @param int $orderId 订单ID
|
|
|
+ * @param FUND_CURRENCY_TYPE|null $currencyType 币种类型,默认使用钻石
|
|
|
* @return array 操作结果
|
|
|
*/
|
|
|
- public static function processBuyOrder(int $userId, int $itemId, int $quantity, string $price, string $totalAmount, int $orderId): array
|
|
|
+ public static function processBuyOrder(int $userId, int $itemId, int $quantity, string $price, string $totalAmount, int $orderId, ?FUND_CURRENCY_TYPE $currencyType = null): array
|
|
|
{
|
|
|
try {
|
|
|
DB::beginTransaction();
|
|
|
-
|
|
|
+
|
|
|
+ // 获取币种类型,默认使用钻石
|
|
|
+ $currencyType = $currencyType ?? FundLogic::getDefaultCurrency();
|
|
|
+
|
|
|
+ // 获取对应的账户类型
|
|
|
+ $availableAccountType = FundLogic::getAvailableAccountType($currencyType);
|
|
|
+ if (!$availableAccountType) {
|
|
|
+ DB::rollBack();
|
|
|
+ return ['success' => false, 'message' => '不支持的币种类型'];
|
|
|
+ }
|
|
|
+
|
|
|
// 1. 验证仓库是否有足够的物品
|
|
|
if (!MexWarehouseLogic::checkStockSufficient($itemId, $quantity)) {
|
|
|
DB::rollBack();
|
|
|
return ['success' => false, 'message' => '仓库库存不足'];
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
// 2. 验证用户是否有足够的资金
|
|
|
- $userFundService = new FundService($userId, self::DEFAULT_FUND_TYPE);
|
|
|
- $fundAmount = (int)bcmul($totalAmount, '100', 0); // 转换为整数(分)
|
|
|
+ $userFundService = new FundService($userId, $availableAccountType->value);
|
|
|
+ $precision = $currencyType->getPrecision();
|
|
|
+ $fundAmount = (int)bcmul($totalAmount, bcpow('10', $precision), 0); // 根据币种精度转换
|
|
|
|
|
|
if ($userFundService->balance() < $fundAmount) {
|
|
|
DB::rollBack();
|
|
|
@@ -227,6 +252,7 @@ class MexAccountLogic
|
|
|
'buyer_id' => $userId,
|
|
|
'seller_id' => self::WAREHOUSE_USER_ID,
|
|
|
'item_id' => $itemId,
|
|
|
+ 'currency_type' => $currencyType->value,
|
|
|
'quantity' => $quantity,
|
|
|
'price' => $price,
|
|
|
'total_amount' => $totalAmount,
|
|
|
@@ -267,23 +293,39 @@ class MexAccountLogic
|
|
|
|
|
|
/**
|
|
|
* 检查仓库账户资金余额
|
|
|
- *
|
|
|
- * @return int 余额(分)
|
|
|
+ *
|
|
|
+ * @param FUND_CURRENCY_TYPE|null $currencyType 币种类型,默认使用钻石
|
|
|
+ * @return int 余额(按币种精度存储的整数)
|
|
|
*/
|
|
|
- public static function getWarehouseFundBalance(): int
|
|
|
+ public static function getWarehouseFundBalance(?FUND_CURRENCY_TYPE $currencyType = null): int
|
|
|
{
|
|
|
- $warehouseFundService = new FundService(self::WAREHOUSE_USER_ID, self::DEFAULT_FUND_TYPE);
|
|
|
+ $currencyType = $currencyType ?? FundLogic::getDefaultCurrency();
|
|
|
+ $availableAccountType = FundLogic::getAvailableAccountType($currencyType);
|
|
|
+
|
|
|
+ if (!$availableAccountType) {
|
|
|
+ return 0;
|
|
|
+ }
|
|
|
+
|
|
|
+ $warehouseFundService = new FundService(self::WAREHOUSE_USER_ID, $availableAccountType->value);
|
|
|
return $warehouseFundService->balance();
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 检查调控账户资金余额
|
|
|
- *
|
|
|
- * @return int 余额(分)
|
|
|
+ *
|
|
|
+ * @param FUND_CURRENCY_TYPE|null $currencyType 币种类型,默认使用钻石
|
|
|
+ * @return int 余额(按币种精度存储的整数)
|
|
|
*/
|
|
|
- public static function getRegulationFundBalance(): int
|
|
|
+ public static function getRegulationFundBalance(?FUND_CURRENCY_TYPE $currencyType = null): int
|
|
|
{
|
|
|
- $regulationFundService = new FundService(self::REGULATION_USER_ID, self::DEFAULT_FUND_TYPE);
|
|
|
+ $currencyType = $currencyType ?? FundLogic::getDefaultCurrency();
|
|
|
+ $availableAccountType = FundLogic::getAvailableAccountType($currencyType);
|
|
|
+
|
|
|
+ if (!$availableAccountType) {
|
|
|
+ return 0;
|
|
|
+ }
|
|
|
+
|
|
|
+ $regulationFundService = new FundService(self::REGULATION_USER_ID, $availableAccountType->value);
|
|
|
return $regulationFundService->balance();
|
|
|
}
|
|
|
}
|