6
0
Fork 0
jiqu-library-server/app/Traits/Release.php

162 lines
4.4 KiB
PHP

<?php
namespace App\Traits;
use App\Models\ProductSku;
use App\Models\ProductSkuVerify;
trait Release
{
/**
* SKU上架
*
* @param array|ProductSku $data
* @return void
*/
public static function skuUp(array|ProductSku $ids)
{
$nowTime = now();
if ($ids instanceof ProductSku) {
$ids[] = $ids->id;
}
$query = ProductSku::query()->whereIn('id', $ids)->where('verify_state', 0)->whereNull('release_at');
//执行上架审核动作
$query->update([
'verify_state'=> 1,
]);
ProductSkuVerify::query()->insert(array_map(function ($key) use ($nowTime) {
return [
'sku_id' => $key,
'created_at' => $nowTime,
'updated_at' => $nowTime,
];
}, $ids));
}
/**
* SKU下架操作
*
* @param array|ProductSku $ids
* @return void
*/
public static function skuDown(array|ProductSku $ids)
{
if ($ids instanceof ProductSku) {
$ids[] = $ids->id;
}
$query = ProductSku::query()->whereIn('id', $ids)->where('verify_state', 0)->whereNotNull('release_at');
//执行下架动作
$query->update([
'release_at'=> null,
]);
}
/**
* 取消SKU上架申请
*
* @param array|ProductSku $ids
* @return void
*/
public static function releaseCancel(array|ProductSku $ids)
{
if ($ids instanceof ProductSku) {
$ids[] = $ids->id;
}
$query = ProductSku::query()->whereIn('id', $ids)->where('verify_state', 1)->whereNull('release_at');
//执行取消上架申请
$query->update([
'verify_state'=> 0,
]);
ProductSkuVerify::whereIn('sku_id', $ids)->where('status', 0)->update(['status'=>3]);
}
/**
* 审核SKU
*
* @param array $ids
* @param integer $status
* @param string $remarks
* @return void
*/
public static function skuVerify(array $ids, int $status, ?string $remarks)
{
//获得审核中商品
$query = ProductSku::query()->whereIn('id', $ids)->where('verify_state', 1)->whereNull('release_at');
switch ($status) {
case 1://成功
$query->update([
'verify_state'=> 0,
'release_at'=>now(),
]);
break;
case 2://失败
$query->update([
'verify_state'=> 2,
]);
break;
default:
break;
}
ProductSkuVerify::whereIn('sku_id', $ids)->where('status', 0)->update([
'status'=>$status,
'remarks'=>$remarks,
]);
return;
}
/**
* 通过ID同步SPU
*
* @param integer $id
* @return void
*/
public static function skuSyncSpuById(int $id)
{
static::skuSyncSpu(ProductSku::with('spu')->findOrFail($id));
}
/**
* 同步spu的基本信息
*
* @param ProductSku $sku
* @return void
*/
public static function skuSyncSpu(ProductSku $sku)
{
$_price = 0;
foreach ($sku->specs as $id => $value) {
$spuSpec = $sku->spu->specs->first(function ($item) use ($id) {
return $item->id == $id;
});
foreach ($spuSpec->items as $item) {
if ($item['name'] == $value) {
$_price += $item['value'];
}
}
}
$sku->update(
[
'name' => trim($sku->spu->name.' '.implode(',', $sku->specs)),
'subtitle'=> $sku->spu->subtitle,
'cover' => $sku->spu->cover,
// 'images' => $sku->spu->images,
'description' => $sku->spu->description,
'sell_price' => bcadd($sku->spu->sell_price, $_price),
'market_price' => $sku->spu->market_price,
'cost_price' => $sku->spu->cost_price,
'vip_price' => !is_null($sku->spu->vip_price) ? bcadd($sku->spu->vip_price, $_price) : null,
'media' => $sku->spu->media,
'weight' => $sku->spu->weight,
'attrs' => $sku->spu->attrs,
'verify_state' => 0, //默认调整为正常
]
);
}
}