ProductLogic.php
4.3 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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
<?php
namespace App\Http\Logic\Bside\Product;
use App\Helper\Arr;
use App\Http\Logic\Bside\BaseLogic;
use App\Http\Logic\Bside\User\UserLogic;
use App\Models\Product\CategoryRelated;
use App\Models\Product\KeywordRelated;
use App\Models\Product\Product;
use App\Models\RouteMap;
use Illuminate\Support\Facades\DB;
/**
* Class ProductLogic
* @package App\Http\Logic\Bside\Product
* @author zbj
* @date 2023/4/14
*/
class ProductLogic extends BaseLogic
{
public function __construct()
{
parent::__construct();
$this->model = new Product();
}
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 = $this->formatData($v);
}
return $this->success($data);
}
public function getInfo($id)
{
$info = parent::getInfo($id);
$info = $this->formatData($info);
return $this->success($info);
}
public function formatData($info){
foreach ($info['category_id'] as $category_id) {
$info['category_id_text'][] = (new CategoryLogic())->getCacheInfo($category_id)['title'] ?? '';
}
foreach ($info['keyword_id'] as $keyword_id){
$info['keyword_id_text'][] =(new KeywordLogic())->getCacheInfo($keyword_id)['title']??'';
}
$info['category_id_text'] = Arr::arrToSet($info['category_id_text'], 'trim');
$info['keyword_id_text'] = Arr::arrToSet($info['keyword_id_text'], 'trim');
$info['status_text'] = Product::statusMap()[$info['status']] ?? '';
$info['created_uid_text'] = (new UserLogic())->getCacheInfo($info['created_uid'])['name'] ?? '';
$info['url'] = $this->getProjectDomain() . $info['route'] ;
return $info;
}
public function save($param){
//封面取第一个图片
$param['thumb'] = $param['gallery'][0] ?? '';
DB::beginTransaction();
try {
$data = $param;
unset($data['route']);
$data['created_uid'] = $this->user['id'];
$res = parent::save($data);
//关联分类
CategoryRelated::saveRelated($res['id'], $data['category_id']);
//关联关键词
KeywordRelated::saveRelated($res['id'], $data['keyword_id']);
//路由映射
RouteMap::setRoute($param['route'], RouteMap::SOURCE_PRODUCT, $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 {
foreach ($ids as $k => $id) {
$info = $this->getCacheInfo($id);
if(!$info){
unset($ids[$k]);
continue;
}
if($info->status == Product::STATUS_RECYCLE){
//删除路由映射
RouteMap::delRoute(RouteMap::SOURCE_PRODUCT, $id, $this->user['project_id']);
//删除分类关联
CategoryRelated::where('product_id', $id)->delete();
//删除关键词关联
KeywordRelated::where('product_id', $id)->delete();
}else{
//回收站
parent::save(['id' => $id, 'status' => Product::STATUS_RECYCLE]);
unset($ids[$k]);
}
}
parent::delete($ids);
DB::commit();
}catch (\Exception $e){
DB::rollBack();
$this->fail('删除失败');
}
return $this->success();
}
public function getStatusNumber(){
//三种状态 0:草稿 1:发布 2:回收站
$data = ['dra'=>0,'pub'=>1,'del'=>2,'tal'=>3];
foreach ($data as $k => $v){
if($v == 3){
$data[$k] = $this->model->count();
}else{
$data[$k] = $this->model->where(['status'=>$v])->count();
}
}
return $this->success($data);
}
}