quanwei
2025-12-04 12913c1069347ea4b1f6ab87f480da0f8d8c646a
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
<?php
 
namespace app\api\model\plus\points;
 
use app\common\exception\BaseException;
use app\common\model\plus\point\Product as PointProductModel;
use app\api\model\product\Product as ProductModel;
use app\api\model\supplier\Supplier as SupplierModel;
use app\api\model\supplier\ServiceApply;
/**
 * 积分商城模型
 */
class Product extends PointProductModel
{
    /*
     * 获取列表
     */
    public function getList($params,$user=[])
    {
        // 获取列表数据
        $list = $this->alias("a")
            ->with(['product.image.file','sku'])
            ->join('product product', 'product.product_id=a.product_id')
            ->join('supplier supplier', 'product.shop_supplier_id = supplier.shop_supplier_id', 'left')
            ->where('a.is_delete','=', 0)
            ->where('a.status', '=', 10)
            ->where('a.state', '=', 10)
            ->where('product.is_delete', '=', 0)
            ->where('supplier.is_delete', '=', 0)
            ->where('supplier.status', '=', 0)
            ->where('supplier.is_recycle', '=', 0)
            ->where('product.product_status', '=', 10)
            ->where('product.audit_status', '=', 10)
            ->field('a.*')
            ->order(['a.sort' => 'asc'])
            ->paginate($params);
        foreach ($list as $key => $val) {
            $list[$key]['product_image'] = $val['product']['image'][0]['file_path'];
            //获取用户等级对应的兑换积分 by yj 2024.1.8
            $list[$key]['sku'] = $this->getPointNum($val["sku"],$user);
        }
        return $list;
    }
 
    /*
     * 获取用户等级对应的兑换积分 by yj 2024.1.8
     */
    public function getPointNum($sku,$user=[])
    {
        if(!empty($sku) && !empty($user["grade_id"])){
            foreach ($sku as &$vv){
                if(!empty($vv["grade_point"])){
                    foreach ($vv["grade_point"] as $v){
                        if($v["grade_id"] == $user["grade_id"] && $v["point_num"] > 0){
                            $vv["point_num"] = $v["point_num"];
                            continue;
                        }
                    }
                }
 
            }
        }
        return $sku;
    }
 
    /**
     * 获取积分任务的商品列表(用于订单结算)
     */
    public static function getPointsProduct($params,$user=[])
    {
        // 积分商品详情
        $points = self::detail($params['point_product_id'], ['sku']);
        if (empty($points) || $points['status'] == 20 || $points['is_delete'] == 1 || $points['state'] == 20) {
            throw new BaseException(['msg' => '积分兑换商品不存在或已结束']);
        }
 
        // 积分商品详情
        $product = ProductModel::detail($points['product_id']);
        // 积分商品sku信息
        $point_sku = null;
        //获取用户等级对应的兑换积分 by yj 2024.1.8
        $points['sku'] = (new static())->getPointNum($points['sku'],$user);
        if($product['spec_type'] == 10){
            $point_sku = $points['sku'][0];
        }else{
            //多规格
            foreach ($points['sku'] as $sku){
                if($sku['point_product_sku_id'] == $params['point_product_sku_id']){
                    $point_sku = $sku;
                    break;
                }
            }
        }
        if ($point_sku == null) {
            throw new BaseException(['msg' => '积分兑换商品规格不存在']);
        }
        // 商品sku信息
        $product['product_sku'] = ProductModel::getProductSku($product, $params['product_sku_id']);
        $product['point_sku'] = $point_sku;
 
        // 商品列表
        $productList = [$product->hidden(['category', 'content', 'image', 'sku'])];
        // 只会有一个商品
        foreach ($productList as &$item) {
            // 商品单价
            $item['product_price'] = $point_sku['point_money'];
            // 商品购买数量
            $item['total_num'] = $params['product_num'];
            $item['spec_sku_id'] = $item['product_sku']['spec_sku_id'];
            // 商品购买总金额
            $item['total_price'] =  $point_sku['point_money'] * $item['total_num'];
            $item['points_num'] = $point_sku['point_num'] * $item['total_num'];
            $item['point_product_sku_id'] = $point_sku['point_product_sku_id'];
            $item['product_sku_id'] = $params['product_sku_id'];
            $item['product_source_id'] = $point_sku['point_product_id'];
            $item['sku_source_id'] = $point_sku['point_product_sku_id'];
            // 积分商品最大兑换数
            $item['point_product'] = [
                'limit_num' => $points['limit_num']
            ];
            // 积分抵扣金额
            $item['points_money'] = ($item['product_sku']['product_price'] - $point_sku['point_money']) * $params['product_num'];
        }
        $supplierData[] = [
            'shop_supplier_id' => $product['shop_supplier_id'],
            'supplier' => $product['supplier'],
            'productList' => $productList
        ];
        unset($product['supplier']);
        return $supplierData;
    }
 
    /**
     * 积分商品详情
     */
    public function getPointDetail($point_product_id,$user=[])
    {
        $result = self::detail($point_product_id, ['product' => ['image.file', 'contentImage.file'], 'sku.productSku.image']);
 
        if (!empty($result)) {
            $point_arr = array_column($result->toArray()['sku'], 'assemble_price');
            $product_arr = array_column($result->toArray()['sku'], 'product_price');
            sort($point_arr);
            sort($product_arr);
            $result['point_price'] =  current($point_arr);
            $result['line_price'] = current($product_arr);
            if (count($point_arr) > 1) {
                $res['point_high_price'] = end($point_arr);
                $res['line_high_price'] = end($product_arr);
            }
            $SupplierModel = new SupplierModel;
            if ($result['shop_supplier_id']) {
                $supplier = $SupplierModel::detail($result['shop_supplier_id'], ['logo', 'category']);
                $supplier['logos'] = $supplier['logo']['file_path'];
                unset($supplier['logo']);
                $supplier['category_name'] = $supplier['category']['name'];
                unset($supplier['category']);
                $supplier->visible(['logos', 'category_name', 'name', 'shop_supplier_id', 'product_sales', 'server_score', 'store_type']);
                $server = (new ServiceApply())->getList($result['shop_supplier_id']);
            } else {
                $supplier = [];
                $server = [];
            }
            $result['supplier'] = $supplier;
            $result['server'] = $server;
            $result['sku'] = $this->getPointNum($result['sku'],$user);
        }
        return $result;
    }
}