KeywordLogic.php
3.0 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
<?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 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->getProjectDomain() . $v['route'];
}
return $this->success($data);
}
public function getInfo($id)
{
$info = parent::getInfo($id);
$info['url'] = $this->getProjectDomain() . $info['route'];
return $this->success($info);
}
public function save($param){
DB::beginTransaction();
try {
$res = parent::save($param);
//路由映射
$route = 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('保存失败');
}
//通知更新
$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');
}
}