GoodsViewFavCart.php 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. <?php
  2. namespace app\admin\model;
  3. use think\db\Query;
  4. use think\Model;
  5. use app\common\model\Goods;
  6. /**
  7. * @method Query|static typeView()
  8. * @method Query|static typeFav()
  9. * @method Query|static typeCart()
  10. */
  11. class GoodsViewFavCart extends Model
  12. {
  13. public function scopeTypeView(Query $query){
  14. $query->where('type','view');
  15. }
  16. public function scopeTypeCart(Query $query){
  17. $query->where('type','cart');
  18. }
  19. public function scopeTypeFav(Query $query){
  20. $query->where('type','fav');
  21. }
  22. public static function increment($goods,$type){
  23. $goods_id=$goods instanceof Goods ?$goods['id']:$goods;
  24. $date=date('Y-m-d');
  25. $has=self::where('goods_id',$goods_id)
  26. ->where('date',$date)
  27. ->where('type',$type)
  28. ->find();
  29. if($has){
  30. $has->setInc('num');
  31. }else{
  32. self::create([
  33. 'goods_id'=>$goods_id,
  34. 'date'=>$date,
  35. 'type'=>$type,
  36. 'num'=>1,
  37. ]);
  38. }
  39. }
  40. }