<?php
namespace app\api\model\sharp;
use app\common\library\helper;
use app\common\model\sharp\ActiveGoods as ActiveGoodsModel;
use app\api\model\Goods as GoodsModel;
class ActiveGoods extends ActiveGoodsModel
{
protected $hidden = [
'wxapp_id',
'create_time',
'update_time',
];
public static function getGoodsActive($activeTimeId, $sharpGoodsId)
{
return (new static)->with(['active', 'activeTime'])
->where('active_time_id', '=', $activeTimeId)
->where('sharp_goods_id', '=', $sharpGoodsId)
->find();
}
public function getGoodsActiveDetail($active, $sharpGoodsId, $isCheckStatus = true)
{
$goods = $this->getGoodsDetail($sharpGoodsId);
if (empty($goods)) return false;
if ($isCheckStatus == true && ($goods['is_delete'] || !$goods['status'])) {
$this->error = '很抱歉,秒杀商品不存在或已下架';
return false;
}
$goods['sales_actual'] = $active['sales_actual'];
$goods['progress'] = $this->getProgress($active['sales_actual'], $goods['seckill_stock']);
return $goods;
}
private function getGoodsDetail($sharpGoodsId)
{
$model = $this->getGoodsModel();
$sharpGoods = $model::detail($sharpGoodsId, ['sku']);
if (empty($sharpGoods)) {
$this->error = '秒杀商品信息不存在';
return false;
}
$goods = GoodsModel::detail($sharpGoods['goods_id']);
if (empty($goods)) return false;
$goods['sharp_goods_id'] = $sharpGoods['sharp_goods_id'];
$goods['deduct_stock_type'] = $sharpGoods['deduct_stock_type'];
$goods['limit_num'] = $sharpGoods['limit_num'];
$goods['seckill_stock'] = $sharpGoods['seckill_stock'];
$goods['total_sales'] = $sharpGoods['total_sales'];
$goods['status'] = $sharpGoods['status'];
$goods['is_delete'] = $sharpGoods['is_delete'];
$goods['sku'] = $this->getSharpSku($sharpGoods['sku'], $goods['sku']);
return $goods->hidden(['category']);
}
protected function getSharpSku($sharpSku, $goodsSku)
{
$sharpSku = helper::arrayColumn2Key($sharpSku, 'spec_sku_id');
foreach ($goodsSku as &$item) {
$sharpSkuItem = clone $sharpSku[$item['spec_sku_id']];
$item['original_price'] = $item['goods_price'];
$item['seckill_price'] = $sharpSkuItem['seckill_price'];
$item['seckill_stock'] = $sharpSkuItem['seckill_stock'];
}
return $goodsSku;
}
}