ProductController.php 10.9 KB
<?php

namespace App\Http\Controllers\Bside\Product;

use App\Enums\Common\Code;
use App\Exceptions\BsideGlobalException;
use App\Helper\Arr;
use App\Helper\Common;
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\Template\Setting;
use App\Models\Template\BTemplate;
use App\Models\User\User;
use App\Rules\Ids;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\DB;

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

    /**
     * @remark :列表
     * @name   :index
     * @author :lyh
     * @method :post
     * @time   :2023/8/28 16:30
     */
    public function index(Product $product)
    {
        $this->map = $this->searchParam();
        $filed = ['id', 'project_id', 'title', 'sort' ,'thumb', 'gallery' ,'product_type' , 'route' ,
            'category_id', 'keyword_id', 'status', 'created_uid', 'created_at', 'updated_at'];
        $this->order = 'sort';
        $query = $product->orderBy($this->order ,'desc');
        $query = $this->searchParam($query);
        $lists = $query->select($filed)->paginate($this->row, ['*'], 'page', $this->page);
        if(!empty($lists) && !empty($lists['list'])){
            $cate_data = $this->getCategoryList();//分类
            $key_data = $this->getKeywordsList();//关键字
            //获取当前用户选择的模版
            $templateSettingModel = new Setting();
            $info = $templateSettingModel->read(['project_id'=>$this->user['project_id']]);
            $userModel = new User();
            foreach ($lists['list'] as $k=>$v){
                $v['category_id_text'] = $this->categoryName($v['category_id'],$cate_data);
                $v['keyword_id_text'] = $this->keywordName($v['keyword_id'],$key_data);
                $v['created_uid_text'] = $userModel->getName($v['created_uid']);
                $v['is_renovation']  =  $this->getProductIsRenovation($info,$v['id']);
                $v['url'] = $this->user['domain'].$v['route'];
                $lists['list'][$k] = $v;
            }
        }
        return $this->response('success',Code::SUCCESS,$lists);
    }

    /**
     * @remark :处理列表返回参数
     * @name   :handleReturnParam
     * @author :lyh
     * @method :post
     * @time   :2023/9/14 10:01
     */
    public function searchParams(&$query){
        $query = $query->where('project_id',$this->user['project_id']);
        if (isset($this->map['category_id']) && !empty($this->map['category_id'])) {
            $str = [];
            $this->getLastLevelIds($this->map['category_id'],$str);
            $query->where(function ($subQuery) use ($str) {
                foreach ($str as $v) {
                    $subQuery->orWhereRaw("FIND_IN_SET(?, category_id) > 0", [$v]);
                }
            });
        }
        if(!empty($this->map['start_at']) && !empty($this->map['end_at'])){
            $query->whereBetween('created_at', [$this->map['start_at'],$this->map['end_at']]);
        }
        return $query;
    }

    /**
     * @remark :搜索参数处理
     * @name   :searchParam
     * @author :lyh
     * @method :post
     * @time   :2023/9/14 14:32
     */
    public function searchParam(){
        if(isset($this->map['title']) && !empty($this->map['title'])){
            $this->map['title'] = ['like','%'.$this->map['title'].'%'];
        }
        $this->map['project_id'] = $this->user['project_id'];
        return $this->map;
    }

    /**
     * @remark :查看产品是否已装修
     * @name   :getProductIsRenovation
     * @author :lyh
     * @method :post
     * @time   :2023/9/13 14:02
     */
    public function getProductIsRenovation($info,$id){
        if($info !== false){
            $webTemplateModel = new BTemplate();
            $param = [
                'source'=>2,
                'project_id'=>$this->user['project_id'],
                'source_id'=>$id,
                'template_id'=>$info['template_id']
            ];
            $templateInfo = $webTemplateModel->read($param);
            if($templateInfo !== false){
                return 1;
            }
        }
        return 0;
    }

    /**
     * @remark :获取所有分类
     * @name   :getCategoryList
     * @author :lyh
     * @method :post
     * @time   :2023/9/14 13:56
     */
    public function getCategoryList(){
        $data = Common::get_user_cache('product_category',$this->user['project_id']);
        if(empty($data)){
            $categoryModel = new Category();
            $data = [];
            $cateList = $categoryModel->list(['project_id'=>$this->user['project_id']],['id','title']);
            if(!empty($cateList)){
                foreach ($cateList as $value){
                    $data[$value['id']] = $value['title'];
                }
            }
            Common::set_user_cache($data,'product_category',$this->user['project_id']);
        }
        return $data;
    }

    /**
     * @remark :获取所有关键词
     * @name   :getCategoryList
     * @author :lyh
     * @method :post
     * @time   :2023/9/14 13:56
     */
    public function getKeywordsList(){
        $data = Common::get_user_cache('product_keyword',$this->user['project_id']);
        if(empty($data)) {
            $keywordModel = new Keyword();
            $data = [];
            $cateList = $keywordModel->list(['project_id' => $this->user['project_id']], ['id', 'title']);
            if (!empty($cateList)) {
                foreach ($cateList as $value) {
                    $data[$value['id']] = $value['title'];
                }
            }
            Common::set_user_cache($data,'product_keyword',$this->user['project_id']);
        }
        return $data;
    }

    /**
     * @remark :获取分类名称
     * @name   :categoryName
     * @author :lyh
     * @method :post
     * @time   :2023/9/14 13:58
     */
    public function categoryName($category_id,$data){
        $category_name = '';
        if(!empty($category_id) && !empty($data)){
            foreach ($category_id as $v){
                if(isset($data[$v])){
                    $category_name .= $data[$v].',';
                }
            }
            $category_name = trim($category_name,',');
        }
        return $category_name;
    }

    /**
     * @remark :获取关键词名称
     * @name   :categoryName
     * @author :lyh
     * @method :post
     * @time   :2023/9/14 13:58
     */
    public function keywordName($keyword_id,$data){
        $keyword_name = '';
        if(!empty($keyword_id) && !empty($data)){
            foreach ($keyword_id as $v){
                if(isset($data[$v])){
                    $keyword_name .= $data[$v].',';
                }
            }
            $keyword_name = trim($keyword_name,',');
        }
        return $keyword_name;
    }

    /**
     * @remark :详情
     * @name   :info
     * @author :lyh
     * @method :post
     * @time   :2023/8/21 18:12
     */
    public function info(Product $product){
        $this->request->validate([
            'id'=>'required'
        ],[
            'id.required' => 'ID不能为空'
        ]);
        $info = $product->read(['id'=>$this->param['id']]);
        $info = $this->handleParam($info);
        return $this->response('success',Code::SUCCESS,$info);
    }

    /**
     * @remark :处理列表参数
     * @name   :handleParam
     * @author :lyh
     * @method :post
     * @time   :2023/8/17 9:15
     */
    public function handleParam($v){
        $v['keyword_id_text'] = '';
        if(!empty($v['keyword_id'])){
            $keywordModel = new Keyword();
            $keyword_data = $keywordModel->list(['id'=>['in',$v['keyword_id']]]);
            foreach ($keyword_data as $v1){
                $v['keyword_id_text'] .= $v1['title'].',';
            }
            $v['keyword_id_text'] = trim($v['keyword_id_text'],',');
        }
        $v['status_text'] = Product::statusMap()[$v['status']] ?? '';
        //获取当前用户选择的模版
        $templateSettingModel = new Setting();
        $templateInfo = $templateSettingModel->read(['project_id'=>$this->user['project_id']]);
        $v['is_renovation']  =  $this->getProductIsRenovation($templateInfo,$v['id']);
        $v['url'] = $this->user['domain'].$v['route'];
        return $v;
    }


    /**
     * @remark :保存产品数据
     * @name   :save
     * @author :lyh
     * @method :post
     * @time   :2023/8/17 15:01
     */
    public function save(ProductRequest $request, ProductLogic $logic)
    {
        $request->validated();
        $logic->productSave();
        $this->response('success');
    }

    /**
     * @remark :删除
     * @name   :delete
     * @author :lyh
     * @method :post
     * @time   :2023/8/22 13:45
     */
    public function delete(ProductLogic $logic)
    {
        $this->request->validate([
            'ids'=>['required', new Ids()]
        ],[
            'ids.required' => 'ID不能为空'
        ]);
        $logic->productDelete();
        $this->response('success');
    }


    /**
     * @remark :根据状态获取数量
     * @name   :getStatusNumber
     * @author :lyh
     * @method :post
     * @time   :2023/8/21 18:33
     */
    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.required' => '分类ID不能为空',
            'status.required'=>'状态不能为空'
        ]);
        $logic->batchSetCategory();
        $this->response('success');
    }

    /**
     * @remark :修改排序
     * @name   :sort
     * @author :lyh
     * @method :post
     * @time   :2023/8/19 11:13
     */
    public function sort(ProductLogic $logic){
        $this->request->validate([
            'id'=>'required',
            'sort'=>'required'
        ],[
            'id.required' => '产品ID不能为空',
            'sort.required'=>'排序字段不能为空'
        ]);
        $logic->setSort();
        $this->response('success');
    }
}