quanwei
2025-11-03 ef5d748e3e3331bd20d4065c33e7867c2637d1db
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
<?php
 
namespace app\supplier\model\plus\point;
 
use app\common\model\plus\point\Product as PointProductModel;
use app\common\model\plus\point\ProductSku as PointSkuModel;
 
/**
 * 参加记录模型
 */
class Product extends PointProductModel
{
 
    /*
     * 获取列表
     */
    public function getList($shop_supplier_id,$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('shop_supplier_id', '=', $shop_supplier_id)
            ->where('is_delete','=', 0)
            ->order(['sort' => 'asc'])
            ->paginate($query);
    }
    /**
     * 添加商品
     * @param $data
     * @return bool
     */
    public function saveProduct($shop_supplier_id, $data,$grade_list=[])
    {
        $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'],
                'stock' => $stock,
                'shop_supplier_id' => $shop_supplier_id,
                'app_id' => self::$app_id,
            ]);
            //商品规格
            $sku_model = new PointSkuModel();
            $sku_data  = [];
            foreach ($product['spec_list'] as $sku) {
                //整理会员等级
                $grade_point = [];
                if(!empty($grade_list)){
                    foreach ($grade_list as $kk => $val){
                        $val["point_num"] = empty($sku['grade_point'][$kk]) ? $sku['point_num'] : $sku['grade_point'][$kk];//没有设置则设默认积分
                        $grade_point[]=$val;
                    }
                }
                $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'],
                    'grade_point' => $grade_point,
                    'app_id' => self::$app_id,
                ];
            }
            //新增规格
            $sku_model->saveAll($sku_data);
            $this->commit();
        } catch (\Exception $e) {
            $this->error = $e->getMessage();
            $this->rollback();
            return false;
        }
        return true;
    }
    /**
     * 修改
     */
    public function edit($data,$grade_list=[]){
        $this->startTrans();
        try {
            //添加商品
            $stock = array_sum(array_column($data['product']['spec_list'], 'point_stock'));
            $this->save([
                'status' => 0,
                'remark' => '',
                'stock' => $stock,
                'limit_num' => $data['limit_num']
            ]);
            //商品规格
            $sku_model = new PointSkuModel();
            //先删除所有规格
            $sku_model->where('point_product_id', '=', $this['point_product_id'])->delete();
            $sku_data  = [];
            foreach ($data['product']['spec_list'] as $sku) {
                //整理会员等级
                $grade_point = [];
                if(!empty($grade_list)){
                    foreach ($grade_list as $kk => $val){
                        $val["point_num"] = empty($sku['grade_point'][$kk]) ? $sku['point_num'] : $sku['grade_point'][$kk];//没有设置则设默认积分
                        $grade_point[]=$val;
                    }
                }
                $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'],
                    'grade_point' => $grade_point,
                    'app_id' => self::$app_id,
                ];
            }
            $sku_model->saveAll($sku_data);
            // 事务提交
            $this->commit();
            return true;
        } catch (\Exception $e) {
            $this->error = $e->getMessage();
            $this->rollback();
            return false;
        }
    }
    /**
     * 检查商品是否存在
     */
    public function checkProduct($product_id)
    {
        return $this->where('product_id', '=', $product_id)->where('is_delete', '=', 0)->find();
    }
 
    /**
     * 获取排除商品id,报名了活动,待审核,或者通过了,并且活动未结束
     */
    public function getExcludeIds($shop_supplier_id){
        $list = $this->distinct(true)->field('product.*')
            ->where('is_delete', '=', 0)
            ->where('shop_supplier_id', '=', $shop_supplier_id)
            ->where('status', 'in', [0,10])
            ->column('product_id');
        $exclude_ids = [];
        foreach ($list as $key => $item){
            array_push($exclude_ids, $item);
        }
        return $exclude_ids;
    }
 
    /**
     * 删除
     */
    public function remove($point_product_id, $shop_supplier_id){
        // 如果不是审核通过,真删
        if($this['status'] != 10){
            return $this->where('point_product_id', '=', $point_product_id)
                ->where('shop_supplier_id', '=', $shop_supplier_id)
                ->delete();
        }else{
            // 逻辑删
            return $this->where('point_product_id', '=', $point_product_id)
                ->where('shop_supplier_id', '=', $shop_supplier_id)
                ->update([
                    'is_delete' => 1
                ]);
        }
    }
}