UserRelationServices.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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\user;
  12. use app\dao\user\UserRelationDao;
  13. use app\jobs\product\ProductLogJob;
  14. use app\services\BaseServices;
  15. use app\services\product\product\StoreProductServices;
  16. use crmeb\traits\ServicesTrait;
  17. use think\db\exception\DataNotFoundException;
  18. use think\db\exception\DbException;
  19. use think\db\exception\ModelNotFoundException;
  20. use think\exception\ValidateException;
  21. /**
  22. * Class UserRelationServices
  23. * @package app\services\user
  24. * @mixin UserRelationDao
  25. */
  26. class UserRelationServices extends BaseServices
  27. {
  28. use ServicesTrait;
  29. const CATEGORY_PRODUCT = 'product';//商品
  30. const CATEGORY_REPLY = 'reply';//评价
  31. const CATEGORY_COMMENT = 'comment';//评价回复
  32. const CATEGORY_VIDEO = 'video';//短视频
  33. const CATEGORY_VIDEO_COMMENT = 'video_comment';//短视频评价
  34. const TYPE_COLLECT = 'collect';//收藏
  35. const TYPE_LIKE = 'like';//点赞
  36. const TYPE_SHARE = 'share';//分享
  37. const TYPE_PLAY = 'play';//播放
  38. const TYPE_NAME = [
  39. 'collect' => '收藏',
  40. 'like' => '点赞',
  41. 'share' => '分享',
  42. 'play' => '播放',
  43. ];
  44. /**
  45. * UserRelationServices constructor.
  46. * @param UserRelationDao $dao
  47. */
  48. public function __construct(UserRelationDao $dao)
  49. {
  50. $this->dao = $dao;
  51. }
  52. /**
  53. * 用户是否点赞或收藏商品
  54. * @param array $where
  55. * @return bool
  56. * @throws DataNotFoundException
  57. * @throws DbException
  58. * @throws ModelNotFoundException
  59. */
  60. public function isProductRelation(array $where)
  61. {
  62. $res = $this->dao->getOne($where);
  63. if ($res) {
  64. return true;
  65. } else {
  66. return false;
  67. }
  68. }
  69. /**
  70. * @param array $where
  71. * @return mixed
  72. * @author 等风来
  73. * @email 136327134@qq.com
  74. * @date 2022/11/17
  75. */
  76. public function isProductRelationCache(array $where)
  77. {
  78. return $this->cacheTag()->remember(md5(json_encode($where)), function () use ($where) {
  79. return $this->isProductRelation($where);
  80. });
  81. }
  82. /**
  83. * 获取用户收藏数量
  84. * @param int $uid
  85. * @param int $relationId
  86. * @param string $type
  87. * @param string $category
  88. * @return int
  89. */
  90. public function getUserCount(int $uid, int $relationId = 0, string $type = self::TYPE_COLLECT, string $category = self::CATEGORY_PRODUCT)
  91. {
  92. $where = ['uid' => $uid];
  93. if ($type) {
  94. $where['type'] = $type;
  95. }
  96. if ($category) {
  97. $where['category'] = $category;
  98. }
  99. if ($relationId) {
  100. $where['relation_id'] = $relationId;
  101. }
  102. return $this->dao->count($where);
  103. }
  104. /**
  105. * @param int $uid
  106. * @return mixed
  107. * @throws DataNotFoundException
  108. * @throws DbException
  109. * @throws ModelNotFoundException
  110. */
  111. public function getUserRelationList(int $uid, string $category = self::CATEGORY_PRODUCT, string $type = self::TYPE_COLLECT)
  112. {
  113. $where['uid'] = $uid;
  114. $where['type'] = $type;
  115. $where['category'] = $category;
  116. [$page, $limit] = $this->getPageValue();
  117. $with = [];
  118. switch ($category) {
  119. case 'product':
  120. $with = ['product'];
  121. break;
  122. case 'video':
  123. $with = ['video'];
  124. break;
  125. }
  126. //短视频未启用
  127. if ($category == 'video' && !sys_config('video_func_status', 1)) {
  128. $list = [];
  129. } else {
  130. $list = $this->dao->getList($where, 'relation_id,category', $with, $page, $limit);
  131. }
  132. $result = [];
  133. foreach ($list as $k => $item) {
  134. switch ($category) {
  135. case 'product':
  136. if (isset($item['product']) && isset($item['product']['id'])) {
  137. $product = $item['product'];
  138. $data = [
  139. 'id' => $product['id'] ?? 0,
  140. 'product_id' => $item['relation_id'],
  141. 'type' => $product['type'] ?? 0,
  142. 'pid' => $product['pid'] ?? 0,
  143. 'relation_id' => $product['relation_id'] ?? 0,
  144. 'store_name' => $product['store_name'] ?? 0,
  145. 'price' => $product['price'] ?? 0,
  146. 'ot_price' => $product['ot_price'] ?? 0,
  147. 'sales' => $product['sales'] ?? 0,
  148. 'image' => get_thumb_water($product['image'] ?? 0),
  149. 'is_del' => $product['is_del'] ?? 0,
  150. 'is_show' => $product['is_show'] ?? 0,
  151. 'is_fail' => ($product['is_del'] ?? 1) && !($product['is_show'] ?? 0),
  152. 'activity' => $product['activity'] ?? ''
  153. ];
  154. $result[] = $data;
  155. }
  156. break;
  157. case 'video':
  158. if (isset($item['video']) && isset($item['video']['id'])) {
  159. $video = $item['video'];
  160. $data = [
  161. 'id' => $video['id'] ?? 0,
  162. 'video_id' => $item['relation_id'],
  163. 'image' => $video['image'] ?? '',
  164. 'desc' => $video['desc'] ?? '',
  165. 'video_url' => $video['video_url'] ?? '',
  166. 'like_num' => $video['like_num'] ?? 0
  167. ];
  168. $result[] = $data;
  169. }
  170. break;
  171. }
  172. }
  173. if ($result && $category == 'product') {
  174. /** @var StoreProductServices $productServices */
  175. $productServices = app()->make(StoreProductServices::class);
  176. $result = $productServices->getActivityList($result);
  177. $result = $productServices->getProductPromotions($result);
  178. }
  179. return $result;
  180. }
  181. /**
  182. * 添加点赞 收藏
  183. * @param int $uid
  184. * @param array $productIds
  185. * @param string $relationType
  186. * @param string $category
  187. * @return bool
  188. * @throws DataNotFoundException
  189. * @throws DbException
  190. * @throws ModelNotFoundException
  191. */
  192. public function productRelation(int $uid, array $productIds, string $relationType, string $category = self::CATEGORY_PRODUCT)
  193. {
  194. $relationType = strtolower($relationType);
  195. $category = strtolower($category);
  196. $relationId = $this->dao->getColumn([['uid', '=', $uid], ['relation_id', 'IN', $productIds], ['type', '=', $relationType], ['category', '=', $category]], 'relation_id');
  197. $data = ['uid' => $uid, 'add_time' => time(), 'type' => $relationType, 'category' => $category];
  198. $dataAll = [];
  199. foreach ($productIds as $key => $product_id) {
  200. if (in_array($product_id, $relationId)) {
  201. continue;
  202. }
  203. $data['relation_id'] = $product_id;
  204. $dataAll[] = $data;
  205. }
  206. if ($dataAll) {
  207. if (!$this->dao->saveAll($dataAll)) {
  208. throw new ValidateException('添加失败');
  209. }
  210. }
  211. if ($category == 'product') {
  212. //收藏记录
  213. ProductLogJob::dispatch(['collect', ['uid' => $uid, 'relation_id' => $productIds, 'product_id' => $productIds]]);
  214. }
  215. $this->cacheTag()->clear();
  216. return true;
  217. }
  218. /**
  219. * 取消 点赞 收藏
  220. * @param int $uid
  221. * @param array $productId
  222. * @param string $relationType
  223. * @param string $category
  224. * @return bool
  225. * @throws \Exception
  226. */
  227. public function unProductRelation(int $uid, array $productId, string $relationType = self::TYPE_COLLECT, string $category = self::CATEGORY_PRODUCT)
  228. {
  229. $relationType = strtolower($relationType);
  230. $category = strtolower($category);
  231. $storeProductRelation = $this->dao->delete(['uid' => $uid, 'relation_id' => $productId, 'type' => $relationType, 'category' => $category]);
  232. if ($category == 'video') {
  233. foreach ($productId as $id) {
  234. $this->dao->bcDec($id, $relationType. '_num', 1);
  235. }
  236. }
  237. if (!$storeProductRelation) throw new ValidateException('取消失败');
  238. $this->cacheTag()->clear();
  239. return true;
  240. }
  241. }