204 lines
6.6 KiB
PHP
204 lines
6.6 KiB
PHP
<?php
|
|
|
|
namespace App\Services;
|
|
|
|
use Illuminate\Support\Facades\DB;
|
|
use Carbon\Carbon;
|
|
|
|
class ZentaoService
|
|
{
|
|
/**
|
|
* 获取所有用户列表
|
|
*/
|
|
public function getUsers(): array
|
|
{
|
|
try {
|
|
$users = DB::connection('zentao')
|
|
->table('user')
|
|
->select('id', 'account', 'realname', 'dept')
|
|
->where('deleted', '0')
|
|
->orderBy('realname')
|
|
->get();
|
|
|
|
return $users->map(function ($user) {
|
|
return [
|
|
'id' => $user->id,
|
|
'account' => $user->account,
|
|
'realname' => $user->realname ?: $user->account,
|
|
'dept' => $user->dept,
|
|
];
|
|
})->toArray();
|
|
} catch (\Exception $e) {
|
|
throw new \Exception('连接禅道数据库失败: ' . $e->getMessage());
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 获取指定用户本周完成的任务
|
|
*/
|
|
public function getWeeklyTasks(string $account, ?string $startDate = null, ?string $endDate = null): array
|
|
{
|
|
// 默认本周
|
|
$start = $startDate ? Carbon::parse($startDate)->format('Y-m-d') . ' 00:00:00' : Carbon::now()->startOfWeek()->format('Y-m-d') . ' 00:00:00';
|
|
$end = $endDate ? Carbon::parse($endDate)->format('Y-m-d') . ' 23:59:59' : Carbon::now()->endOfWeek()->format('Y-m-d') . ' 23:59:59';
|
|
|
|
try {
|
|
// 查询本周完成的任务 - 根据finishedDate完成时间判断
|
|
$tasks = DB::connection('zentao')
|
|
->table('task')
|
|
->leftJoin('project', 'task.project', '=', 'project.id')
|
|
->select(
|
|
'task.id',
|
|
'task.name',
|
|
'task.desc',
|
|
'task.status',
|
|
'task.finishedDate',
|
|
'task.finishedBy',
|
|
'task.assignedTo',
|
|
'task.estimate',
|
|
'task.consumed',
|
|
'task.pri',
|
|
'project.name as projectName'
|
|
)
|
|
->where('task.deleted', '0')
|
|
->where(function ($query) use ($account) {
|
|
// 任务完成人或指派人是该用户
|
|
$query->where('task.finishedBy', $account)
|
|
->orWhere('task.assignedTo', $account);
|
|
})
|
|
->where(function ($query) use ($start, $end) {
|
|
// 本周完成的任务(根据完成时间判断)
|
|
$query->where(function ($q) use ($start, $end) {
|
|
$q->whereIn('task.status', ['done', 'closed'])
|
|
->where('task.finishedDate', '>=', $start)
|
|
->where('task.finishedDate', '<=', $end)
|
|
->where('task.finishedDate', '!=', '0000-00-00 00:00:00');
|
|
});
|
|
})
|
|
->orderBy('task.finishedDate', 'desc')
|
|
->get();
|
|
|
|
return $tasks->map(function ($task) {
|
|
return [
|
|
'id' => $task->id,
|
|
'name' => $task->name,
|
|
'description' => $this->formatTaskDescription($task),
|
|
'status' => $this->translateStatus($task->status),
|
|
'projectName' => $task->projectName ?: '未分类',
|
|
'finishedDate' => $task->finishedDate,
|
|
'consumed' => $task->consumed,
|
|
];
|
|
})->toArray();
|
|
} catch (\Exception $e) {
|
|
throw new \Exception('获取任务失败: ' . $e->getMessage());
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 获取指定用户下周的任务(已分配但未完成)
|
|
*/
|
|
public function getNextWeekTasks(string $account, ?string $startDate = null): array
|
|
{
|
|
// 下周开始时间
|
|
$nextWeekStart = $startDate
|
|
? Carbon::parse($startDate)->addWeek()->startOfWeek()
|
|
: Carbon::now()->addWeek()->startOfWeek();
|
|
|
|
try {
|
|
// 查询未完成的任务作为下周计划
|
|
$tasks = DB::connection('zentao')
|
|
->table('task')
|
|
->leftJoin('project', 'task.project', '=', 'project.id')
|
|
->select(
|
|
'task.id',
|
|
'task.name',
|
|
'task.desc',
|
|
'task.status',
|
|
'task.deadline',
|
|
'task.estimate',
|
|
'task.pri',
|
|
'project.name as projectName'
|
|
)
|
|
->where('task.assignedTo', $account)
|
|
->where('task.deleted', '0')
|
|
->whereIn('task.status', ['wait', 'doing', 'pause'])
|
|
->orderBy('task.pri')
|
|
->orderBy('task.deadline')
|
|
->limit(20)
|
|
->get();
|
|
|
|
return $tasks->map(function ($task) {
|
|
return [
|
|
'id' => $task->id,
|
|
'name' => $task->name,
|
|
'description' => $this->formatNextWeekTaskDescription($task),
|
|
'status' => $this->translateStatus($task->status),
|
|
'projectName' => $task->projectName ?: '未分类',
|
|
'deadline' => $task->deadline,
|
|
'estimate' => $task->estimate,
|
|
];
|
|
})->toArray();
|
|
} catch (\Exception $e) {
|
|
throw new \Exception('获取下周任务失败: ' . $e->getMessage());
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 格式化任务描述
|
|
*/
|
|
private function formatTaskDescription($task): string
|
|
{
|
|
$desc = $task->name;
|
|
|
|
if ($task->projectName) {
|
|
$desc = "[{$task->projectName}] {$desc}";
|
|
}
|
|
|
|
return $desc;
|
|
}
|
|
|
|
/**
|
|
* 格式化下周任务描述
|
|
*/
|
|
private function formatNextWeekTaskDescription($task): string
|
|
{
|
|
$desc = $task->name;
|
|
|
|
if ($task->projectName) {
|
|
$desc = "[{$task->projectName}] {$desc}";
|
|
}
|
|
|
|
return $desc;
|
|
}
|
|
|
|
/**
|
|
* 翻译任务状态
|
|
*/
|
|
private function translateStatus(string $status): string
|
|
{
|
|
$map = [
|
|
'wait' => '未开始',
|
|
'doing' => '进行中',
|
|
'done' => '已完成',
|
|
'pause' => '已暂停',
|
|
'cancel' => '已取消',
|
|
'closed' => '已关闭',
|
|
];
|
|
|
|
return $map[$status] ?? $status;
|
|
}
|
|
|
|
/**
|
|
* 测试数据库连接
|
|
*/
|
|
public function testConnection(): bool
|
|
{
|
|
try {
|
|
DB::connection('zentao')->getPdo();
|
|
return true;
|
|
} catch (\Exception $e) {
|
|
return false;
|
|
}
|
|
}
|
|
}
|