Files
lotteryLaravel/app/Services/AgentSettlement/AgentSettlementPeriodOpenService.php
kang d4779660c8
Some checks failed
lotterLaravel CI / test (push) Has been cancelled
lotterLaravel E2E / e2e-api (push) Has been cancelled
feat(draw): 支持多玩法provider及本地化结算时区功能
- Draw相关控制器添加provider_code和provider_name字段支持
- 允许手动录入开奖批次时指定provider_code
- RNG开奖批次生成支持多provider,分别生成对应批次数据
- DrawPublishService和DrawManualResultService中支持基于provider_code管理开奖版本和发布流程
- DrawResultViewService调整,支持按provider汇总及筛选开奖结果
- Settlement处理逻辑调整,按provider区分待结算票据及批次,分开结算
- 结算期间管理相关服务支持使用站点本地结算时区计算开账建议和日期处理
- 增加AdminSite的settlement_timezone字段及相关请求验证和数据存取支持
- 优化AdminSettlementPeriod相关代码,防止存在未结清票据时关账
- Ticket明细返回接口添加结算时区信息,提升用户时间体验
- 多处查询排序和版本号处理改进,保证多provider数据正确顺序与一致性
2026-07-09 15:48:21 +08:00

84 lines
2.7 KiB
PHP

<?php
namespace App\Services\AgentSettlement;
use App\Support\AgentSettlementPeriodWindow;
use App\Support\AgentSettlementSiteTimezone;
use Illuminate\Support\Facades\DB;
use Illuminate\Validation\ValidationException;
final class AgentSettlementPeriodOpenService
{
/**
* @param array{admin_site_id: int, period_start: string, period_end: string} $data
* @return object{id: int, admin_site_id: int, period_start: string, period_end: string, status: string}
*/
public function open(array $data): object
{
$siteId = (int) $data['admin_site_id'];
$timezone = AgentSettlementSiteTimezone::forSiteId($siteId);
[$start, $end] = AgentSettlementPeriodWindow::normalizeInputBounds(
(string) $data['period_start'],
(string) $data['period_end'],
$timezone,
);
$existingSameRange = DB::table('settlement_periods')
->where('admin_site_id', $siteId)
->where('status', 'open')
->where('period_start', $start)
->where('period_end', $end)
->orderByDesc('id')
->first();
if ($existingSameRange !== null) {
throw ValidationException::withMessages([
'period_start' => ['period_already_open'],
]);
}
$otherOpen = DB::table('settlement_periods')
->where('admin_site_id', $siteId)
->where('status', 'open')
->orderByDesc('id')
->first();
if ($otherOpen !== null) {
throw ValidationException::withMessages([
'period_start' => ['period_site_has_open'],
]);
}
if ($this->overlapsExistingPeriod($siteId, $start, $end)) {
throw ValidationException::withMessages([
'period_start' => ['period_overlaps_existing'],
]);
}
$id = (int) DB::table('settlement_periods')->insertGetId([
'admin_site_id' => $siteId,
'period_start' => $start,
'period_end' => $end,
'status' => 'open',
'created_at' => now(),
'updated_at' => now(),
]);
$row = DB::table('settlement_periods')->where('id', $id)->first();
if ($row === null) {
throw new \RuntimeException('period_insert_failed');
}
return $row;
}
private function overlapsExistingPeriod(int $siteId, string $start, string $end): bool
{
return DB::table('settlement_periods')
->where('admin_site_id', $siteId)
->where('period_start', '<=', $end)
->where('period_end', '>=', $start)
->exists();
}
}