quanwei
3 days ago 04102f7237efefa744090ed7c25f7b5d0807b679
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
<?php
 
namespace app\supplier\model\plus\advance;
 
use app\common\model\plus\advance\Product as ProductModel;
use app\common\model\plus\advance\AdvanceSku as AdvanceSkuModel;
 
/**
 * Class Partake
 * 预售商品模型
 */
class Product extends ProductModel
{
    /*
     * 获取列表
     */
    public function getList($param, $shop_supplier_id)
    {
        $model = $this;
        if (isset($param['audit_status']) && $param['audit_status']) {
            $model = $model->where('audit_status', '=', $param['audit_status']);
        }
        // 获取列表数据
        return $model->with(['product.image.file', 'sku'])
            ->where('shop_supplier_id', '=', $shop_supplier_id)
            ->where('is_delete', '=', 0)
            ->order(['sort' => 'asc', 'create_time' => 'asc'])
            ->paginate($param);
    }
 
    /*
     * 获取排除id
     */
    public function getExcludeIds($shop_supplier_id)
    {
        // 获取列表数据
        return $this->where('shop_supplier_id', '=', $shop_supplier_id)
            ->where('is_delete', '=', 0)
            ->select()->toArray();
    }
 
    /**
     * 添加商品
     * @param $data
     * @return bool
     */
    public function saveProduct($data, $shop_supplier_id)
    {
        $product = $data['product'];
        $this->startTrans();
        try {
            $stock = array_sum(array_column($product['spec_list'], 'advance_stock'));
            //添加商品表
            $this->save([
                'product_id' => $data['product_id'],
                'money' => $data['money'],
                'limit_num' => $data['limit_num'],
                'sort' => $data['sort'],
                'status' => $data['status'],
                'stock' => $stock,
                'start_time' => strtotime($data['active_time'][0]),
                'end_time' => strtotime($data['active_time'][1]),
                'shop_supplier_id' => $shop_supplier_id,
                'app_id' => self::$app_id
            ]);
            //商品规格
            $sku_model = new AdvanceSkuModel();
            $save_data = [];
            $not_in_sku_id = [];
            foreach ($product['spec_list'] as $sku) {
                $sku_data = [
                    'advance_product_id' => $this['advance_product_id'],
                    'product_id' => $data['product_id'],
                    'product_sku_id' => $sku['product_sku_id'],
                    'advance_stock' => $sku['advance_stock'],
                    'product_attr' => $sku['product_attr'],
                    'product_price' => $sku['product_price'],
                    'advance_price' => $sku['advance_price'],
                    'app_id' => self::$app_id,
                ];
                if ($sku['advance_product_sku_id'] > 0) {
                    $detail = $sku_model->find($sku['advance_product_sku_id']);
                    if ($detail) {
                        $detail->save($sku_data);
                        array_push($not_in_sku_id, $sku['advance_product_sku_id']);
                    }
                } else {
                    $save_data[] = $sku_data;
                }
            }
 
            //删除规格
            count($not_in_sku_id) > 0 && $sku_model->where('advance_product_id', '=', $data['advance_product_id'])
                ->whereNotIn('advance_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('advance_product_id', '=', $arr['advance_product_id'])->with(['product.image.file', 'sku'])->find();
    }
 
    /**
     * 删除
     */
    public function del($id)
    {
        return $this->where('advance_product_id', '=', $id)->update([
            'is_delete' => 1
        ]);
    }
 
    /**
     * 商品ID是否存在
     */
    public static function isExistProductId($productId)
    {
        return !!(new static)->where('product_id', '=', $productId)
            ->where('is_delete', '=', 0)
            ->value('advance_product_id');
    }
 
    /**
     * 获取diy预售活动商品
     */
    public function getDiyProduct()
    {
        $res = $this->with(['advanceProduct.advanceSku', 'advanceProduct.product'])->where('start_time', '<=', time())
            ->where('end_time', '>=', time())->find();
        if (isset($res['advanceProduct'])) {
            $list = [];
            foreach ($res['advanceProduct'] as $k => $val) {
                $list[$k]['product_name'] = $val['product']['product_name'];
                $list[$k]['product_id'] = $val['product_id'];
                $list[$k]['product_name'] = $val['product']['product_name'];
            }
            return $res['advanceProduct'];
        }
        return [];
    }
}