You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

79 lines
2.3 KiB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
  1. <?php
  2. namespace App\Service\v3\Implementations;
  3. use App\Constants\v3\ErrorCode;
  4. use App\Exception\ErrorCodeException;
  5. use App\Service\v3\Interfaces\GoodsServiceInterface;
  6. use Hyperf\DbConnection\Db;
  7. use App\Model\v3\Goods;
  8. use App\Model\v3\GoodsBanner;
  9. use App\Constants\v3\Store;
  10. use App\Constants\v3\goods as goodsConstants;
  11. use Hyperf\Redis\Redis;
  12. use Hyperf\Utils\ApplicationContext;
  13. class GoodsService implements GoodsServiceInterface
  14. {
  15. public function do($goodsId)
  16. {
  17. }
  18. public function check(Goods $goods,$num = 1)
  19. {
  20. $redis = ApplicationContext::getContainer()->get(Redis::class);
  21. $inventoryKey = 'goods_inventory_sold_1_'.$goods->id; // 拼接activity_type和goods_id
  22. if (empty($goods)) {
  23. return ErrorCode::GOODS_NOT_EXISTS;
  24. }
  25. // 商户歇业
  26. if($goods->store->is_rest == 1){
  27. return ErrorCode::STORE_REST;
  28. }
  29. // 商品下架或已删除
  30. if($goods->on_sale == 0 || !is_null($goods->deleted_at)){
  31. return ErrorCode::GOODS_ON_SALE_NO;
  32. }
  33. // 商品库存不足
  34. // 获取冻结的库存
  35. //$inventoryFrozen = (int)$redis->get($inventoryKey);
  36. $inventoryFrozen = 0;
  37. if($goods->is_infinite != 1 && $goods->inventory < ($num+$inventoryFrozen)){
  38. return ErrorCode::GOODS_INVENTORY_ERROR;
  39. }
  40. // 压redis库存
  41. // redis记录当前商品的购买数量,压库存,下单失败、下单成功扣库存成功、订单取消的时候释放
  42. if (!$redis->exists($inventoryKey)) {
  43. $redis->set($inventoryKey, $num);
  44. } else {
  45. $redis->incrBy($inventoryKey, intval($num));
  46. }
  47. // 是否超过限购数量
  48. if ($goods->restrict_num != 0 && $goods->restrict_num < $num) {
  49. return ErrorCode::GOODS_RESTRICT_LIMIT;
  50. }
  51. return true;
  52. }
  53. public function undo()
  54. {
  55. // TODO: Implement undo() method.
  56. }
  57. public function getBanner($goodsId)
  58. {
  59. $banner = GoodsBanner::query()->where('goods_id',$goodsId)->orderByDesc('type')->get();
  60. return $banner;
  61. }
  62. public function detail($goodsId)
  63. {
  64. $res = Goods::query()->with('store')->where('id',$goodsId)->first();
  65. return $res;
  66. }
  67. }