<?php

namespace App\Distributor\Repositories;

use Dcat\Admin\Form;
use App\Models\DistProduct as Model;
use Dcat\Admin\Repositories\EloquentRepository;

class DistProduct extends EloquentRepository
{
    /**
     * Model.
     *
     * @var string
     */
    protected $eloquentClass = Model::class;

    public function delete(Form $form, array $originalData)
    {
        collect(explode(',', $form->getKey()))->filter()->each(function ($id) {

            $product = Model::find($id);
            if ($product) {
                // 验证 dist_id
                if ($product->dist_id !== getDistributorId()) {
                    throw new \Exception('Unable to modify the product because the distributor ID does not match.');
                    return;
                }
                Model::find($id)->images()->delete();
                Model::find($id)->delete();
            }
        });
        return true;
    }

    public static function findById($id)
    {
        return Model::find($id); // 查找并返回相应的记录
    }

}
