GoodsStock.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. <?php
  2. namespace app\frontend\modules\orderGoods\stock;
  3. use app\common\facades\SiteSetting;
  4. use app\common\models\OrderGoods;
  5. use app\frontend\modules\orderGoods\stock\types\AfterOrderCreate;
  6. use app\frontend\modules\orderGoods\stock\types\Never;
  7. use app\frontend\modules\orderGoods\stock\types\StockType;
  8. use app\frontend\modules\orderGoods\stock\types\AfterOrderPaid;
  9. use Illuminate\Database\Eloquent\Model;
  10. class GoodsStock
  11. {
  12. /**
  13. * @var StockType
  14. */
  15. private $type;
  16. /**
  17. * @var Model
  18. */
  19. private $source;
  20. /**
  21. * @var OrderGoods
  22. */
  23. private $orderGoods;
  24. public function __construct(OrderGoods $orderGoods)
  25. {
  26. $this->orderGoods = $orderGoods;
  27. if ($this->orderGoods->goods_option_id) {
  28. $source = $this->orderGoods->goodsOption;
  29. } else {
  30. $source = $this->orderGoods->goods;
  31. }
  32. if(!$source){
  33. $type = new Never();
  34. }else{
  35. switch ($this->orderGoods->goods->reduce_stock_method) {
  36. case 0:
  37. $type = new AfterOrderCreate($this, $source);
  38. break;
  39. case 1:
  40. $type = new AfterOrderPaid($this, $source);
  41. break;
  42. case 2:
  43. $type = new Never();
  44. break;
  45. default:
  46. $type = new Never();
  47. }
  48. }
  49. $this->type = $type;
  50. $this->source = $source;
  51. }
  52. public function orderGoods()
  53. {
  54. return $this->orderGoods;
  55. }
  56. private function source()
  57. {
  58. return $this->source;
  59. }
  60. public function withholdRecord(){
  61. if (!$this->type->shouldWithhold()) {
  62. return true;
  63. }
  64. // 记录预扣库存的对应记录
  65. return $this->type->withholdRecord();
  66. }
  67. public function withhold()
  68. {
  69. if (!$this->type->shouldWithhold()) {
  70. return true;
  71. }
  72. // 记录预扣库存的对应记录
  73. return $this->type->withhold();
  74. }
  75. public function reduce()
  76. {
  77. return $this->type->reduce();
  78. }
  79. public function rollback()
  80. {
  81. return $this->type->rollback();
  82. }
  83. public function enough()
  84. {
  85. return $this->type->enough();
  86. }
  87. public function keyOfWithholdKeySet()
  88. {
  89. return "withhold_order_goods_id_keys";
  90. }
  91. public function withholdKey()
  92. {
  93. return $this->source()->getTable() . ":{$this->source()->id}:withhold_order_goods_id";
  94. }
  95. }