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
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
136
137
138
139
140
<?php
 
namespace app\operations\model\settings;
 
use app\common\model\settings\DeliveryRule as DeliveryRuleModel;
use app\operations\model\product\Product as ProductModel;
use app\common\model\settings\Delivery as DeliveryModel;
 
/**
 * 配送模板模型
 */
class Delivery extends DeliveryModel
{
    /**
     * 添加新记录
     */
    public function add($data)
    {
        if (!isset($data['rule']) || empty($data['rule'])) {
            $this->error = '请选择可配送区域';
            return false;
        }
 
        $delivery = [
            'name' => $data['name'],
            'method' => $data['radio'],
            'sort' => $data['sort'],
            'app_id' => self::$app_id
        ];
 
        if ($this->save($delivery)) {
            return $this->createDeliveryRule($data['rule'], 'add');
        }
        return false;
    }
 
    /**
     * 编辑记录
     */
    public function edit($data)
    {
        if (!isset($data['rule']) || empty($data['rule'])) {
            $this->error = '请选择可配送区域';
            return false;
        }
 
        $delivery = [
            'name' => $data['name'],
            'method' => $data['radio'],
            'sort' => $data['sort'],
        ];
 
        if ($this->save($delivery)) {
            return $this->createDeliveryRule($data['rule'],'edit');
        }
        return false;
    }
 
    /**
     * 添加模板区域及运费
     */
    private function createDeliveryRule($data, $scene = 'add')
    {
        $save = [];
        $count = count($data);
        for ($i = 0; $i < $count; $i++) {
            $save[] = [
                'region' => join(',', array_values($data[$i]['citys'])),
                'first' => $data[$i]['first'],
                'first_fee' => $data[$i]['first_fee'],
                'additional' => $data[$i]['additional'],
                'additional_fee' => $data[$i]['additional_fee'],
                'app_id' => self::$app_id
            ];
        }
        if($scene == 'edit'){
            (new DeliveryRuleModel())->where('delivery_id', '=', $this['delivery_id'])->delete();
        }
        return $this->rule()->saveAll($save);
    }
 
    /**
     * 获取配送区域及运费设置项
     */
    public function getFormList()
    {
        // 所有地区
        $regions = Region::getCacheAll();
        $list = [];
        foreach ($this['rule'] as $rule) {
            $citys = explode(',', $rule['region']);
            $province = [];
            foreach ($citys as $cityId) {
                if (!isset($regions[$cityId])) continue;
                !in_array($regions[$cityId]['pid'], $province) && $province[] = $regions[$cityId]['pid'];
            }
            $list[] = [
                'first' => $rule['first'],
                'first_fee' => $rule['first_fee'],
                'additional' => $rule['additional'],
                'additional_fee' => $rule['additional_fee'],
                'province' => $province,
                'citys' => $citys,
            ];
        }
        return $list;
    }
 
 
    /**
     * 删除记录
     */
    public function remove()
    {
        // 验证运费模板是否被商品使用
        if (!$this->checkIsUseProduct($this['delivery_id'])) {
            return false;
        }
        // 删除运费模板
        $this->rule()->delete();
        return $this->delete();
    }
 
    /**
     * 验证运费模板是否被商品使用
     */
    private function checkIsUseProduct($deliveryId)
    {
        // 判断是否存在商品
        $productCount = (new ProductModel)->where('delivery_id', '=', $deliveryId)
            ->where('is_delete', '=', 0)
            ->count();
        if ($productCount > 0) {
            $this->error = '该模板被' . $productCount . '个商品使用,不允许删除';
            return false;
        }
        return true;
    }
 
}