Favorite.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. <?php
  2. namespace app\common\model\user;
  3. use app\common\model\BaseModel;
  4. use app\api\model\product\Product as ProductModel;
  5. use app\api\model\supplier\Category as CategoryModel;
  6. /**
  7. * 收藏模型
  8. */
  9. class Favorite extends BaseModel
  10. {
  11. protected $pk = 'favorite_id';
  12. protected $name = 'user_favorite';
  13. //获取收藏商品列表
  14. public function getList($where,$param){
  15. $list = $this->where($where)
  16. ->with(['product','image.file'])
  17. ->field("*")
  18. ->order('create_time desc')
  19. ->paginate($param);
  20. foreach ($list as &$product) {
  21. // 商品主图
  22. $product['product_image'] = $product['image'][0]['file_path'];
  23. $product['product_name'] = $product['product'][0]['product_name'];
  24. $product['product_price'] = $product['product'][0]['product_price'];
  25. $product['product_id'] = $product['product'][0]['product_id'];
  26. $product['city_name'] = $product['product'][0]['city_name'];
  27. $product['line_price'] = $product['product'][0]['line_price'];
  28. $product['product_sales'] = $product['product'][0]['product_sales'];
  29. unset($product['image']);unset($product['product']);
  30. }
  31. return $list;
  32. }
  33. //获取关注店铺列表
  34. public function getMySupplier($where,$param){
  35. $product_model = new ProductModel;
  36. $list = $this->alias('s')->where($where)
  37. ->with(['supplier','supplier.logo'])
  38. ->field("*")
  39. ->order('create_time desc')
  40. ->paginate($param);
  41. foreach ($list as $key => &$value) {
  42. $value['store_name'] = $value['supplier']['name'];
  43. $value['shop_supplier_id'] = $value['supplier']['shop_supplier_id'];
  44. $value['logo'] = $value['supplier']['logo']['file_path'];
  45. $value['score'] = $value['supplier']['score'];
  46. $value['fav_count'] = $value['supplier']['fav_count'];
  47. $value['categoryName'] = $value['supplier']['category_id']?(new CategoryModel())->where('category_id','=',$value['supplier']['category_id'])->value('name'):'';
  48. unset($value['supplier']);
  49. //获取最新上架商品
  50. $productList = $product_model->with(['image.file'])->where(['product_status'=>10,'shop_supplier_id'=>$value['shop_supplier_id'],'audit_status'=>10,'is_delete'=>0])->field("product_name,product_id,sales_initial,sales_actual,product_price,line_price")->order('product_id desc')->limit(3)->select();
  51. foreach ($productList as $kk => &$vv) {
  52. $vv['logo'] = $vv['image'][0]['file_path'];
  53. unset($vv['image']);
  54. }
  55. $value['productList'] = $productList;
  56. }
  57. return $list;
  58. }
  59. /**
  60. * 关联商品图片表
  61. */
  62. public function image()
  63. {
  64. return $this->hasMany('app\\common\\model\\product\\ProductImage','product_id','pid')->order(['id' => 'asc']);
  65. }
  66. /**
  67. * 关联商品表
  68. */
  69. public function product()
  70. {
  71. return $this->hasMany('app\\common\\model\\product\\Product','product_id','pid')->bind(['product_id','sales_actual']);
  72. }
  73. /**
  74. * 关联店铺
  75. */
  76. public function supplier()
  77. {
  78. return $this->hasOne('app\\common\\model\\supplier\\Supplier','shop_supplier_id','pid')->hidden(['user_id','password','total_money','money','freeze_money','cash_money','is_delete','app_id','create_time','update_time']);
  79. }
  80. /**
  81. * 关联用户
  82. */
  83. public function user()
  84. {
  85. return $this->hasOne('app\\common\\model\\user\\User','user_id','user_id')
  86. ->field(['user_id','nickName','mobile','avatarUrl','create_time','country','province','city','gender']);
  87. }
  88. /**
  89. * 根据收藏id,类型,用户查询,判断用户是否收藏
  90. */
  91. public static function detail($pid, $type, $user_id){
  92. $where['pid'] = $pid;
  93. $where['type'] = $type;
  94. $where['user_id'] = $user_id;
  95. return (new static())->where($where)->find();
  96. }
  97. }