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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
<?php
 
namespace app\operations\controller\setting;
 
use app\operations\controller\Controller;
use app\operations\model\settings\Region as RegionModel;
use app\operations\model\settings\Delivery as DeliveryModel;
 
/**
 * 运费模板控制器
 */
class Delivery extends Controller
{
    /**
     * 运费模板列表
     */
    public function index()
    {
        $model = new DeliveryModel;
        $list = $model->getList($this->postData());
        return $this->renderSuccess('', compact('list'));
    }
 
 
    /**
     * 配送区域
     */
    public function area()
    {
        // 获取所有地区
        $regionData = RegionModel::getCacheTree();
        $arr = $this->dataFormat($regionData);
        return $this->renderSuccess('', compact('arr'));
    }
 
 
    /**
     * 新增
     */
    public function add()
    {
        // 新增记录
        $model = new DeliveryModel;
        $data = $this->postData();
        if ($model->add($data)) {
            return $this->renderSuccess('添加成功');
        }
        return $this->renderError($model->getError() ?: '添加失败');
    }
 
    /**
     * 详情
     */
    public function detail($delivery_id = 0)
    {
        // 获取所有地区
        $regionData = RegionModel::getCacheTree();
        $arr = $this->dataFormat($regionData);
        // 地区总数
        $cityCount = RegionModel::getCacheCounts()['city'];
        //新增
        if($delivery_id == 0){
            return $this->renderSuccess('', compact('arr', 'cityCount'));
        }
        // 详情
        $detail = DeliveryModel::detail($delivery_id);
        // 获取配送区域及运费设置项
        $formData = $detail->getFormList();
        $returnData = $this->dataFormat1($arr, $formData);
 
        $arr = $returnData['arr'];
 
        return $this->renderSuccess('', compact('detail', 'arr', 'cityCount', 'formData'));
    }
 
    /**
     * 格式化数据
     */
    public function dataFormat($regionData)
    {
        $arr = [];
        foreach ($regionData as $val) {
            $city = array_column($val['city'], 'name');
            $id = array_column($val['city'], 'id');
            $len = count($city);
            $arr2 = [];
            for ($i = 0; $i < $len; $i++) {
                $arr1 = [
                    'value' => $id[$i],
                    'label' => $city[$i],
                ];
                array_push($arr2, $arr1);
            }
            $arr[] = [
                'value' => $val['id'],
                'label' => $val['name'],
                'children' => $arr2
            ];
            $arr2 = [];
        }
        return $arr;
    }
 
    /**
     * 格式化数据1
     */
    public function dataFormat1($arr, $formData)
    {
        foreach ($arr as $key => &$val) {
            $val['index'] = null;
            $index = 0;
            foreach ($formData as $k => $v) {
                if (in_array($val['value'], $v['province'])) {
                    $citys = array();
                    $val['checked'] = true;
                    if (is_array($val['index'])) {
                        $list = $val['index'];
                        array_push($list, $index);
                        $val['index'] = $list;
                    } else {
                        $val['index'] = array($index);
 
                    }
                    foreach ($val['children'] as $c => &$city) {
                        if (in_array($city['value'], $v['citys'])) {
                            $city['checked'] = true;
                            $city['index'] = $index;
                            $citys[] = $city;
                        }
                    }
                    $province = array(
                        'value' => $arr[$key]['value'],
                        'label' => $arr[$key]['label'],
                        'children' => $citys);
                    $formData[$k]['areas'][] = $province;
                }
                $index++;
            }
        }
 
        return array('arr' => $arr, "formData" => $formData);
    }
 
    /**
     * 修改
     */
    public function edit($delivery_id = 0)
    {
        if($this->request->isGet()){
            return $this->detail($delivery_id);
        }
        if ($delivery_id == 0) {
            return $this->add();
        }
        $model = DeliveryModel::detail($delivery_id);
        // 更新记录
        if ($model->edit($this->postData())) {
            return $this->renderSuccess('更新成功');
        }
        return $this->renderError($model->getError() ?: '更新失败');
    }
 
    /**
     * 格式化修改提交的数据
     */
    public function dataFormat2($data)
    {
 
        foreach ($data['delivery'] as $key => $val) {
            $str = '';
            foreach ($val['areas'] as $v) {
                $city = array_column($v['children'], 'value');
                $str .= implode(',', $city) . ',';
            }
            $str = substr($str, 0, -1);
            $data['delivery'][$key]['region'] = $str;
        }
        return $data;
    }
 
    /**
     * 删除记录
     */
    public function delete($delivery_id)
    {
        $model = DeliveryModel::detail($delivery_id);
        if (!$model->remove()) {
            return $this->renderError($model->getError() ?: '删除失败');
        }
        return $this->renderSuccess('删除成功');
    }
 
}