<?php
namespace app\api\service\sharp;
use app\api\service\Basics;
use app\api\model\Goods as GoodsModel;
use app\api\model\sharp\Goods as SharpGoodsModel;
use app\api\model\sharp\Active as ActiveModel;
use app\api\model\sharp\ActiveTime as ActiveTimeModel;
use app\api\model\sharp\ActiveGoods as ActiveGoodsModel;
use app\common\enum\sharp\GoodsStatus as GoodsStatusEnum;
use app\common\enum\sharp\ActiveStatus as ActiveStatusEnum;
use app\common\library\helper;
class Active extends Basics
{
private $ActiveModel;
private $ActiveTimeModel;
public function __construct()
{
$this->ActiveModel = new ActiveModel;
$this->ActiveTimeModel = new ActiveTimeModel;
}
public function getHallIndex()
{
$tabbar = $this->getActiveTabbar();
if (empty($tabbar)) return ['tabbar' => [], 'goodsList' => []];
$goodsList = $this->getGoodsListByActiveTimeId($tabbar[0]['active_time_id']);
return compact('tabbar', 'goodsList');
}
public function getSharpModular($goodsParm = [])
{
$tabbar = $this->getActiveTabbar();
if (empty($tabbar)) return ['active' => null, 'goodsList' => []];
return [
'active' => $tabbar[0],
'goodsList' => $this->getGoodsListByActiveTimeId($tabbar[0]['active_time_id'], $goodsParm),
];
}
public function getGoodsListByActiveTimeId($activeTimeId, $goodsParm = [])
{
return ActiveGoodsModel::getGoodsListByActiveTimeId($activeTimeId, $goodsParm);
}
public function getyActiveGoodsDetail($activeTimeId, $sharpGoodsId)
{
$active = $this->getGoodsActive($activeTimeId, $sharpGoodsId);
if (empty($active)) return false;
$model = new ActiveGoodsModel;
$goods = $model->getGoodsActiveDetail($active, $sharpGoodsId, true);
if (empty($goods)) {
$this->error = $model->getError();
return false;
}
$goods['goods_multi_spec'] = (new SharpGoodsModel)->getSpecData($goods);
$specData = (new SharpGoodsModel)->getSpecData($goods);
return compact('active', 'goods', 'specData');
}
public function getCheckoutGoodsList($activeTimeId, $sharpGoodsId, $goodsSkuId, $goodsNum)
{
$active = $this->getGoodsActive($activeTimeId, $sharpGoodsId);
if (empty($active)) return false;
$model = new ActiveGoodsModel;
$goods = $model->getGoodsActiveDetail($active, $sharpGoodsId, false);
if (empty($goods)) return false;
$goods['goods_sku'] = GoodsModel::getGoodsSku($goods, $goodsSkuId);
$goodsList = [$goods->hidden(['category', 'content', 'image', 'sku'])];
foreach ($goodsList as &$item) {
$item['goods_price'] = $item['goods_sku']['seckill_price'];
$item['line_price'] = $item['goods_sku']['original_price'];
$item['spec_sku_id'] = $item['goods_sku']['spec_sku_id'];
$item['goods_source_id'] = $item['sharp_goods_id'];
$item['total_num'] = $goodsNum;
$item['total_price'] = helper::bcmul($item['goods_price'], $goodsNum);
}
return $goodsList;
}
private function getGoodsActive($activeTimeId, $sharpGoodsId)
{
$model = $this->getActiveGoods($activeTimeId, $sharpGoodsId);
if (empty($model) || !($model['active']['status'] && $model['active_time']['status'])) {
$this->error = '很抱歉,该活动不存在或已结束';
return false;
}
$startTime = $model['active']['active_date'] + ($model->active_time->getData('active_time') * 60 * 60);
$endTime = $startTime + (1 * 60 * 60);
$activeStatus = $this->getActivcGoodsStatus($startTime, $endTime);
$data = [
'active_id' => $model['active_id'],
'active_time_id' => $model['active_time_id'],
'active_time' => $model['active_time']['active_time'],
'sales_actual' => $model['sales_actual'],
'start_time' => $this->onFormatTime($startTime),
'end_time' => $this->onFormatTime($endTime),
'active_status' => $activeStatus,
'count_down_time' => $this->getGoodsActiveCountDownTime($activeStatus, $startTime, $endTime),
'wxapp_id' => $model['wxapp_id'],
];
return $data;
}
public function getActiveGoods($activeTimeId, $sharpGoodsId)
{
static $data = [];
if (!isset($data["{$activeTimeId}_{$sharpGoodsId}"])) {
$model = ActiveGoodsModel::getGoodsActive($activeTimeId, $sharpGoodsId);
!empty($model) && $data["{$activeTimeId}_{$sharpGoodsId}"] = $model;
}
return $data["{$activeTimeId}_{$sharpGoodsId}"];
}
private function getGoodsActiveCountDownTime($activeStatus, $startTime, $endTime)
{
if ($activeStatus == GoodsStatusEnum::STATE_BEGIN) {
return $this->onFormatTime($startTime);
}
if ($activeStatus == GoodsStatusEnum::STATE_SOON) {
return $this->onFormatTime($endTime);
}
return false;
}
private function getActivcGoodsStatus($startTime, $endTime)
{
$nowTime = time();
if ($nowTime < $startTime) {
return GoodsStatusEnum::STATE_SOON;
}
if ($nowTime >= $startTime && $nowTime < $endTime) {
return GoodsStatusEnum::STATE_BEGIN;
}
return GoodsStatusEnum::STATE_END;
}
private function getActiveTabbar()
{
$todyActive = $this->ActiveModel->getNowActive();
$data = [];
if (!empty($todyActive)) {
$data[] = $this->getBeginActive($todyActive);
$data = array_merge($data, $this->getSoonActive($todyActive));
}
$data[] = $this->getNoticeActive();
return array_values(array_filter($data));
}
private function getBeginActive($todyActive)
{
$model = $this->ActiveTimeModel->getNowActiveTime($todyActive['active_id']);
if (empty($model)) return [];
$startTime = $todyActive['active_date'] + ($model->getData('active_time') * 60 * 60);
$endTime = $startTime + (1 * 60 * 60);
return [
'active_id' => $todyActive['active_id'],
'active_time_id' => $model['active_time_id'],
'active_time' => $model['active_time'],
'start_time' => $this->onFormatTime($startTime),
'end_time' => $this->onFormatTime($endTime),
'count_down_time' => $this->onFormatTime($endTime),
'status' => ActiveStatusEnum::ACTIVE_STATE_BEGIN,
'status_text' => '已开抢',
'status_text2' => '正在疯抢',
'sharp_modular_text' => '正在疯抢',
];
}
private function getSoonActive($todyActive)
{
$list = $this->ActiveTimeModel->getNextActiveTimes($todyActive['active_id']);
if (empty($list) || $list->isEmpty()) return [];
$data = [];
foreach ($list as $item) {
$startTime = $todyActive['active_date'] + ($item->getData('active_time') * 60 * 60);
$endTime = $startTime + (1 * 60 * 60);
$data[] = [
'active_id' => $todyActive['active_id'],
'active_time_id' => $item['active_time_id'],
'active_time' => $item['active_time'],
'start_time' => $this->onFormatTime($startTime),
'end_time' => $this->onFormatTime($endTime),
'count_down_time' => $this->onFormatTime($startTime),
'status' => ActiveStatusEnum::ACTIVE_STATE_SOON,
'status_text' => '即将开抢',
'status_text2' => '即将开抢',
'sharp_modular_text' => "{$item['active_time']} 场预告",
];
}
return $data;
}
private function getNoticeActive()
{
$nextActive = $this->ActiveModel->getNextActive();
if (empty($nextActive)) return [];
$model = $this->ActiveTimeModel->getRecentActiveTime($nextActive['active_id']);
if (empty($model)) return [];
$startTime = $nextActive['active_date'] + ($model->getData('active_time') * 60 * 60);
$endTime = $startTime + (1 * 60 * 60);
return [
'active_id' => $nextActive['active_id'],
'active_time_id' => $model['active_time_id'],
'active_time' => $model['active_time'],
'start_time' => $this->onFormatTime($startTime),
'end_time' => $this->onFormatTime($endTime),
'count_down_time' => $this->onFormatTime($startTime),
'status' => ActiveStatusEnum::ACTIVE_STATE_NOTICE,
'status_text' => '预告',
'status_text2' => $this->onFormatTime($startTime) . ' 开始',
'sharp_modular_text' => $this->onFormatTime($startTime) . ' 开始',
];
}
private function onFormatTime($timeStamp)
{
return date('Y-m-d H:i', $timeStamp);
}
}