Active.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  1. <?php
  2. namespace app\api\service\sharp;
  3. use app\api\service\Basics;
  4. use app\api\model\Goods as GoodsModel;
  5. use app\api\model\sharp\Goods as SharpGoodsModel;
  6. use app\api\model\sharp\Active as ActiveModel;
  7. use app\api\model\sharp\ActiveTime as ActiveTimeModel;
  8. use app\api\model\sharp\ActiveGoods as ActiveGoodsModel;
  9. use app\common\enum\sharp\GoodsStatus as GoodsStatusEnum;
  10. use app\common\enum\sharp\ActiveStatus as ActiveStatusEnum;
  11. use app\common\library\helper;
  12. /**
  13. * 秒杀活动服务类
  14. * Class Active
  15. * @package app\api\service\sharp
  16. */
  17. class Active extends Basics
  18. {
  19. private $ActiveModel;
  20. private $ActiveTimeModel;
  21. /**
  22. * 构造方法
  23. * Active constructor.
  24. */
  25. public function __construct()
  26. {
  27. $this->ActiveModel = new ActiveModel;
  28. $this->ActiveTimeModel = new ActiveTimeModel;
  29. }
  30. /**
  31. * 获取秒杀活动会场首页数据
  32. * @return array
  33. * @throws \think\db\exception\DataNotFoundException
  34. * @throws \think\db\exception\ModelNotFoundException
  35. * @throws \think\exception\DbException
  36. */
  37. public function getHallIndex()
  38. {
  39. // 获取秒杀首页顶部菜单
  40. $tabbar = $this->getActiveTabbar();
  41. if (empty($tabbar)) return ['tabbar' => [], 'goodsList' => []];
  42. // 获取活动商品
  43. $goodsList = $this->getGoodsListByActiveTimeId($tabbar[0]['active_time_id']);
  44. return compact('tabbar', 'goodsList');
  45. }
  46. /**
  47. * 获取秒杀活动组件数据
  48. * @param array $goodsParm
  49. * @return array
  50. * @throws \think\db\exception\DataNotFoundException
  51. * @throws \think\db\exception\ModelNotFoundException
  52. * @throws \think\exception\DbException
  53. */
  54. public function getSharpModular($goodsParm = [])
  55. {
  56. // 获取秒杀活动列表
  57. $tabbar = $this->getActiveTabbar();
  58. if (empty($tabbar)) return ['active' => null, 'goodsList' => []];
  59. return [
  60. // 秒杀活动
  61. 'active' => $tabbar[0],
  62. // 活动商品列表
  63. 'goodsList' => $this->getGoodsListByActiveTimeId($tabbar[0]['active_time_id'], $goodsParm),
  64. ];
  65. }
  66. /**
  67. * 根据活动场次ID获取商品列表
  68. * @param int $activeTimeId
  69. * @param array $goodsParm
  70. * @return false|\PDOStatement|string|\think\Collection
  71. */
  72. public function getGoodsListByActiveTimeId($activeTimeId, $goodsParm = [])
  73. {
  74. return ActiveGoodsModel::getGoodsListByActiveTimeId($activeTimeId, $goodsParm);
  75. }
  76. /**
  77. * 获取活动商品详情
  78. * @param $activeTimeId
  79. * @param $sharpGoodsId
  80. * @return array|bool
  81. * @throws \think\exception\DbException
  82. */
  83. public function getActiveGoodsDetail($activeTimeId, $sharpGoodsId)
  84. {
  85. // 活动详情
  86. $active = $this->getGoodsActive($activeTimeId, $sharpGoodsId);
  87. if (empty($active)) return false;
  88. // 商品详情
  89. $model = new ActiveGoodsModel;
  90. $goods = $model->getGoodsActiveDetail($active, $sharpGoodsId, true);
  91. if (empty($goods)) {
  92. $this->error = $model->getError();
  93. return false;
  94. }
  95. // 商品多规格信息
  96. $goods['goods_multi_spec'] = (new SharpGoodsModel)->getSpecData($goods, $goods['sku']);
  97. return compact('active', 'goods');
  98. }
  99. /**
  100. * 获取订单提交的商品列表
  101. * @param $activeTimeId
  102. * @param $sharpGoodsId
  103. * @param $goodsSkuId
  104. * @param $goodsNum
  105. * @return array|bool
  106. * @throws \think\exception\DbException
  107. */
  108. public function getCheckoutGoodsList($activeTimeId, $sharpGoodsId, $goodsSkuId, $goodsNum)
  109. {
  110. // 活动详情
  111. $active = $this->getGoodsActive($activeTimeId, $sharpGoodsId);
  112. if (empty($active)) return false;
  113. // 商品详情
  114. $model = new ActiveGoodsModel;
  115. $goods = $model->getGoodsActiveDetail($active, $sharpGoodsId, false);
  116. if (empty($goods)) return false;
  117. // 商品sku信息
  118. $goods['goods_sku'] = GoodsModel::getGoodsSku($goods, $goodsSkuId);
  119. // 商品列表
  120. $goodsList = [$goods->hidden(['category', 'content', 'image', 'sku'])];
  121. foreach ($goodsList as &$item) {
  122. // 商品价格
  123. $item['goods_price'] = $item['goods_sku']['seckill_price'];
  124. $item['line_price'] = $item['goods_sku']['original_price'];
  125. // 商品id
  126. $item['spec_sku_id'] = $item['goods_sku']['spec_sku_id'];
  127. $item['goods_source_id'] = $item['sharp_goods_id'];
  128. // 商品购买数量
  129. $item['total_num'] = $goodsNum;
  130. // 商品购买总金额
  131. $item['total_price'] = helper::bcmul($item['goods_price'], $goodsNum);
  132. }
  133. return $goodsList;
  134. }
  135. /**
  136. * 活动详情
  137. * @param $activeTimeId
  138. * @param $sharpGoodsId
  139. * @return array|bool
  140. */
  141. private function getGoodsActive($activeTimeId, $sharpGoodsId)
  142. {
  143. // 获取活动商品的关联信息
  144. $model = $this->getActiveGoods($activeTimeId, $sharpGoodsId);
  145. if (empty($model) || !($model['active']['status'] && $model['active_time']['status'])) {
  146. $this->error = '很抱歉,该活动不存在或已结束';
  147. return false;
  148. }
  149. // 整理数据
  150. $startTime = $model['active']['active_date'] + ($model->active_time->getData('active_time') * 60 * 60);
  151. $endTime = $startTime + (1 * 60 * 60);
  152. $activeStatus = $this->getActivcGoodsStatus($startTime, $endTime);
  153. return [
  154. 'active_id' => $model['active_id'],
  155. 'active_time_id' => $model['active_time_id'],
  156. 'active_time' => $model['active_time']['active_time'],
  157. 'sales_actual' => $model['sales_actual'],
  158. 'start_time' => $this->onFormatTime($startTime),
  159. 'end_time' => $this->onFormatTime($endTime),
  160. 'active_status' => $activeStatus,
  161. 'count_down_time' => $this->getGoodsActiveCountDownTime($activeStatus, $startTime, $endTime),
  162. 'wxapp_id' => $model['wxapp_id'],
  163. ];
  164. }
  165. /**
  166. * 获取活动商品的关联信息
  167. * @param $activeTimeId
  168. * @param $sharpGoodsId
  169. * @return mixed
  170. */
  171. public function getActiveGoods($activeTimeId, $sharpGoodsId)
  172. {
  173. static $data = [];
  174. if (!isset($data["{$activeTimeId}_{$sharpGoodsId}"])) {
  175. $model = ActiveGoodsModel::getGoodsActive($activeTimeId, $sharpGoodsId);
  176. !empty($model) && $data["{$activeTimeId}_{$sharpGoodsId}"] = $model;
  177. }
  178. return $data["{$activeTimeId}_{$sharpGoodsId}"];
  179. }
  180. /**
  181. * 活动商品倒计时
  182. * @param $activeStatus
  183. * @param $startTime
  184. * @param $endTime
  185. * @return bool|false|string
  186. */
  187. private function getGoodsActiveCountDownTime($activeStatus, $startTime, $endTime)
  188. {
  189. if ($activeStatus == GoodsStatusEnum::STATE_BEGIN) {
  190. return $this->onFormatTime($startTime);
  191. }
  192. if ($activeStatus == GoodsStatusEnum::STATE_SOON) {
  193. return $this->onFormatTime($endTime);
  194. }
  195. return false;
  196. }
  197. /**
  198. * 活动商品状态
  199. * @param $startTime
  200. * @param $endTime
  201. * @return int
  202. */
  203. private function getActivcGoodsStatus($startTime, $endTime)
  204. {
  205. $nowTime = time();
  206. if ($nowTime < $startTime) {
  207. return GoodsStatusEnum::STATE_SOON;
  208. }
  209. if ($nowTime >= $startTime && $nowTime < $endTime) {
  210. return GoodsStatusEnum::STATE_BEGIN;
  211. }
  212. return GoodsStatusEnum::STATE_END;
  213. }
  214. /**
  215. * 获取秒杀首页顶部菜单
  216. * @return array
  217. * @throws \think\db\exception\DataNotFoundException
  218. * @throws \think\db\exception\ModelNotFoundException
  219. * @throws \think\exception\DbException
  220. */
  221. private function getActiveTabbar()
  222. {
  223. // 当天的活动
  224. $todyActive = $this->ActiveModel->getNowActive();
  225. $data = [];
  226. if (!empty($todyActive)) {
  227. // 当前进行中的活动
  228. $data[] = $this->getBeginActive($todyActive);
  229. // 获取即将开始的活动
  230. $data = array_merge($data, $this->getSoonActive($todyActive));
  231. }
  232. // 获取预告的活动
  233. $data[] = $this->getNoticeActive();
  234. return array_values(array_filter($data));
  235. }
  236. /**
  237. * 获取当前进行中的活动
  238. * @param $todyActive
  239. * @return array
  240. * @throws \think\db\exception\DataNotFoundException
  241. * @throws \think\db\exception\ModelNotFoundException
  242. * @throws \think\exception\DbException
  243. */
  244. private function getBeginActive($todyActive)
  245. {
  246. // 当前的时间点
  247. $model = $this->ActiveTimeModel->getNowActiveTime($todyActive['active_id']);
  248. if (empty($model)) return [];
  249. // 整理数据
  250. $startTime = $todyActive['active_date'] + ($model->getData('active_time') * 60 * 60);
  251. $endTime = $startTime + (1 * 60 * 60);
  252. return [
  253. 'active_id' => $todyActive['active_id'],
  254. 'active_time_id' => $model['active_time_id'],
  255. 'active_time' => $model['active_time'],
  256. 'start_time' => $this->onFormatTime($startTime),
  257. 'end_time' => $this->onFormatTime($endTime),
  258. 'count_down_time' => $this->onFormatTime($endTime),
  259. 'status' => ActiveStatusEnum::ACTIVE_STATE_BEGIN,
  260. 'status_text' => '已开抢',
  261. 'status_text2' => '正在疯抢',
  262. 'sharp_modular_text' => '正在疯抢',
  263. ];
  264. }
  265. /**
  266. * 获取即将开始的活动
  267. * @param $todyActive
  268. * @return array
  269. * @throws \think\db\exception\DataNotFoundException
  270. * @throws \think\db\exception\ModelNotFoundException
  271. * @throws \think\exception\DbException
  272. */
  273. private function getSoonActive($todyActive)
  274. {
  275. // 当前的时间点
  276. $list = $this->ActiveTimeModel->getNextActiveTimes($todyActive['active_id']);
  277. if (empty($list) || $list->isEmpty()) return [];
  278. // 整理数据
  279. $data = [];
  280. foreach ($list as $item) {
  281. $startTime = $todyActive['active_date'] + ($item->getData('active_time') * 60 * 60);
  282. $endTime = $startTime + (1 * 60 * 60);
  283. $data[] = [
  284. 'active_id' => $todyActive['active_id'],
  285. 'active_time_id' => $item['active_time_id'],
  286. 'active_time' => $item['active_time'],
  287. 'start_time' => $this->onFormatTime($startTime),
  288. 'end_time' => $this->onFormatTime($endTime),
  289. 'count_down_time' => $this->onFormatTime($startTime),
  290. 'status' => ActiveStatusEnum::ACTIVE_STATE_SOON,
  291. 'status_text' => '即将开抢',
  292. 'status_text2' => '即将开抢',
  293. 'sharp_modular_text' => "{$item['active_time']} 场预告",
  294. ];
  295. }
  296. return $data;
  297. }
  298. /**
  299. * 获取预告的活动
  300. * @throws \think\db\exception\DataNotFoundException
  301. * @throws \think\db\exception\ModelNotFoundException
  302. * @throws \think\exception\DbException
  303. */
  304. private function getNoticeActive()
  305. {
  306. // 下一场活动
  307. $nextActive = $this->ActiveModel->getNextActive();
  308. if (empty($nextActive)) return [];
  309. // 第一个时间点
  310. $model = $this->ActiveTimeModel->getRecentActiveTime($nextActive['active_id']);
  311. if (empty($model)) return [];
  312. // 整理数据
  313. $startTime = $nextActive['active_date'] + ($model->getData('active_time') * 60 * 60);
  314. $endTime = $startTime + (1 * 60 * 60);
  315. return [
  316. 'active_id' => $nextActive['active_id'],
  317. 'active_time_id' => $model['active_time_id'],
  318. 'active_time' => $model['active_time'],
  319. 'start_time' => $this->onFormatTime($startTime),
  320. 'end_time' => $this->onFormatTime($endTime),
  321. 'count_down_time' => $this->onFormatTime($startTime),
  322. 'status' => ActiveStatusEnum::ACTIVE_STATE_NOTICE,
  323. 'status_text' => '预告',
  324. 'status_text2' => $this->onFormatTime($startTime) . ' 开始',
  325. 'sharp_modular_text' => $this->onFormatTime($startTime) . ' 开始',
  326. ];
  327. }
  328. /**
  329. * 将时间戳格式化为日期时间
  330. * @param $timeStamp
  331. * @return false|string
  332. */
  333. private function onFormatTime($timeStamp)
  334. {
  335. return date('Y-m-d H:i', $timeStamp);
  336. }
  337. }