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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
<?php
 
namespace app\operations\model\plus\point;
 
use app\common\model\plus\point\Product as ProductModel;
use app\common\model\plus\point\ProductSku as ProductSkuModel;
 
/**
 * Class Exchange
 * 积分兑换模型
 * @package app\operations\model\plus\exchange
 */
class Product extends ProductModel
{
    /*
     * 获取列表
     */
    public function getList($query = [])
    {
        $model = $this;
        if($query['status'] == 'has_audit'){
            $model = $model->where('status' ,'=', 10);
        }
        if($query['status'] == 'wait_audit'){
            $model = $model->where('status' ,'=', 0);
        }
        // 获取列表数据
        return $model->with(['product.image.file','sku'])
            ->where('is_delete','=', 0)
            ->order(['sort' => 'asc'])
            ->paginate($query);
    }
 
    /*
     * 获取排除id
     */
    public function getExcludeIds()
    {
        // 获取列表数据
        return $this->field(['product_id'])->where('is_delete','=', 0)
            ->select()->toArray();
    }
    /**
     * 添加商品
     * @param $data
     * @return bool
     */
    public function saveProduct($data, $isUpdate = false)
    {
        $product = $data['product'];
        $this->startTrans();
        try {
            $stock = array_sum(array_column($product['spec_list'], 'point_stock'));
            //添加商品表
            $this->save([
                'product_id' => $data['product_id'],
                'limit_num' => $data['limit_num'],
                'sort' => $data['sort'],
                'status' => $data['status'],
                'stock' => $stock,
                'app_id' => self::$app_id
            ]);
            //商品规格
            $sku_model = new ProductSkuModel();
            $save_data = [];
            $not_in_sku_id = [];
            foreach ($product['spec_list'] as $sku) {
                $sku_data = [
                    'point_product_id' => $this['point_product_id'],
                    'product_id' => $data['product_id'],
                    'product_sku_id' => $sku['product_sku_id'],
                    'point_stock' => $sku['point_stock'],
                    'point_num' => $sku['point_num'],
                    'product_attr' => $sku['product_attr'],
                    'product_price' => $sku['product_price'],
                    'point_money' => $sku['point_money'],
                    'app_id' => self::$app_id,
                ];
                if($sku['point_product_sku_id'] > 0){
                    $detail = $sku_model->find($sku['point_product_sku_id']);
                    if($detail){
                        $detail->save($sku_data);
                        array_push($not_in_sku_id, $sku['point_product_sku_id']);
                    }
                }else{
                    $save_data[] = $sku_data;
                }
            }
 
            //删除规格
            count($not_in_sku_id) > 0 && $sku_model->where('point_product_id', '=', $data['point_product_id'])
                ->whereNotIn('point_product_sku_id', $not_in_sku_id)
                ->delete();
            //新增规格
            count($save_data) > 0 && $sku_model->saveAll($save_data);
            $this->commit();
        } catch (\Exception $e) {
            $this->error = $e->getMessage();
            $this->rollback();
            return false;
        }
        return true;
    }
 
    /**
     * 检查商品是否存在
     * @param int $product_id
     */
    public function checkProduct($product_id)
    {
        return $this->where('product_id', '=', $product_id)->where('is_delete', '=', 0)->find();
    }
 
    /**
     * 获取商品信息
     * @param $id
     */
    public function getPointData($arr)
    {
        return $this->where('point_product_id', '=', $arr['point_product_id'])->with(['product.image.file','sku'])->find();
    }
 
    /**
     * @param $data
     * 修改商品信息
     */
    public function edit($data)
    {
        $this->startTrans();
        try {
            //添加商品表
            $this->save([
                'limit_num' => $data['limit_num'],
                'sort' => $data['sort'],
                'stock' => isset($data['stock'])?$data['stock']: 0,
                'status' => $data['status'],
            ]);
            if($data['spec_type'] == 20) {
                $stock = 0;//总库存
                foreach ($data['specData']['spec_list'] as $item) {
                    $sku = $this->getSkuModel($item['spec_form']['point_product_sku_id']);
                    $sku->save([
                        'stock' => $item['spec_form']['stock'],
                        'point_num' => $item['spec_form']['point_num'],
                        'point_money' => $item['spec_form']['point_money'],
                    ]);
                    $stock += $item['spec_form']['stock'];
                }
                $this->save([
                    'stock' => $stock,
                ]);
            }else{
                $sku_model = $this['sku'][0];
                //单规格
                $sku_model->save([
                    'stock' => $data['skuData']['stock'],
                    'point_num' => $data['skuData']['point_num'],
                    'point_money' => $data['skuData']['point_money'],
                ]);
            }
            $this->commit();
        } catch (\Exception $e) {
            $this->error = $e->getMessage();
            $this->rollback();
            return false;
        }
        return true;
    }
 
    private function getSkuModel($point_product_sku_id)
    {
        foreach ($this['sku'] as $sku) {
            if ($sku['point_product_sku_id'] == $point_product_sku_id) {
                return $sku;
            }
        }
    }
 
    /**
     * 删除
     * @param $id
     */
    public function del($id)
    {
        return $this->where('point_product_id', '=', $id)->update([
            'is_delete' => 1
        ]);
    }
 
    /**
     * 获取待审核数量
     */
    public static function getApplyCount()
    {
        return (new static())->where('status', '=', 0)
            ->where('is_delete', '=', 0)
            ->count();
    }
 
    /**
     * 上下架
     */
    public function setState($data)
    {
        return $this->save($data) !== false;
    }
}