StoreProductRelation.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. <?php
  2. namespace app\models\store;
  3. use crmeb\traits\ModelTrait;
  4. use crmeb\basic\BaseModel;
  5. /**
  6. * TODO 点赞收藏model
  7. * Class StoreProductRelation
  8. * @package app\models\store
  9. */
  10. class StoreProductRelation extends BaseModel
  11. {
  12. /**
  13. * 模型名称
  14. * @var string
  15. */
  16. protected $name = 'store_product_relation';
  17. use ModelTrait;
  18. /**
  19. * 获取用户点赞所有产品的个数
  20. * @param $uid
  21. * @return int|string
  22. */
  23. public static function getUserIdLike($uid = 0)
  24. {
  25. $count = self::where('uid', $uid)->where('type', 'like')->count();
  26. return $count;
  27. }
  28. /**
  29. * 获取用户收藏所有产品的个数
  30. * @param $uid
  31. * @return int|string
  32. */
  33. public static function getUserIdCollect($uid = 0)
  34. {
  35. $count = self::where('uid', $uid)->where('type', 'collect')->count();
  36. return $count;
  37. }
  38. /**
  39. * 添加点赞 收藏
  40. * @param $productId
  41. * @param $uid
  42. * @param $relationType
  43. * @param string $category
  44. * @return bool
  45. */
  46. public static function productRelation($productId, $uid, $relationType, $category = 'product')
  47. {
  48. if (!$productId) return self::setErrorInfo('产品不存在!');
  49. $relationType = strtolower($relationType);
  50. $category = strtolower($category);
  51. $data = ['uid' => $uid, 'product_id' => $productId, 'type' => $relationType, 'category' => $category];
  52. if (self::be($data)) return true;
  53. $data['add_time'] = time();
  54. self::create($data);
  55. event('StoreProductUserOperationConfirmAfter', [$category, $productId, $relationType, $uid]);
  56. return true;
  57. }
  58. /**
  59. * 批量 添加点赞 收藏
  60. * @param $productIdS
  61. * @param $uid
  62. * @param $relationType
  63. * @param string $category
  64. * @return bool
  65. */
  66. public static function productRelationAll($productIdS, $uid, $relationType, $category = 'product')
  67. {
  68. $res = true;
  69. if (is_array($productIdS)) {
  70. self::beginTrans();
  71. foreach ($productIdS as $productId) {
  72. $res = $res && self::productRelation($productId, $uid, $relationType, $category);
  73. }
  74. if ($res) {
  75. foreach ($productIdS as $productId) {
  76. event('StoreProductUserOperationConfirmAfter', [$category, $productId, $relationType, $uid]);
  77. }
  78. }
  79. self::checkTrans($res);
  80. return $res;
  81. }
  82. return $res;
  83. }
  84. /**
  85. * 取消 点赞 收藏
  86. * @param $productId
  87. * @param $uid
  88. * @param $relationType
  89. * @param string $category
  90. * @return bool
  91. */
  92. public static function unProductRelation($productId, $uid, $relationType, $category = 'product')
  93. {
  94. if (!$productId) return self::setErrorInfo('产品不存在!');
  95. $relationType = strtolower($relationType);
  96. $category = strtolower($category);
  97. self::where('uid', $uid)->where('product_id', $productId)->where('type', $relationType)->where('category', $category)->delete();
  98. event('StoreProductUserOperationCancelAfter', [$category, $productId, $relationType, $uid]);
  99. return true;
  100. }
  101. public static function productRelationNum($productId, $relationType, $category = 'product')
  102. {
  103. $relationType = strtolower($relationType);
  104. $category = strtolower($category);
  105. return self::where('type', $relationType)->where('product_id', $productId)->where('category', $category)->count();
  106. }
  107. public static function isProductRelation($product_id, $uid, $relationType, $category = 'product')
  108. {
  109. $type = strtolower($relationType);
  110. $category = strtolower($category);
  111. return self::be(compact('product_id', 'uid', 'type', 'category'));
  112. }
  113. /*
  114. * 获取某个用户收藏产品
  115. * @param int uid 用户id
  116. * @param int $first 行数
  117. * @param int $limit 展示行数
  118. * @return array
  119. * */
  120. public static function getUserCollectProduct($uid, $page, $limit)
  121. {
  122. if (!$limit) return [];
  123. if ($page) {
  124. $list = self::where('A.uid', $uid)
  125. ->field('B.id pid,A.category,B.store_name,B.price,B.ot_price,B.sales,B.image,B.is_del,B.is_show')
  126. ->alias('A')
  127. ->where('A.type', 'collect')/*->where('A.category','product')*/
  128. ->order('A.add_time DESC')
  129. ->join('store_product B', 'A.product_id = B.id')
  130. ->page($page, $limit)
  131. ->select();
  132. } else {
  133. $list = self::where('A.uid', $uid)
  134. ->field('B.id pid,A.category,B.store_name,B.price,B.ot_price,B.sales,B.image,B.is_del,B.is_show')->alias('A')
  135. ->where('A.type', 'collect')/*->where('A.category','product')*/
  136. ->order('A.add_time DESC')->join('store_product B', 'A.product_id = B.id')
  137. ->select();
  138. }
  139. if (!$list) return [];
  140. $list = $list->toArray();
  141. foreach ($list as $k => $product) {
  142. if ($product['pid']) {
  143. $list[$k]['is_fail'] = $product['is_del'] && $product['is_show'];
  144. } else {
  145. unset($list[$k]);
  146. }
  147. }
  148. return $list;
  149. }
  150. /**
  151. * TODO 获取普通产品收藏
  152. * @param $uid
  153. * @param int $first
  154. * @param int $limit
  155. * @return array
  156. */
  157. public static function getProductRelation($uid, $first = 0, $limit = 8)
  158. {
  159. $model = new self;
  160. $model = $model->alias('A');
  161. $model = $model->join('StoreProduct B', 'A.product_id = B.id');
  162. $model = $model->where('A.uid', $uid);
  163. $model = $model->field('B.id pid,B.store_name,B.price,B.ot_price,B.ficti sales,B.image,B.is_del,B.is_show,A.category,A.add_time');
  164. $model = $model->where('A.type', 'collect');
  165. $model = $model->where('A.category', 'product');
  166. $model = $model->order('A.add_time DESC');
  167. $model = $model->limit($first, $limit);
  168. $list = $model->select();
  169. if ($list) return $list->toArray();
  170. else return [];
  171. }
  172. /**
  173. * TODO 获取秒杀产品收藏
  174. * @param $uid
  175. * @param int $first
  176. * @param int $limit
  177. * @return array
  178. */
  179. public static function getSeckillRelation($uid, $first = 0, $limit = 8)
  180. {
  181. $model = new self;
  182. $model = $model->alias('A');
  183. $model = $model->join('StoreSeckill B', 'A.product_id = B.id');
  184. $model = $model->where('A.uid', $uid);
  185. $model = $model->field('B.id pid,B.title store_name,B.price,B.ot_price,B.sales,B.image,B.is_del,B.is_show,A.category,A.add_time');
  186. $model = $model->where('A.type', 'collect');
  187. $model = $model->where('A.category', 'product_seckill');
  188. $model = $model->order('A.add_time DESC');
  189. $model = $model->limit($first, $limit);
  190. $list = $model->select();
  191. if ($list) return $list->toArray();
  192. else return [];
  193. }
  194. }