quanwei
18 hours ago c441dea81bd86bdfb12dff35821fed51f4cc91c2
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
<?php
 
namespace app\operations\controller\product;
 
use app\operations\controller\Controller;
use app\operations\model\product\Spec as SpecModel;
use app\operations\model\product\SpecValue as SpecValueModel;
 
/**
 * 商品规格控制器
 */
class Spec extends Controller
{
 
    /**
     * 添加规则组
     */
    public function addSpec($spec_name, $spec_value)
    {
        $specModel = new SpecModel();
        $specValueModel = new SpecValueModel();
        // 判断规格组是否存在
        if (!$specId = $specModel->getSpecIdByName($spec_name)) {
            // 新增规格组and规则值
            if ($specModel->add($spec_name)
                && $specValueModel->add($specModel['spec_id'], $spec_value))
                return $this->renderSuccess('', [
                    'spec_id' => (int)$specModel['spec_id'],
                    'spec_value_id' => (int)$specValueModel['spec_value_id'],
                ]);
            return $this->renderError();
        }
        // 判断规格值是否存在
        if ($specValueId = $specValueModel->getSpecValueIdByName($specId, $spec_value)) {
            return $this->renderSuccess('',  [
                'spec_id' => (int)$specId,
                'spec_value_id' => (int)$specValueId,
            ]);
        }
        // 添加规则值
        if ($specValueModel->add($specId, $spec_value)){
            return $this->renderSuccess('', [
                'spec_id' => (int)$specId,
                'spec_value_id' => (int)$specValueModel['spec_value_id'],
            ]);
        }
 
        return $this->renderError();
    }
 
    /**
     * 添加规格值
     */
    public function addSpecValue($spec_id, $spec_value)
    {
        $specValueModel = new SpecValueModel();
        // 判断规格值是否存在
        if ($specValueId = $specValueModel->getSpecValueIdByName($spec_id, $spec_value)) {
            return $this->renderSuccess('',  [
                'spec_value_id' => (int)$specValueId,
            ]);
        }
        // 添加规则值
        if ($specValueModel->add($spec_id, $spec_value))
            return $this->renderSuccess('', [
                'spec_value_id' => (int)$specValueModel['spec_value_id'],
            ]);
        return $this->renderError();
    }
 
}