KeywordLogic.php 2.1 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\KeywordRelated;
use App\Models\RouteMap;
use App\Models\Product\Keyword;
use Illuminate\Support\Facades\DB;

/**
 * Class KeywordLogic
 * @package App\Http\Logic\Bside\Product
 * @author zbj
 * @date 2023/4/15
 */
class KeywordLogic extends  BaseLogic
{
    public function __construct()
    {
        parent::__construct();

        $this->model = new Keyword();
    }

    public function save($param){
        DB::beginTransaction();
        try {
           $res = parent::save($param);
           //路由映射
           RouteMap::setRoute($param['title'], RouteMap::SOURCE_PRODUCT_KEYWORD, $res['id'], $this->user['project_id']);
           DB::commit();
        }catch (\Exception $e){
            DB::rollBack();
            errorLog('产品关键词保存失败', $param, $e);
            $this->fail('保存失败');
        }
        return $this->success();
    }

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

        DB::beginTransaction();
        try {
            parent::delete($ids);

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

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