DistProduct.php 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. <?php
  2. namespace App\Distributor\Repositories;
  3. use App\Traits\DistSlugTrait;
  4. use Dcat\Admin\Form;
  5. use App\Models\DistProduct as Model;
  6. use Dcat\Admin\Repositories\EloquentRepository;
  7. class DistProduct extends EloquentRepository
  8. {
  9. use DistSlugTrait;
  10. /**
  11. * Model.
  12. *
  13. * @var string
  14. */
  15. protected $eloquentClass = Model::class;
  16. public function delete(Form $form, array $originalData)
  17. {
  18. collect(explode(',', $form->getKey()))->filter()->each(function ($id) {
  19. $product = Model::find($id);
  20. if ($product) {
  21. // 验证 dist_id
  22. if ($product->dist_id !== getDistributorId()) {
  23. throw new \Exception('Unable to modify the product because the distributor ID does not match.');
  24. return;
  25. }
  26. Model::find($id)->images()->delete();
  27. Model::find($id)->delete();
  28. }
  29. });
  30. return true;
  31. }
  32. public static function findById($id)
  33. {
  34. return Model::find($id); // 查找并返回相应的记录
  35. }
  36. /*
  37. * 查找最新的N个产品
  38. */
  39. public static function selectOptionsNew($limit=30)
  40. {
  41. return Model::where('dist_id', getDistributorId())->where('enabled', 1)->where('status', 2)->orderBy('created_at', 'desc')->limit($limit)->pluck('title', 'id');
  42. }
  43. /*
  44. * 获取一个标签
  45. */
  46. public static function getOneById($id)
  47. {
  48. return Model::where('id', $id)->where('dist_id', getDistributorId())->first();
  49. }
  50. /*
  51. * 是否导入
  52. * true 已导入
  53. * false 未导入
  54. */
  55. public static function isAlbumImport($albumId) {
  56. $count = Model::where('album_id', $albumId)->where('dist_id', getDistributorId())->count();
  57. if ($count > 0) {
  58. return true;
  59. }
  60. return false;
  61. }
  62. /*
  63. * 查询已导入的相册IDS
  64. */
  65. public static function getImportAlbumIds() {
  66. $albumIds = Model::where('dist_id', getDistributorId())->where('album_id', '!=', '')->distinct('album_id')->pluck('album_id');
  67. if ($albumIds->count() > 0) {
  68. return $albumIds->toArray();
  69. } else {
  70. return [];
  71. }
  72. }
  73. }