ProductController.php 2.8 KB
<?php

namespace App\Http\Controllers\Bside\Product;

use App\Enums\Common\Code;
use App\Exceptions\BsideGlobalException;
use App\Helper\Arr;
use App\Http\Controllers\Bside\BaseController;
use App\Http\Logic\Bside\Product\ProductLogic;
use App\Http\Requests\Bside\Product\ProductRequest;
use App\Models\Product\CategoryRelated;
use App\Models\Product\KeywordRelated;
use App\Rules\Ids;
use Illuminate\Http\Request;

/**
 * Class ProductController
 * @package App\Http\Controllers\Bside
 * @author zbj
 * @date 2023/4/17
 */
class ProductController extends BaseController
{

    public function index(ProductLogic $logic)
    {
        $map = [];
        if(!empty($this->param['search'])){
            $map[] = ['title', 'like', "%{$this->param['search']}%"];
        }
        if(!empty($this->param['created_at'][0]) && !empty($this->param['created_at'][1])){
            $map[] = ['created_at', '>=', $this->param['created_at'][0] . ' 00:00:00'];
            $map[] = ['created_at', '<=', $this->param['created_at'][1] . ' 59:59:59'];
        }
        if(!empty($this->param['category_id'])){
            $ids = CategoryRelated::where('cate_id', $this->param['category_id'])->pluck('product_id')->toArray();
            $map[] =  ['id', 'in', $ids];
        }
        if(!empty($this->param['keyword_id'])){
            $ids = KeywordRelated::where('keyword_id', $this->param['keyword_id'])->pluck('product_id')->toArray();
            $map[] =  ['id', 'in', $ids];
        }
        if(isset($this->param['status'])){
            $map[] =  ['status', $this->param['status']];
        }
        $sort = ['id' => 'desc'];
        $data = $logic->getList($map, $sort, ['id', 'title', 'thumb', 'category_id', 'keyword_id', 'status', 'created_uid', 'created_at', 'updated_at']);
        return $this->success($data);
    }

    public function info(Request $request, ProductLogic $logic){
        $request->validate([
            'id'=>'required'
        ],[
            'id.required' => 'ID不能为空'
        ]);
        $data = $logic->getInfo($this->param['id']);
        return $this->success(Arr::twoKeepKeys($data, ['id', 'title', 'gallery', 'attrs', 'category_id', 'keyword_id', 'attr_id', 'describe_id', 'intro', 'content',
            'describe', 'seo_mate', 'related_product_id', 'status', 'category_id_text', 'keyword_id_text', 'status_text', 'created_uid', 'created_uid_text', 'route']));
    }

    public function save(ProductRequest $request, ProductLogic $logic)
    {
        $data = $logic->save($this->param);
        return $this->success($data);
    }

    public function delete(Request $request, ProductLogic $logic)
    {
        $request->validate([
            'ids'=>['required', new Ids()]
        ],[
            'ids.required' => 'ID不能为空'
        ]);

        $data = $logic->delete($this->param['ids']);
        return $this->success($data);
    }

}