1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586 |
- <?php
- namespace App\Distributor\Repositories;
- use App\Traits\DistSlugTrait;
- use Dcat\Admin\Form;
- use App\Models\DistProduct as Model;
- use Dcat\Admin\Repositories\EloquentRepository;
- class DistProduct extends EloquentRepository
- {
- use DistSlugTrait;
- /**
- * 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); // 查找并返回相应的记录
- }
- /*
- * 查找最新的N个产品
- */
- public static function selectOptionsNew($limit=30)
- {
- return Model::where('dist_id', getDistributorId())->where('enabled', 1)->where('status', 2)->orderBy('created_at', 'desc')->limit($limit)->pluck('title', 'id');
- }
- /*
- * 获取一个标签
- */
- public static function getOneById($id)
- {
- return Model::where('id', $id)->where('dist_id', getDistributorId())->first();
- }
- /*
- * 是否导入
- * true 已导入
- * false 未导入
- */
- public static function isAlbumImport($albumId) {
- $count = Model::where('album_id', $albumId)->where('dist_id', getDistributorId())->count();
- if ($count > 0) {
- return true;
- }
- return false;
- }
- /*
- * 查询已导入的相册IDS
- */
- public static function getImportAlbumIds() {
- $albumIds = Model::where('dist_id', getDistributorId())->where('album_id', '!=', '')->distinct('album_id')->pluck('album_id');
- if ($albumIds->count() > 0) {
- return $albumIds->toArray();
- } else {
- return [];
- }
- }
- }
|