完善生产查询与登录权限配置
This commit is contained in:
+98
@@ -0,0 +1,98 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Database\Schema\Builder;
|
||||
|
||||
return new class {
|
||||
public function up(Builder $schema): void
|
||||
{
|
||||
$table = 'produce_repair_record';
|
||||
if (!$schema->hasTable($table)) {
|
||||
return;
|
||||
}
|
||||
|
||||
$hasProcessId = $schema->hasColumn($table, 'process_id');
|
||||
$hasStepId = $schema->hasColumn($table, 'step_id');
|
||||
$hasProcessName = $schema->hasColumn($table, 'process_name');
|
||||
$hasStepName = $schema->hasColumn($table, 'step_name');
|
||||
|
||||
$schema->table($table, function (Blueprint $blueprint) use ($hasProcessId, $hasStepId, $hasProcessName, $hasStepName): void {
|
||||
if (!$hasProcessId) {
|
||||
$blueprint->bigInteger('process_id')->nullable()->comment('维修工序ID快照');
|
||||
$blueprint->index('process_id', 'idx_repair_record_process_id');
|
||||
}
|
||||
if (!$hasStepId) {
|
||||
$blueprint->bigInteger('step_id')->nullable()->comment('维修步骤ID快照');
|
||||
$blueprint->index('step_id', 'idx_repair_record_step_id');
|
||||
}
|
||||
if (!$hasProcessName) {
|
||||
$blueprint->string('process_name', 100)->nullable()->comment('维修工序名称快照');
|
||||
}
|
||||
if (!$hasStepName) {
|
||||
$blueprint->string('step_name', 100)->nullable()->comment('维修步骤名称快照');
|
||||
}
|
||||
});
|
||||
|
||||
$this->backfillSnapshots($schema);
|
||||
}
|
||||
|
||||
private function backfillSnapshots(Builder $schema): void
|
||||
{
|
||||
$connection = $schema->getConnection();
|
||||
if (!$schema->hasTable('produce_exception_record')) {
|
||||
$connection->table('produce_repair_record')->update([
|
||||
'process_name' => '历史数据未记录',
|
||||
'step_name' => '历史数据未记录',
|
||||
]);
|
||||
return;
|
||||
}
|
||||
$records = $connection->table('produce_repair_record')->get(['id', 'exception_id']);
|
||||
foreach ($records as $record) {
|
||||
$exception = $connection->table('produce_exception_record')
|
||||
->where('id', $record->exception_id)
|
||||
->first(['process_id', 'step_id']);
|
||||
$processId = $exception?->process_id;
|
||||
$stepId = $exception?->step_id;
|
||||
$processName = $processId
|
||||
? (string) ($connection->table('produce_process')->where('id', $processId)->value('process_name') ?? '')
|
||||
: '';
|
||||
$stepName = $stepId
|
||||
? (string) ($connection->table('produce_process_step')->where('id', $stepId)->value('step_name') ?? '')
|
||||
: '';
|
||||
|
||||
$connection->table('produce_repair_record')->where('id', $record->id)->update([
|
||||
'process_id' => $processId,
|
||||
'step_id' => $stepId,
|
||||
'process_name' => $processName ?: '历史数据未记录',
|
||||
'step_name' => $stepName ?: '历史数据未记录',
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
public function down(Builder $schema): void
|
||||
{
|
||||
$table = 'produce_repair_record';
|
||||
if (!$schema->hasTable($table)) {
|
||||
return;
|
||||
}
|
||||
|
||||
$schema->table($table, function (Blueprint $blueprint) use ($schema): void {
|
||||
if ($schema->hasColumn('produce_repair_record', 'process_id')) {
|
||||
$blueprint->dropIndex('idx_repair_record_process_id');
|
||||
$blueprint->dropColumn('process_id');
|
||||
}
|
||||
if ($schema->hasColumn('produce_repair_record', 'step_id')) {
|
||||
$blueprint->dropIndex('idx_repair_record_step_id');
|
||||
$blueprint->dropColumn('step_id');
|
||||
}
|
||||
if ($schema->hasColumn('produce_repair_record', 'process_name')) {
|
||||
$blueprint->dropColumn('process_name');
|
||||
}
|
||||
if ($schema->hasColumn('produce_repair_record', 'step_name')) {
|
||||
$blueprint->dropColumn('step_name');
|
||||
}
|
||||
});
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user