DistProduct.php 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. <?php
  2. namespace App\Admin\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)->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. */
  53. public static function auditProduct($id , $status , $reviewReply) {
  54. $product = Model::find($id);
  55. if ($product) {
  56. $product->status = $status;
  57. $product->review_reply = $reviewReply;
  58. $product->save();
  59. }
  60. return true;
  61. }
  62. }