StoreSeckill.php 6.0 KB

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