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.

110 lines
3.3 KiB

5 years ago
6 years ago
5 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
5 years ago
5 years ago
5 years ago
6 years ago
6 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 update($params)
  65. {
  66. $goods = Goods::query()->where(
  67. [
  68. 'id' => $params['id'],
  69. 'market_id' => $params['market_id'],
  70. 'store_id' => $params['store_id']
  71. ])
  72. ->update(
  73. [
  74. 'name' => $params['name'],
  75. 'category_id' => $params['market_id'],
  76. 'goods_unit' => $params['goods_unit'],
  77. 'price' => $params['price'],
  78. 'original_price' => $params['original_price'],
  79. 'inventory' => $params['inventory'],
  80. 'restrict_num' => $params['restrict_num'],
  81. 'start_num' => $params['start_num'],
  82. //'spec' => $params['spec'],
  83. //'tags' => $params['tags'],
  84. 'remark' => $params['remark'],
  85. 'on_sale' => $params['on_sale'],
  86. 'is_infinite' => $params['is_infinite'],
  87. ]);
  88. return $goods;
  89. }
  90. public function info($goodsId)
  91. {
  92. $res = Goods::query()->withoutGlobalScope('normal')->where('id',$goodsId)->first();
  93. return $res;
  94. }
  95. }