CloudStockGoodsService.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. <?php
  2. namespace addons\CloudStock\api\services;
  3. use addons\CloudStock\common\models\CloudStockGoods;
  4. use addons\CloudStock\common\services\BaseService;
  5. use common\enums\StatusEnum;
  6. use common\models\goods\Goods;
  7. use RuntimeException;
  8. class CloudStockGoodsService extends BaseService
  9. {
  10. /**
  11. * 获取有效的云库存商品
  12. *
  13. * @param array $goodsIds
  14. *
  15. * @return array
  16. */
  17. public function getEnableGoodsList(array $goodsIds): array
  18. {
  19. if (empty($goodsIds)) {
  20. return [];
  21. }
  22. $goodsIds = Goods::find()
  23. ->where([
  24. 'mall_id' => $this->mall_id,
  25. 'status' => StatusEnum::ENABLED,
  26. 'id' => $goodsIds,
  27. 'is_on_sale' => 1,
  28. 'is_attr' => 0
  29. ])
  30. ->select('id')
  31. ->column();
  32. return CloudStockGoods::find()
  33. ->where([
  34. 'mall_id' => $this->mall_id,
  35. 'status' => StatusEnum::ENABLED,
  36. 'goods_id' => $goodsIds
  37. ])
  38. ->select('goods_id')
  39. ->column();
  40. }
  41. /**
  42. * 计算汇总价格及利润
  43. *
  44. * @param array $stockGoodsList
  45. * @param array $goodsList
  46. * @param array $goodsIdNumberMap
  47. *
  48. * @return array
  49. */
  50. public function getTotalOrderFields(array &$stockGoodsList, array $goodsList, array $goodsIdNumberMap): array
  51. {
  52. $res = [
  53. 'replenish_price' => 0,
  54. 'profit' => 0
  55. ];
  56. foreach ($stockGoodsList as &$item) {
  57. $item['agent_price'] = json_decode($item['agent_price'], true) ?: [];
  58. $computePrice = $this->computePrice($item['agent_price'], $goodsList[$item['goods_id']]); // 得到单价及利润
  59. // 商品价格
  60. $res['replenish_price'] = bcadd(
  61. $res['replenish_price'],
  62. bcmul($computePrice['replenish_price'], $goodsIdNumberMap[$item['goods_id']], 2),
  63. 2
  64. );
  65. // 利润
  66. $res['profit'] = bcadd(
  67. $res['profit'],
  68. bcmul($computePrice['profit'], $goodsIdNumberMap[$item['goods_id']], 2),
  69. 2
  70. );
  71. // 修改商品价格及利润
  72. $item['goods_info']['price'] = $computePrice['replenish_price'] ?? $item['goods_info']['price'];
  73. $item['goods_info']['profit'] = $computePrice['profit'] ?? 0;
  74. unset($item['agent_price']);
  75. }
  76. return $res;
  77. }
  78. /**
  79. * @param array $agentPrice
  80. * @param array $goods
  81. *
  82. * @return array
  83. */
  84. public function computePrice(array $agentPrice, array $goods)
  85. {
  86. $agentLevel = $this->stock_agent['level'];
  87. $agent_price = array_column($agentPrice, null, 'level');
  88. $replenish_price = $goods['price'] ?? 0;
  89. if (!empty($agent_price[$agentLevel])) {
  90. $replenish_price = $agent_price[$agentLevel]['price'];
  91. }
  92. return [
  93. 'replenish_price' => $replenish_price, // 单价
  94. 'profit' => bcsub($goods['price'], $replenish_price, 2), // 利润
  95. ];
  96. }
  97. public function getGoodsPrice(array $cartList)
  98. {
  99. $goodsList = Goods::lists([
  100. 'select' => 'id, price',
  101. 'where' => [
  102. [
  103. 'mall_id' => $this->mall_id,
  104. 'status' => StatusEnum::ENABLED,
  105. 'id' => array_column($cartList, 'goods_id'),
  106. 'is_on_sale' => 1,
  107. 'is_attr' => 0
  108. ]
  109. ],
  110. 'index' => 'id'
  111. ]);
  112. $cloudStockGoodsList = CloudStockGoods::lists([
  113. 'where' => [
  114. [
  115. 'mall_id' => $this->mall_id,
  116. 'status' => StatusEnum::ENABLED,
  117. 'goods_id' => array_column($goodsList, 'id')
  118. ]
  119. ],
  120. 'select' => 'id, goods_id, agent_price'
  121. ]);
  122. return $this->getTotalOrderFields(
  123. $cloudStockGoodsList,
  124. $goodsList,
  125. array_column($cartList, 'num', 'goods_id')
  126. );
  127. }
  128. }