按日期生成总账录入任务

main
Jing Li 2024-04-15 10:32:55 +08:00
parent b3177862bc
commit 826cbb379c
9 changed files with 275 additions and 2 deletions

View File

@ -0,0 +1,92 @@
<?php
namespace App\Console\Commands;
use App\Enums\PlanStatus;
use App\Enums\TaskStatus;
use App\Models\Ledger;
use App\Models\PlanLedger;
use App\Models\Store;
use App\Models\TaskLedger;
use Illuminate\Console\Command;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\DB;
class TaskLedgerGenerateCommand extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'app:task-ledger-generate {date?}';
/**
* The console command description.
*
* @var string
*/
protected $description = '生成门店每天的总账录入任务';
/**
* Execute the console command.
*/
public function handle()
{
$date = $this->argument('date');
$this->generateTasks(
$date ? Carbon::parse($date) : today()
);
}
protected function generateTasks(Carbon $datetime): void
{
/** @var \App\Models\PlanLedger */
$planable = DB::transaction(function () use ($datetime) {
$date = $datetime->format('Y-m-d');
if (! is_null($planable = PlanLedger::where('date', $date)->first())) {
return $planable;
}
/** @var \App\Models\PlanLedger */
$planable = PlanLedger::create(['date' => $date]);
$plan = $planable->plan()->create([
'name' => "{$date} 总账录入",
'plan_status' => PlanStatus::Published,
]);
return $planable->setRelation('plan', $plan);
});
Store::lazyById()->each(function (Store $store) use ($planable) {
DB::transaction(function () use ($store, $planable) {
$taskable = TaskLedger::firstOrNew([
'store_id' => $store->id,
'date' => $planable->date->format('Y-m-d'),
]);
if ($taskable->exists) {
return;
}
$taskable->save();
$ledger = Ledger::where('store_id', $store->id)
->where('date', $planable->date->format('Y-m-d'))
->first();
$taskable->task()->create([
'plan_id' => $planable->plan->id,
'name' => '总账录入',
'start_at' => $planable->date->copy()->startOfDay(),
'end_at' => $planable->date->copy()->endOfDay(),
'task_status' => $ledger ? TaskStatus::Success : TaskStatus::Pending,
'completed_at' => $ledger?->created_at,
]);
});
});
}
}

View File

@ -12,7 +12,7 @@ class Kernel extends ConsoleKernel
*/
protected function schedule(Schedule $schedule): void
{
// $schedule->command('inspire')->hourly();
$schedule->command(Commands\TaskLedgerGenerateCommand::class)->dailyAt('01:00');
}
/**

View File

@ -0,0 +1,39 @@
<?php
namespace App\Enums;
enum TaskStatus: int
{
case Pending = 1; // 待完成
case Processing = 2; // 进行中
case Success = 8; // 已完成
case Failed = 9; // 未完成
case Revoked = 10; // 已撤销/已取消
public function text(): string
{
return self::options()[$this->value];
}
public static function options(): array
{
return [
self::Pending->value => '待完成',
self::Processing->value => '进行中',
self::Success->value => '已完成',
self::Failed->value => '未完成',
self::Revoked->value => '已撤销',
];
}
public static function labelMap(): array
{
return [
self::Pending->value => '<span class="label label-primary">'.self::Pending->text().'</span>',
self::Processing->value => '<span class="label bg-pink-500">'.self::Processing->text().'</span>',
self::Success->value => '<span class="label label-success">'.self::Success->text().'</span>',
self::Failed->value => '<span class="label label-danger">'.self::Failed->text().'</span>',
self::Revoked->value => '<span class="label bg-gray-300">'.self::Revoked->text().'</span>',
];
}
}

View File

@ -0,0 +1,40 @@
<?php
namespace App\Models;
use App\Enums\TaskStatus;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\MorphTo;
class Task extends Model
{
use HasFactory;
protected $attributes = [
'task_status' => TaskStatus::Pending,
];
protected $casts = [
'start_at' => 'datetime',
'end_at' => 'datetime',
'task_status' => TaskStatus::class,
'completed_at' => 'datetime',
];
protected $fillable = [
'plan_id',
'taskable_type',
'taskable_id',
'name',
'start_at',
'end_at',
'task_status',
'completed_at',
];
public function taskable(): MorphTo
{
return $this->morphTo();
}
}

View File

@ -0,0 +1,32 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\MorphOne;
class TaskLedger extends Model
{
use HasFactory;
protected $casts = [
'date' => 'date',
];
protected $fillable = [
'store_id',
'date',
];
public function task(): MorphOne
{
return $this->morphOne(Task::class, 'taskable');
}
public function store(): BelongsTo
{
return $this->belongsTo(Store::class);
}
}

View File

@ -45,6 +45,7 @@ class AppServiceProvider extends ServiceProvider
\App\Models\PlanPerformance::class,
\App\Models\Reimbursement::class,
\App\Models\StoreMasterCommission::class,
\App\Models\TaskLedger::class,
\App\Models\EmployeePromotion::class,
\App\Models\Agreement::class,
])->mapWithKeys(fn ($model) => [(new $model)->getTable() => $model])->all()

View File

@ -13,10 +13,13 @@ return new class extends Migration
{
Schema::create('plans', function (Blueprint $table) {
$table->id();
$table->string('planable_type');
$table->unsignedBigInteger('planable_id');
$table->string('name')->comment('名称');
$table->morphs('planable');
$table->tinyInteger('plan_status')->comment('状态');
$table->timestamps();
$table->unique(['planable_type', 'planable_id'], 'planable_type');
});
}

View File

@ -0,0 +1,37 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::create('tasks', function (Blueprint $table) {
$table->id();
$table->string('taskable_type');
$table->unsignedBigInteger('taskable_id');
$table->foreignId('plan_id')->comment('任务计划');
$table->string('name')->comment('任务名称');
$table->dateTime('start_at')->nullable()->comment('开始时间');
$table->dateTime('end_at')->nullable()->comment('结束时间');
$table->tinyInteger('task_status')->comment('任务状态');
$table->dateTime('completed_at')->nullable()->comment('完成时间');
$table->timestamps();
$table->unique(['taskable_type', 'taskable_id'], 'taskable_type');
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::dropIfExists('tasks');
}
};

View File

@ -0,0 +1,29 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::create('task_ledgers', function (Blueprint $table) {
$table->id();
$table->foreignId('store_id')->comment('门店ID');
$table->date('date')->comment('日期');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::dropIfExists('task_ledgers');
}
};