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.

147 lines
4.6 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
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. // 是否超过限购数量
  41. if ($goods->restrict_num != 0 && $goods->restrict_num < $num) {
  42. return ErrorCode::GOODS_RESTRICT_LIMIT;
  43. }
  44. return true;
  45. }
  46. public function undo()
  47. {
  48. // TODO: Implement undo() method.
  49. }
  50. public function getBanner($goodsId)
  51. {
  52. $banner = GoodsBanner::query()->where('goods_id',$goodsId)->orderByDesc('type')->get();
  53. return $banner;
  54. }
  55. public function detail($goodsId)
  56. {
  57. $res = Goods::query()->with('store')->where('id',$goodsId)->first();
  58. return $res;
  59. }
  60. public function getByType($storeId,$typeId)
  61. {
  62. return Goods::query()->withoutGlobalScope('normal')->where(['store_id' => $storeId,'category_id' => $typeId])->orderByDesc('on_sale')->orderByDesc('created_at')->get()->toArray();
  63. }
  64. public function create($params)
  65. {
  66. $data =
  67. [
  68. 'market_id' => $params['market_id'],
  69. 'store_id' => $params['store_id'],
  70. 'name' => $params['name'],
  71. 'category_id' => $params['category_id'],
  72. 'goods_category_id' => $params['goods_category_id'],
  73. 'goods_unit' => $params['goods_unit'],
  74. 'price' => $params['price'],
  75. 'original_price' => $params['original_price'],
  76. 'inventory' => $params['inventory'],
  77. 'restrict_num' => $params['restrict_num'],
  78. 'start_num' => $params['start_num'],
  79. 'spec' => $params['spec'],
  80. 'tags' => $params['tags'],
  81. 'remark' => $params['remark'],
  82. 'on_sale' => $params['on_sale'],
  83. 'is_infinite' => $params['is_infinite']
  84. ];
  85. return Goods::create($data);
  86. }
  87. public function update($params)
  88. {
  89. var_dump($params);
  90. $goods = Goods::query()->withoutGlobalScope('normal')
  91. ->where(
  92. [
  93. 'id' => $params['id'],
  94. 'market_id' => $params['market_id'],
  95. 'store_id' => $params['store_id']
  96. ]
  97. )
  98. ->update(
  99. [
  100. 'name' => $params['name'],
  101. 'category_id' => $params['category_id'],
  102. 'goods_category_id' => $params['goods_category_id'],
  103. 'goods_unit' => $params['goods_unit'],
  104. 'price' => $params['price'],
  105. 'original_price' => $params['original_price'],
  106. 'inventory' => $params['inventory'],
  107. 'restrict_num' => $params['restrict_num'],
  108. 'start_num' => $params['start_num'],
  109. 'spec' => $params['spec'],
  110. 'tags' => $params['tags'],
  111. 'remark' => $params['remark'],
  112. 'on_sale' => $params['on_sale'],
  113. 'is_infinite' => $params['is_infinite']
  114. ]);
  115. return $goods;
  116. }
  117. public function info($goodsId)
  118. {
  119. $res = Goods::query()->withoutGlobalScope('normal')->where('id',$goodsId)->first();
  120. return $res;
  121. }
  122. public function getTags()
  123. {
  124. return [
  125. '新品',
  126. '热销',
  127. '新鲜'
  128. ];
  129. }
  130. }