StoreSeckillServices.php 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2023 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. declare (strict_types=1);
  12. namespace app\services\activity\seckill;
  13. use app\Request;
  14. use app\services\BaseServices;
  15. use app\dao\activity\seckill\StoreSeckillDao;
  16. use app\services\order\StoreOrderServices;
  17. use app\services\other\QrcodeServices;
  18. use app\services\product\product\StoreCategoryServices;
  19. use app\services\product\product\StoreDescriptionServices;
  20. use app\services\product\product\StoreProductRelationServices;
  21. use app\services\product\product\StoreProductReplyServices;
  22. use app\services\product\product\StoreProductServices;
  23. use app\services\product\sku\StoreProductAttrResultServices;
  24. use app\services\product\sku\StoreProductAttrServices;
  25. use app\services\product\sku\StoreProductAttrValueServices;
  26. use app\services\system\config\SystemGroupDataServices;
  27. use crmeb\exceptions\AdminException;
  28. use app\jobs\ProductLogJob;
  29. use crmeb\exceptions\ApiException;
  30. use crmeb\services\CacheService;
  31. use crmeb\utils\Arr;
  32. /**
  33. *
  34. * Class StoreSeckillServices
  35. * @package app\services\activity
  36. * @method getSeckillIdsArray(array $ids, array $field)
  37. * @method get(int $id, array $field) 获取一条数据
  38. */
  39. class StoreSeckillServices extends BaseServices
  40. {
  41. /**
  42. * StoreSeckillServices constructor.
  43. * @param StoreSeckillDao $dao
  44. */
  45. public function __construct(StoreSeckillDao $dao)
  46. {
  47. $this->dao = $dao;
  48. }
  49. public function getCount(array $where)
  50. {
  51. $this->dao->count($where);
  52. }
  53. /**
  54. * 秒杀是否存在
  55. * @param int $id
  56. * @return int
  57. */
  58. public function getSeckillCount(int $id = 0, string $field = 'time_id')
  59. {
  60. $where = [];
  61. $where[] = ['is_del', '=', 0];
  62. $where[] = ['status', '=', 1];
  63. if ($id) {
  64. $time = time();
  65. $where[] = ['id', '=', $id];
  66. $where[] = ['start_time', '<=', $time];
  67. $where[] = ['stop_time', '>=', $time - 86400];
  68. $seckill_one = $this->dao->getOne($where, $field);
  69. if (!$seckill_one) {
  70. throw new ApiException(410322);
  71. }
  72. /** @var SystemGroupDataServices $systemGroupDataService */
  73. $systemGroupDataService = app()->make(SystemGroupDataServices::class);
  74. $seckillTime = array_column($systemGroupDataService->getConfigNameValue('routine_seckill_time'), null, 'id');
  75. $config = $seckillTime[$seckill_one['time_id']] ?? false;
  76. if (!$config) {
  77. throw new ApiException(410322);
  78. }
  79. $now_hour = date('H', time());
  80. $start_hour = $config['time'];
  81. $end_hour = (int)$start_hour + (int)$config['continued'];
  82. if ($start_hour <= $now_hour && $end_hour > $now_hour) {
  83. return $seckill_one;
  84. } else if ($start_hour > $now_hour) {
  85. throw new ApiException(410321);
  86. } else {
  87. throw new ApiException(410322);
  88. }
  89. } else {
  90. $seckillTime = sys_data('routine_seckill_time') ?: [];//秒杀时间段
  91. $timeInfo = ['time' => 0, 'continued' => 0];
  92. foreach ($seckillTime as $key => $value) {
  93. $currentHour = date('H');
  94. $activityEndHour = (int)$value['time'] + (int)$value['continued'];
  95. if ($currentHour >= (int)$value['time'] && $currentHour < $activityEndHour && $activityEndHour < 24) {
  96. $timeInfo = $value;
  97. break;
  98. }
  99. }
  100. if ($timeInfo['time'] == 0) return 0;
  101. $activityEndHour = $timeInfo['time'] + (int)$timeInfo['continued'];
  102. $startTime = strtotime(date('Y-m-d')) + (int)$timeInfo['time'] * 3600;
  103. $stopTime = strtotime(date('Y-m-d')) + (int)$activityEndHour * 3600;
  104. $where[] = ['start_time', '<', $startTime];
  105. $where[] = ['stop_time', '>', $stopTime];
  106. return $this->dao->getCount($where);
  107. }
  108. }
  109. /**
  110. * 保存数据
  111. * @param int $id
  112. * @param array $data
  113. */
  114. public function saveData(int $id, array $data)
  115. {
  116. if ($data['section_time']) {
  117. [$start_time, $end_time] = $data['section_time'];
  118. if (strtotime($end_time) + 86400 < time()) {
  119. throw new AdminException(400507);
  120. }
  121. }
  122. $seckill = [];
  123. if ($id) {
  124. $seckill = $this->get((int)$id);
  125. if (!$seckill) {
  126. throw new AdminException(100026);
  127. }
  128. }
  129. //限制编辑
  130. if ($data['copy'] == 0 && $seckill) {
  131. if ($seckill['stop_time'] + 86400 < time()) {
  132. throw new AdminException(400508);
  133. }
  134. }
  135. if ($data['num'] < $data['once_num']) {
  136. throw new AdminException(400500);
  137. }
  138. if ($data['copy'] == 1) {
  139. $id = 0;
  140. unset($data['copy']);
  141. }
  142. $description = $data['description'];
  143. $detail = $data['attrs'];
  144. $items = $data['items'];
  145. $data['start_time'] = strtotime($data['section_time'][0]);
  146. $data['stop_time'] = strtotime($data['section_time'][1]);
  147. $data['image'] = $data['images'][0];
  148. $data['images'] = json_encode($data['images']);
  149. $data['price'] = min(array_column($detail, 'price'));
  150. $data['ot_price'] = min(array_column($detail, 'ot_price'));
  151. $data['quota'] = $data['quota_show'] = array_sum(array_column($detail, 'quota'));
  152. $data['stock'] = array_sum(array_column($detail, 'stock'));
  153. $data['logistics'] = implode(',', $data['logistics']);
  154. unset($data['section_time'], $data['description'], $data['attrs'], $data['items']);
  155. /** @var StoreDescriptionServices $storeDescriptionServices */
  156. $storeDescriptionServices = app()->make(StoreDescriptionServices::class);
  157. /** @var StoreProductAttrServices $storeProductAttrServices */
  158. $storeProductAttrServices = app()->make(StoreProductAttrServices::class);
  159. /** @var StoreProductServices $storeProductServices */
  160. $storeProductServices = app()->make(StoreProductServices::class);
  161. if ($data['quota'] > $storeProductServices->value(['id' => $data['product_id']], 'stock')) {
  162. throw new AdminException(400090);
  163. }
  164. $this->transaction(function () use ($id, $data, $description, $detail, $items, $storeDescriptionServices, $storeProductAttrServices, $storeProductServices) {
  165. if ($id) {
  166. $res = $this->dao->update($id, $data);
  167. $storeDescriptionServices->saveDescription((int)$id, $description, 1);
  168. $skuList = $storeProductServices->validateProductAttr($items, $detail, (int)$id, 1);
  169. $valueGroup = $storeProductAttrServices->saveProductAttr($skuList, (int)$id, 1);
  170. if (!$res) throw new AdminException(100007);
  171. } else {
  172. if (!$storeProductServices->getOne(['is_show' => 1, 'is_del' => 0, 'id' => $data['product_id']])) {
  173. throw new AdminException(400091);
  174. }
  175. $data['add_time'] = time();
  176. $res = $this->dao->save($data);
  177. $storeDescriptionServices->saveDescription((int)$res->id, $description, 1);
  178. $skuList = $storeProductServices->validateProductAttr($items, $detail, (int)$res->id, 1);
  179. $valueGroup = $storeProductAttrServices->saveProductAttr($skuList, (int)$res->id, 1);
  180. if (!$res) throw new AdminException(100022);
  181. }
  182. });
  183. }
  184. /**
  185. * 获取列表
  186. * @param array $where
  187. * @return array
  188. * @throws \think\db\exception\DataNotFoundException
  189. * @throws \think\db\exception\DbException
  190. * @throws \think\db\exception\ModelNotFoundException
  191. */
  192. public function systemPage(array $where)
  193. {
  194. [$page, $limit] = $this->getPageValue();
  195. $list = $this->dao->getList($where, $page, $limit);
  196. $count = $this->dao->count($where + ['is_del' => 0]);
  197. foreach ($list as &$item) {
  198. $item['store_name'] = $item['title'];
  199. if ($item['status']) {
  200. if ($item['start_time'] > time())
  201. $item['start_name'] = '未开始';
  202. else if (bcadd($item['stop_time'], '86400') < time())
  203. $item['start_name'] = '已结束';
  204. else if (bcadd($item['stop_time'], '86400') > time() && $item['start_time'] < time()) {
  205. $item['start_name'] = '进行中';
  206. }
  207. } else $item['start_name'] = '已结束';
  208. $end_time = $item['stop_time'] ? date('Y/m/d', (int)$item['stop_time']) : '';
  209. $item['_stop_time'] = $end_time;
  210. $item['stop_status'] = $item['stop_time'] + 86400 < time() ? 1 : 0;
  211. }
  212. return compact('list', 'count');
  213. }
  214. /**
  215. * 后台页面设计获取商品列表
  216. * @param $where
  217. * @return array
  218. * @throws \think\db\exception\DataNotFoundException
  219. * @throws \think\db\exception\DbException
  220. * @throws \think\db\exception\ModelNotFoundException
  221. */
  222. public function getDiySeckillList($where)
  223. {
  224. unset($where['is_show']);
  225. $where['storeProductId'] = true;
  226. $where['status'] = 1;
  227. [$page, $limit] = $this->getPageValue();
  228. $list = $this->dao->getList($where, $page, $limit);
  229. $count = $this->dao->getCount($where);
  230. $cateIds = implode(',', array_column($list, 'cate_id'));
  231. /** @var StoreCategoryServices $storeCategoryServices */
  232. $storeCategoryServices = app()->make(StoreCategoryServices::class);
  233. $cateList = $storeCategoryServices->getCateArray($cateIds);
  234. foreach ($list as &$item) {
  235. $cateName = '';
  236. $item['cate_name'] = '';
  237. if ($item['cate_id']) {
  238. $cateName = array_filter($cateList, function ($val) use ($item) {
  239. if (in_array($val['id'], explode(',', $item['cate_id']))) {
  240. return $val;
  241. }
  242. });
  243. $item['cate_name'] = implode(',', array_column($cateName, 'cate_name'));
  244. }
  245. $item['store_name'] = $item['title'];
  246. $item['price'] = floatval($item['price']);
  247. $item['is_product_type'] = 3;
  248. }
  249. return compact('count', 'list');
  250. }
  251. /**
  252. * 首页秒杀数据
  253. * @param $where
  254. * @return array|int
  255. * @throws \think\db\exception\DataNotFoundException
  256. * @throws \think\db\exception\DbException
  257. * @throws \think\db\exception\ModelNotFoundException
  258. */
  259. public function getHomeSeckillList($where)
  260. {
  261. $data = [];
  262. $seckillTime = sys_data('routine_seckill_time') ?: [];//秒杀时间段
  263. $today = strtotime(date('Y-m-d'));
  264. $timeInfo = ['time' => 0, 'continued' => 0];
  265. foreach ($seckillTime as $key => $value) {
  266. $currentHour = date('H');
  267. $activityEndHour = (int)$value['time'] + (int)$value['continued'];
  268. if ($currentHour >= (int)$value['time'] && $currentHour < $activityEndHour && $activityEndHour <= 24) {
  269. $timeInfo = $value;
  270. break;
  271. }
  272. }
  273. if ($timeInfo['time'] == 0) return [];
  274. $data['time'] = $timeInfo['time'] . ':00';
  275. $activityEndHour = bcadd($timeInfo['time'], $timeInfo['continued'], 0);
  276. $data['stop'] = (int)bcadd((string)$today, bcmul($activityEndHour, '3600', 0));
  277. $where['time_id'] = $timeInfo['id'];
  278. [$page, $limit] = $this->getPageValue();
  279. $data['list'] = $this->dao->getHomeList($where, $page, $limit);
  280. foreach ($data['list'] as &$item) {
  281. $item['price'] = floatval($item['price']);
  282. }
  283. return $data;
  284. }
  285. /**
  286. * 获取秒杀详情
  287. * @param int $id
  288. * @return array|\think\Model|null
  289. */
  290. public function getInfo(int $id)
  291. {
  292. $info = $this->dao->get($id);
  293. if ($info) {
  294. if ($info['start_time'])
  295. $start_time = date('Y-m-d', (int)$info['start_time']);
  296. if ($info['stop_time'])
  297. $stop_time = date('Y-m-d', (int)$info['stop_time']);
  298. if (isset($start_time) && isset($stop_time))
  299. $info['section_time'] = [$start_time, $stop_time];
  300. else
  301. $info['section_time'] = [];
  302. unset($info['start_time'], $info['stop_time']);
  303. $info['give_integral'] = intval($info['give_integral']);
  304. $info['price'] = floatval($info['price']);
  305. $info['ot_price'] = floatval($info['ot_price']);
  306. $info['postage'] = floatval($info['postage']);
  307. $info['cost'] = floatval($info['cost']);
  308. $info['weight'] = floatval($info['weight']);
  309. $info['volume'] = floatval($info['volume']);
  310. $info['logistics'] = explode(',', $info['logistics']);
  311. /** @var StoreDescriptionServices $storeDescriptionServices */
  312. $storeDescriptionServices = app()->make(StoreDescriptionServices::class);
  313. $info['description'] = $storeDescriptionServices->getDescription(['product_id' => $id, 'type' => 1]);
  314. $info['attrs'] = $this->attrList($id, $info['product_id']);
  315. }
  316. return $info;
  317. }
  318. /**
  319. * 获取规格
  320. * @param int $id
  321. * @param int $pid
  322. * @return mixed
  323. */
  324. public function attrList(int $id, int $pid)
  325. {
  326. /** @var StoreProductAttrResultServices $storeProductAttrResultServices */
  327. $storeProductAttrResultServices = app()->make(StoreProductAttrResultServices::class);
  328. $seckillResult = $storeProductAttrResultServices->value(['product_id' => $id, 'type' => 1], 'result');
  329. $items = json_decode($seckillResult, true)['attr'];
  330. $productAttr = $this->getAttr($items, $pid, 0);
  331. $seckillAttr = $this->getAttr($items, $id, 1);
  332. foreach ($productAttr as $pk => $pv) {
  333. foreach ($seckillAttr as &$sv) {
  334. if ($pv['detail'] == $sv['detail']) {
  335. $productAttr[$pk] = $sv;
  336. }
  337. }
  338. $productAttr[$pk]['detail'] = json_decode($productAttr[$pk]['detail']);
  339. }
  340. $attrs['items'] = $items;
  341. $attrs['value'] = $productAttr;
  342. foreach ($items as $key => $item) {
  343. $header[] = ['title' => $item['value'], 'key' => 'value' . ($key + 1), 'align' => 'center', 'minWidth' => 80];
  344. }
  345. $header[] = ['title' => '图片', 'slot' => 'pic', 'align' => 'center', 'minWidth' => 120];
  346. $header[] = ['title' => '秒杀价', 'slot' => 'price', 'align' => 'center', 'minWidth' => 80];
  347. $header[] = ['title' => '成本价', 'key' => 'cost', 'align' => 'center', 'minWidth' => 80];
  348. $header[] = ['title' => '原价', 'key' => 'ot_price', 'align' => 'center', 'minWidth' => 80];
  349. $header[] = ['title' => '库存', 'key' => 'stock', 'align' => 'center', 'minWidth' => 80];
  350. $header[] = ['title' => '限量', 'slot' => 'quota', 'type' => 1, 'align' => 'center', 'minWidth' => 80];
  351. $header[] = ['title' => '重量(KG)', 'key' => 'weight', 'align' => 'center', 'minWidth' => 80];
  352. $header[] = ['title' => '体积(m³)', 'key' => 'volume', 'align' => 'center', 'minWidth' => 80];
  353. $header[] = ['title' => '商品编号', 'key' => 'bar_code', 'align' => 'center', 'minWidth' => 80];
  354. $attrs['header'] = $header;
  355. return $attrs;
  356. }
  357. /**
  358. * 获取规格
  359. * @param $attr
  360. * @param $id
  361. * @param $type
  362. * @return array
  363. */
  364. public function getAttr($attr, $id, $type)
  365. {
  366. /** @var StoreProductAttrValueServices $storeProductAttrValueServices */
  367. $storeProductAttrValueServices = app()->make(StoreProductAttrValueServices::class);
  368. list($value, $head) = attr_format($attr);
  369. $valueNew = [];
  370. $count = 0;
  371. foreach ($value as $suk) {
  372. $detail = explode(',', $suk);
  373. $sukValue = $storeProductAttrValueServices->getColumn(['product_id' => $id, 'type' => $type, 'suk' => $suk], 'bar_code,cost,price,ot_price,stock,image as pic,weight,volume,brokerage,brokerage_two,quota', 'suk');
  374. if (count($sukValue)) {
  375. foreach ($detail as $k => $v) {
  376. $valueNew[$count]['value' . ($k + 1)] = $v;
  377. }
  378. $valueNew[$count]['detail'] = json_encode(array_combine($head, $detail));
  379. $valueNew[$count]['pic'] = $sukValue[$suk]['pic'] ?? '';
  380. $valueNew[$count]['price'] = $sukValue[$suk]['price'] ? floatval($sukValue[$suk]['price']) : 0;
  381. $valueNew[$count]['cost'] = $sukValue[$suk]['cost'] ? floatval($sukValue[$suk]['cost']) : 0;
  382. $valueNew[$count]['ot_price'] = isset($sukValue[$suk]['ot_price']) ? floatval($sukValue[$suk]['ot_price']) : 0;
  383. $valueNew[$count]['stock'] = $sukValue[$suk]['stock'] ? intval($sukValue[$suk]['stock']) : 0;
  384. $valueNew[$count]['quota'] = $sukValue[$suk]['quota'] ? intval($sukValue[$suk]['quota']) : 0;
  385. $valueNew[$count]['bar_code'] = $sukValue[$suk]['bar_code'] ?? '';
  386. $valueNew[$count]['weight'] = $sukValue[$suk]['weight'] ? floatval($sukValue[$suk]['weight']) : 0;
  387. $valueNew[$count]['volume'] = $sukValue[$suk]['volume'] ? floatval($sukValue[$suk]['volume']) : 0;
  388. $valueNew[$count]['brokerage'] = $sukValue[$suk]['brokerage'] ? floatval($sukValue[$suk]['brokerage']) : 0;
  389. $valueNew[$count]['brokerage_two'] = $sukValue[$suk]['brokerage_two'] ? floatval($sukValue[$suk]['brokerage_two']) : 0;
  390. $valueNew[$count]['_checked'] = $type != 0;
  391. $count++;
  392. }
  393. }
  394. return $valueNew;
  395. }
  396. /**
  397. * 获取某个时间段的秒杀列表
  398. * @param int $time
  399. * @return array
  400. * @throws \think\db\exception\DataNotFoundException
  401. * @throws \think\db\exception\DbException
  402. * @throws \think\db\exception\ModelNotFoundException
  403. */
  404. public function getListByTime(int $time)
  405. {
  406. [$page, $limit] = $this->getPageValue();
  407. $seckillInfo = $this->dao->getListByTime($time, $page, $limit);
  408. if (count($seckillInfo)) {
  409. foreach ($seckillInfo as $key => &$item) {
  410. if ($item['quota'] > 0) {
  411. $percent = (int)(($item['quota_show'] - $item['quota']) / $item['quota_show'] * 100);
  412. $item['percent'] = $percent;
  413. $item['stock'] = $item['quota'];
  414. } else {
  415. $item['percent'] = 100;
  416. $item['stock'] = 0;
  417. }
  418. $item['price'] = floatval($item['price']);
  419. $item['ot_price'] = floatval($item['ot_price']);
  420. }
  421. }
  422. return $seckillInfo;
  423. }
  424. /**
  425. * 获取秒杀详情
  426. * @param Request $request
  427. * @param int $id
  428. * @return mixed
  429. * @throws \think\db\exception\DataNotFoundException
  430. * @throws \think\db\exception\DbException
  431. * @throws \think\db\exception\ModelNotFoundException
  432. */
  433. public function seckillDetail(Request $request, int $id)
  434. {
  435. $uid = (int)$request->uid();
  436. $storeInfo = $this->dao->getOne(['id' => $id], '*', ['description']);
  437. if (!$storeInfo) {
  438. throw new ApiException(410294);
  439. } else {
  440. $storeInfo = $storeInfo->toArray();
  441. }
  442. $siteUrl = sys_config('site_url');
  443. $storeInfo['image'] = set_file_url($storeInfo['image'], $siteUrl);
  444. $storeInfo['image_base'] = set_file_url($storeInfo['image'], $siteUrl);
  445. $storeInfo['store_name'] = $storeInfo['title'];
  446. /** @var StoreProductServices $storeProductService */
  447. $storeProductService = app()->make(StoreProductServices::class);
  448. $productInfo = $storeProductService->get($storeInfo['product_id']);
  449. $storeInfo['total'] = $productInfo['sales'] + $productInfo['ficti'] + $storeInfo['sales'];
  450. if (sys_config('share_qrcode', 0) && request()->isWechat()) {
  451. /** @var QrcodeServices $qrcodeService */
  452. $qrcodeService = app()->make(QrcodeServices::class);
  453. $storeInfo['wechat_code'] = $qrcodeService->getTemporaryQrcode('seckill-' . $id, $uid)->url;
  454. } else {
  455. $storeInfo['wechat_code'] = '';
  456. }
  457. /** @var StoreOrderServices $storeOrderServices */
  458. $storeOrderServices = app()->make(StoreOrderServices::class);
  459. $data['buy_num'] = $storeOrderServices->getBuyCount($uid, 'seckill_id', $id);
  460. /** @var StoreProductRelationServices $storeProductRelationServices */
  461. $storeProductRelationServices = app()->make(StoreProductRelationServices::class);
  462. $storeInfo['userCollect'] = $storeProductRelationServices->isProductRelation(['uid' => $uid, 'product_id' => $storeInfo['product_id'], 'type' => 'collect', 'category' => 'product']);
  463. $storeInfo['userLike'] = false;
  464. $storeInfo['uid'] = $uid;
  465. if ($storeInfo['quota'] > 0) {
  466. $percent = (int)(($storeInfo['quota_show'] - $storeInfo['quota']) / $storeInfo['quota_show'] * 100);
  467. $storeInfo['percent'] = $percent;
  468. $storeInfo['stock'] = $storeInfo['quota'];
  469. } else {
  470. $storeInfo['percent'] = 100;
  471. $storeInfo['stock'] = 0;
  472. }
  473. //到期时间
  474. /** @var SystemGroupDataServices $groupDataService */
  475. $groupDataService = app()->make(SystemGroupDataServices::class);
  476. $timeInfo = json_decode($groupDataService->value(['id' => $storeInfo['time_id']], 'value'), true);
  477. $today = strtotime(date('Y-m-d'));
  478. $activityEndHour = $timeInfo['time']['value'] + $timeInfo['continued']['value'];
  479. $storeInfo['last_time'] = (int)bcadd((string)$today, (string)bcmul((string)$activityEndHour, '3600', 0));
  480. //获取秒杀商品状态
  481. if ($storeInfo['status'] == 1) {
  482. if ($storeInfo['start_time'] > time()) {
  483. $storeInfo['status'] = 2;
  484. } elseif (($storeInfo['stop_time'] + 86400) < time()) {
  485. $storeInfo['status'] = 0;
  486. } else {
  487. /** @var SystemGroupDataServices $systemGroupDataService */
  488. $systemGroupDataService = app()->make(SystemGroupDataServices::class);
  489. $seckillTime = array_column($systemGroupDataService->getConfigNameValue('routine_seckill_time'), null, 'id');
  490. $config = $seckillTime[$storeInfo['time_id']] ?? false;
  491. if (!$config) {
  492. throw new ApiException(410322);
  493. }
  494. $now_hour = date('H', time());
  495. $start_hour = $config['time'];
  496. $end_hour = (int)$start_hour + (int)$config['continued'];
  497. if ($start_hour <= $now_hour && $end_hour > $now_hour) {
  498. $storeInfo['status'] = 1;
  499. } else if ($start_hour > $now_hour) {
  500. $storeInfo['status'] = 2;
  501. } else {
  502. $storeInfo['status'] = 0;
  503. }
  504. }
  505. } else {
  506. $storeInfo['status'] == 0;
  507. }
  508. /** @var SystemGroupDataServices $groupDataService */
  509. $groupDataService = app()->make(SystemGroupDataServices::class);
  510. $timeInfo = json_decode($groupDataService->value(['id' => $storeInfo['time_id']], 'value'), true);
  511. $today = strtotime(date('Y-m-d'));
  512. $activityEndHour = $timeInfo['time']['value'] + $timeInfo['continued']['value'];
  513. $storeInfo['last_time'] = (int)bcadd((string)$today, (string)bcmul((string)$activityEndHour, '3600', 0));
  514. //商品详情
  515. $data['storeInfo'] = get_thumb_water($storeInfo, 'big', ['image', 'images']);
  516. $storeInfoNew = get_thumb_water($storeInfo, 'small');
  517. $data['storeInfo']['small_image'] = $storeInfoNew['image'];
  518. /** @var StoreProductReplyServices $storeProductReplyService */
  519. $storeProductReplyService = app()->make(StoreProductReplyServices::class);
  520. $data['reply'] = get_thumb_water($storeProductReplyService->getRecProductReply($storeInfo['product_id']), 'small', ['pics']);
  521. [$replyCount, $goodReply, $replyChance] = $storeProductReplyService->getProductReplyData((int)$storeInfo['product_id']);
  522. $data['replyChance'] = $replyChance;
  523. $data['replyCount'] = $replyCount;
  524. /** @var StoreProductAttrServices $storeProductAttrServices */
  525. $storeProductAttrServices = app()->make(StoreProductAttrServices::class);
  526. list($productAttr, $productValue) = $storeProductAttrServices->getProductAttrDetail($id, $uid, 0, 1, $storeInfo['product_id']);
  527. $data['productAttr'] = $productAttr;
  528. $data['productValue'] = $productValue;
  529. $data['routine_contact_type'] = sys_config('routine_contact_type', 0);
  530. //用户访问事件
  531. event('UserVisitListener', [$uid, $id, 'seckill', $storeInfo['product_id'], 'view']);
  532. //浏览记录
  533. ProductLogJob::dispatch(['visit', ['uid' => $uid, 'product_id' => $storeInfo['product_id']]]);
  534. return $data;
  535. }
  536. /**
  537. * 获取秒杀数据
  538. * @param array $ids
  539. * @param string $field
  540. * @return array
  541. * @throws \think\db\exception\DataNotFoundException
  542. * @throws \think\db\exception\DbException
  543. * @throws \think\db\exception\ModelNotFoundException
  544. */
  545. public function getSeckillColumn(array $ids, string $field = '')
  546. {
  547. $seckillProduct = $systemGroupData = [];
  548. $seckillInfoField = 'id,image,price,ot_price,postage,give_integral,sales,stock,title as store_name,unit_name,is_show,is_del,is_postage,cost,temp_id,weight,volume,start_time,stop_time,time_id';
  549. if (!empty($seckill_ids)) {
  550. $seckillProduct = $this->dao->idSeckillList($ids, $field ?: $seckillInfoField);
  551. if (!empty($seckillProduct)) {
  552. $timeIds = Arr::getUniqueKey($seckillProduct, 'time_id');
  553. $seckillProduct = array_combine(array_column($seckillProduct, 'id'), $seckillProduct);
  554. /** @var SystemGroupDataServices $groupServices */
  555. $groupServices = app()->make(SystemGroupDataServices::class);
  556. $systemGroupData = $groupServices->getGroupDataColumn($timeIds);
  557. }
  558. }
  559. return [$seckillProduct, $systemGroupData];
  560. }
  561. /**
  562. * 检查秒杀库存
  563. * @param int $uid
  564. * @param int $seckillId
  565. * @param int $cartNum
  566. * @param string $unique
  567. * @return array
  568. * @throws \think\db\exception\DataNotFoundException
  569. * @throws \think\db\exception\DbException
  570. * @throws \think\db\exception\ModelNotFoundException
  571. * @author 吴汐
  572. * @email 442384644@qq.com
  573. * @date 2023/03/01
  574. */
  575. public function checkSeckillStock(int $uid, int $seckillId, int $cartNum = 1, string $unique = '')
  576. {
  577. /** @var StoreProductAttrValueServices $attrValueServices */
  578. $attrValueServices = app()->make(StoreProductAttrValueServices::class);
  579. if ($unique == '') {
  580. $unique = $attrValueServices->value(['product_id' => $seckillId, 'type' => 1], 'unique');
  581. }
  582. //检查商品活动状态
  583. $StoreSeckillinfo = $this->getSeckillCount($seckillId, '*,title as store_name');
  584. if ($StoreSeckillinfo['once_num'] < $cartNum) {
  585. throw new ApiException(410313, ['num' => $StoreSeckillinfo['once_num']]);
  586. }
  587. /** @var StoreOrderServices $orderServices */
  588. $orderServices = app()->make(StoreOrderServices::class);
  589. $userBuyCount = $orderServices->getBuyCount($uid, 'seckill_id', $seckillId);
  590. if ($StoreSeckillinfo['num'] < ($userBuyCount + $cartNum)) {
  591. throw new ApiException(410298, ['num' => $StoreSeckillinfo['num']]);
  592. }
  593. if ($StoreSeckillinfo['num'] < $cartNum) {
  594. throw new ApiException(410317, ['num' => $StoreSeckillinfo['num']]);
  595. }
  596. $attrInfo = $attrValueServices->getOne(['product_id' => $seckillId, 'unique' => $unique, 'type' => 1]);
  597. if (!$attrInfo || $attrInfo['product_id'] != $seckillId) {
  598. throw new ApiException(410305);
  599. }
  600. if ($cartNum > $attrInfo['quota']) {
  601. throw new ApiException(410296);
  602. }
  603. return [$attrInfo, $unique, $StoreSeckillinfo];
  604. }
  605. /**
  606. * 修改秒杀库存
  607. * @param int $num
  608. * @param int $seckillId
  609. * @param string $unique
  610. * @return bool
  611. * @throws \think\db\exception\DataNotFoundException
  612. * @throws \think\db\exception\DbException
  613. * @throws \think\db\exception\ModelNotFoundException
  614. * @author 吴汐
  615. * @email 442384644@qq.com
  616. * @date 2023/03/01
  617. */
  618. public function decSeckillStock(int $num, int $seckillId, string $unique = '')
  619. {
  620. $product_id = $this->dao->value(['id' => $seckillId], 'product_id');
  621. if ($unique) {
  622. /** @var StoreProductAttrValueServices $skuValueServices */
  623. $skuValueServices = app()->make(StoreProductAttrValueServices::class);
  624. //减去秒杀商品的sku库存增加销量
  625. $res = false !== $skuValueServices->decProductAttrStock($seckillId, $unique, $num, 1);
  626. //减去秒杀库存
  627. $res = $res && $this->dao->decStockIncSales(['id' => $seckillId, 'type' => 1], $num);
  628. //减去当前普通商品sku的库存增加销量
  629. $suk = $skuValueServices->value(['unique' => $unique, 'product_id' => $seckillId, 'type' => 1], 'suk');
  630. $productUnique = $skuValueServices->value(['suk' => $suk, 'product_id' => $product_id, 'type' => 0], 'unique');
  631. if ($productUnique) {
  632. $res = $res && $skuValueServices->decProductAttrStock($product_id, $productUnique, $num);
  633. }
  634. } else {
  635. $res = false !== $this->dao->decStockIncSales(['id' => $seckillId, 'type' => 1], $num);
  636. }
  637. /** @var StoreProductServices $services */
  638. $services = app()->make(StoreProductServices::class);
  639. //减去普通商品库存
  640. return $res && $services->decProductStock($num, $product_id);
  641. }
  642. /**
  643. * 加库存减销量
  644. * @param int $num
  645. * @param int $seckillId
  646. * @param string $unique
  647. * @return bool
  648. */
  649. public function incSeckillStock(int $num, int $seckillId, string $unique = '')
  650. {
  651. $product_id = $this->dao->value(['id' => $seckillId], 'product_id');
  652. if ($unique) {
  653. /** @var StoreProductAttrValueServices $skuValueServices */
  654. $skuValueServices = app()->make(StoreProductAttrValueServices::class);
  655. //减去秒杀商品的sku库存增加销量
  656. $res = false !== $skuValueServices->incProductAttrStock($seckillId, $unique, $num, 1);
  657. //减去秒杀库存
  658. $res = $res && $this->dao->incStockDecSales(['id' => $seckillId, 'type' => 1], $num);
  659. //减去当前普通商品sku的库存增加销量
  660. $suk = $skuValueServices->value(['unique' => $unique, 'product_id' => $seckillId], 'suk');
  661. $productUnique = $skuValueServices->value(['suk' => $suk, 'product_id' => $product_id], 'unique');
  662. if ($productUnique) {
  663. $res = $res && $skuValueServices->incProductAttrStock($product_id, $productUnique, $num);
  664. }
  665. } else {
  666. $res = false !== $this->dao->incStockDecSales(['id' => $seckillId, 'type' => 1], $num);
  667. }
  668. /** @var StoreProductServices $services */
  669. $services = app()->make(StoreProductServices::class);
  670. //减去普通商品库存
  671. $res = $res && $services->incProductStock($num, $product_id);
  672. return $res;
  673. }
  674. /**
  675. * 获取一条秒杀商品
  676. * @param $id
  677. * @param string $field
  678. * @return array|false|\PDOStatement|string|\think\Model
  679. * @throws \think\db\exception\DataNotFoundException
  680. * @throws \think\db\exception\DbException
  681. * @throws \think\db\exception\ModelNotFoundException
  682. */
  683. public function getValidProduct($id, $field = '*')
  684. {
  685. return $this->dao->validProduct($id, $field);
  686. }
  687. /**
  688. * 秒杀统计
  689. * @param $id
  690. * @return array
  691. * @throws \think\db\exception\DataNotFoundException
  692. * @throws \think\db\exception\DbException
  693. * @throws \think\db\exception\ModelNotFoundException
  694. */
  695. public function seckillStatistics($id)
  696. {
  697. /** @var StoreOrderServices $orderServices */
  698. $orderServices = app()->make(StoreOrderServices::class);
  699. $pay_count = $orderServices->getDistinctCount([['seckill_id', '=', $id], ['paid', '=', 1], ['refund_type', 'in', [0, 3]]], 'uid', false);
  700. $order_count = $orderServices->getDistinctCount([['seckill_id', '=', $id], ['refund_type', 'in', [0, 3]]], 'uid', false);
  701. $all_price = $orderServices->sum([['seckill_id', '=', $id], ['refund_type', 'in', [0, 3]], ['paid', '=', 1]], 'pay_price');
  702. $seckillInfo = $this->dao->get($id);
  703. $pay_rate = $seckillInfo['quota'] . '/' . $seckillInfo['quota_show'];
  704. return compact('pay_count', 'order_count', 'all_price', 'pay_rate');
  705. }
  706. /**
  707. * 秒杀参与人统计
  708. * @param $id
  709. * @param string $keyword
  710. * @return array
  711. */
  712. public function seckillPeople($id, $keyword = '')
  713. {
  714. /** @var StoreOrderServices $orderServices */
  715. $orderServices = app()->make(StoreOrderServices::class);
  716. [$page, $limit] = $this->getPageValue();
  717. $list = $orderServices->seckillPeople($id, $keyword, $page, $limit);
  718. $count = $orderServices->getDistinctCount([['seckill_id', '=', $id], ['real_name|uid|user_phone', 'like', '%' . $keyword . '%']], 'uid', false);
  719. foreach ($list as &$item) {
  720. $item['add_time'] = date('Y-m-d H:i:s', $item['add_time']);
  721. }
  722. return compact('list', 'count');
  723. }
  724. /**
  725. * 秒杀订单统计
  726. * @param $id
  727. * @param array $where
  728. * @return array
  729. */
  730. public function seckillOrder($id, $where = [])
  731. {
  732. /** @var StoreOrderServices $orderServices */
  733. $orderServices = app()->make(StoreOrderServices::class);
  734. [$page, $limit] = $this->getPageValue();
  735. $where = $where + ['paid' => 1, 'refund_status' => 0, 'is_del' => 0];
  736. $list = $orderServices->seckillOrder($id, $where, $page, $limit);
  737. $count = $orderServices->seckillCount($id, $where);
  738. foreach ($list as &$item) {
  739. if ($item['status'] == 0) {
  740. if ($item['paid'] == 0) {
  741. $item['status'] = '未支付';
  742. } else {
  743. $item['status'] = '未发货';
  744. }
  745. } elseif ($item['status'] == 1) {
  746. $item['status'] = '待收货';
  747. } elseif ($item['status'] == 2) {
  748. $item['status'] = '待评价';
  749. } elseif ($item['status'] == 3) {
  750. $item['status'] = '已完成';
  751. } elseif ($item['status'] == -2) {
  752. $item['status'] = '已退款';
  753. } else {
  754. $item['status'] = '未知';
  755. }
  756. $item['add_time'] = date('Y-m-d H:i:s', $item['add_time']);
  757. $item['pay_time'] = $item['pay_time'] ? date('Y-m-d H:i:s', $item['pay_time']) : '';
  758. }
  759. return compact('list', 'count');
  760. }
  761. }