Active.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  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 getyActiveGoodsDetail($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);
  97. $specData = (new SharpGoodsModel)->getSpecData($goods);
  98. return compact('active', 'goods', 'specData');
  99. }
  100. /**
  101. * 获取订单提交的商品列表
  102. * @param $activeTimeId
  103. * @param $sharpGoodsId
  104. * @param $goodsSkuId
  105. * @param $goodsNum
  106. * @return array|bool
  107. * @throws \think\exception\DbException
  108. */
  109. public function getCheckoutGoodsList($activeTimeId, $sharpGoodsId, $goodsSkuId, $goodsNum)
  110. {
  111. // 活动详情
  112. $active = $this->getGoodsActive($activeTimeId, $sharpGoodsId);
  113. if (empty($active)) return false;
  114. // 商品详情
  115. $model = new ActiveGoodsModel;
  116. $goods = $model->getGoodsActiveDetail($active, $sharpGoodsId, false);
  117. if (empty($goods)) return false;
  118. // 商品sku信息
  119. $goods['goods_sku'] = GoodsModel::getGoodsSku($goods, $goodsSkuId);
  120. // 商品列表
  121. $goodsList = [$goods->hidden(['category', 'content', 'image', 'sku'])];
  122. foreach ($goodsList as &$item) {
  123. // 商品价格
  124. $item['goods_price'] = $item['goods_sku']['seckill_price'];
  125. $item['line_price'] = $item['goods_sku']['original_price'];
  126. // 商品id
  127. $item['spec_sku_id'] = $item['goods_sku']['spec_sku_id'];
  128. $item['goods_source_id'] = $item['sharp_goods_id'];
  129. // 商品购买数量
  130. $item['total_num'] = $goodsNum;
  131. // 商品购买总金额
  132. $item['total_price'] = helper::bcmul($item['goods_price'], $goodsNum);
  133. }
  134. return $goodsList;
  135. }
  136. /**
  137. * 活动详情
  138. * @param $activeTimeId
  139. * @param $sharpGoodsId
  140. * @return array|bool
  141. */
  142. private function getGoodsActive($activeTimeId, $sharpGoodsId)
  143. {
  144. // 获取活动商品的关联信息
  145. $model = $this->getActiveGoods($activeTimeId, $sharpGoodsId);
  146. if (empty($model) || !($model['active']['status'] && $model['active_time']['status'])) {
  147. $this->error = '很抱歉,该活动不存在或已结束';
  148. return false;
  149. }
  150. // 整理数据
  151. $startTime = $model['active']['active_date'] + ($model->active_time->getData('active_time') * 60 * 60);
  152. $endTime = $startTime + (1 * 60 * 60);
  153. $activeStatus = $this->getActivcGoodsStatus($startTime, $endTime);
  154. $data = [
  155. 'active_id' => $model['active_id'],
  156. 'active_time_id' => $model['active_time_id'],
  157. 'active_time' => $model['active_time']['active_time'],
  158. 'sales_actual' => $model['sales_actual'],
  159. 'start_time' => $this->onFormatTime($startTime),
  160. 'end_time' => $this->onFormatTime($endTime),
  161. 'active_status' => $activeStatus,
  162. 'count_down_time' => $this->getGoodsActiveCountDownTime($activeStatus, $startTime, $endTime),
  163. 'wxapp_id' => $model['wxapp_id'],
  164. ];
  165. return $data;
  166. }
  167. /**
  168. * 获取活动商品的关联信息
  169. * @param $activeTimeId
  170. * @param $sharpGoodsId
  171. * @return mixed
  172. */
  173. public function getActiveGoods($activeTimeId, $sharpGoodsId)
  174. {
  175. static $data = [];
  176. if (!isset($data["{$activeTimeId}_{$sharpGoodsId}"])) {
  177. $model = ActiveGoodsModel::getGoodsActive($activeTimeId, $sharpGoodsId);
  178. !empty($model) && $data["{$activeTimeId}_{$sharpGoodsId}"] = $model;
  179. }
  180. return $data["{$activeTimeId}_{$sharpGoodsId}"];
  181. }
  182. /**
  183. * 活动商品倒计时
  184. * @param $activeStatus
  185. * @param $startTime
  186. * @param $endTime
  187. * @return bool|false|string
  188. */
  189. private function getGoodsActiveCountDownTime($activeStatus, $startTime, $endTime)
  190. {
  191. if ($activeStatus == GoodsStatusEnum::STATE_BEGIN) {
  192. return $this->onFormatTime($startTime);
  193. }
  194. if ($activeStatus == GoodsStatusEnum::STATE_SOON) {
  195. return $this->onFormatTime($endTime);
  196. }
  197. return false;
  198. }
  199. /**
  200. * 活动商品状态
  201. * @param $startTime
  202. * @param $endTime
  203. * @return int
  204. */
  205. private function getActivcGoodsStatus($startTime, $endTime)
  206. {
  207. $nowTime = time();
  208. if ($nowTime < $startTime) {
  209. return GoodsStatusEnum::STATE_SOON;
  210. }
  211. if ($nowTime >= $startTime && $nowTime < $endTime) {
  212. return GoodsStatusEnum::STATE_BEGIN;
  213. }
  214. return GoodsStatusEnum::STATE_END;
  215. }
  216. /**
  217. * 获取秒杀首页顶部菜单
  218. * @return array
  219. * @throws \think\db\exception\DataNotFoundException
  220. * @throws \think\db\exception\ModelNotFoundException
  221. * @throws \think\exception\DbException
  222. */
  223. private function getActiveTabbar()
  224. {
  225. // 当天的活动
  226. $todyActive = $this->ActiveModel->getNowActive();
  227. $data = [];
  228. if (!empty($todyActive)) {
  229. // 当前进行中的活动
  230. $data[] = $this->getBeginActive($todyActive);
  231. // 获取即将开始的活动
  232. $data = array_merge($data, $this->getSoonActive($todyActive));
  233. }
  234. // 获取预告的活动
  235. $data[] = $this->getNoticeActive();
  236. return array_values(array_filter($data));
  237. }
  238. /**
  239. * 获取当前进行中的活动
  240. * @param $todyActive
  241. * @return array
  242. * @throws \think\db\exception\DataNotFoundException
  243. * @throws \think\db\exception\ModelNotFoundException
  244. * @throws \think\exception\DbException
  245. */
  246. private function getBeginActive($todyActive)
  247. {
  248. // 当前的时间点
  249. $model = $this->ActiveTimeModel->getNowActiveTime($todyActive['active_id']);
  250. if (empty($model)) return [];
  251. // 整理数据
  252. $startTime = $todyActive['active_date'] + ($model->getData('active_time') * 60 * 60);
  253. $endTime = $startTime + (1 * 60 * 60);
  254. return [
  255. 'active_id' => $todyActive['active_id'],
  256. 'active_time_id' => $model['active_time_id'],
  257. 'active_time' => $model['active_time'],
  258. 'start_time' => $this->onFormatTime($startTime),
  259. 'end_time' => $this->onFormatTime($endTime),
  260. 'count_down_time' => $this->onFormatTime($endTime),
  261. 'status' => ActiveStatusEnum::ACTIVE_STATE_BEGIN,
  262. 'status_text' => '已开抢',
  263. 'status_text2' => '正在疯抢',
  264. 'sharp_modular_text' => '正在疯抢',
  265. ];
  266. }
  267. /**
  268. * 获取即将开始的活动
  269. * @param $todyActive
  270. * @return array
  271. * @throws \think\db\exception\DataNotFoundException
  272. * @throws \think\db\exception\ModelNotFoundException
  273. * @throws \think\exception\DbException
  274. */
  275. private function getSoonActive($todyActive)
  276. {
  277. // 当前的时间点
  278. $list = $this->ActiveTimeModel->getNextActiveTimes($todyActive['active_id']);
  279. if (empty($list) || $list->isEmpty()) return [];
  280. // 整理数据
  281. $data = [];
  282. foreach ($list as $item) {
  283. $startTime = $todyActive['active_date'] + ($item->getData('active_time') * 60 * 60);
  284. $endTime = $startTime + (1 * 60 * 60);
  285. $data[] = [
  286. 'active_id' => $todyActive['active_id'],
  287. 'active_time_id' => $item['active_time_id'],
  288. 'active_time' => $item['active_time'],
  289. 'start_time' => $this->onFormatTime($startTime),
  290. 'end_time' => $this->onFormatTime($endTime),
  291. 'count_down_time' => $this->onFormatTime($startTime),
  292. 'status' => ActiveStatusEnum::ACTIVE_STATE_SOON,
  293. 'status_text' => '即将开抢',
  294. 'status_text2' => '即将开抢',
  295. 'sharp_modular_text' => "{$item['active_time']} 场预告",
  296. ];
  297. }
  298. return $data;
  299. }
  300. /**
  301. * 获取预告的活动
  302. * @throws \think\db\exception\DataNotFoundException
  303. * @throws \think\db\exception\ModelNotFoundException
  304. * @throws \think\exception\DbException
  305. */
  306. private function getNoticeActive()
  307. {
  308. // 下一场活动
  309. $nextActive = $this->ActiveModel->getNextActive();
  310. if (empty($nextActive)) return [];
  311. // 第一个时间点
  312. $model = $this->ActiveTimeModel->getRecentActiveTime($nextActive['active_id']);
  313. if (empty($model)) return [];
  314. // 整理数据
  315. $startTime = $nextActive['active_date'] + ($model->getData('active_time') * 60 * 60);
  316. $endTime = $startTime + (1 * 60 * 60);
  317. return [
  318. 'active_id' => $nextActive['active_id'],
  319. 'active_time_id' => $model['active_time_id'],
  320. 'active_time' => $model['active_time'],
  321. 'start_time' => $this->onFormatTime($startTime),
  322. 'end_time' => $this->onFormatTime($endTime),
  323. 'count_down_time' => $this->onFormatTime($startTime),
  324. 'status' => ActiveStatusEnum::ACTIVE_STATE_NOTICE,
  325. 'status_text' => '预告',
  326. 'status_text2' => $this->onFormatTime($startTime) . ' 开始',
  327. 'sharp_modular_text' => $this->onFormatTime($startTime) . ' 开始',
  328. ];
  329. }
  330. /**
  331. * 将时间戳格式化为日期时间
  332. * @param $timeStamp
  333. * @return false|string
  334. */
  335. private function onFormatTime($timeStamp)
  336. {
  337. return date('Y-m-d H:i', $timeStamp);
  338. }
  339. }