ProductServices.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2020 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\StoreProductRelationServices;
  14. use think\exception\ValidateException;
  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. * @mixin StoreProductDao
  22. */
  23. class ProductServices extends BaseServices
  24. {
  25. const FMJUAVFD = 'WuJEUS';
  26. /**
  27. * ProductServices constructor.
  28. * @param StoreProductDao $dao
  29. */
  30. public function __construct(StoreProductDao $dao)
  31. {
  32. $this->dao = $dao;
  33. }
  34. /**
  35. * 获取用户购买记录
  36. * @param int $uid
  37. * @return array
  38. * @throws \think\db\exception\DataNotFoundException
  39. * @throws \think\db\exception\DbException
  40. * @throws \think\db\exception\ModelNotFoundException
  41. */
  42. public function getProductCartList(int $uid, string $storeName = '')
  43. {
  44. [$page, $limit] = $this->getPageValue();
  45. $where = [];
  46. if (!$storeName) {//搜索不局限加入购物车|浏览
  47. /** @var StoreOrderStoreOrderCartInfoServices $services */
  48. $services = app()->make(StoreOrderStoreOrderCartInfoServices::class);
  49. $where['id'] = $services->getUserCartProductIds(['uid' => $uid]);
  50. } else {
  51. $where['store_name'] = $storeName;
  52. $where['pid'] = 0;
  53. }
  54. return $this->dao->getProductCartList($where, $page, $limit, ['id', 'IFNULL(sales,0) + IFNULL(ficti,0) as sales', 'store_name', 'image', 'stock', 'price']);
  55. }
  56. /**
  57. * 获取用户浏览足记
  58. * @param int $uid
  59. * @return mixed
  60. */
  61. public function getVisitProductList(int $uid, string $storeName = '')
  62. {
  63. [$page, $limit] = $this->getPageValue();
  64. if ($storeName) {
  65. return $this->getProductCartList($uid, $storeName);
  66. } else {
  67. /** @var StoreProductVisitServices $service */
  68. $service = app()->make(StoreProductVisitServices::class);
  69. return $service->getUserVisitProductList(['uid' => $uid, 'store_name' => $storeName], $page, $limit);
  70. }
  71. }
  72. /**
  73. * 获取热销商品前20
  74. * @param int $uid
  75. * @return array
  76. * @throws \think\db\exception\DataNotFoundException
  77. * @throws \think\db\exception\DbException
  78. * @throws \think\db\exception\ModelNotFoundException
  79. */
  80. public function getProductHotSale(int $uid, string $storeName = '')
  81. {
  82. $where = [];
  83. $where['is_verify'] = 1;
  84. $where['is_show'] = 1;
  85. $where['is_del'] = 0;
  86. if (!$storeName) {//搜索不局限加入购物车|浏览
  87. /** @var StoreOrderStoreOrderCartInfoServices $services */
  88. $services = app()->make(StoreOrderStoreOrderCartInfoServices::class);
  89. $productIds = $services->getUserCartProductIds(['uid' => $uid]);
  90. /** @var StoreProductRelationServices $cateService */
  91. $cateService = app()->make(StoreProductRelationServices::class);
  92. $where['id'] = $cateService->cateIdByProduct($cateService->productIdByCateId($productIds));
  93. } else {
  94. $where['store_name'] = $storeName;
  95. $where['pid'] = 0;
  96. }
  97. return $this->dao->getUserProductHotSale($where);
  98. }
  99. /**
  100. * 获取商品详情
  101. * @param int $id
  102. * @return array
  103. * @throws \think\db\exception\DataNotFoundException
  104. * @throws \think\db\exception\DbException
  105. * @throws \think\db\exception\ModelNotFoundException
  106. */
  107. public function getProductInfo(int $id)
  108. {
  109. $productInfo = $this->dao->get($id, ['store_name', 'IFNULL(sales,0) + IFNULL(ficti,0) as sales', 'image',
  110. 'slider_image', 'price', 'vip_price', 'ot_price', 'stock', 'id'], ['descriptions']);
  111. if (!$productInfo) {
  112. throw new ValidateException('商品未查到');
  113. }
  114. return $productInfo->toArray();
  115. }
  116. }