fix: 日志统计固定使用中国时区 #1105

Merged
renshen merged 1 commits from fix/renshen-worklog-china-timezone into main 2026-11-11 18:56:54 +08:00
7 changed files with 120 additions and 13 deletions

View File

@ -39,7 +39,11 @@ class SyncDingWorklogs extends Command
$days = max((int) ($this->option('days') ?: config('dingtalk.worklog.lookback_days', 35)), 1);
$templates = $this->templates();
$to = now();
$timezone = (string) config(
'dingtalk.worklog.timezone',
config('app.platform_timezone', config('app.timezone', 'Asia/Shanghai')),
);
$to = now($timezone);
$from = $to->copy()->subDays($days)->startOfDay();
try {

View File

@ -56,6 +56,7 @@ return [
explode(',', (string) env('DINGTALK_WORKLOG_FINANCE_ONLY_TEMPLATES', '空白日志')),
))),
'display_name' => env('DINGTALK_WORKLOG_DISPLAY_NAME', '财务日报 / 日志'),
'timezone' => env('DINGTALK_WORKLOG_TIMEZONE', env('APP_TIMEZONE', 'Asia/Shanghai')),
'penalty_start_date' => env('DINGTALK_WORKLOG_PENALTY_START_DATE', '2026-11-04'),
// 江陵不参与扣分;周生辰待离职,按负责人要求从扣分明细移除。
'penalty_excluded_user_ids' => array_values(array_filter(array_map(

View File

@ -217,7 +217,7 @@ class DingWorklogMirrorRepository
public static function statisticsDate(CarbonInterface|int|string $submittedAt): Carbon
{
$submitted = self::submittedAt($submittedAt)->timezone(
(string) config('app.timezone', 'Asia/Shanghai')
self::timezone()
);
return $submitted->hour < 12
@ -227,7 +227,7 @@ class DingWorklogMirrorRepository
public static function submittedAt(CarbonInterface|int|string $value): Carbon
{
$timezone = (string) config('app.timezone', 'Asia/Shanghai');
$timezone = self::timezone();
if ($value instanceof CarbonInterface) {
return Carbon::instance($value)->timezone($timezone);
@ -245,6 +245,14 @@ class DingWorklogMirrorRepository
return Carbon::parse((string) $value, $timezone)->timezone($timezone);
}
private static function timezone(): string
{
return (string) config(
'dingtalk.worklog.timezone',
config('app.platform_timezone', config('app.timezone', 'Asia/Shanghai')),
);
}
public static function reportId(array $row): string
{
$reportId = trim((string) ($row['report_id'] ?? $row['id'] ?? ''));

View File

@ -38,7 +38,10 @@ class DingWorklogPenaltyCalculator
*/
public function summary(string $from, string $to, CarbonInterface $at): array
{
$timezone = (string) config('app.timezone', 'Asia/Shanghai');
$timezone = (string) config(
'dingtalk.worklog.timezone',
config('app.platform_timezone', config('app.timezone', 'Asia/Shanghai')),
);
$now = Carbon::instance($at)->timezone($timezone);
$requestedStart = Carbon::parse($from, $timezone)->startOfDay();
$requestedEnd = Carbon::parse($to, $timezone)->startOfDay();

View File

@ -17,12 +17,17 @@ class DingWorklogStatisticsPageData
public function page(Request $request): array
{
$latestStatisticsDate = now()->hour < 12
? now()->subDay()->startOfDay()
: now()->startOfDay();
$from = $this->date($request->query('from'))
$timezone = (string) config(
'dingtalk.worklog.timezone',
config('app.platform_timezone', config('app.timezone', 'Asia/Shanghai')),
);
$now = now($timezone);
$latestStatisticsDate = $now->hour < 12
? $now->copy()->subDays(2)->startOfDay()
: $now->copy()->subDay()->startOfDay();
$from = $this->date($request->query('from'), $timezone)
?? $latestStatisticsDate->copy()->subDays(13);
$to = $this->date($request->query('to')) ?? $latestStatisticsDate->copy();
$to = $this->date($request->query('to'), $timezone) ?? $latestStatisticsDate->copy();
if ($from->greaterThan($to)) {
[$from, $to] = [$to, $from];
}
@ -43,7 +48,7 @@ class DingWorklogStatisticsPageData
$permissionRequired = DingWorklogMirrorRepository::isPermissionError($errorCode);
$daily = [];
$selectedDate = $this->date($request->query('date'))?->toDateString();
$selectedDate = $this->date($request->query('date'), $timezone)?->toDateString();
$showDetails = $request->boolean('details');
$submissions = [];
$penalty = [
@ -78,7 +83,7 @@ class DingWorklogStatisticsPageData
$penalty = $this->penaltyCalculator->summary(
$from->toDateString(),
$to->toDateString(),
now(),
$now,
);
} catch (Throwable $exception) {
report($exception);
@ -129,7 +134,7 @@ class DingWorklogStatisticsPageData
];
}
private function date(mixed $value): ?Carbon
private function date(mixed $value, string $timezone): ?Carbon
{
$value = trim((string) $value);
if (preg_match('/^\d{4}-\d{2}-\d{2}$/', $value) !== 1) {
@ -137,7 +142,7 @@ class DingWorklogStatisticsPageData
}
try {
return Carbon::createFromFormat('!Y-m-d', $value)->startOfDay();
return Carbon::createFromFormat('!Y-m-d', $value, $timezone)->startOfDay();
} catch (Throwable) {
return null;
}

View File

@ -17,6 +17,7 @@ class DingWorklogPenaltyCalculatorTest extends TestCase
config([
'dingtalk.worklog.penalty_start_date' => '2026-10-10',
'dingtalk.worklog.penalty_excluded_user_ids' => [],
'dingtalk.worklog.timezone' => 'Asia/Shanghai',
'app.timezone' => 'Asia/Shanghai',
]);
$submitted = [];
@ -84,6 +85,7 @@ class DingWorklogPenaltyCalculatorTest extends TestCase
config([
'dingtalk.worklog.penalty_start_date' => '2026-11-04',
'dingtalk.worklog.penalty_excluded_user_ids' => [],
'dingtalk.worklog.timezone' => 'Asia/Shanghai',
'app.timezone' => 'Asia/Shanghai',
]);
$repository = Mockery::mock(DingWorklogMirrorRepository::class);
@ -123,6 +125,7 @@ class DingWorklogPenaltyCalculatorTest extends TestCase
'02591267217229',
'17097754648162977',
],
'dingtalk.worklog.timezone' => 'Asia/Shanghai',
'app.timezone' => 'Asia/Shanghai',
]);
$repository = Mockery::mock(DingWorklogMirrorRepository::class);
@ -159,6 +162,7 @@ class DingWorklogPenaltyCalculatorTest extends TestCase
config([
'dingtalk.worklog.penalty_start_date' => '2026-11-04',
'dingtalk.worklog.penalty_excluded_user_ids' => [],
'dingtalk.worklog.timezone' => 'Asia/Shanghai',
'app.timezone' => 'Asia/Shanghai',
]);
$repository = Mockery::mock(DingWorklogMirrorRepository::class);
@ -200,6 +204,7 @@ class DingWorklogPenaltyCalculatorTest extends TestCase
config([
'dingtalk.worklog.penalty_start_date' => '2026-11-04',
'dingtalk.worklog.penalty_excluded_user_ids' => [],
'dingtalk.worklog.timezone' => 'Asia/Shanghai',
'app.timezone' => 'Asia/Shanghai',
]);
$repository = Mockery::mock(DingWorklogMirrorRepository::class);
@ -227,6 +232,42 @@ class DingWorklogPenaltyCalculatorTest extends TestCase
$this->assertSame('0', $result['total_points']);
}
public function test_worklog_timezone_does_not_follow_company_timezone(): void
{
config([
'dingtalk.worklog.penalty_start_date' => '2026-11-04',
'dingtalk.worklog.penalty_excluded_user_ids' => [],
'dingtalk.worklog.timezone' => 'Asia/Shanghai',
'app.timezone' => 'Europe/London',
'app.platform_timezone' => 'Asia/Shanghai',
]);
$repository = Mockery::mock(DingWorklogMirrorRepository::class);
$repository->shouldReceive('earliestStatisticsDate')->once()->andReturn('2026-11-04');
$repository->shouldReceive('submittedDatesByUser')
->once()->with('2026-11-10', '2026-11-10')->andReturn([]);
$roster = Mockery::mock(FinanceRoster::class);
$roster->shouldReceive('cachedFinanceMembers')->once()->andReturn([
['userid' => 'shanshi', 'name' => '山时'],
]);
$attendance = Mockery::mock(DingAttendanceMirrorRepository::class);
$attendance->shouldReceive('ready')->once()->andReturnTrue();
$attendance->shouldReceive('scheduleReady')->once()->andReturnTrue();
$attendance->shouldReceive('exemptDatesForUsers')->once()->andReturn([]);
$attendance->shouldReceive('scheduledWorkDatesForUsers')
->once()
->with(['shanshi'], '2026-11-10', '2026-11-10')
->andReturn(['shanshi' => ['2026-11-10' => true]]);
$result = (new DingWorklogPenaltyCalculator($repository, $roster, $attendance))->summary(
'2026-11-10',
'2026-11-10',
Carbon::parse('2026-11-11 18:51:00', 'Asia/Shanghai'),
);
$this->assertSame(['2026-11-10'], $result['members'][0]['missed_dates']);
$this->assertSame('10', $result['total_points']);
}
/** @param list<string> $userIds */
private function scheduledWorkDates(array $userIds, string $from, string $to): array
{

View File

@ -56,6 +56,51 @@ class DingWorklogStatisticsPageDataTest extends TestCase
}
}
public function test_page_uses_worklog_timezone_instead_of_company_timezone(): void
{
config([
'dingtalk.worklog.timezone' => 'Asia/Shanghai',
'app.timezone' => 'Europe/London',
'app.platform_timezone' => 'Asia/Shanghai',
]);
Carbon::setTestNow(Carbon::parse('2026-11-11 18:51:00', 'Asia/Shanghai'));
try {
$repository = Mockery::mock(DingWorklogMirrorRepository::class);
$repository->shouldReceive('available')->andReturnTrue();
$repository->shouldReceive('state')->once()->andReturn([
'status' => 'succeeded',
'last_attempted_at' => '2026-11-11 18:51:03',
'last_succeeded_at' => '2026-11-11 18:51:03',
'mirror_row_count' => 131,
'last_error_code' => null,
]);
$repository->shouldReceive('dailySummary')
->once()
->with('2026-10-28', '2026-11-10')
->andReturn([]);
$penalty = Mockery::mock(DingWorklogPenaltyCalculator::class);
$penalty->shouldReceive('summary')->once()->andReturn([
'start_date' => '2026-11-04',
'end_date' => '2026-11-10',
'total_points' => '20',
'total_points_formatted' => '20',
'members' => [],
]);
$data = (new DingWorklogStatisticsPageData($repository, $penalty))->page(
Request::create('/?from=2026-10-28&to=2026-11-10')
);
$this->assertSame('2026-11-10', $data['worklogCurrent']['statistics_date']);
$this->assertSame('2026-11-11 12:00:00', $data['worklogCutoffAt']);
$this->assertSame('20', $data['worklogPenalty']['total_points']);
} finally {
Carbon::setTestNow();
}
}
public function test_schedule_not_ready_is_reported_without_calculating_penalties(): void
{
Carbon::setTestNow(Carbon::parse('2026-11-10 13:00:00', 'Asia/Shanghai'));