StoreProductRelationServices.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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\product\product;
  12. use app\dao\product\product\StoreProductRelationDao;
  13. use app\services\BaseServices;
  14. use app\jobs\ProductLogJob;
  15. use crmeb\exceptions\ApiException;
  16. /**
  17. * Class StoreProductRelationService
  18. * @package app\services\product\product
  19. */
  20. class StoreProductRelationServices extends BaseServices
  21. {
  22. /**
  23. * StoreProductRelationServices constructor.
  24. * @param StoreProductRelationDao $dao
  25. */
  26. public function __construct(StoreProductRelationDao $dao)
  27. {
  28. $this->dao = $dao;
  29. }
  30. /**
  31. * 用户是否点赞或收藏商品
  32. * @param array $where
  33. * @return bool
  34. * @throws \think\db\exception\DataNotFoundException
  35. * @throws \think\db\exception\DbException
  36. * @throws \think\db\exception\ModelNotFoundException
  37. */
  38. public function isProductRelation(array $where)
  39. {
  40. $res = $this->dao->getOne($where);
  41. if ($res) {
  42. return true;
  43. } else {
  44. return false;
  45. }
  46. }
  47. /**
  48. * 获取用户收藏数量
  49. * @param int $uid
  50. * @return int
  51. */
  52. public function getUserCollectCount(int $uid)
  53. {
  54. return $this->dao->count(['uid' => $uid, 'type' => 'collect']);
  55. }
  56. /**
  57. * 收藏
  58. * @param int $uid
  59. * @return array
  60. * @throws \think\db\exception\DataNotFoundException
  61. * @throws \think\db\exception\DbException
  62. * @throws \think\db\exception\ModelNotFoundException
  63. * @author 吴汐
  64. * @email 442384644@qq.com
  65. * @date 2023/03/01
  66. */
  67. public function getUserCollectProduct(int $uid)
  68. {
  69. $where['uid'] = $uid;
  70. $where['type'] = 'collect';
  71. [$page, $limit] = $this->getPageValue();
  72. $list = $this->dao->getList($where, 'product_id,category', $page, $limit);
  73. foreach ($list as $k => $product) {
  74. if ($product['product'] && isset($product['product']['id'])) {
  75. $list[$k]['pid'] = $product['product']['id'] ?? 0;
  76. $list[$k]['store_name'] = $product['product']['store_name'] ?? 0;
  77. $list[$k]['price'] = $product['product']['price'] ?? 0;
  78. $list[$k]['ot_price'] = $product['product']['ot_price'] ?? 0;
  79. $list[$k]['sales'] = $product['product']['sales'] ?? 0;
  80. $list[$k]['image'] = get_thumb_water($product['product']['image'] ?? 0);
  81. $list[$k]['is_del'] = $product['product']['is_del'] ?? 0;
  82. $list[$k]['is_show'] = $product['product']['is_show'] ?? 0;
  83. $list[$k]['is_fail'] = $product['product']['is_del'] && $product['product']['is_show'];
  84. } else {
  85. unset($list[$k]);
  86. }
  87. }
  88. $count = $this->dao->count($where);
  89. return compact('list', 'count');
  90. }
  91. /**
  92. * 添加点赞 收藏
  93. * @param int $productId
  94. * @param int $uid
  95. * @param string $relationType
  96. * @param string $category
  97. * @return bool
  98. */
  99. public function productRelation(int $productId, int $uid, string $relationType, string $category = 'product')
  100. {
  101. $relationType = strtolower($relationType);
  102. $category = strtolower($category);
  103. $data = ['uid' => $uid, 'product_id' => $productId, 'type' => $relationType, 'category' => $category];
  104. if ($this->dao->getOne($data)) {
  105. return true;
  106. }
  107. $data['add_time'] = time();
  108. if (!$this->dao->save($data)) {
  109. throw new ApiException(100006);
  110. }
  111. //收藏记录
  112. ProductLogJob::dispatch(['collect', ['uid' => $uid, 'product_id' => $productId]]);
  113. //自定义事件-用户商品收藏
  114. event('CustomEventListener', ['user_product_collect', [
  115. 'product_id' => $productId,
  116. 'uid' => $uid,
  117. 'collect_time' => date('Y-m-d H:i:s'),
  118. ]]);
  119. return true;
  120. }
  121. /**
  122. * 取消 点赞 收藏
  123. * @param array $productId
  124. * @param int $uid
  125. * @param string $relationType
  126. * @param string $category
  127. * @return bool
  128. * @throws \Exception
  129. */
  130. public function unProductRelation(array $productId, int $uid, string $relationType, string $category = 'product')
  131. {
  132. $relationType = strtolower($relationType);
  133. $category = strtolower($category);
  134. $storeProductRelation = $this->dao->delete([
  135. ['uid', '=', $uid],
  136. ['product_id', 'in', $productId],
  137. ['type', '=', $relationType],
  138. ['category', '=', $category]
  139. ]);
  140. if (!$storeProductRelation) throw new ApiException(100020);
  141. return true;
  142. }
  143. /**
  144. * 批量 添加点赞 收藏
  145. * @param array $productIdS
  146. * @param int $uid
  147. * @param string $relationType
  148. * @param string $category
  149. * @return bool
  150. */
  151. public function productRelationAll(array $productIdS, int $uid, string $relationType, string $category = 'product')
  152. {
  153. $relationType = strtolower($relationType);
  154. $category = strtolower($category);
  155. $relationData = [];
  156. $productIdS = array_unique($productIdS);
  157. $relationProductIdS = $this->dao->getColumn(['uid' => $uid, 'type' => $relationType, 'category' => $category, 'product_id' => $productIdS], 'product_id');
  158. foreach ($productIdS as $productId) {
  159. if (!in_array($productId, $relationProductIdS)) {
  160. $relationData[] = ['uid' => $uid, 'product_id' => $productId, 'type' => $relationType, 'category' => $category];
  161. }
  162. }
  163. if ($relationData) {
  164. if (!$this->dao->saveAll($relationData)) {
  165. throw new ApiException(100022);
  166. }
  167. }
  168. return true;
  169. }
  170. }