KeywordLogic.php 3.5 KB
<?php

namespace App\Http\Logic\Bside\Product;

use App\Exceptions\BsideGlobalException;
use App\Helper\Arr;
use App\Http\Logic\Bside\BaseLogic;
use App\Models\Product\Keyword;
use App\Models\Product\KeywordRelated;
use App\Models\RouteMap\RouteMap;
use Illuminate\Support\Facades\DB;

/**
 * Class KeywordLogic
 * @package App\Http\Logic\Bside\APublicModel
 * @author zbj
 * @date 2023/4/15
 */
class KeywordLogic extends  BaseLogic
{
    public function __construct()
    {
        parent::__construct();
        $this->param = $this->requestAll;
        $this->model = new Keyword();
    }

    public function getList(array $map = [], array $sort = ['id' => 'desc'], array $columns = ['*'], int $limit = 20)
    {
        $data = parent::getList($map, $sort, $columns, $limit);
        foreach ($data['list'] as &$v){
            $v['product_num'] = $this->getProductNum($v['id']);
            $v['tdk'] = boolval($v['seo_title']) * boolval($v['seo_keywords']) * boolval($v['seo_description']);
            $v['url'] = $this->user['domain'] . $v['route'];
        }
        return $this->success($data);
    }

    public function getInfo($id)
    {
        $info = parent::getInfo($id);
        $info['url'] = $this->user['domain'] . $info['route'];
        return $this->success($info);
    }

    public function keywordSave(){
        DB::beginTransaction();
        try {
            if(isset($this->param['id']) && !empty($this->param['id'])){
                $id = $this->param['id'];
                $this->model->edit($this->param,['id'=>$this->param['id']]);
            }else{
                $this->param['project_id'] = $this->user['project_id'];
                $this->param['created_at'] = date('Y-m-d H:i:s');
                $this->param['updated_at'] = $this->param['created_at'];
                $id = $this->model->insertGetId($this->param);
            }
           //路由映射
            $route = RouteMap::setRoute($this->param['title'], RouteMap::SOURCE_PRODUCT_KEYWORD, $id, $this->user['project_id']);
           DB::commit();
        }catch (\Exception $e){
            DB::rollBack();
            $this->fail('保存失败');
        }
        //通知更新
        $this->updateNotify(['project_id'=>$this->user['project_id'], 'type'=>RouteMap::SOURCE_PRODUCT_KEYWORD, 'route'=>$route]);
        return $this->success();
    }

    public function delete($ids, $map = []){
        $ids= array_filter(Arr::splitFilterToArray($ids), 'intval');

        DB::beginTransaction();
        try {
            foreach ($ids as $id){
                $info = $this->getCacheInfo($id);
                if(!$info){
                    continue;
                }

                //是否有对应商品
                if(KeywordRelated::where('keyword_id', $id)->count()){
                    $this->fail("关键词{$info['title']}存在产品,不能删除");
                }

                //删除路由映射
                RouteMap::delRoute(RouteMap::SOURCE_PRODUCT_KEYWORD, $id, $this->user['project_id']);
            }
            parent::delete($ids);

            DB::commit();
        } catch (BsideGlobalException $e){
            DB::rollBack();
            $this->fail($e->getMessage());
        }catch (\Exception $e){
            DB::rollBack();
            $this->fail('删除失败');
        }

        return $this->success();
    }

    public function getProductNum($keyword_id){
        return KeywordRelated::where('keyword_id', $keyword_id)->distinct()->count('product_id');
    }
}