ProductServices.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2023 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\services\kefu;
  12. use app\services\BaseServices;
  13. use app\services\product\product\StoreProductCateServices;
  14. use crmeb\exceptions\ApiException;
  15. use app\dao\product\product\StoreProductDao;
  16. use app\services\order\StoreOrderStoreOrderCartInfoServices;
  17. use app\services\product\product\StoreProductVisitServices;
  18. /**
  19. * Class ProductServices
  20. * @package app\services\kefu
  21. */
  22. class ProductServices extends BaseServices
  23. {
  24. /**
  25. * ProductServices constructor.
  26. * @param StoreProductDao $dao
  27. */
  28. public function __construct(StoreProductDao $dao)
  29. {
  30. $this->dao = $dao;
  31. }
  32. /**
  33. * 获取用户购买记录
  34. * @param int $uid
  35. * @return array
  36. * @throws \think\db\exception\DataNotFoundException
  37. * @throws \think\db\exception\DbException
  38. * @throws \think\db\exception\ModelNotFoundException
  39. */
  40. public function getProductCartList(int $uid, string $storeName = '')
  41. {
  42. [$page, $limit] = $this->getPageValue();
  43. /** @var StoreOrderStoreOrderCartInfoServices $services */
  44. $services = app()->make(StoreOrderStoreOrderCartInfoServices::class);
  45. $where['id'] = $services->getUserCartProductIds(['uid' => $uid]);
  46. $where['store_name'] = $storeName;
  47. return $this->dao->getProductCartList($where, $page, $limit, ['id', 'IFNULL(sales,0) + IFNULL(ficti,0) as sales', 'store_name', 'image', 'stock', 'price']);
  48. }
  49. /**
  50. * 获取用户浏览足记
  51. * @param int $uid
  52. * @return mixed
  53. */
  54. public function getVisitProductList(int $uid, string $storeName = '')
  55. {
  56. [$page, $limit] = $this->getPageValue();
  57. /** @var StoreProductVisitServices $service */
  58. $service = app()->make(StoreProductVisitServices::class);
  59. return $service->getUserVisitProductList(['uid' => $uid, 'store_name' => $storeName], $page, $limit);
  60. }
  61. /**
  62. * 获取热销商品前20
  63. * @param int $uid
  64. * @return array
  65. * @throws \think\db\exception\DataNotFoundException
  66. * @throws \think\db\exception\DbException
  67. * @throws \think\db\exception\ModelNotFoundException
  68. */
  69. public function getProductHotSale(int $uid, string $storeName = '')
  70. {
  71. [$page, $limit] = $this->getPageValue();
  72. /** @var StoreOrderStoreOrderCartInfoServices $services */
  73. $services = app()->make(StoreOrderStoreOrderCartInfoServices::class);
  74. $productIds = $services->getUserCartProductIds(['uid' => $uid]);
  75. /** @var StoreProductCateServices $cateService */
  76. $cateService = app()->make(StoreProductCateServices::class);
  77. $where['id'] = $cateService->cateIdByProduct($cateService->productIdByCateId($productIds));
  78. $where['store_name'] = $storeName;
  79. return $this->dao->getUserProductHotSale($where, $page, $limit);
  80. }
  81. /**
  82. * 获取商品详情
  83. * @param int $id
  84. * @return array
  85. * @throws \think\db\exception\DataNotFoundException
  86. * @throws \think\db\exception\DbException
  87. * @throws \think\db\exception\ModelNotFoundException
  88. */
  89. public function getProductInfo(int $id)
  90. {
  91. $productInfo = $this->dao->get($id, ['store_name', 'IFNULL(sales,0) + IFNULL(ficti,0) as sales', 'image',
  92. 'slider_image', 'price', 'vip_price', 'ot_price', 'stock', 'id'], ['description']);
  93. if (!$productInfo) {
  94. throw new ApiException(410143);
  95. }
  96. return $productInfo->toArray();
  97. }
  98. }