136 lines
4.0 KiB
PHP
136 lines
4.0 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use App\Services\JiraService;
|
|
use Carbon\Carbon;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Http\JsonResponse;
|
|
|
|
class JiraController extends Controller
|
|
{
|
|
private JiraService $jiraService;
|
|
|
|
public function __construct(JiraService $jiraService)
|
|
{
|
|
$this->jiraService = $jiraService;
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
* 生成上周周报
|
|
*/
|
|
public function generateWeeklyReport(Request $request): JsonResponse
|
|
{
|
|
try {
|
|
$username = $request->input('username') ?: config('jira.default_user');
|
|
|
|
if (!$username) {
|
|
return response()->json([
|
|
'success' => false,
|
|
'message' => '请提供用户名'
|
|
], 400);
|
|
}
|
|
|
|
$report = $this->jiraService->generateWeeklyReport($username);
|
|
|
|
return response()->json([
|
|
'success' => true,
|
|
'data' => [
|
|
'report' => $report,
|
|
'username' => $username,
|
|
'generated_at' => Carbon::now()->format('Y-m-d H:i:s')
|
|
]
|
|
]);
|
|
} catch (\Exception $e) {
|
|
return response()->json([
|
|
'success' => false,
|
|
'message' => '生成周报失败: ' . $e->getMessage()
|
|
], 500);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 获取工时记录
|
|
*/
|
|
public function getWorkLogs(Request $request): JsonResponse
|
|
{
|
|
$request->validate([
|
|
'username' => 'required|string',
|
|
'start_date' => 'required|date',
|
|
'end_date' => 'required|date|after_or_equal:start_date',
|
|
]);
|
|
|
|
try {
|
|
$username = $request->input('username');
|
|
$startDate = Carbon::parse($request->input('start_date'))->startOfDay();
|
|
$endDate = Carbon::parse($request->input('end_date'))->endOfDay();
|
|
|
|
$workLogs = $this->jiraService->getWorkLogs($username, $startDate, $endDate);
|
|
|
|
return response()->json([
|
|
'success' => true,
|
|
'data' => [
|
|
'work_logs' => $workLogs->values()->toArray(),
|
|
'total_hours' => $workLogs->sum('hours'),
|
|
'total_records' => $workLogs->count(),
|
|
'date_range' => [
|
|
'start' => $startDate->format('Y-m-d'),
|
|
'end' => $endDate->format('Y-m-d')
|
|
]
|
|
]
|
|
]);
|
|
} catch (\Exception $e) {
|
|
return response()->json([
|
|
'success' => false,
|
|
'message' => '获取工时记录失败: ' . $e->getMessage()
|
|
], 500);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 获取JIRA配置信息
|
|
*/
|
|
public function getConfig(): JsonResponse
|
|
{
|
|
return response()->json([
|
|
'success' => true,
|
|
'data' => [
|
|
'default_user' => config('jira.default_user', ''),
|
|
'host' => config('jira.host', '')
|
|
]
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* 下载周报文件
|
|
*/
|
|
public function downloadWeeklyReport(Request $request)
|
|
{
|
|
try {
|
|
$username = $request->input('username') ?: config('jira.default_user');
|
|
|
|
if (!$username) {
|
|
return response()->json([
|
|
'success' => false,
|
|
'message' => '请提供用户名'
|
|
], 400);
|
|
}
|
|
|
|
$report = $this->jiraService->generateWeeklyReport($username);
|
|
$filename = sprintf('weekly_report_%s_%s.md', $username, Carbon::now()->subWeek()->format('Y-m-d'));
|
|
|
|
return response($report)
|
|
->header('Content-Type', 'text/markdown')
|
|
->header('Content-Disposition', 'attachment; filename="' . $filename . '"');
|
|
} catch (\Exception $e) {
|
|
return response()->json([
|
|
'success' => false,
|
|
'message' => '下载周报失败: ' . $e->getMessage()
|
|
], 500);
|
|
}
|
|
}
|
|
|
|
}
|