YzMaterialCenterSeeder.php 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. <?php
  2. use Illuminate\Database\Seeder;
  3. class YzMaterialCenterSeeder extends Seeder
  4. {
  5. protected $table = 'yz_goods_material';
  6. protected $category_table = 'yz_material_category';
  7. protected $goods_category_table = 'yz_material_goods_category';
  8. protected $uniTable = 'uni_account';//微擎
  9. public function __construct()
  10. {
  11. if (config('app.framework') == 'platform') {
  12. $this->uniTable = 'yz_uniacid_app';
  13. }
  14. }
  15. /**
  16. * Run the database seeds.
  17. *
  18. * @return void
  19. */
  20. public function run()
  21. {
  22. //获取全部平台 id
  23. $uniAccount = \Illuminate\Support\Facades\DB::table($this->uniTable)->get();
  24. //获取全部素材id,批量填充分类到表中
  25. $material_ids = \Illuminate\Support\Facades\DB::table($this->table)->select('id','uniacid')->get();
  26. $has_category_table = \Illuminate\Support\Facades\DB::table($this->category_table)->select('id')->first();
  27. $goods_category_table = \Illuminate\Support\Facades\DB::table($this->goods_category_table)->select('id')->first();
  28. $category_data = $goods_category_data = [];
  29. $i = 1;
  30. if(!empty($material_ids) && !$has_category_table && !$goods_category_table){
  31. //每个平台生成默认分类
  32. foreach ($uniAccount as $u) {
  33. $category_data[] = [
  34. 'uniacid' => $u['uniacid'],
  35. 'name' => '默认分类(一级)',
  36. 'parent_id' => 0,
  37. 'level' => 1,
  38. 'created_at' => time(),
  39. 'updated_at' => time(),
  40. ];
  41. $category_data[] = [
  42. 'uniacid' => $u['uniacid'],
  43. 'name' => '默认分类',
  44. 'parent_id' => $i,
  45. 'level' => 2,
  46. 'created_at' => time(),
  47. 'updated_at' => time(),
  48. ];
  49. foreach ($material_ids as $v){
  50. $ids = $i+1;
  51. if($v['uniacid'] == $u['uniacid']){
  52. $goods_category_data[] = [
  53. 'material_id' => $v['id'],
  54. 'category_id' => $ids,
  55. 'category_ids' => $i.",".$ids,
  56. 'created_at' => time(),
  57. 'updated_at' => time(),
  58. ];
  59. }
  60. }
  61. $i += 2;
  62. }
  63. //添加数据
  64. \Illuminate\Support\Facades\DB::table($this->category_table)->insert($category_data);
  65. \Illuminate\Support\Facades\DB::table($this->goods_category_table)->insert($goods_category_data);
  66. }
  67. }
  68. }