ProductController.php 5.4 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\Category;
use App\Models\Product\CategoryRelated;
use App\Models\Product\Keyword;
use App\Models\Product\KeywordRelated;
use App\Models\Product\Product;
use App\Models\User\User;
use App\Rules\Ids;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

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

    public function index(Product $product)
    {
//        $map = [];
//        if(!empty($this->param['search'])){
//            $map[] = ['title', 'like', "%{$this->param['search']}%"];
//        }
//        if(!empty($this->param['created_at'][0])){
//            $this->param['created_at'][0] .= ' 00:00:00';
//        }
//        if(!empty($this->param['created_at'][1])){
//            $this->param['created_at'][1] .= ' 23:59:59';
//        }
//        if(empty($this->param['created_at'][0]) && !empty($this->param['created_at'][1])){
//            $map[] = ['created_at', '>=', $this->param['created_at'][0]];
//        }
//        if(!empty($this->param['created_at'][0]) && empty($this->param['created_at'][1])){
//            $map[] = ['created_at', '<=', $this->param['created_at']];
//        }
//        if(!empty($this->param['created_at'][0]) && !empty($this->param['created_at'][1])){
//            $map[] = ['created_at', 'between', [$this->param['created_at']]];
//        }
//        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'];
        $filed = ['id', 'project_id', 'title', 'thumb', 'product_type' , 'route' ,'category_id', 'keyword_id', 'status', 'created_uid', 'created_at', 'updated_at'];
        $lists = $product->lists($this->map,$this->page,$this->row,$this->order,$filed);
        if(!empty($lists['list'])){
            foreach ($lists['list'] as &$v){
                $v['thumb']['image_link'] = getImageUrl($v['thumb']['url']);//图片统一
                $v['category_id_text'] = '';
                $v['keyword_id_text'] = '';
                if(!empty($v['category_id'])){
                    $category_arr = explode(',',$v['category_id']);
                    $categoryModel = new Category();
                    $category_data = $categoryModel->list(['id'=>['in',$category_arr]]);
                }
                if(!empty($v['keyword_id'])){
                    $keyword_arr = explode(',',$v['keyword_id']);
                    $keywordModel = new Keyword();
                    $keyword_data = $keywordModel->list(['id'=>['in',$keyword_arr]]);
                }
                $v['status_text'] = Product::statusMap()[$info['status']] ?? '';
                $v['created_uid_text'] = (new User())->read(['id'=>$v['created_uid']])['name'] ?? '';
            }
        }
        return $this->response('success',Code::SUCCESS,$lists);
    }



    public function info(Request $request, ProductLogic $logic){
        $request->validate([
            'id'=>'required'
        ],[
            'id.required' => 'ID不能为空'
        ]);
        $data = $logic->getInfo($this->param['id']);
        return $this->success($data);
    }

    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);
    }


    public function getStatusNumber(ProductLogic $logic){
        $data = $logic->getStatusNumber();
        $this->response('success',Code::SUCCESS,$data);
    }

    /**
     * @remark :复制产品
     * @name   :copyProduct
     * @author :lyh
     * @method :post
     * @time   :2023/7/29 14:59
     */
    public function copyProduct(ProductLogic $logic){
        $rs = $logic->setCopyProduct();
        $this->response('success',Code::SUCCESS,$rs);
    }

    /**
     * @remark :批量设置产品分类及状态
     * @name   :batchSetCategory
     * @author :lyh
     * @method :post
     * @time   :2023/8/15 17:51
     */
    public function batchSetCategory(ProductLogic $logic){
        $this->request->validate([
            'id'=>'required',
            'category_id'=>'required',
            'status'=>'required'
        ],[
            'id.required' => '产品ID不能为空',
            'category_id' => '分类ID不能为空',
            'status'=>'状态不能为空'
        ]);
        $logic->batchSetCategory();
        $this->response('success');
    }
}