diff --git a/modules/EcommerceFinance/Repositories/DingWorklogMirrorRepository.php b/modules/EcommerceFinance/Repositories/DingWorklogMirrorRepository.php index 3a372d19..dbd70a56 100644 --- a/modules/EcommerceFinance/Repositories/DingWorklogMirrorRepository.php +++ b/modules/EcommerceFinance/Repositories/DingWorklogMirrorRepository.php @@ -144,12 +144,15 @@ class DingWorklogMirrorRepository * statistics_date:string, * submission_count:int, * submitter_count:int, + * late_submission_count:int, * first_submitted_at:?string, * last_submitted_at:?string * }> */ public function dailySummary(string $from, string $to): array { + $lateSubmissionCounts = $this->lateSubmissionCounts($from, $to); + return $this->connection->table(self::TABLE) ->whereBetween('statistics_date', [$from, $to]) ->groupBy('statistics_date') @@ -164,6 +167,7 @@ class DingWorklogMirrorRepository 'statistics_date' => (string) $row->statistics_date, 'submission_count' => (int) $row->submission_count, 'submitter_count' => (int) $row->submitter_count, + 'late_submission_count' => $lateSubmissionCounts[(string) $row->statistics_date] ?? 0, 'first_submitted_at' => $row->first_submitted_at ? (string) $row->first_submitted_at : null, 'last_submitted_at' => $row->last_submitted_at ? (string) $row->last_submitted_at : null, ]) @@ -171,6 +175,48 @@ class DingWorklogMirrorRepository ->all(); } + /** + * 超时日志:下一统计日中午以后提交,且该成员在目标统计日没有按时日志。 + * + * @return list + */ + public function lateSubmissionsForDate(string $date, int $limit = 200): array + { + $timezone = self::timezone(); + $targetDate = Carbon::parse($date, $timezone)->startOfDay(); + $nextStatisticsDate = $targetDate->copy()->addDay(); + $cutoffAt = $nextStatisticsDate->copy()->setTime(12, 0)->toDateTimeString(); + $normalSubmitters = $this->connection->table(self::TABLE) + ->where('statistics_date', $targetDate->toDateString()) + ->whereNotNull('creator_userid') + ->pluck('creator_userid') + ->map(static fn (mixed $userId): string => trim((string) $userId)) + ->filter() + ->unique() + ->values() + ->all(); + + $query = $this->connection->table(self::TABLE) + ->where('statistics_date', $nextStatisticsDate->toDateString()) + ->where('submitted_at', '>', $cutoffAt); + if ($normalSubmitters !== []) { + $query->whereNotIn('creator_userid', $normalSubmitters); + } + + return $query + ->orderBy('submitted_at') + ->limit(min(max($limit, 1), 500)) + ->get([ + 'report_id', + 'template_name', + 'creator_userid', + 'creator_name', + 'submitted_at', + 'statistics_date', + ]) + ->all(); + } + /** @return list */ public function submissionsForDate(string $date, int $limit = 200): array { @@ -253,6 +299,61 @@ class DingWorklogMirrorRepository ); } + /** + * @return array + */ + private function lateSubmissionCounts(string $from, string $to): array + { + $timezone = self::timezone(); + $fromDate = Carbon::parse($from, $timezone)->startOfDay(); + $toDate = Carbon::parse($to, $timezone)->startOfDay(); + $normalSubmitters = $this->connection->table(self::TABLE) + ->whereBetween('statistics_date', [$fromDate->toDateString(), $toDate->toDateString()]) + ->whereNotNull('creator_userid') + ->get(['statistics_date', 'creator_userid']); + $submittedDates = []; + foreach ($normalSubmitters as $row) { + $userId = trim((string) ($row->creator_userid ?? '')); + if ($userId !== '') { + $submittedDates[(string) $row->statistics_date][$userId] = true; + } + } + + $candidateRows = $this->connection->table(self::TABLE) + ->whereBetween('statistics_date', [ + $fromDate->copy()->addDay()->toDateString(), + $toDate->copy()->addDay()->toDateString(), + ]) + ->get(['statistics_date', 'creator_userid', 'submitted_at']); + $counts = []; + foreach ($candidateRows as $row) { + $statisticsDate = (string) $row->statistics_date; + $targetDate = Carbon::parse($statisticsDate, $timezone)->subDay(); + $targetDateKey = $targetDate->toDateString(); + if ($targetDate->lessThan($fromDate) || $targetDate->greaterThan($toDate)) { + continue; + } + + $userId = trim((string) ($row->creator_userid ?? '')); + if ($userId === '' || isset($submittedDates[$targetDateKey][$userId])) { + continue; + } + + try { + $submittedAt = self::submittedAt((string) $row->submitted_at); + } catch (Throwable) { + continue; + } + if (! $submittedAt->greaterThan($targetDate->copy()->addDay()->setTime(12, 0))) { + continue; + } + + $counts[$targetDateKey] = ($counts[$targetDateKey] ?? 0) + 1; + } + + return $counts; + } + public static function reportId(array $row): string { $reportId = trim((string) ($row['report_id'] ?? $row['id'] ?? '')); diff --git a/modules/EcommerceFinance/View/DingWorklogStatisticsPageData.php b/modules/EcommerceFinance/View/DingWorklogStatisticsPageData.php index 2faeef60..f7825ab9 100644 --- a/modules/EcommerceFinance/View/DingWorklogStatisticsPageData.php +++ b/modules/EcommerceFinance/View/DingWorklogStatisticsPageData.php @@ -50,7 +50,9 @@ class DingWorklogStatisticsPageData $daily = []; $selectedDate = $this->date($request->query('date'), $timezone)?->toDateString(); $showDetails = $request->boolean('details'); + $showLateDetails = $request->boolean('late'); $submissions = []; + $lateSubmissions = []; $penalty = [ 'start_date' => null, 'end_date' => null, @@ -73,6 +75,33 @@ class DingWorklogStatisticsPageData 'statistics_date' => (string) $row->statistics_date, ], $this->repository->submissionsForDate($selectedDate)); } + if ($showLateDetails) { + $deadline = Carbon::parse($selectedDate, $timezone)->addDay()->setTime(12, 0); + $lateSubmissions = array_map(function (object $row) use ($deadline, $selectedDate, $timezone): array { + $submittedAt = DingWorklogMirrorRepository::submittedAt((string) $row->submitted_at); + $lateSeconds = max(0, (int) $deadline->diffInSeconds($submittedAt, false)); + $duration = $this->formatDuration($lateSeconds); + + return [ + 'report_id' => (string) $row->report_id, + 'template_name' => (string) ($row->template_name ?? ''), + 'creator_userid' => (string) ($row->creator_userid ?? ''), + 'creator_name' => (string) ($row->creator_name ?? ''), + 'submitted_at' => $submittedAt->timezone($timezone)->toDateTimeString(), + 'statistics_date' => (string) $row->statistics_date, + 'late_for_date' => $selectedDate, + 'deadline_at' => $deadline->toDateTimeString(), + 'late_seconds' => $lateSeconds, + 'late_duration' => $duration, + 'reason' => sprintf( + '实际提交时间晚于截止时间 %s;该日志归入 %s 统计日,不能补记为 %s 的按时日志。扣分仍按排班和请假规则判断。', + $duration, + (string) $row->statistics_date, + $selectedDate, + ), + ]; + }, $this->repository->lateSubmissionsForDate($selectedDate)); + } } catch (Throwable $exception) { report($exception); $syncState = 'failed'; @@ -100,6 +129,7 @@ class DingWorklogStatisticsPageData 'statistics_date' => $currentDate, 'submission_count' => 0, 'submitter_count' => 0, + 'late_submission_count' => 0, 'first_submitted_at' => null, 'last_submitted_at' => null, ]; @@ -125,10 +155,13 @@ class DingWorklogStatisticsPageData 'worklogDaily' => $daily, 'worklogSubmissions' => $submissions, 'worklogShowDetails' => $showDetails, + 'worklogLateSubmissions' => $lateSubmissions, + 'worklogShowLateDetails' => $showLateDetails, 'worklogPenalty' => $penalty, 'worklogPenaltyError' => $penaltyError, 'worklogDays' => (int) $from->diffInDays($to) + 1, 'worklogTotalSubmissions' => array_sum(array_column($daily, 'submission_count')), + 'worklogTotalLateSubmissions' => array_sum(array_column($daily, 'late_submission_count')), 'worklogTemplateName' => (string) config('dingtalk.worklog.display_name', '财务日报 / 日志'), 'worklogCutoffAt' => $latestStatisticsDate->copy()->addDay()->setTime(12, 0)->toDateTimeString(), ]; @@ -147,4 +180,35 @@ class DingWorklogStatisticsPageData return null; } } + + private function formatDuration(int $seconds): string + { + if ($seconds < 60) { + return $seconds . ' 秒'; + } + + $minutes = intdiv($seconds, 60); + if ($minutes < 60) { + $remainingSeconds = $seconds % 60; + + return $remainingSeconds > 0 + ? $minutes . ' 分 ' . $remainingSeconds . ' 秒' + : $minutes . ' 分'; + } + + $hours = intdiv($minutes, 60); + $remainingMinutes = $minutes % 60; + if ($hours < 24) { + return $remainingMinutes > 0 + ? $hours . ' 小时 ' . $remainingMinutes . ' 分' + : $hours . ' 小时'; + } + + $days = intdiv($hours, 24); + $remainingHours = $hours % 24; + + return $remainingHours > 0 + ? $days . ' 天 ' . $remainingHours . ' 小时' + : $days . ' 天'; + } } diff --git a/resources/views/business/pages/other/log-statistics.blade.php b/resources/views/business/pages/other/log-statistics.blade.php index cc08bb82..049d96d9 100644 --- a/resources/views/business/pages/other/log-statistics.blade.php +++ b/resources/views/business/pages/other/log-statistics.blade.php @@ -374,7 +374,7 @@
钉钉 {{ $worklogTemplateName }} · 次日 12:00 截止 - 统计日当天提交的日志,以及次日 12:00 前补交的日志,统一计入统计日;每日中午 12:00 后提交归入下一统计日。 + 统计日当天提交的日志,以及次日 12:00 前补交的日志,统一计入统计日;每日中午 12:00 后提交归入下一统计日。下一统计日中午后提交、且该成员目标统计日没有按时日志的记录,会显示为目标统计日的超时日志。
@@ -491,6 +491,7 @@ 截止时间 提交人数 日志条数 + 超时日志数量 首次提交 最后提交 @@ -508,6 +509,14 @@ 'date' => $day['statistics_date'], 'details' => 1, ]) . '#worklog-detail'; + $lateDetailUrl = route('business.pages.show', [ + 'section' => 'other', + 'page' => 'log-statistics', + 'from' => $worklogFilters['from'], + 'to' => $worklogFilters['to'], + 'date' => $day['statistics_date'], + 'late' => 1, + ]) . '#worklog-late-detail'; @endphp $isSelected])> @@ -520,6 +529,13 @@ {{ number_format((int) $day['submission_count']) }} + + @if ((int) $day['late_submission_count'] > 0) + {{ number_format((int) $day['late_submission_count']) }} + @else + 0 + @endif + {{ $day['first_submitted_at'] ?: '-' }} {{ $day['last_submitted_at'] ?: '-' }} @@ -577,6 +593,64 @@ @endif + @if ($worklogShowLateDetails) +
+
+
+ 超时日志明细 + 解释为什么当日没有按时日志,但稍后能看到提交记录 +
+
+ {{ $worklogFilters['date'] }} + + + 收起明细 + +
+
+ @if ($worklogLateSubmissions === []) +
所选统计日暂无超时日志记录。
+ @else +
+ + + + + + + + + + + + + + + @foreach ($worklogLateSubmissions as $submission) + + + + + + + + + + + @endforeach + +
提交人漏写统计日截止时间实际提交时间超时时长判定说明日志模板日志ID
{{ $submission['creator_name'] ?: ($submission['creator_userid'] ?: '未知提交人') }}{{ $submission['late_for_date'] }}{{ $submission['deadline_at'] }}{{ $submission['submitted_at'] }}{{ $submission['late_duration'] }}{{ $submission['reason'] }}{{ $submission['template_name'] ?: $worklogTemplateName }}{{ $submission['report_id'] }}
+
+ @endif +
+ @endif + @if ($hasWorklogPenalty) diff --git a/tests/Feature/Business/PagesTest.php b/tests/Feature/Business/PagesTest.php index 9d6b2821..47ea48c7 100644 --- a/tests/Feature/Business/PagesTest.php +++ b/tests/Feature/Business/PagesTest.php @@ -108,30 +108,52 @@ class PagesTest extends FeatureTestCase 'statistics_date' => '2026-11-03', 'submission_count' => 2, 'submitter_count' => 2, + 'late_submission_count' => 1, 'first_submitted_at' => '2026-11-04 10:26:00', 'last_submitted_at' => '2026-11-04 11:20:00', ]]); + $submissionLookupDates = []; $repository->shouldReceive('submissionsForDate') ->once() - ->with('2026-11-03') - ->andReturn([ - (object) [ - 'report_id' => 'report-a', - 'template_name' => '财务日报', - 'creator_userid' => 'user-a', - 'creator_name' => '秋香', - 'submitted_at' => '2026-11-04 10:26:00', - 'statistics_date' => '2026-11-03', - ], - (object) [ - 'report_id' => 'report-b', - 'template_name' => '财务日报', - 'creator_userid' => 'user-b', - 'creator_name' => '李斯', - 'submitted_at' => '2026-11-04 11:20:00', - 'statistics_date' => '2026-11-03', - ], - ]); + ->andReturnUsing(function (string $date) use (&$submissionLookupDates): array { + $submissionLookupDates[] = $date; + + return [ + (object) [ + 'report_id' => 'report-a', + 'template_name' => '财务日报', + 'creator_userid' => 'user-a', + 'creator_name' => '秋香', + 'submitted_at' => '2026-11-04 10:26:00', + 'statistics_date' => '2026-11-03', + ], + (object) [ + 'report_id' => 'report-b', + 'template_name' => '财务日报', + 'creator_userid' => 'user-b', + 'creator_name' => '李斯', + 'submitted_at' => '2026-11-04 11:20:00', + 'statistics_date' => '2026-11-03', + ], + ]; + }); + $lateLookupDates = []; + $repository->shouldReceive('lateSubmissionsForDate') + ->once() + ->andReturnUsing(function (string $date) use (&$lateLookupDates): array { + $lateLookupDates[] = $date; + + return [ + (object) [ + 'report_id' => 'report-late', + 'template_name' => '财务日报', + 'creator_userid' => 'user-mountain', + 'creator_name' => '山时', + 'submitted_at' => '2026-11-04 12:11:33', + 'statistics_date' => '2026-11-04', + ], + ]; + }); $repository->shouldReceive('permissionApplyUrl') ->once() ->andReturn('https://open-dev.dingtalk.com/appscope/apply?content=test'); @@ -155,13 +177,19 @@ class PagesTest extends FeatureTestCase $this->app->instance(DingWorklogPenaltyCalculator::class, $penalty); try { - $this->loginAs($renshen) + $response = $this->loginAs($renshen) ->get(route('business.pages.show', [ 'section' => 'other', 'page' => 'log-statistics', + 'from' => '2026-10-21', + 'to' => '2026-11-03', 'date' => '2026-11-03', 'details' => 1, - ])) + 'late' => 1, + ])); + $this->assertSame(['2026-11-03'], $submissionLookupDates); + $this->assertSame(['2026-11-03'], $lateLookupDates); + $response ->assertOk() ->assertSee('data-log-statistics', false) ->assertSeeText('钉钉 财务日报 / 日志 · 次日 12:00 截止') @@ -178,6 +206,12 @@ class PagesTest extends FeatureTestCase ->assertSee('title="查看 2026-11-03 提交人姓名"', false) ->assertSee('title="查看 2026-11-03 日志明细"', false) ->assertSee('收起明细') + ->assertSeeText('超时日志数量') + ->assertSeeText('超时日志明细') + ->assertSee('data-worklog-late-detail', false) + ->assertSeeText('山时') + ->assertSeeText('11 分 33 秒') + ->assertSeeText('不能补记为 2026-11-03 的按时日志') ->assertSeeText('点击查看扣分明细') ->assertSeeText('点击查看提交人') ->assertSeeText('点击查看日志明细') diff --git a/tests/Unit/EcommerceFinance/DingWorklogMirrorRepositoryTest.php b/tests/Unit/EcommerceFinance/DingWorklogMirrorRepositoryTest.php index c2459acf..a5ca7838 100644 --- a/tests/Unit/EcommerceFinance/DingWorklogMirrorRepositoryTest.php +++ b/tests/Unit/EcommerceFinance/DingWorklogMirrorRepositoryTest.php @@ -138,6 +138,7 @@ class DingWorklogMirrorRepositoryTest extends TestCase 'statistics_date' => '2026-11-05', 'submission_count' => 1, 'submitter_count' => 1, + 'late_submission_count' => 0, 'first_submitted_at' => '2026-11-05 12:00:00', 'last_submitted_at' => '2026-11-05 12:00:00', ], @@ -145,6 +146,7 @@ class DingWorklogMirrorRepositoryTest extends TestCase 'statistics_date' => '2026-11-03', 'submission_count' => 2, 'submitter_count' => 2, + 'late_submission_count' => 0, 'first_submitted_at' => '2026-11-04 10:26:00', 'last_submitted_at' => '2026-11-04 11:20:00', ], @@ -159,6 +161,72 @@ class DingWorklogMirrorRepositoryTest extends TestCase } } + public function test_late_submissions_only_include_members_missing_the_target_day(): void + { + $now = Carbon::parse('2026-11-12 13:00:00', 'Asia/Shanghai'); + $this->repository->upsertReports([ + [ + 'report_id' => 'report-regular', + 'template_name' => '财务日报', + 'creator_userid' => 'user-regular', + 'creator_name' => '苏木', + 'submitted_at' => '2026-11-10 20:08:43', + 'statistics_date' => '2026-11-10', + 'remark' => null, + 'raw_payload' => '{}', + 'synced_at' => $now, + 'created_at' => $now, + 'updated_at' => $now, + ], + [ + 'report_id' => 'report-late-missing', + 'template_name' => '财务日报', + 'creator_userid' => 'user-missing', + 'creator_name' => '山时', + 'submitted_at' => '2026-11-11 12:11:33', + 'statistics_date' => '2026-11-11', + 'remark' => null, + 'raw_payload' => '{}', + 'synced_at' => $now, + 'created_at' => $now, + 'updated_at' => $now, + ], + [ + 'report_id' => 'report-next-day-regular', + 'template_name' => '财务日报', + 'creator_userid' => 'user-regular', + 'creator_name' => '苏木', + 'submitted_at' => '2026-11-11 18:10:40', + 'statistics_date' => '2026-11-11', + 'remark' => null, + 'raw_payload' => '{}', + 'synced_at' => $now, + 'created_at' => $now, + 'updated_at' => $now, + ], + [ + 'report_id' => 'report-exact-noon', + 'template_name' => '财务日报', + 'creator_userid' => 'user-noon', + 'creator_name' => '正午', + 'submitted_at' => '2026-11-11 12:00:00', + 'statistics_date' => '2026-11-11', + 'remark' => null, + 'raw_payload' => '{}', + 'synced_at' => $now, + 'created_at' => $now, + 'updated_at' => $now, + ], + ]); + + $summary = $this->repository->dailySummary('2026-11-10', '2026-11-11'); + $lateDay = collect($summary)->firstWhere('statistics_date', '2026-11-10'); + $this->assertSame(1, $lateDay['late_submission_count']); + + $details = $this->repository->lateSubmissionsForDate('2026-11-10'); + $this->assertSame(['report-late-missing'], array_column($details, 'report_id')); + } + public function test_permission_failure_is_exposed_to_the_page(): void { $this->repository->recordFailure('dingtalk_worklog_permission_required'); diff --git a/tests/Unit/EcommerceFinance/DingWorklogStatisticsPageDataTest.php b/tests/Unit/EcommerceFinance/DingWorklogStatisticsPageDataTest.php index aa8d5699..009b5472 100644 --- a/tests/Unit/EcommerceFinance/DingWorklogStatisticsPageDataTest.php +++ b/tests/Unit/EcommerceFinance/DingWorklogStatisticsPageDataTest.php @@ -30,6 +30,7 @@ class DingWorklogStatisticsPageDataTest extends TestCase 'statistics_date' => '2026-11-04', 'submission_count' => 9, 'submitter_count' => 9, + 'late_submission_count' => 1, 'first_submitted_at' => '2026-11-04 20:16:55', 'last_submitted_at' => '2026-11-05 11:32:59', ]]); @@ -56,6 +57,80 @@ class DingWorklogStatisticsPageDataTest extends TestCase } } + public function test_late_submission_details_explain_the_cutoff_overrun(): void + { + Carbon::setTestNow(Carbon::parse('2026-11-12 13:00: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-12 12:45:00', + 'last_succeeded_at' => '2026-11-12 12:45:00', + 'mirror_row_count' => 131, + 'last_error_code' => null, + ]); + $repository->shouldReceive('dailySummary')->once()->andReturn([[ + 'statistics_date' => '2026-11-10', + 'submission_count' => 10, + 'submitter_count' => 10, + 'late_submission_count' => 1, + 'first_submitted_at' => '2026-11-10 20:08:43', + 'last_submitted_at' => '2026-11-11 10:11:13', + ]]); + $repository->shouldReceive('submissionsForDate') + ->once() + ->with('2026-11-10') + ->andReturn([ + (object) [ + 'report_id' => 'report-regular', + 'template_name' => '财务日报', + 'creator_userid' => 'user-regular', + 'creator_name' => '苏木', + 'submitted_at' => '2026-11-10 20:08:43', + 'statistics_date' => '2026-11-10', + ], + ]); + $repository->shouldReceive('lateSubmissionsForDate') + ->once() + ->with('2026-11-10') + ->andReturn([ + (object) [ + 'report_id' => 'report-late', + 'template_name' => '财务日报', + 'creator_userid' => 'user-mountain', + 'creator_name' => '山时', + 'submitted_at' => '2026-11-11 12:11:33', + 'statistics_date' => '2026-11-11', + ], + ]); + + $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-11-01&to=2026-11-10&date=2026-11-10&details=1&late=1') + ); + + $this->assertTrue($data['worklogShowDetails']); + $this->assertSame('苏木', $data['worklogSubmissions'][0]['creator_name']); + $this->assertTrue($data['worklogShowLateDetails']); + $this->assertSame(1, $data['worklogTotalLateSubmissions']); + $this->assertSame('2026-11-10', $data['worklogLateSubmissions'][0]['late_for_date']); + $this->assertSame('2026-11-11 12:00:00', $data['worklogLateSubmissions'][0]['deadline_at']); + $this->assertSame('11 分 33 秒', $data['worklogLateSubmissions'][0]['late_duration']); + } finally { + Carbon::setTestNow(); + } + } + public function test_page_uses_worklog_timezone_instead_of_company_timezone(): void { config([