| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161 |
- <?php
- namespace app\api\model\plus\assemble;
- use app\common\exception\BaseException;
- use app\common\model\plus\assemble\Product as AssembleProductModel;
- use app\api\model\product\Product as ProductModel;
- /**
- * 限时拼团模型
- */
- class Product extends AssembleProductModel
- {
- /**
- * 隐藏字段
- */
- protected $hidden = [
- 'sales_initial',
- 'total_sales',
- 'is_delete',
- 'app_id',
- 'create_time',
- 'update_time'
- ];
- /**
- * 获取首页拼团商品显示
- */
- public function getProductList($assemble_activity_id, $limit)
- {
- // 获取列表数据
- $list = $this->with(['product.image.file', 'assembleSku'])
- ->where('assemble_activity_id', '=', $assemble_activity_id)
- ->where('is_delete', '=', 0)
- ->limit($limit)
- ->visible(['product.product_id','product.product_name','product.file_path'])
- ->select();
- foreach ($list as $product) {
- $assemble_arr = array_column($product['assembleSku']->toArray(), 'assemble_price');
- $product_arr = array_column($product['assembleSku']->toArray(), 'product_price');
- sort($assemble_arr);
- sort($product_arr);
- $product['assemble_price'] = current($assemble_arr);
- $product['product_price'] = current($product_arr);
- $real_product = $product['product'];
- $real_product['file_path'] = $product['product']['image'][0]['file_path'];
- unset($product['assembleSku']);
- unset($real_product['image']);
- }
- return $list;
- }
- /**
- * 获取列表页拼团数据
- * 目前未分页,后续有可能会分页
- */
- public function getActivityList($assemble_activity_id)
- {
- // 获取列表数据
- $list = $this->with(['product.image.file', 'assembleSku'])
- ->where('assemble_activity_id', '=', $assemble_activity_id)
- ->where('is_delete', '=', 0)
- ->visible(['product.product_id','product.product_name','product.file_path'])
- ->select();
- foreach ($list as $product) {
- $assemble_arr = array_column($product['assembleSku']->toArray(), 'assemble_price');
- $product_arr = array_column($product['assembleSku']->toArray(), 'product_price');
- sort($assemble_arr);
- sort($product_arr);
- $product['assemble_price'] = current($assemble_arr);
- $product['product_price'] = current($product_arr);
- $product['product']['file_path'] = $product['product']['image'][0]['file_path'];
- unset($product['assembleSku']);
- unset($product['product']['image']);
- }
- return $list;
- }
- /**
- * 获取拼团商品列表
- */
- public static function getAssembleProduct($params)
- {
- // 拼团详情
- $assemble = self::detail($params['assemble_product_id'], ['assembleSku']);
- if (empty($assemble)) {
- throw new BaseException(['msg' => '拼团商品不存在或已结束']);
- }
- // 拼团商品详情
- $product = ProductModel::detail($assemble['product_id']);
- // 拼团商品sku信息
- $assemble_sku = null;
- if ($product['spec_type'] == 10) {
- $assemble_sku = $assemble['assembleSku'][0];
- } else {
- //多规格
- foreach ($assemble['assembleSku'] as $sku) {
- if ($sku['assemble_product_sku_id'] == $params['assemble_product_sku_id']) {
- $assemble_sku = $sku;
- break;
- }
- }
- }
- if ($assemble_sku == null) {
- throw new BaseException(['msg' => '拼团商品规格不存在']);
- }
- // 拼团商品sku信息
- $product['product_sku'] = ProductModel::getProductSku($product, $params['product_sku_id']);
- $product['assemble_sku'] = $assemble_sku;
- // 拼团商品列表
- $productList = [$product->hidden(['category', 'content', 'image', 'sku'])];
- foreach ($productList as &$item) {
- // 商品单价
- $item['product_price'] = $assemble_sku['assemble_price'];
- // 商品购买数量
- $item['total_num'] = $params['product_num'];
- $item['spec_sku_id'] = $item['product_sku']['spec_sku_id'];
- // 商品购买总金额
- $item['total_price'] = $assemble_sku['assemble_price'] * $item['total_num'];
- $item['point_num'] = $assemble_sku['point_num'];
- $item['assemble_product_sku_id'] = $assemble_sku['assemble_product_sku_id'];
- $item['product_sku_id'] = $params['product_sku_id'];
- $item['product_source_id'] = $assemble_sku['assemble_product_id'];
- $item['sku_source_id'] = $assemble_sku['assemble_product_sku_id'];
- // 拼团活动id
- $item['activity_id'] = $assemble['assemble_activity_id'];
- // 拼团订单id
- $item['bill_source_id'] = $params['assemble_bill_id'];
- // 拼团最大购买数
- $item['assemble_product'] = [
- 'limit_num' => $assemble['limit_num']
- ];
- }
- return $productList;
- }
- /**
- * 拼团商品详情
- */
- public function getAssembleDetail($assemble_product_id)
- {
- $result = $this->with(['product.image.file', 'assembleSku.productSku.image'])
- ->where('assemble_product_id', '=', $assemble_product_id)->find();
- if (!empty($result)) {
- $assemble_arr = array_column($result->toArray()['assembleSku'], 'assemble_price');
- $product_arr = array_column($result->toArray()['assembleSku'], 'product_price');
- sort($assemble_arr);
- sort($product_arr);
- $result['assemble_price'] = current($assemble_arr);
- $result['line_price'] = current($product_arr);
- if (count($assemble_arr) > 1) {
- $res['assemble_high_price'] = end($assemble_arr);
- $res['line_high_price'] = end($product_arr);
- }
- }
- return $result;
- }
- }
|