StoreSeckill.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. <?php
  2. /**
  3. *
  4. * @author: xaboy<365615158@qq.com>
  5. * @day: 2017/12/18
  6. */
  7. namespace app\ebapi\model\store;
  8. use basic\ModelBasic;
  9. use app\core\util\GroupDataService;
  10. class StoreSeckill extends ModelBasic
  11. {
  12. protected function getImagesAttr($value)
  13. {
  14. return json_decode($value,true)?:[];
  15. }
  16. public static function getSeckillCount()
  17. {
  18. $seckillTime = GroupDataService::getData('routine_seckill_time')?:[];//秒杀时间段
  19. $timeInfo=['time'=>0,'continued'=>0];
  20. foreach($seckillTime as $key=>$value){
  21. $currentHour = date('H');
  22. $activityEndHour = bcadd((int)$value['time'],(int)$value['continued'],0);
  23. if($currentHour >= (int)$value['time'] && $currentHour < $activityEndHour && $activityEndHour < 24){
  24. $timeInfo=$value;
  25. break;
  26. }
  27. }
  28. if($timeInfo['time']==0) return 0;
  29. $activityEndHour = bcadd((int)$timeInfo['time'],(int)$timeInfo['continued'],0);
  30. $startTime = bcadd(strtotime(date('Y-m-d')),bcmul($timeInfo['time'],3600,0));
  31. $stopTime = bcadd(strtotime(date('Y-m-d')),bcmul($activityEndHour,3600,0));
  32. return self::where('is_del',0)->where('status',1)->where('start_time','<=',$startTime)->where('stop_time','>=',$stopTime)->count();
  33. }
  34. /*
  35. * 获取秒杀列表
  36. *
  37. * */
  38. public static function seckillList($startTime,$stopTime,$offset = 0,$limit = 20)
  39. {
  40. $list = StoreSeckill::where('is_del',0)->where('status',1)->where('start_time','<=',$startTime)->where('stop_time','>=',$stopTime)->order('sort desc')->limit($offset,$limit)->select();
  41. if($list) return $list->toArray();
  42. else return [];
  43. }
  44. /**
  45. * 获取所有秒杀产品
  46. * @param string $field
  47. * @return array
  48. */
  49. public static function getListAll($offset = 0,$limit = 10,$field = 'id,product_id,image,title,price,ot_price,start_time,stop_time,stock,sales'){
  50. $time = time();
  51. $model = self::where('is_del',0)->where('status',1)->where('stock','>',0)->field($field)
  52. ->where('start_time','<',$time)->where('stop_time','>',$time)->order('sort DESC,add_time DESC');
  53. $model = $model->limit($offset,$limit);
  54. $list = $model->select();
  55. if($list) return $list->toArray();
  56. else return [];
  57. }
  58. /**
  59. * 获取热门推荐的秒杀产品
  60. * @param int $limit
  61. * @param string $field
  62. * @return array
  63. */
  64. public static function getHotList($limit = 0,$field = 'id,product_id,image,title,price,ot_price,start_time,stop_time,stock')
  65. {
  66. $time = time();
  67. $model = self::where('is_hot',1)->where('is_del',0)->where('status',1)->where('stock','>',0)->field($field)
  68. ->where('start_time','<',$time)->where('stop_time','>',$time)->order('sort DESC,add_time DESC');
  69. if($limit) $model->limit($limit);
  70. $list = $model->select();
  71. if($list) return $list->toArray();
  72. else return [];
  73. }
  74. /**
  75. * 获取一条秒杀产品
  76. * @param $id
  77. * @param string $field
  78. * @return array|false|\PDOStatement|string|\think\Model
  79. */
  80. public static function getValidProduct($id,$field = '*')
  81. {
  82. $time = time();
  83. return self::where('id',$id)->where('is_del',0)->where('status',1)->where('start_time','<',$time)->where('stop_time','>',$time)
  84. ->field($field)->find();
  85. }
  86. public static function initFailSeckill()
  87. {
  88. self::where('is_hot',1)->where('is_del',0)->where('status','<>',1)->where('stop_time','<',time())->update(['status'=>'-1']);
  89. }
  90. public static function idBySimilaritySeckill($id,$limit = 4,$field='*')
  91. {
  92. $time = time();
  93. $list = [];
  94. $productId = self::where('id',$id)->value('product_id');
  95. if($productId){
  96. $list = array_merge($list, self::where('product_id',$productId)->where('id','<>',$id)
  97. ->where('is_del',0)->where('status',1)->where('stock','>',0)
  98. ->field($field)->where('start_time','<',$time)->where('stop_time','>',$time)
  99. ->order('sort DESC,add_time DESC')->limit($limit)->select()->toArray());
  100. }
  101. $limit = $limit - count($list);
  102. if($limit){
  103. $list = array_merge($list,self::getHotList($limit,$field));
  104. }
  105. return $list;
  106. }
  107. /** 获取秒杀产品库存
  108. * @param $id
  109. * @return mixed
  110. */
  111. public static function getProductStock($id){
  112. return self::where('id',$id)->value('stock');
  113. }
  114. /**
  115. * 修改秒杀库存
  116. * @param int $num
  117. * @param int $seckillId
  118. * @return bool
  119. */
  120. public static function decSeckillStock($num = 0,$seckillId = 0){
  121. $res = false !== self::where('id',$seckillId)->dec('stock',$num)->inc('sales',$num)->update();
  122. return $res;
  123. }
  124. /**
  125. * 增加库存较少销量
  126. * @param int $num
  127. * @param int $seckillId
  128. * @return bool
  129. */
  130. public static function incSeckillStock($num = 0,$seckillId = 0){
  131. $res = false !== self::where('id',$seckillId)->inc('stock',$num)->dec('sales',$num)->update();
  132. return $res;
  133. }
  134. }