quanwei
2025-12-31 48d31672b4d88900080093cd1632f9d2eb978d4d
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
<?php
 
namespace app\supplier\controller\activity;
 
use app\supplier\controller\Controller;
use app\supplier\model\plus\advance\Product as AdvanceProductModel;
use app\common\model\product\Product as ProductModel;
use app\supplier\service\ProductService;
 
/**
 * 活动控制器
 */
class Advance extends Controller
{
    /**
     * 新增预售商品
     */
    public function index()
    {
        $model = new AdvanceProductModel();
        $list = $model->getList($this->postData(), $this->getSupplierId());
        //获取排除id
        $exclude_ids = $model->getExcludeIds($this->getSupplierId());
        $exclude_ids = array_column($exclude_ids, 'product_id');
        return $this->renderSuccess('', compact('list', 'exclude_ids'));
    }
 
    /**
     *添加商品
     */
    public function add($product_id)
    {
        if ($this->request->isGet()) {
            $model = ProductModel::detail($product_id);
            $specData = ProductService::getSpecData($model);
            $specList = $this->transSpecData($specData);
            return $this->renderSuccess('', compact('model', 'specList'));
        }
        $model = new AdvanceProductModel();
        if ($model->checkProduct($product_id)) {
            return $this->renderError('商品已经存在');
        }
        if ($model->saveProduct($this->postData(), $this->getSupplierId())) {
            return $this->renderSuccess('添加成功');
        }
        return $this->renderError($model->getError() ?: '添加失败');
    }
 
    /**
     * 组装前端用的数据
     */
    private function transSpecData($specData)
    {
        if (!$specData) {
            return [];
        }
        $specList = [];
        foreach ($specData['spec_list'] as $spec) {
            $specIds = explode('_', $spec['spec_sku_id']);
            $spec['spec_name'] = '';
            foreach ($specIds as $specId) {
                $spec['spec_name'] .= $this->searchSpecItem($specData['spec_attr'], $specId) . ';';
            }
            array_push($specList, $spec);
        }
        return $specList;
    }
 
    /**
     * 规格值
     */
    private function searchSpecItem($spec_attr, $item_id)
    {
        $specValue = '';
        foreach ($spec_attr as $attr) {
            foreach ($attr['spec_items'] as $item) {
                if ($item['item_id'] == $item_id) {
                    $specValue = $attr['group_name'] . ',' . $item['spec_value'];
                    break 2;
                }
            }
        }
        return $specValue;
    }
 
    /**
     *修改商品
     */
    public function edit($advance_product_id)
    {
        $model = AdvanceProductModel::detail($advance_product_id, ['product.image.file', 'sku']);
        if ($this->request->isGet()) {
            return $this->renderSuccess('', compact('model'));
        }
 
        if ($model->saveProduct($this->postData(), $this->getSupplierId())) {
            return $this->renderSuccess('修改成功');
        }
        return $this->renderError($model->getError() ?: '修改失败');
    }
 
    /**
     *删除商品
     */
    public function delete($id)
    {
        $model = new AdvanceProductModel();
        if ($model->del($id)) {
            return $this->renderSuccess('删除成功');
        }
        return $this->renderError('删除失败');
    }
}