<?php
|
|
namespace app\common\model\branch;
|
|
use app\common\library\helper;
|
use app\common\model\BaseModel;
|
use app\common\model\plus\table\Record as RecordModel;
|
use app\common\model\product\Product as ProductModel;
|
use app\common\model\branch\ActivityCart as ActivityCartModel;
|
use app\shop\service\ProductService;
|
|
/**
|
* 参与活动的商品价格模型
|
* @property int $id
|
* @property int $activity_id 活动id
|
* @property int $product_id 产品id
|
* @property int $product_sku_id 产品的属性id
|
* @property float $product_price 产品的价格
|
* @property int $app_id 小程序id
|
* @property int $shop_supplier_id 商户id
|
* @property string $create_time 创建时间
|
* @property string $update_time 更新时间
|
* @property int $is_delete 是否删除
|
*/
|
class ActivityProductPrice extends BaseModel
|
{
|
protected $name = 'branch_activity_product_price';
|
protected $pk = 'id';
|
|
protected $type = [
|
'can_user_ids'=>'json'
|
];
|
|
/**
|
* 关联商品规格表
|
*/
|
public function sku()
|
{
|
return $this->hasMany('app\\common\\model\\product\\ProductSku')->order(['product_sku_id' => 'asc']);
|
}
|
|
/**
|
* 关联商品规格关系表
|
*/
|
public function specRel()
|
{
|
return $this->belongsToMany('app\\common\\model\\product\\SpecValue', 'ProductSpecRel', '')->order(['id' => 'asc']);
|
}
|
|
/**
|
* 关联商品图片表
|
*/
|
public function image()
|
{
|
return $this->hasMany('app\\common\\model\\product\\ProductImage')->where('image_type', '=', 0)->order(['id' => 'asc']);
|
}
|
|
/**
|
读取活动设置的商品列表
|
**/
|
public function getProductPricelist($id=''){
|
$model = new static();
|
$list = $model
|
->where('is_delete','=',0)
|
->where(['activity_id'=>$id])
|
->hidden(['create_time','is_delete','update_time','activity_id','app_id','shop_supplier_id'])
|
->select()->toArray();
|
if(empty($list)){
|
return [];
|
}
|
foreach ($list as &$item){
|
$model = ProductModel::detail($item['product_id']);
|
$item['product_name'] = $model['product_name'];
|
$item['image'] = $model['image'][0]['file_path'] ?? 0;
|
$specData = ProductService::getSpecData($model);
|
$specList = [];
|
if(!empty($specData)){
|
$specList = $this->transSpecData($specData);
|
foreach ($specList as $otem){
|
if($item['product_sku_id']==$otem['product_sku_id']){
|
$item['product_sku_name'] = $otem['spec_name'];
|
}
|
}
|
}else{
|
$item['product_sku_name'] = '单规格';
|
}
|
}
|
unset($item);
|
return $list;
|
}
|
|
/**
|
读取某个活动的所有商品
|
**/
|
public function getProductlist($activity_id=''){
|
$model = new static();
|
$list = $model
|
->where('is_delete','=',0)
|
->where(['activity_id'=>$activity_id])
|
->field('*,MIN(product_new_price) as min_price')
|
->hidden(['create_time','is_delete','update_time','activity_id','app_id'])
|
->group('product_id') // 按product_id分组
|
->select();
|
if(empty($list)){
|
return [];
|
}
|
$back_column = $model->where('is_delete','=',0)->where(['activity_id'=>$activity_id])->column('product_sku_id','spec_sku_id');
|
$spec_sku_id_arr = array_keys($back_column);
|
$spec_sku_id = [];
|
foreach ($spec_sku_id_arr as $key=> $dd){
|
if($dd!=0){
|
$new_arr = explode('_',$dd);
|
$spec_sku_id[] = $new_arr[0];
|
}
|
}
|
$product_sku_id = array_values($back_column);
|
$new_list = [];
|
foreach ($list as &$item){
|
$products = $this->detail($item['product_id'],$product_sku_id);
|
if(!$products){
|
continue;
|
}
|
// 整理商品数据并返回
|
$item = $this->setProductListData($products, false,$item);
|
$item['select_spec'] = [];
|
// 多规格商品sku信息
|
$item['specData'] = $item['spec_type'] == 20 ? $this->getManySpecData($item['spec_rel'], $item['sku'],$spec_sku_id,$activity_id,$item['product_id']) : null;
|
//获取商品最大价格
|
$productPrice = helper::getArrayColumn($item['sku'], 'product_price');
|
rsort($productPrice);
|
$item['product_max_price'] = isset($productPrice[0]) ? $productPrice[0] : 0;
|
$new_list[] = $item;
|
}
|
unset($item);
|
return $new_list;
|
}
|
|
/**
|
* 获取商品详情
|
*/
|
public function detail($product_id, $product_sku_id)
|
{
|
$infos = (new ProductModel())->with([
|
'image.file',
|
'sku.image'=>function($query) use($product_sku_id){
|
$query->where(['product_sku_id'=>$product_sku_id]);
|
},
|
'spec_rel.spec',
|
])->where('product_id', '=', $product_id)
|
->where('product_status','=',10)
|
->find();
|
if($infos){
|
return $infos->toArray();
|
}
|
return $infos;
|
}
|
|
/**
|
* 商品多规格信息
|
*/
|
public function getManySpecData($specRel, $skuData, $spec_sku_id, $activity_id = 0, $product_id)
|
{
|
$price_model = new static();
|
// spec_attr
|
$specAttrData = [];
|
foreach ($specRel as $key=>$item) {
|
if($key==0 && !in_array($item['spec_id'],$spec_sku_id)){
|
continue;
|
}
|
if (!isset($specAttrData[$item['spec_id']])) {
|
$specAttrData[$item['spec_id']] = [
|
'group_id' => $item['spec']['spec_id'],
|
'group_name' => $item['spec']['spec_name'],
|
'spec_items' => [],
|
];
|
}
|
$specAttrData[$item['spec_id']]['spec_items'][] = [
|
'item_id' => $item['spec_value_id'],
|
'spec_value' => $item['spec_value'],
|
];
|
}
|
// spec_list
|
$specListData = [];
|
foreach ($skuData as $item) {
|
$image = (isset($item['image']) && !empty($item['image'])) ? $item['image'] : ['file_id' => 0, 'file_path' => ''];
|
$product_price = $price_model->where(['spec_sku_id'=>$item['spec_sku_id'],'product_id'=>$product_id,'activity_id'=>$activity_id,'is_delete'=>0])->value('product_new_price');
|
$specListData[] = [
|
'product_sku_id' => $item['product_sku_id'],
|
'spec_sku_id' => $item['spec_sku_id'],
|
'rows' => [],
|
'spec_form' => [
|
'image_id' => $image['file_id'],
|
'image_path' => $image['file_path'],
|
'product_no' => $item['product_no'],
|
'product_price' => $product_price,
|
'product_weight' => $item['product_weight'],
|
'line_price' => $item['line_price'],
|
'stock_num' => $item['stock_num']
|
],
|
];
|
}
|
return ['spec_attr' => array_values($specAttrData), 'spec_list' => $specListData];
|
}
|
|
/**
|
* 设置商品展示的数据
|
*/
|
protected function setProductListData($data, $isMultiple = true, $product_price=[])
|
{
|
|
if (!$isMultiple) $dataSource = [&$data]; else $dataSource = &$data;
|
// 整理商品列表数据
|
foreach ($dataSource as &$product) {
|
// 商品主图
|
$product['product_image'] = $product['image'][0]['file_path'];
|
// 商品默认规格
|
$product['product_sku'] = $this->getShowSku($product,$product_price);
|
// 可核销门店ids by yj
|
if(!is_array($product['verify_store_ids'])){
|
if($product['verify_store_ids'] != ''){
|
$product['verify_store_ids'] = explode(',', $product['verify_store_ids']);
|
} else {
|
$product['verify_store_ids'] = [];
|
}
|
}
|
// 核销有效时间段
|
if($product['verify_type'] == 20){
|
$product['verify_time'] = [date('Y-m-d H:i:s', $product['verify_start_time']), date('Y-m-d H:i:s', $product['verify_end_time'])];
|
}
|
}
|
return $data;
|
}
|
|
/**
|
* 显示的sku,目前取最低价
|
*/
|
protected function getShowSku($product,$product_price=[])
|
{
|
//如果是单规格
|
if($product['spec_type'] == 10){
|
if(empty($product['sku'])){
|
$product['sku'] = [
|
['product_price'=>$product_price['product_new_price'],'line_price'=>$product['line_price']]
|
];
|
}else{
|
$product['sku'][0]['product_price'] = $product_price['product_new_price'];
|
}
|
return $product['sku'][0];
|
}else{
|
if(empty($product['sku'])){
|
$product['sku'] = [
|
['product_price'=>$product_price['product_new_price'],'line_price'=>$product['line_price']]
|
];
|
}else{
|
$product['sku'][0]['product_price'] = $product_price['product_new_price'];
|
}
|
}
|
// 兼容历史数据,如果找不到返回第一个
|
return isset($product['sku'][0]) ? $product['sku'][0]: $product['sku'];
|
}
|
|
|
/**
|
* 组装前端用的数据
|
*/
|
private function transSpecData($specData){
|
$specList = [];
|
foreach($specData['spec_list'] as $spec){
|
$specIds = explode('_',$spec['spec_sku_id']);
|
$spec['spec_name'] = '';
|
foreach ($specIds as $specId){
|
$spec['spec_name'] .= $this->searchSpecItem($specData['spec_attr'], $specId) . ';';
|
}
|
array_push($specList, $spec);
|
}
|
return $specList;
|
}
|
|
/**
|
* 规格值
|
*/
|
private function searchSpecItem($spec_attr, $item_id){
|
$specValue = '';
|
foreach ($spec_attr as $attr){
|
foreach ($attr['spec_items'] as $item){
|
if($item['item_id'] == $item_id){
|
$specValue = $attr['group_name'] . ',' . $item['spec_value'];
|
break 2;
|
}
|
}
|
}
|
return $specValue;
|
}
|
|
/**
|
* 立即购买:获取订单商品列表
|
*/
|
public static function getOrderProductListByNow($params)
|
{
|
$price_model = new static();
|
// 商品详情
|
$product = \app\api\model\product\Product::detail($params['product_id']);
|
// 商品sku信息
|
$product['product_sku'] = ProductModel::getProductSku($product, $params['product_sku_id']);
|
// 自定义表单数据id,直接读取最新的那条记录 by lyzflash
|
$product['table_record_id'] = 0;
|
// 商品列表
|
$productList = [$product->hidden(['category', 'content', 'image', 'sku'])];
|
foreach ($productList as $key=> &$item) {
|
$item['spec_sku_id'] = $item['product_sku']['spec_sku_id'];
|
//获取规格的图片 by yj 2024.1.16
|
if(!empty($product['product_sku']) && !empty($product['product_sku']["image"])){
|
$item['product_image'] = $product['product_sku']["image"]["file_path"];
|
}
|
$product_price = $price_model->where(['spec_sku_id'=>$item['spec_sku_id'],'product_id'=>$item['product_id'],'activity_id'=>$params['activity_id'],'is_delete'=>0])->find();
|
if(!$product_price){
|
unset($productList[$key]);
|
}
|
|
// 商品单价
|
$item['product_price'] = $product_price['product_new_price'];
|
//商品服务费
|
if(!empty($item["product_service"]) && isset($params["service_index"])){
|
$product_service = $item["product_service"][$params["service_index"]];
|
$item['service_name'] = $product_service["service_name"];
|
$item['service_price'] = $product_service["price"];
|
}
|
// 商品购买数量
|
$item['total_num'] = $params['product_num'];
|
// 商品购买总金额
|
$item['total_price'] = helper::bcmul($item['product_price'], $params['product_num']);
|
}
|
$supplierData[] = [
|
'shop_supplier_id' => $product['shop_supplier_id'],
|
'supplier' => $product['supplier'],
|
'productList' => $productList
|
];
|
unset($product['supplier']);
|
return $supplierData;
|
}
|
|
}
|