StoreSeckill.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. <?php
  2. /**
  3. *
  4. * @author: xaboy<365615158@qq.com>
  5. * @day: 2017/12/18
  6. */
  7. namespace app\wap\model\store;
  8. use basic\ModelBasic;
  9. class StoreSeckill extends ModelBasic
  10. {
  11. protected function getImagesAttr($value)
  12. {
  13. return json_decode($value,true)?:[];
  14. }
  15. /**
  16. * 获取所有秒杀产品
  17. * @param string $field
  18. * @return array
  19. */
  20. public static function getListAll($field = 'id,product_id,image,title,price,ot_price,start_time,stop_time,stock,sales'){
  21. $time = time();
  22. $model = self::where('is_del',0)->where('status',1)->where('stock','>',0)->field($field)
  23. ->where('start_time','<',$time)->where('stop_time','>',$time)->order('sort DESC,add_time DESC');
  24. $list = $model->select();
  25. if($list) return $list->toArray();
  26. else return [];
  27. }
  28. /**
  29. * 获取热门推荐的秒杀产品
  30. * @param int $limit
  31. * @param string $field
  32. * @return array
  33. */
  34. public static function getHotList($limit = 0,$field = 'id,product_id,image,title,price,ot_price,start_time,stop_time,stock')
  35. {
  36. $time = time();
  37. $model = self::where('is_hot',1)->where('is_del',0)->where('status',1)->where('stock','>',0)->field($field)
  38. ->where('start_time','<',$time)->where('stop_time','>',$time)->order('sort DESC,add_time DESC');
  39. if($limit) $model->limit($limit);
  40. $list = $model->select();
  41. if($list) return $list->toArray();
  42. else return [];
  43. }
  44. /**
  45. * 获取一条秒杀产品
  46. * @param $id
  47. * @param string $field
  48. * @return array|false|\PDOStatement|string|\think\Model
  49. */
  50. public static function getValidProduct($id,$field = '*')
  51. {
  52. $time = time();
  53. return self::where('id',$id)->where('is_del',0)->where('status',1)->where('start_time','<',$time)->where('stop_time','>',$time)
  54. ->field($field)->find();
  55. }
  56. public static function initFailSeckill()
  57. {
  58. self::where('is_hot',1)->where('is_del',0)->where('status','<>',1)->where('stop_time','<',time())->update(['status'=>'-1']);
  59. }
  60. public static function idBySimilaritySeckill($id,$limit = 4,$field='*')
  61. {
  62. $time = time();
  63. $list = [];
  64. $productId = self::where('id',$id)->value('product_id');
  65. if($productId){
  66. $list = array_merge($list, self::where('product_id',$productId)->where('id','<>',$id)
  67. ->where('is_del',0)->where('status',1)->where('stock','>',0)
  68. ->field($field)->where('start_time','<',$time)->where('stop_time','>',$time)
  69. ->order('sort DESC,add_time DESC')->limit($limit)->select()->toArray());
  70. }
  71. $limit = $limit - count($list);
  72. if($limit){
  73. $list = array_merge($list,self::getHotList($limit,$field));
  74. }
  75. return $list;
  76. }
  77. public static function getProductStock($id){
  78. $stock = self::where('id',$id)->value('stock');
  79. if(self::where('id',$id)->where('num','gt',$stock)->count()){//单次购买的产品多于库存
  80. return $stock;
  81. }else{
  82. return self::where('id',$id)->value('num');
  83. }
  84. }
  85. /**
  86. * 修改秒杀库存
  87. * @param int $num
  88. * @param int $seckillId
  89. * @return bool
  90. */
  91. public static function decSeckillStock($num = 0,$seckillId = 0){
  92. $res = false !== self::where('id',$seckillId)->dec('stock',$num)->inc('sales',$num)->update();
  93. return $res;
  94. }
  95. }