diff --git a/app/Enums/TaskPerformanceStatus.php b/app/Enums/TaskPerformanceStatus.php
new file mode 100644
index 0000000..957a89a
--- /dev/null
+++ b/app/Enums/TaskPerformanceStatus.php
@@ -0,0 +1,11 @@
+value => '待完成',
- self::Processing->value => '进行中',
self::Success->value => '已完成',
self::Failed->value => '未完成',
- // self::Revoked->value => '已撤销',
];
}
@@ -30,10 +26,8 @@ enum TaskStatus: int
{
return [
self::Pending->value => ''.self::Pending->text().'',
- self::Processing->value => ''.self::Processing->text().'',
self::Success->value => ''.self::Success->text().'',
self::Failed->value => ''.self::Failed->text().'',
- // self::Revoked->value => ''.self::Revoked->text().'',
];
}
}
diff --git a/app/Http/Controllers/Api/Account/StorePerformanceController.php b/app/Http/Controllers/Api/Account/StorePerformanceController.php
new file mode 100644
index 0000000..e609602
--- /dev/null
+++ b/app/Http/Controllers/Api/Account/StorePerformanceController.php
@@ -0,0 +1,47 @@
+validate(
+ rules: [
+ 'month' => ['filled', 'date_format:Y-m'],
+ ],
+ attributes: [
+ 'month' => '月份',
+ ],
+ );
+
+ /** @var \Carbon\Carbon */
+ $month = Carbon::createFromFormat('Y-m', $request->input('month') ?: date('Y-m'));
+
+ /** @var \App\Models\Employee */
+ $user = $request->user();
+
+ /**
+ * 当月的业绩指标任务
+ * @var \App\Models\TaskPerformance
+ */
+ $taskPerformance = TaskPerformance::where('store_id', $user->store_id)
+ ->where('month', $month->format('Y-m'))
+ ->first();
+
+ $actualPerformance = Ledger::where('store_id', $user->store_id)
+ ->whereBetween('date', [$month->copy()->startOfMonth()->format('Y-m-d'), $month->copy()->endOfMonth()->format('Y-m-d')])
+ ->sum('sales');
+
+ return [
+ 'actual_performance' => trim_zeros($actualPerformance),
+ 'expected_performance' => trim_zeros($taskPerformance->expected_performance ?? 0),
+ ];
+ }
+}
diff --git a/app/Http/Controllers/Api/Account/TaskPerformanceController.php b/app/Http/Controllers/Api/Account/TaskPerformanceController.php
new file mode 100644
index 0000000..baa38b8
--- /dev/null
+++ b/app/Http/Controllers/Api/Account/TaskPerformanceController.php
@@ -0,0 +1,58 @@
+user();
+
+ /** @var \Illuminate\Database\Eloquent\Collection */
+ $taskPerformances = TaskPerformance::where('store_id', $user->store_id)
+ ->when($request->input('filter'), function ($query, $filter) {
+ $now = now();
+ switch ($filter) {
+ case 'future':
+ $query->whereHas('task', fn ($query) => $query->where('end_at', '>', $now));
+ break;
+ case 'history':
+ $query->whereHas('task', fn ($query) => $query->where('end_at', '<=', $now));
+ break;
+ }
+ })
+ ->get();
+
+ return $taskPerformances->map(function (TaskPerformance $taskable) {
+ return [
+ 'id' => $taskable->id,
+ 'month' => $taskable->month,
+ 'actual_performance' => trim_zeros($taskable->actual_performance),
+ 'expected_performance' => trim_zeros($taskable->expected_performance),
+ 'status' => $taskable->task_status,
+ ];
+ });
+
+ // return $taskPerformances->groupBy(fn (TaskPerformance $taskable) => Carbon::createFromFormat('Y-m', $taskable->month)->year)
+ // ->map(function (Collection $collection) {
+ // return $collection->mapWithKeys(function (TaskPerformance $taskable) {
+ // return [
+ // Carbon::createFromFormat('Y-m', $taskable->month)->month => [
+ // 'id' => $taskable->id,
+ // 'month' => $taskable->month,
+ // 'actual_performance' => trim_zeros($taskable->actual_performance),
+ // 'expected_performance' => trim_zeros($taskable->expected_performance),
+ // 'status' => $taskable->task_status,
+ // ],
+ // ];
+ // })->sortKeysDesc();
+ // });
+ }
+}
diff --git a/app/Models/TaskPerformance.php b/app/Models/TaskPerformance.php
index e3aaef7..c861e84 100644
--- a/app/Models/TaskPerformance.php
+++ b/app/Models/TaskPerformance.php
@@ -2,11 +2,14 @@
namespace App\Models;
+use App\Enums\TaskPerformanceStatus;
use App\Traits\HasDateTimeFormatter;
+use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\MorphOne;
+use Illuminate\Support\Carbon;
class TaskPerformance extends Model
{
@@ -42,4 +45,32 @@ class TaskPerformance extends Model
{
return bccomp($this->actual_performance, $this->expected_performance, 2) >= 0;
}
+
+ /**
+ * 任务状态
+ */
+ protected function taskStatus(): Attribute
+ {
+ return Attribute::make(
+ get: function (mixed $value, array $attributes) {
+ if ($this->isCompleted()) {
+ return TaskPerformanceStatus::Success;
+ }
+
+ // 任务开始时间
+ $startAt = Carbon::createFromFormat('Y-m-d H:i:s', "{$this->month}-01 00:00:00");
+ // 任务结束时间
+ $endAt = $startAt->copy()->endOfMonth();
+ // 当前时间
+ $datetime = now();
+
+ if ($datetime->lt($startAt)) {
+ return TaskPerformanceStatus::Todo;
+ } elseif ($datetime->gte($endAt)) {
+ return TaskPerformanceStatus::Failed;
+ }
+ return TaskPerformanceStatus::Processing;
+ },
+ );
+ }
}
diff --git a/routes/api.php b/routes/api.php
index f843b64..5ce4df4 100644
--- a/routes/api.php
+++ b/routes/api.php
@@ -1,6 +1,8 @@