CloudStockAgentGoodsService.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. <?php
  2. namespace addons\CloudStock\common\services;
  3. use addons\CloudStock\common\models\CloudStockAgentGoods;
  4. use common\enums\StatusEnum;
  5. use common\models\goods\Goods;
  6. use common\models\goods\GoodsAttr;
  7. class CloudStockAgentGoodsService extends BaseService
  8. {
  9. /**
  10. * 检查代理商品是否存在
  11. *
  12. * @param int $goodsId
  13. *
  14. * @return bool
  15. */
  16. public function goodsExist(int $goodsId)
  17. {
  18. return (bool)CloudStockAgentGoods::find()
  19. ->where([
  20. 'goods_id' => $goodsId,
  21. 'user_id' => $this->user_id,
  22. 'status' => StatusEnum::ENABLED,
  23. 'mall_id' => $this->mall_id
  24. ])
  25. ->count();
  26. }
  27. /**
  28. * 获取商品库存
  29. *
  30. * @param array $goodsIds
  31. *
  32. * @return array
  33. */
  34. public function getGoodsStock(array $goodsIds): array
  35. {
  36. return CloudStockAgentGoods::find()
  37. ->where([
  38. 'goods_id' => $goodsIds,
  39. 'user_id' => $this->user_id,
  40. 'status' => StatusEnum::ENABLED,
  41. 'mall_id' => $this->mall_id
  42. ])
  43. ->select('num')
  44. ->indexBy('goods_id')
  45. ->column();
  46. }
  47. /**
  48. * 验证给定商品id和数量,验证商品库存是否足够
  49. *
  50. * @param array $goodsGoodsNumList
  51. *
  52. * @return bool
  53. */
  54. public function checkGoodsStockByGoodsIds(array $goodsGoodsNumList): bool
  55. {
  56. $goodsIds = array_column($goodsGoodsNumList, 'goods_id');
  57. $goodsStock = $this->getGoodsStock($goodsIds);
  58. foreach ($goodsGoodsNumList as $item) {
  59. $goodsId = $item['goods_id'];
  60. if (!isset($goodsStock[$goodsId])) {
  61. $this->setError(sprintf('未查询到商品,商品ID:%d', $goodsId));
  62. return false;
  63. }
  64. if ($goodsStock[$goodsId] < $item['num']) {
  65. $this->setError(sprintf('商品ID:%d 库存不足,剩余%d件', $goodsId, $goodsStock[$goodsId]));
  66. return false;
  67. }
  68. }
  69. return true;
  70. }
  71. /**
  72. * @param array $goodsIds
  73. *
  74. * @return bool
  75. */
  76. public function checkGoodsDataByGoodsIds(array $goodsIds): bool
  77. {
  78. $findGoodsIds = Goods::find()
  79. ->where([
  80. 'mall_id' => $this->mall_id,
  81. 'status' => StatusEnum::ENABLED,
  82. 'id' => $goodsIds,
  83. 'is_on_sale' => 1,
  84. 'is_attr' => 0
  85. ])
  86. ->select('id')
  87. ->column();
  88. foreach ($goodsIds as $goodsId) {
  89. if (!in_array($goodsId, $findGoodsIds)) {
  90. $this->setError(sprintf('商品数据异常,商品ID:%d', $goodsId));
  91. return false;
  92. }
  93. }
  94. return true;
  95. }
  96. /**
  97. * 获取单规格商品的attr id
  98. *
  99. * @return void
  100. */
  101. public function getGoodsAttrMap(array $goodsIds): array
  102. {
  103. return GoodsAttr::find()
  104. ->where(['goods_id' => $goodsIds, 'status' => StatusEnum::ENABLED])
  105. ->select('id')
  106. ->indexBy('goods_id')
  107. ->column();
  108. }
  109. }