DistAppearanceBak.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. <?php
  2. namespace App\Models;
  3. use Carbon\Carbon;
  4. use Dcat\Admin\Traits\HasDateTimeFormatter;
  5. use Illuminate\Database\Eloquent\Model;
  6. use Illuminate\Support\Facades\DB;
  7. use Illuminate\Support\Facades\Log;
  8. class DistAppearanceBak extends Model
  9. {
  10. use HasDateTimeFormatter;
  11. protected $table = 'dist_appearance_bak';
  12. protected $fillable = [
  13. 'original_table',
  14. 'data',
  15. 'created_at',
  16. 'updated_at',
  17. 'dist_id',
  18. 'appearance_id',
  19. 'version'
  20. ];
  21. /*
  22. * 备份数据dist_appearance_template和dist_appearance_variable到dist_appearance_bak的data字段中
  23. */
  24. public static function backupData($appearanceId,$distId)
  25. {
  26. $version = generateVersionNumber();
  27. // 获取需要备份的数据
  28. $templates = DistAppearanceTemplate::where('dist_id', $distId)->where('appearance_id', $appearanceId)->get();
  29. $variables = DistAppearanceVariable::where('dist_id', $distId)->where('appearance_id', $appearanceId)->get();
  30. // 备份模板数据
  31. foreach ($templates as $template) {
  32. self::create([
  33. 'dist_id' => $distId,
  34. 'appearance_id' => $appearanceId,
  35. 'original_table' => 'dist_appearance_template',
  36. 'data' => json_encode($template->getAttributes()),
  37. 'created_at' => Carbon::now(),
  38. 'updated_at' => Carbon::now(),
  39. 'version' => $version
  40. ]);
  41. }
  42. // 备份变量数据
  43. foreach ($variables as $variable) {
  44. self::create([
  45. 'dist_id' => $distId,
  46. 'appearance_id' => $appearanceId,
  47. 'original_table' => 'dist_appearance_variable',
  48. 'data' => json_encode($variable->getAttributes()),
  49. 'created_at' => Carbon::now(),
  50. 'updated_at' => Carbon::now(),
  51. 'version' => $version
  52. ]);
  53. }
  54. }
  55. /*
  56. * 用于恢复指定版本的备份数据
  57. * @param $version 版本号
  58. */
  59. public function restoreDataByVersion($version)
  60. {
  61. DB::beginTransaction();
  62. try {
  63. // Fetch all backup records with the specified version
  64. $backupRecords = DistAppearanceBak::where('version', $version)->get();
  65. // Group records by appearance_id and dist_id
  66. $groupedRecords = [];
  67. foreach ($backupRecords as $record) {
  68. $key = "{$record->appearance_id}-{$record->dist_id}";
  69. $groupedRecords[$key][] = $record;
  70. }
  71. // Process each group
  72. foreach ($groupedRecords as $key => $records) {
  73. list($appearanceId, $distId) = explode('-', $key);
  74. // Delete existing records in dist_appearance_template
  75. DB::table('dist_appearance_template')
  76. ->where('appearance_id', $appearanceId)
  77. ->where('dist_id', $distId)
  78. ->delete();
  79. // Delete existing records in dist_appearance_variable
  80. DB::table('dist_appearance_variable')
  81. ->where('appearance_id', $appearanceId)
  82. ->where('dist_id', $distId)
  83. ->delete();
  84. // Insert backup data into original tables
  85. foreach ($records as $record) {
  86. $originalTable = $record->original_table;
  87. $data = json_decode($record->data, true);
  88. if (!is_array($data)) {
  89. continue; // Skip if data is not an array
  90. }
  91. // Insert data into the original table
  92. DB::table($originalTable)->insert($data);
  93. }
  94. }
  95. DB::commit();
  96. } catch (\Exception $e) {
  97. DB::rollback();
  98. Log::error('Error restoring data: ' . $e->getMessage());
  99. throw $e; // Rethrow or handle the exception as needed
  100. }
  101. }
  102. }