<?php
namespace app\common\model\sharing;
use app\common\model\BaseModel;
use app\common\library\helper;
class Goods extends BaseModel
{
protected $name = 'sharing_goods';
protected $append = ['goods_sales'];
public function getGoodsSalesAttr($value, $data)
{
return $data['sales_initial'] + $data['sales_actual'];
}
public function getAloneGradeEquityAttr($json)
{
return json_decode($json, true);
}
public function setAloneGradeEquityAttr($data)
{
return json_encode($data);
}
public function category()
{
return $this->belongsTo('Category');
}
public function sku()
{
return $this->hasMany('GoodsSku', 'goods_id')->order(['goods_sku_id' => 'asc']);
}
public function specRel()
{
$module = self::getCalledModule() ?: 'common';
return $this->belongsToMany(
"app\\{$module}\\model\\SpecValue",
'SharingGoodsSpecRel',
'spec_value_id',
'goods_id'
);
}
public function image()
{
$module = self::getCalledModule() ?: 'common';
return $this->hasMany("app\\{$module}\\model\\sharing\\GoodsImage", 'goods_id')
->order(['id' => 'asc']);
}
public function delivery()
{
$module = self::getCalledModule() ?: 'common';
return $this->BelongsTo("app\\{$module}\\model\\Delivery");
}
public function commentData()
{
return $this->hasMany('Comment', 'goods_id');
}
public function getGoodsStatusAttr($value)
{
$status = [10 => '上架', 20 => '下架'];
return ['text' => $status[$value], 'value' => $value];
}
public function getList($param)
{
$params = array_merge([
'status' => 10, 'category_id' => 0, 'search' => '', 'sortType' => 'all', 'sortPrice' => false, 'listRows' => 15, ], $param);
$filter = [];
$params['category_id'] > 0 && $filter['category_id'] = ['IN', Category::getSubCategoryId($params['category_id'])];
$params['status'] > 0 && $filter['goods_status'] = $params['status'];
!empty($params['search']) && $filter['goods_name'] = ['like', '%' . trim($params['search']) . '%'];
$sort = [];
if ($params['sortType'] === 'all') {
$sort = ['goods_sort', 'goods_id' => 'desc'];
} elseif ($params['sortType'] === 'sales') {
$sort = ['goods_sales' => 'desc'];
} elseif ($params['sortType'] === 'price') {
$sort = $params['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($params['listRows'], false, [
'query' => \request()->request()
]);
return $this->setGoodsListData($list, true);
}
protected function setGoodsListData(&$data, $isMultiple = true, callable $callback = null)
{
if (!$isMultiple) $dataSource = [&$data]; else $dataSource = &$data;
foreach ($dataSource as &$goods) {
$goodsSku = $goods['sku'][0];
$goods['goods_image'] = $goods['image'][0]['file_path'];
$goods['goods_sku'] = $goodsSku;
is_callable($callback) && call_user_func($callback, $goods);
}
return $data;
}
public function getListByIds($goodsIds, $status = null)
{
$filter = ['goods_id' => ['in', $goodsIds]];
$status > 0 && $filter['goods_status'] = $status;
if (!empty($goodsIds)) {
$this->orderRaw('field(goods_id, ' . implode(',', $goodsIds) . ')');
}
$data = $this->with(['category', 'image.file', 'sku', 'spec_rel.spec', 'delivery.rule'])
->where($filter)
->select();
if ($data->isEmpty()) return $data;
foreach ($data as &$item) {
$item['goods_image'] = $item['image'][0]['file_path'];
}
return $data;
}
public function getManySpecData($spec_rel, $skuData)
{
$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'],
];
}
$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];
}
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;
}
public static function detail($goodsId)
{
$model = (new static)->with([
'category',
'image.file',
'sku.image',
'spec_rel.spec',
])->where('goods_id', '=', $goodsId)
->find();
return $model->setGoodsListData($model, false);
}
public static function getGoodsSku($goods, $specSkuId)
{
$goodsSku = [];
foreach ($goods['sku'] as $item) {
if ($item['spec_sku_id'] == $specSkuId) {
$goodsSku = $item;
break;
}
}
if (empty($goodsSku)) {
return false;
}
$goodsSku['goods_attr'] = '';
if ($goods['spec_type'] == 20) {
$specRelData = helper::arrayColumn2Key($goods['spec_rel'], 'spec_value_id');
$attrs = explode('_', $goodsSku['spec_sku_id']);
foreach ($attrs as $specValueId) {
$goodsSku['goods_attr'] .= $specRelData[$specValueId]['spec']['spec_name'] . ':'
. $specRelData[$specValueId]['spec_value'] . '; ';
}
}
return $goodsSku;
}
public function updateStockSales($goodsList)
{
$goodsSave = [];
$goodsSpecSave = [];
foreach ($goodsList as $goods) {
$goodsSave[] = [
'goods_id' => $goods['goods_id'],
'sales_actual' => ['inc', $goods['total_num']]
];
if ($goods['deduct_stock_type'] == 20) {
$goodsSpecSave[] = [
'data' => ['stock_num' => ['dec', $goods['total_num']]],
'where' => [
'goods_id' => $goods['goods_id'],
'spec_sku_id' => $goods['spec_sku_id'],
],
];
}
}
$this->allowField(true)->isUpdate()->saveAll($goodsSave);
!empty($goodsSpecSave) && (new GoodsSku)->updateAll($goodsSpecSave);
}
}