StoreSeckill.php 3.5 KB

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