StoreSeckillServices.php 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807
  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_del' => 0, 'id' => $data['product_id']])) {
  173. throw new AdminException('无法添加回收站商品');
  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. $stopIds = [];
  198. foreach ($list as &$item) {
  199. $item['store_name'] = $item['title'];
  200. if ($item['status']) {
  201. if ($item['start_time'] > time()) {
  202. $item['start_name'] = '未开始';
  203. } else if (bcadd($item['stop_time'], '86400') < time()) {
  204. $item['start_name'] = '已结束';
  205. $item['status'] = 0;
  206. $stopIds[] = $item['id'];
  207. } else if (bcadd($item['stop_time'], '86400') > time() && $item['start_time'] < time()) {
  208. $item['start_name'] = '进行中';
  209. }
  210. } else $item['start_name'] = '已结束';
  211. $end_time = $item['stop_time'] ? date('Y/m/d', (int)$item['stop_time']) : '';
  212. $item['_stop_time'] = $end_time;
  213. $item['stop_status'] = $item['stop_time'] + 86400 < time() ? 1 : 0;
  214. }
  215. if ($stopIds) {
  216. $this->dao->batchUpdate($stopIds, ['status' => 0]);
  217. }
  218. return compact('list', 'count');
  219. }
  220. /**
  221. * 后台页面设计获取商品列表
  222. * @param $where
  223. * @return array
  224. * @throws \think\db\exception\DataNotFoundException
  225. * @throws \think\db\exception\DbException
  226. * @throws \think\db\exception\ModelNotFoundException
  227. */
  228. public function getDiySeckillList($where)
  229. {
  230. unset($where['is_show']);
  231. $where['storeProductId'] = true;
  232. $where['status'] = 1;
  233. [$page, $limit] = $this->getPageValue();
  234. $list = $this->dao->getList($where, $page, $limit);
  235. $count = $this->dao->getCount($where);
  236. $cateIds = implode(',', array_column($list, 'cate_id'));
  237. /** @var StoreCategoryServices $storeCategoryServices */
  238. $storeCategoryServices = app()->make(StoreCategoryServices::class);
  239. $cateList = $storeCategoryServices->getCateArray($cateIds);
  240. foreach ($list as &$item) {
  241. $cateName = '';
  242. $item['cate_name'] = '';
  243. if ($item['cate_id']) {
  244. $cateName = array_filter($cateList, function ($val) use ($item) {
  245. if (in_array($val['id'], explode(',', $item['cate_id']))) {
  246. return $val;
  247. }
  248. });
  249. $item['cate_name'] = implode(',', array_column($cateName, 'cate_name'));
  250. }
  251. $item['store_name'] = $item['title'];
  252. $item['price'] = floatval($item['price']);
  253. $item['is_product_type'] = 3;
  254. }
  255. return compact('count', 'list');
  256. }
  257. /**
  258. * 首页秒杀数据
  259. * @param $where
  260. * @return array|int
  261. * @throws \think\db\exception\DataNotFoundException
  262. * @throws \think\db\exception\DbException
  263. * @throws \think\db\exception\ModelNotFoundException
  264. */
  265. public function getHomeSeckillList($where)
  266. {
  267. $data = [];
  268. $seckillTime = sys_data('routine_seckill_time') ?: [];//秒杀时间段
  269. $today = strtotime(date('Y-m-d'));
  270. $timeInfo = ['time' => 0, 'continued' => 0];
  271. foreach ($seckillTime as $key => $value) {
  272. $currentHour = date('H');
  273. $activityEndHour = (int)$value['time'] + (int)$value['continued'];
  274. if ($currentHour >= (int)$value['time'] && $currentHour < $activityEndHour && $activityEndHour <= 24) {
  275. $timeInfo = $value;
  276. break;
  277. }
  278. }
  279. if ($timeInfo['time'] == 0) return [];
  280. $data['time'] = $timeInfo['time'] . ':00';
  281. $activityEndHour = bcadd($timeInfo['time'], $timeInfo['continued'], 0);
  282. $data['stop'] = (int)bcadd((string)$today, bcmul($activityEndHour, '3600', 0));
  283. $where['time_id'] = $timeInfo['id'];
  284. [$page, $limit] = $this->getPageValue();
  285. $data['list'] = $this->dao->getHomeList($where, $page, $limit);
  286. foreach ($data['list'] as &$item) {
  287. $item['price'] = floatval($item['price']);
  288. }
  289. return $data;
  290. }
  291. /**
  292. * 获取秒杀详情
  293. * @param int $id
  294. * @return array|\think\Model|null
  295. */
  296. public function getInfo(int $id)
  297. {
  298. $info = $this->dao->get($id);
  299. if ($info) {
  300. if ($info['start_time'])
  301. $start_time = date('Y-m-d', (int)$info['start_time']);
  302. if ($info['stop_time'])
  303. $stop_time = date('Y-m-d', (int)$info['stop_time']);
  304. if (isset($start_time) && isset($stop_time))
  305. $info['section_time'] = [$start_time, $stop_time];
  306. else
  307. $info['section_time'] = [];
  308. unset($info['start_time'], $info['stop_time']);
  309. $info['give_integral'] = intval($info['give_integral']);
  310. $info['price'] = floatval($info['price']);
  311. $info['ot_price'] = floatval($info['ot_price']);
  312. $info['postage'] = floatval($info['postage']);
  313. $info['cost'] = floatval($info['cost']);
  314. $info['weight'] = floatval($info['weight']);
  315. $info['volume'] = floatval($info['volume']);
  316. $info['logistics'] = explode(',', $info['logistics']);
  317. /** @var StoreDescriptionServices $storeDescriptionServices */
  318. $storeDescriptionServices = app()->make(StoreDescriptionServices::class);
  319. $info['description'] = $storeDescriptionServices->getDescription(['product_id' => $id, 'type' => 1]);
  320. $info['attrs'] = $this->attrList($id, $info['product_id']);
  321. }
  322. return $info;
  323. }
  324. /**
  325. * 获取规格
  326. * @param int $id
  327. * @param int $pid
  328. * @return mixed
  329. */
  330. public function attrList(int $id, int $pid)
  331. {
  332. /** @var StoreProductAttrResultServices $storeProductAttrResultServices */
  333. $storeProductAttrResultServices = app()->make(StoreProductAttrResultServices::class);
  334. $seckillResult = $storeProductAttrResultServices->value(['product_id' => $id, 'type' => 1], 'result');
  335. $items = json_decode($seckillResult, true)['attr'];
  336. $productAttr = $this->getAttr($items, $pid, 0);
  337. $seckillAttr = $this->getAttr($items, $id, 1);
  338. foreach ($productAttr as $pk => $pv) {
  339. foreach ($seckillAttr as &$sv) {
  340. if ($pv['detail'] == $sv['detail']) {
  341. $productAttr[$pk] = $sv;
  342. }
  343. }
  344. $productAttr[$pk]['detail'] = json_decode($productAttr[$pk]['detail']);
  345. }
  346. $attrs['items'] = $items;
  347. $attrs['value'] = $productAttr;
  348. foreach ($items as $key => $item) {
  349. $header[] = ['title' => $item['value'], 'key' => 'value' . ($key + 1), 'align' => 'center', 'minWidth' => 80];
  350. }
  351. $header[] = ['title' => '图片', 'slot' => 'pic', 'align' => 'center', 'minWidth' => 120];
  352. $header[] = ['title' => '秒杀价', 'slot' => 'price', 'align' => 'center', 'minWidth' => 80];
  353. $header[] = ['title' => '成本价', 'key' => 'cost', 'align' => 'center', 'minWidth' => 80];
  354. $header[] = ['title' => '原价', 'key' => 'ot_price', 'align' => 'center', 'minWidth' => 80];
  355. $header[] = ['title' => '库存', 'key' => 'stock', 'align' => 'center', 'minWidth' => 80];
  356. $header[] = ['title' => '限量', 'slot' => 'quota', 'type' => 1, 'align' => 'center', 'minWidth' => 80];
  357. $header[] = ['title' => '重量(KG)', 'key' => 'weight', 'align' => 'center', 'minWidth' => 80];
  358. $header[] = ['title' => '体积(m³)', 'key' => 'volume', 'align' => 'center', 'minWidth' => 80];
  359. $header[] = ['title' => '商品编号', 'key' => 'bar_code', 'align' => 'center', 'minWidth' => 80];
  360. $attrs['header'] = $header;
  361. return $attrs;
  362. }
  363. /**
  364. * 获取规格
  365. * @param $attr
  366. * @param $id
  367. * @param $type
  368. * @return array
  369. */
  370. public function getAttr($attr, $id, $type)
  371. {
  372. /** @var StoreProductAttrValueServices $storeProductAttrValueServices */
  373. $storeProductAttrValueServices = app()->make(StoreProductAttrValueServices::class);
  374. list($value, $head) = attr_format($attr);
  375. $valueNew = [];
  376. $count = 0;
  377. foreach ($value as $suk) {
  378. $detail = explode(',', $suk);
  379. $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');
  380. if (count($sukValue)) {
  381. foreach ($detail as $k => $v) {
  382. $valueNew[$count]['value' . ($k + 1)] = $v;
  383. }
  384. $valueNew[$count]['detail'] = json_encode(array_combine($head, $detail));
  385. $valueNew[$count]['pic'] = $sukValue[$suk]['pic'] ?? '';
  386. $valueNew[$count]['price'] = $sukValue[$suk]['price'] ? floatval($sukValue[$suk]['price']) : 0;
  387. $valueNew[$count]['cost'] = $sukValue[$suk]['cost'] ? floatval($sukValue[$suk]['cost']) : 0;
  388. $valueNew[$count]['ot_price'] = isset($sukValue[$suk]['ot_price']) ? floatval($sukValue[$suk]['ot_price']) : 0;
  389. $valueNew[$count]['stock'] = $sukValue[$suk]['stock'] ? intval($sukValue[$suk]['stock']) : 0;
  390. $valueNew[$count]['quota'] = $sukValue[$suk]['quota'] ? intval($sukValue[$suk]['quota']) : 0;
  391. $valueNew[$count]['bar_code'] = $sukValue[$suk]['bar_code'] ?? '';
  392. $valueNew[$count]['weight'] = $sukValue[$suk]['weight'] ? floatval($sukValue[$suk]['weight']) : 0;
  393. $valueNew[$count]['volume'] = $sukValue[$suk]['volume'] ? floatval($sukValue[$suk]['volume']) : 0;
  394. $valueNew[$count]['brokerage'] = $sukValue[$suk]['brokerage'] ? floatval($sukValue[$suk]['brokerage']) : 0;
  395. $valueNew[$count]['brokerage_two'] = $sukValue[$suk]['brokerage_two'] ? floatval($sukValue[$suk]['brokerage_two']) : 0;
  396. $valueNew[$count]['_checked'] = $type != 0;
  397. $count++;
  398. }
  399. }
  400. return $valueNew;
  401. }
  402. /**
  403. * 获取某个时间段的秒杀列表
  404. * @param int $time
  405. * @return array
  406. * @throws \think\db\exception\DataNotFoundException
  407. * @throws \think\db\exception\DbException
  408. * @throws \think\db\exception\ModelNotFoundException
  409. */
  410. public function getListByTime(int $time)
  411. {
  412. [$page, $limit] = $this->getPageValue();
  413. $seckillInfo = $this->dao->getListByTime($time, $page, $limit);
  414. if (count($seckillInfo)) {
  415. foreach ($seckillInfo as $key => &$item) {
  416. if ($item['quota'] > 0) {
  417. $percent = (int)(($item['quota_show'] - $item['quota']) / $item['quota_show'] * 100);
  418. $item['percent'] = $percent;
  419. $item['stock'] = $item['quota'];
  420. } else {
  421. $item['percent'] = 100;
  422. $item['stock'] = 0;
  423. }
  424. $item['price'] = floatval($item['price']);
  425. $item['ot_price'] = floatval($item['ot_price']);
  426. }
  427. }
  428. return $seckillInfo;
  429. }
  430. /**
  431. * 获取秒杀详情
  432. * @param Request $request
  433. * @param int $id
  434. * @return mixed
  435. * @throws \think\db\exception\DataNotFoundException
  436. * @throws \think\db\exception\DbException
  437. * @throws \think\db\exception\ModelNotFoundException
  438. */
  439. public function seckillDetail(Request $request, int $id)
  440. {
  441. $uid = (int)$request->uid();
  442. $storeInfo = $this->dao->getOne(['id' => $id], '*', ['description']);
  443. if (!$storeInfo) {
  444. throw new ApiException(410294);
  445. } else {
  446. $storeInfo = $storeInfo->toArray();
  447. }
  448. $siteUrl = sys_config('site_url');
  449. $storeInfo['image'] = set_file_url($storeInfo['image'], $siteUrl);
  450. $storeInfo['image_base'] = set_file_url($storeInfo['image'], $siteUrl);
  451. $storeInfo['store_name'] = $storeInfo['title'];
  452. $storeInfo['total'] = $storeInfo['sales'];
  453. $storeInfo['product_is_show'] = app()->make(StoreProductServices::class)->value($storeInfo['product_id'], 'is_show');
  454. if (sys_config('share_qrcode', 0) && request()->isWechat()) {
  455. /** @var QrcodeServices $qrcodeService */
  456. $qrcodeService = app()->make(QrcodeServices::class);
  457. $storeInfo['wechat_code'] = $qrcodeService->getTemporaryQrcode('seckill-' . $id, $uid)->url;
  458. } else {
  459. $storeInfo['wechat_code'] = '';
  460. }
  461. /** @var StoreOrderServices $storeOrderServices */
  462. $storeOrderServices = app()->make(StoreOrderServices::class);
  463. $data['buy_num'] = $storeOrderServices->getBuyCount($uid, 'seckill_id', $id);
  464. /** @var StoreProductRelationServices $storeProductRelationServices */
  465. $storeProductRelationServices = app()->make(StoreProductRelationServices::class);
  466. $storeInfo['userCollect'] = $storeProductRelationServices->isProductRelation(['uid' => $uid, 'product_id' => $storeInfo['product_id'], 'type' => 'collect', 'category' => 'product']);
  467. $storeInfo['userLike'] = false;
  468. $storeInfo['uid'] = $uid;
  469. if ($storeInfo['quota'] > 0) {
  470. $percent = (int)(($storeInfo['quota_show'] - $storeInfo['quota']) / $storeInfo['quota_show'] * 100);
  471. $storeInfo['percent'] = $percent;
  472. $storeInfo['stock'] = $storeInfo['quota'];
  473. } else {
  474. $storeInfo['percent'] = 100;
  475. $storeInfo['stock'] = 0;
  476. }
  477. //到期时间
  478. /** @var SystemGroupDataServices $groupDataService */
  479. $groupDataService = app()->make(SystemGroupDataServices::class);
  480. $timeInfo = json_decode($groupDataService->value(['id' => $storeInfo['time_id']], 'value'), true);
  481. $today = strtotime(date('Y-m-d'));
  482. $activityEndHour = $timeInfo['time']['value'] + $timeInfo['continued']['value'];
  483. $storeInfo['last_time'] = (int)bcadd((string)$today, (string)bcmul((string)$activityEndHour, '3600', 0));
  484. //获取秒杀商品状态
  485. if ($storeInfo['status'] == 1) {
  486. if ($storeInfo['start_time'] > time()) {
  487. $storeInfo['status'] = 2;
  488. } elseif (($storeInfo['stop_time'] + 86400) < time()) {
  489. $storeInfo['status'] = 0;
  490. } else {
  491. /** @var SystemGroupDataServices $systemGroupDataService */
  492. $systemGroupDataService = app()->make(SystemGroupDataServices::class);
  493. $seckillTime = array_column($systemGroupDataService->getConfigNameValue('routine_seckill_time'), null, 'id');
  494. $config = $seckillTime[$storeInfo['time_id']] ?? false;
  495. if (!$config) {
  496. throw new ApiException(410322);
  497. }
  498. $now_hour = date('H', time());
  499. $start_hour = $config['time'];
  500. $end_hour = (int)$start_hour + (int)$config['continued'];
  501. if ($start_hour <= $now_hour && $end_hour > $now_hour) {
  502. $storeInfo['status'] = 1;
  503. } else if ($start_hour > $now_hour) {
  504. $storeInfo['status'] = 2;
  505. } else {
  506. $storeInfo['status'] = 0;
  507. }
  508. }
  509. } else {
  510. $storeInfo['status'] == 0;
  511. }
  512. /** @var SystemGroupDataServices $groupDataService */
  513. $groupDataService = app()->make(SystemGroupDataServices::class);
  514. $timeInfo = json_decode($groupDataService->value(['id' => $storeInfo['time_id']], 'value'), true);
  515. $today = strtotime(date('Y-m-d'));
  516. $activityEndHour = $timeInfo['time']['value'] + $timeInfo['continued']['value'];
  517. $storeInfo['last_time'] = (int)bcadd((string)$today, (string)bcmul((string)$activityEndHour, '3600', 0));
  518. //商品详情
  519. $data['storeInfo'] = get_thumb_water($storeInfo, 'big', ['image', 'images']);
  520. $storeInfoNew = get_thumb_water($storeInfo, 'small');
  521. $data['storeInfo']['small_image'] = $storeInfoNew['image'];
  522. /** @var StoreProductReplyServices $storeProductReplyService */
  523. $storeProductReplyService = app()->make(StoreProductReplyServices::class);
  524. $data['reply'] = get_thumb_water($storeProductReplyService->getRecProductReply($storeInfo['product_id']), 'small', ['pics']);
  525. [$replyCount, $goodReply, $replyChance] = $storeProductReplyService->getProductReplyData((int)$storeInfo['product_id']);
  526. $data['replyChance'] = $replyChance;
  527. $data['replyCount'] = $replyCount;
  528. /** @var StoreProductAttrServices $storeProductAttrServices */
  529. $storeProductAttrServices = app()->make(StoreProductAttrServices::class);
  530. list($productAttr, $productValue) = $storeProductAttrServices->getProductAttrDetail($id, $uid, 0, 1, $storeInfo['product_id']);
  531. $data['productAttr'] = $productAttr;
  532. $data['productValue'] = $productValue;
  533. $data['routine_contact_type'] = sys_config('routine_contact_type', 0);
  534. //用户访问事件
  535. event('UserVisitListener', [$uid, $id, 'seckill', $storeInfo['product_id'], 'view']);
  536. //浏览记录
  537. ProductLogJob::dispatch(['visit', ['uid' => $uid, 'product_id' => $storeInfo['product_id']]]);
  538. return $data;
  539. }
  540. /**
  541. * 获取秒杀数据
  542. * @param array $ids
  543. * @param string $field
  544. * @return array
  545. * @throws \think\db\exception\DataNotFoundException
  546. * @throws \think\db\exception\DbException
  547. * @throws \think\db\exception\ModelNotFoundException
  548. */
  549. public function getSeckillColumn(array $ids, string $field = '')
  550. {
  551. $seckillProduct = $systemGroupData = [];
  552. $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';
  553. if (!empty($seckill_ids)) {
  554. $seckillProduct = $this->dao->idSeckillList($ids, $field ?: $seckillInfoField);
  555. if (!empty($seckillProduct)) {
  556. $timeIds = Arr::getUniqueKey($seckillProduct, 'time_id');
  557. $seckillProduct = array_combine(array_column($seckillProduct, 'id'), $seckillProduct);
  558. /** @var SystemGroupDataServices $groupServices */
  559. $groupServices = app()->make(SystemGroupDataServices::class);
  560. $systemGroupData = $groupServices->getGroupDataColumn($timeIds);
  561. }
  562. }
  563. return [$seckillProduct, $systemGroupData];
  564. }
  565. /**
  566. * 检查秒杀库存
  567. * @param int $uid
  568. * @param int $seckillId
  569. * @param int $cartNum
  570. * @param string $unique
  571. * @return array
  572. * @throws \think\db\exception\DataNotFoundException
  573. * @throws \think\db\exception\DbException
  574. * @throws \think\db\exception\ModelNotFoundException
  575. * @author 吴汐
  576. * @email 442384644@qq.com
  577. * @date 2023/03/01
  578. */
  579. public function checkSeckillStock(int $uid, int $seckillId, int $cartNum = 1, string $unique = '')
  580. {
  581. /** @var StoreProductAttrValueServices $attrValueServices */
  582. $attrValueServices = app()->make(StoreProductAttrValueServices::class);
  583. if ($unique == '') {
  584. $unique = $attrValueServices->value(['product_id' => $seckillId, 'type' => 1], 'unique');
  585. }
  586. //检查商品活动状态
  587. $StoreSeckillinfo = $this->getSeckillCount($seckillId, '*,title as store_name');
  588. if ($StoreSeckillinfo['once_num'] < $cartNum) {
  589. throw new ApiException(410313, ['num' => $StoreSeckillinfo['once_num']]);
  590. }
  591. /** @var StoreOrderServices $orderServices */
  592. $orderServices = app()->make(StoreOrderServices::class);
  593. $userBuyCount = $orderServices->getBuyCount($uid, 'seckill_id', $seckillId);
  594. if ($StoreSeckillinfo['num'] < ($userBuyCount + $cartNum)) {
  595. throw new ApiException(410298, ['num' => $StoreSeckillinfo['num']]);
  596. }
  597. if ($StoreSeckillinfo['num'] < $cartNum) {
  598. throw new ApiException(410317, ['num' => $StoreSeckillinfo['num']]);
  599. }
  600. $attrInfo = $attrValueServices->getOne(['product_id' => $seckillId, 'unique' => $unique, 'type' => 1]);
  601. if (!$attrInfo || $attrInfo['product_id'] != $seckillId) {
  602. throw new ApiException(410305);
  603. }
  604. if ($cartNum > $attrInfo['quota']) {
  605. throw new ApiException(410296);
  606. }
  607. return [$attrInfo, $unique, $StoreSeckillinfo];
  608. }
  609. /**
  610. * 修改秒杀库存
  611. * @param int $num
  612. * @param int $seckillId
  613. * @param string $unique
  614. * @return bool
  615. * @throws \think\db\exception\DataNotFoundException
  616. * @throws \think\db\exception\DbException
  617. * @throws \think\db\exception\ModelNotFoundException
  618. * @author 吴汐
  619. * @email 442384644@qq.com
  620. * @date 2023/03/01
  621. */
  622. public function decSeckillStock(int $num, int $seckillId, string $unique = '')
  623. {
  624. $product_id = $this->dao->value(['id' => $seckillId], 'product_id');
  625. if ($unique) {
  626. /** @var StoreProductAttrValueServices $skuValueServices */
  627. $skuValueServices = app()->make(StoreProductAttrValueServices::class);
  628. //减去秒杀商品的sku库存增加销量
  629. $res = false !== $skuValueServices->decProductAttrStock($seckillId, $unique, $num, 1);
  630. //减去秒杀库存
  631. $res = $res && $this->dao->decStockIncSales(['id' => $seckillId, 'type' => 1], $num);
  632. //减去当前普通商品sku的库存增加销量
  633. $suk = $skuValueServices->value(['unique' => $unique, 'product_id' => $seckillId, 'type' => 1], 'suk');
  634. $productUnique = $skuValueServices->value(['suk' => $suk, 'product_id' => $product_id, 'type' => 0], 'unique');
  635. if ($productUnique) {
  636. $res = $res && $skuValueServices->decProductAttrStock($product_id, $productUnique, $num);
  637. }
  638. } else {
  639. $res = false !== $this->dao->decStockIncSales(['id' => $seckillId, 'type' => 1], $num);
  640. }
  641. /** @var StoreProductServices $services */
  642. $services = app()->make(StoreProductServices::class);
  643. //减去普通商品库存
  644. return $res && $services->decProductStock($num, $product_id);
  645. }
  646. /**
  647. * 加库存减销量
  648. * @param int $num
  649. * @param int $seckillId
  650. * @param string $unique
  651. * @return bool
  652. */
  653. public function incSeckillStock(int $num, int $seckillId, string $unique = '')
  654. {
  655. $product_id = $this->dao->value(['id' => $seckillId], 'product_id');
  656. if ($unique) {
  657. /** @var StoreProductAttrValueServices $skuValueServices */
  658. $skuValueServices = app()->make(StoreProductAttrValueServices::class);
  659. //减去秒杀商品的sku库存增加销量
  660. $res = false !== $skuValueServices->incProductAttrStock($seckillId, $unique, $num, 1);
  661. //减去秒杀库存
  662. $res = $res && $this->dao->incStockDecSales(['id' => $seckillId, 'type' => 1], $num);
  663. //减去当前普通商品sku的库存增加销量
  664. $suk = $skuValueServices->value(['unique' => $unique, 'product_id' => $seckillId], 'suk');
  665. $productUnique = $skuValueServices->value(['suk' => $suk, 'product_id' => $product_id], 'unique');
  666. if ($productUnique) {
  667. $res = $res && $skuValueServices->incProductAttrStock($product_id, $productUnique, $num);
  668. }
  669. } else {
  670. $res = false !== $this->dao->incStockDecSales(['id' => $seckillId, 'type' => 1], $num);
  671. }
  672. /** @var StoreProductServices $services */
  673. $services = app()->make(StoreProductServices::class);
  674. //减去普通商品库存
  675. $res = $res && $services->incProductStock($num, $product_id);
  676. return $res;
  677. }
  678. /**
  679. * 获取一条秒杀商品
  680. * @param $id
  681. * @param string $field
  682. * @return array|false|\PDOStatement|string|\think\Model
  683. * @throws \think\db\exception\DataNotFoundException
  684. * @throws \think\db\exception\DbException
  685. * @throws \think\db\exception\ModelNotFoundException
  686. */
  687. public function getValidProduct($id, $field = '*')
  688. {
  689. return $this->dao->validProduct($id, $field);
  690. }
  691. /**
  692. * 秒杀统计
  693. * @param $id
  694. * @return array
  695. * @throws \think\db\exception\DataNotFoundException
  696. * @throws \think\db\exception\DbException
  697. * @throws \think\db\exception\ModelNotFoundException
  698. */
  699. public function seckillStatistics($id)
  700. {
  701. /** @var StoreOrderServices $orderServices */
  702. $orderServices = app()->make(StoreOrderServices::class);
  703. $pay_count = $orderServices->getDistinctCount([['seckill_id', '=', $id], ['pid', '<>', -1], ['paid', '=', 1], ['refund_type', 'in', [0, 3]]], 'uid', false);
  704. $order_count = $orderServices->getDistinctCount([['seckill_id', '=', $id], ['pid', '<>', -1], ['refund_type', 'in', [0, 3]]], 'uid', false);
  705. $all_price = $orderServices->sum([['seckill_id', '=', $id], ['pid', '<>', -1], ['refund_type', 'in', [0, 3]], ['paid', '=', 1]], 'pay_price');
  706. $seckillInfo = $this->dao->get($id);
  707. $pay_rate = $seckillInfo['quota'] . '/' . $seckillInfo['quota_show'];
  708. return compact('pay_count', 'order_count', 'all_price', 'pay_rate');
  709. }
  710. /**
  711. * 秒杀参与人统计
  712. * @param $id
  713. * @param string $keyword
  714. * @return array
  715. */
  716. public function seckillPeople($id, $keyword = '')
  717. {
  718. /** @var StoreOrderServices $orderServices */
  719. $orderServices = app()->make(StoreOrderServices::class);
  720. [$page, $limit] = $this->getPageValue();
  721. $list = $orderServices->seckillPeople($id, $keyword, $page, $limit);
  722. $count = $orderServices->getDistinctCount([['seckill_id', '=', $id], ['pid', '<>', -1], ['real_name|uid|user_phone', 'like', '%' . $keyword . '%']], 'uid', false);
  723. foreach ($list as &$item) {
  724. $item['add_time'] = date('Y-m-d H:i:s', $item['add_time']);
  725. }
  726. return compact('list', 'count');
  727. }
  728. /**
  729. * 秒杀订单统计
  730. * @param $id
  731. * @param array $where
  732. * @return array
  733. */
  734. public function seckillOrder($id, $where = [])
  735. {
  736. /** @var StoreOrderServices $orderServices */
  737. $orderServices = app()->make(StoreOrderServices::class);
  738. [$page, $limit] = $this->getPageValue();
  739. $where = $where + ['paid' => 1, 'refund_status' => 0, 'is_del' => 0, 'pid' => 0];
  740. $list = $orderServices->seckillOrder($id, $where, $page, $limit);
  741. $count = $orderServices->seckillCount($id, $where);
  742. foreach ($list as &$item) {
  743. if ($item['status'] == 0) {
  744. if ($item['paid'] == 0) {
  745. $item['status'] = '未支付';
  746. } else {
  747. $item['status'] = '未发货';
  748. }
  749. } elseif ($item['status'] == 1) {
  750. $item['status'] = '待收货';
  751. } elseif ($item['status'] == 2) {
  752. $item['status'] = '待评价';
  753. } elseif ($item['status'] == 3) {
  754. $item['status'] = '已完成';
  755. } elseif ($item['status'] == -2) {
  756. $item['status'] = '已退款';
  757. } else {
  758. $item['status'] = '未知';
  759. }
  760. $item['add_time'] = date('Y-m-d H:i:s', $item['add_time']);
  761. $item['pay_time'] = $item['pay_time'] ? date('Y-m-d H:i:s', $item['pay_time']) : '';
  762. }
  763. return compact('list', 'count');
  764. }
  765. }