You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
283 lines
8.8 KiB
283 lines
8.8 KiB
<?php
|
|
|
|
namespace app\common\model\sharing;
|
|
|
|
use app\common\model\BaseModel;
|
|
|
|
/**
|
|
* 拼团商品模型
|
|
* Class Goods
|
|
* @package app\common\model\sharing
|
|
*/
|
|
class Goods extends BaseModel
|
|
{
|
|
protected $name = 'sharing_goods';
|
|
protected $append = ['goods_sales'];
|
|
|
|
/**
|
|
* 计算显示销量 (初始销量 + 实际销量)
|
|
* @param $value
|
|
* @param $data
|
|
* @return mixed
|
|
*/
|
|
public function getGoodsSalesAttr($value, $data)
|
|
{
|
|
return $data['sales_initial'] + $data['sales_actual'];
|
|
}
|
|
|
|
/**
|
|
* 关联商品分类表
|
|
* @return \think\model\relation\BelongsTo
|
|
*/
|
|
public function category()
|
|
{
|
|
return $this->belongsTo('Category');
|
|
}
|
|
|
|
/**
|
|
* 关联商品规格表
|
|
* @return \think\model\relation\HasMany
|
|
*/
|
|
public function sku()
|
|
{
|
|
return $this->hasMany('GoodsSku', 'goods_id')->order(['goods_sku_id' => 'asc']);
|
|
}
|
|
|
|
/**
|
|
* 关联商品规格关系表
|
|
* @return \think\model\relation\BelongsToMany
|
|
*/
|
|
public function specRel()
|
|
{
|
|
$module = self::getCalledModule() ?: 'common';
|
|
return $this->belongsToMany(
|
|
"app\\{$module}\\model\\SpecValue",
|
|
'SharingGoodsSpecRel',
|
|
'spec_value_id',
|
|
'goods_id'
|
|
);
|
|
}
|
|
|
|
/**
|
|
* 关联商品图片表
|
|
* @return \think\model\relation\HasMany
|
|
*/
|
|
public function image()
|
|
{
|
|
$module = self::getCalledModule() ?: 'common';
|
|
return $this->hasMany("app\\{$module}\\model\\sharing\\GoodsImage", 'goods_id')
|
|
->order(['id' => 'asc']);
|
|
}
|
|
|
|
/**
|
|
* 关联运费模板表
|
|
* @return \think\model\relation\BelongsTo
|
|
*/
|
|
public function delivery()
|
|
{
|
|
$module = self::getCalledModule() ?: 'common';
|
|
return $this->BelongsTo("app\\{$module}\\model\\Delivery");
|
|
}
|
|
|
|
/**
|
|
* 关联订单评价表
|
|
* @return \think\model\relation\HasMany
|
|
*/
|
|
public function commentData()
|
|
{
|
|
return $this->hasMany('Comment', 'goods_id');
|
|
}
|
|
|
|
/**
|
|
* 计费方式
|
|
* @param $value
|
|
* @return mixed
|
|
*/
|
|
public function getGoodsStatusAttr($value)
|
|
{
|
|
$status = [10 => '上架', 20 => '下架'];
|
|
return ['text' => $status[$value], 'value' => $value];
|
|
}
|
|
|
|
/**
|
|
* 获取商品列表
|
|
* @param null $status
|
|
* @param int $category_id
|
|
* @param string $search
|
|
* @param string $sortType
|
|
* @param bool $sortPrice
|
|
* @param int $listRows
|
|
* @return \think\Paginator
|
|
* @throws \think\exception\DbException
|
|
*/
|
|
public function getList(
|
|
$status = null,
|
|
$category_id = 0,
|
|
$search = '',
|
|
$sortType = 'all',
|
|
$sortPrice = false,
|
|
$listRows = 15
|
|
)
|
|
{
|
|
// 筛选条件
|
|
$filter = [];
|
|
$category_id > 0 && $filter['category_id'] = ['IN', Category::getSubCategoryId($category_id)];
|
|
$status > 0 && $filter['goods_status'] = $status;
|
|
!empty($search) && $filter['goods_name'] = ['like', '%' . trim($search) . '%'];
|
|
// 排序规则
|
|
$sort = [];
|
|
if ($sortType === 'all') {
|
|
$sort = ['goods_sort', 'goods_id' => 'desc'];
|
|
} elseif ($sortType === 'sales') {
|
|
$sort = ['goods_sales' => 'desc'];
|
|
} elseif ($sortType === 'price') {
|
|
$sort = $sortPrice ? ['goods_max_price' => 'desc'] : ['goods_min_price'];
|
|
}
|
|
// 商品表名称
|
|
$tableName = $this->getTable();
|
|
// 多规格商品 最高价与最低价
|
|
$GoodsSku = new GoodsSku;
|
|
$minPriceSql = $GoodsSku->field(['MIN(sharing_price)'])
|
|
->where('goods_id', 'EXP', "= `$tableName`.`goods_id`")->buildSql();
|
|
$maxPriceSql = $GoodsSku->field(['MAX(sharing_price)'])
|
|
->where('goods_id', 'EXP', "= `$tableName`.`goods_id`")->buildSql();
|
|
// 执行查询
|
|
$list = $this
|
|
->field(['*', '(sales_initial + sales_actual) as goods_sales',
|
|
"$minPriceSql AS goods_min_price",
|
|
"$maxPriceSql AS goods_max_price"
|
|
])
|
|
->with(['category', 'image.file', 'sku'])
|
|
->where('is_delete', '=', 0)
|
|
->where($filter)
|
|
->order($sort)
|
|
->paginate($listRows, false, [
|
|
'query' => \request()->request()
|
|
]);
|
|
return $list;
|
|
}
|
|
|
|
/**
|
|
* 商品多规格信息
|
|
* @param \think\Collection $spec_rel
|
|
* @param \think\Collection $skuData
|
|
* @return array
|
|
*/
|
|
public function getManySpecData($spec_rel, $skuData)
|
|
{
|
|
// spec_attr
|
|
$specAttrData = [];
|
|
foreach ($spec_rel->toArray() as $item) {
|
|
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->toArray() as $item) {
|
|
$image = (isset($item['image']) && !empty($item['image'])) ? $item['image'] : ['file_id' => 0, 'file_path' => ''];
|
|
$specListData[] = [
|
|
'goods_sku_id' => $item['goods_sku_id'],
|
|
'spec_sku_id' => $item['spec_sku_id'],
|
|
'rows' => [],
|
|
'form' => [
|
|
'image_id' => $image['file_id'],
|
|
'image_path' => $image['file_path'],
|
|
'goods_no' => $item['goods_no'],
|
|
'goods_price' => $item['goods_price'],
|
|
'sharing_price' => $item['sharing_price'],
|
|
'goods_weight' => $item['goods_weight'],
|
|
'line_price' => $item['line_price'],
|
|
'stock_num' => $item['stock_num'],
|
|
],
|
|
];
|
|
}
|
|
return ['spec_attr' => array_values($specAttrData), 'spec_list' => $specListData];
|
|
}
|
|
|
|
/**
|
|
* 多规格表格数据
|
|
* @param $goods
|
|
* @return array
|
|
*/
|
|
public function getManySpecTable(&$goods)
|
|
{
|
|
$specData = $this->getManySpecData($goods['spec_rel'], $goods['sku']);
|
|
$totalRow = count($specData['spec_list']);
|
|
foreach ($specData['spec_list'] as $i => &$sku) {
|
|
$rowData = [];
|
|
$rowCount = 1;
|
|
foreach ($specData['spec_attr'] as $attr) {
|
|
$skuValues = $attr['spec_items'];
|
|
$rowCount *= count($skuValues);
|
|
$anInterBankNum = ($totalRow / $rowCount);
|
|
$point = (($i / $anInterBankNum) % count($skuValues));
|
|
if (0 === ($i % $anInterBankNum)) {
|
|
$rowData[] = [
|
|
'rowspan' => $anInterBankNum,
|
|
'item_id' => $skuValues[$point]['item_id'],
|
|
'spec_value' => $skuValues[$point]['spec_value']
|
|
];
|
|
}
|
|
}
|
|
$sku['rows'] = $rowData;
|
|
}
|
|
return $specData;
|
|
}
|
|
|
|
/**
|
|
* 获取商品详情
|
|
* @param $goods_id
|
|
* @return static|false|\PDOStatement|string|\think\Model
|
|
*/
|
|
public static function detail($goods_id)
|
|
{
|
|
$model = new static;
|
|
return $model->with([
|
|
'category',
|
|
'image.file',
|
|
'sku.image',
|
|
'spec_rel.spec',
|
|
'delivery.rule',
|
|
'commentData' => function ($query) {
|
|
$query->with('user')->where(['is_delete' => 0, 'status' => 1])->limit(2);
|
|
}
|
|
])->withCount(['commentData' => function ($query) {
|
|
$query->where(['is_delete' => 0, 'status' => 1]);
|
|
}])->where('goods_id', '=', $goods_id)->find();
|
|
}
|
|
|
|
/**
|
|
* 商品多规格信息
|
|
* @param $goods_sku_id
|
|
* @return array|bool
|
|
*/
|
|
public function getGoodsSku($goods_sku_id)
|
|
{
|
|
$goodsSkuData = array_column($this['sku']->toArray(), null, 'spec_sku_id');
|
|
if (!isset($goodsSkuData[$goods_sku_id])) {
|
|
return false;
|
|
}
|
|
$goods_sku = $goodsSkuData[$goods_sku_id];
|
|
// 多规格文字内容
|
|
$goods_sku['goods_attr'] = '';
|
|
if ($this['spec_type'] == 20) {
|
|
$attrs = explode('_', $goods_sku['spec_sku_id']);
|
|
$spec_rel = array_column($this['spec_rel']->toArray(), null, 'spec_value_id');
|
|
foreach ($attrs as $specValueId) {
|
|
$goods_sku['goods_attr'] .= $spec_rel[$specValueId]['spec']['spec_name'] . ':'
|
|
. $spec_rel[$specValueId]['spec_value'] . '; ';
|
|
}
|
|
}
|
|
return $goods_sku;
|
|
}
|
|
|
|
}
|
|
|