StoreProductRelation.php 7.1 KB

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