CloudStockFillCartService.php 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. <?php
  2. namespace addons\CloudStock\api\services;
  3. use addons\CloudStock\common\models\CloudStockFillCart;
  4. use addons\CloudStock\common\services\BaseService;
  5. use common\enums\StatusEnum;
  6. class CloudStockFillCartService extends BaseService
  7. {
  8. /**
  9. * 购物车数据
  10. *
  11. * @param array $goodsIds
  12. *
  13. * @return array
  14. */
  15. public function getCartList(array $goodsIds = []): array
  16. {
  17. $where = [
  18. ['mall_id' => $this->mall_id, 'user_id' => $this->user_id, 'status' => StatusEnum::ENABLED]
  19. ];
  20. if ($goodsIds) {
  21. $where[] = ['goods_id' => $goodsIds];
  22. }
  23. return CloudStockFillCart::lists(
  24. [
  25. 'where' => $where,
  26. 'select' => 'goods_id, num'
  27. ]
  28. );
  29. }
  30. /**
  31. * 购物车转为订单
  32. *
  33. * @return void
  34. */
  35. public function cart2Order(int $orderId)
  36. {
  37. CloudStockFillCart::updateAll(
  38. [
  39. 'status' => StatusEnum::DISABLED,
  40. 'order_id' => $orderId,
  41. 'updated_at' => time()
  42. ],
  43. [
  44. 'user_id' => $this->user_id,
  45. 'mall_id' => $this->mall_id,
  46. 'status' => StatusEnum::ENABLED,
  47. ]
  48. );
  49. }
  50. /**
  51. * 返回购物车汇总信息
  52. *
  53. * @return array
  54. */
  55. public function getCartInfo(): array
  56. {
  57. $res = [
  58. 'cart_list' => [],
  59. 'replenish_price' => 0,
  60. 'profit' => 0,
  61. ];
  62. $cartList = $this->getCartList();
  63. if (empty($cartList)) {
  64. return $res;
  65. }
  66. $res['cart_list'] = $cartList;
  67. $goodsPriceList = (new CloudStockGoodsService(['mall_id' => $this->mall_id, 'user_id' => $this->user_id, 'stock_agent' => $this->stock_agent]))
  68. ->getGoodsPrice($cartList);
  69. $res['replenish_price'] = $goodsPriceList['replenish_price'];
  70. $res['profit'] = $goodsPriceList['profit'];
  71. return $res;
  72. }
  73. }