StoreProductRelation.php 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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. * 获取某个用户拼团收藏产品
  154. * @param int uid 用户id
  155. * @param int $first 行数
  156. * @param int $limit 展示行数
  157. * @return array
  158. * */
  159. public static function getUserPinkCollectProduct($uid,$page,$limit)
  160. {
  161. if(!$limit) return [];
  162. if($page){
  163. $list = self::where('A.uid',$uid)
  164. ->field('B.id pid,A.category,B.title store_name,B.price,C.ot_price,C.sales,B.image,B.is_del,B.is_show')
  165. ->alias('A')
  166. ->where('A.type','collect')/*->where('A.category','product')*/
  167. ->where('A.category','pink')
  168. ->order('A.add_time DESC')
  169. ->join('store_combination B','A.product_id = B.id')
  170. ->join('store_product C','C.id = B.product_id')
  171. ->page($page, $limit)
  172. ->select();
  173. }else{
  174. $list = self::where('A.uid',$uid)
  175. ->field('B.id pid,A.category,B.title store_name,B.price,C.ot_price,C.sales,B.image,B.is_del,B.is_show')
  176. ->alias('A')
  177. ->where('A.type','collect')/*->where('A.category','product')*/
  178. ->where('A.category','pink')
  179. ->order('A.add_time DESC')
  180. ->join('store_combination B','A.product_id = B.id')
  181. ->join('store_product C','C.id = B.product_id')
  182. ->select();
  183. }
  184. if(!$list) return [];
  185. $list = $list->toArray();
  186. foreach ($list as $k=>$product){
  187. if($product['pid']){
  188. $list[$k]['is_fail'] = $product['is_del'] && $product['is_show'];
  189. }else{
  190. unset($list[$k]);
  191. }
  192. }
  193. return $list;
  194. }
  195. /**
  196. * TODO 获取普通产品收藏
  197. * @param $uid
  198. * @param int $first
  199. * @param int $limit
  200. * @return array
  201. */
  202. public static function getProductRelation($uid, $first = 0,$limit = 8)
  203. {
  204. $model = new self;
  205. $model = $model->alias('A');
  206. $model = $model->join('StoreProduct B','A.product_id = B.id');
  207. $model = $model->where('A.uid',$uid);
  208. $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');
  209. $model = $model->where('A.type','collect');
  210. $model = $model->where('A.category','product');
  211. $model = $model->order('A.add_time DESC');
  212. $model = $model->limit($first,$limit);
  213. $list = $model->select();
  214. if($list) return $list->toArray();
  215. else return [];
  216. }
  217. /**
  218. * TODO 获取秒杀产品收藏
  219. * @param $uid
  220. * @param int $first
  221. * @param int $limit
  222. * @return array
  223. */
  224. public static function getSeckillRelation($uid, $first = 0,$limit = 8)
  225. {
  226. $model = new self;
  227. $model = $model->alias('A');
  228. $model = $model->join('StoreSeckill B','A.product_id = B.id');
  229. $model = $model->where('A.uid',$uid);
  230. $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');
  231. $model = $model->where('A.type','collect');
  232. $model = $model->where('A.category','product_seckill');
  233. $model = $model->order('A.add_time DESC');
  234. $model = $model->limit($first,$limit);
  235. $list = $model->select();
  236. if($list) return $list->toArray();
  237. else return [];
  238. }
  239. }