StoreSeckill.php 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. <?php
  2. /**
  3. *
  4. * @author: xaboy<365615158@qq.com>
  5. * @day: 2017/12/18
  6. */
  7. namespace app\models\store;
  8. use app\admin\model\ump\StoreSeckillTime;
  9. use crmeb\basic\BaseModel;
  10. use crmeb\services\GroupDataService;
  11. use app\admin\model\store\StoreProductAttrValue;
  12. use app\models\store\StoreProduct;
  13. /**
  14. * TODO 秒杀产品Model
  15. * Class StoreSeckill
  16. * @package app\models\store
  17. */
  18. class StoreSeckill extends BaseModel
  19. {
  20. /**
  21. * 数据表主键
  22. * @var string
  23. */
  24. protected $pk = 'id';
  25. /**
  26. * 模型名称
  27. * @var string
  28. */
  29. protected $name = 'store_seckill';
  30. protected function getImagesAttr($value)
  31. {
  32. return json_decode($value, true) ?: [];
  33. }
  34. public function getDescriptionAttr($value)
  35. {
  36. return htmlspecialchars_decode($value);
  37. }
  38. public static function getSeckillCount()
  39. {
  40. $seckillTime = sys_data('routine_seckill_time') ?: [];//秒杀时间段
  41. $timeInfo = ['time' => 0, 'continued' => 0];
  42. foreach ($seckillTime as $key => $value) {
  43. $currentHour = date('H');
  44. $activityEndHour = bcadd((int)$value['time'], (int)$value['continued'], 0);
  45. if ($currentHour >= (int)$value['time'] && $currentHour < $activityEndHour && $activityEndHour < 24) {
  46. $timeInfo = $value;
  47. break;
  48. }
  49. }
  50. if ($timeInfo['time'] == 0) return 0;
  51. $activityEndHour = bcadd((int)$timeInfo['time'], (int)$timeInfo['continued'], 0);
  52. $startTime = bcadd(strtotime(date('Y-m-d')), bcmul($timeInfo['time'], 3600, 0));
  53. $stopTime = bcadd(strtotime(date('Y-m-d')), bcmul($activityEndHour, 3600, 0));
  54. return self::where('is_del', 0)->where('status', 1)->where('start_time', '<=', $startTime)->where('stop_time', '>=', $stopTime)->count();
  55. }
  56. /**
  57. * 获取秒杀列表
  58. * @param $time
  59. * @param int $page
  60. * @param int $limit
  61. * @return array
  62. * @throws \think\db\exception\DataNotFoundException
  63. * @throws \think\db\exception\DbException
  64. * @throws \think\db\exception\ModelNotFoundException
  65. */
  66. public static function seckillList($time, $page = 0, $limit = 20, $type = 1)
  67. {
  68. $today = strtotime(date('Y-m-d'));
  69. $tomorrow = strtotime(date('Y-m-d', strtotime('+1 day')));
  70. $tomorrow_1 = strtotime(date('Y-m-d', strtotime('+2 day'))); //后天
  71. $tomorrow_2 = strtotime(date('Y-m-d', strtotime('+3 day'))); //大后天
  72. $yesterday = strtotime(date('Y-m-d', strtotime('-1 day')));
  73. if ($type == 0) {
  74. $startTime = $yesterday;
  75. } else if ($type == 1) {
  76. $startTime = $today;
  77. } else if ($type == 2) {
  78. $startTime = $tomorrow;
  79. } else if ($type == 3) {
  80. $startTime = $tomorrow_1;
  81. } else if ($type == 4) {
  82. $startTime = $tomorrow_2;
  83. }
  84. if ($page) {
  85. $list = StoreSeckill::where('is_del', 0)->where('status', 1)->where('start_time', '<=', $startTime)->where('stop_time', '>=', $startTime)->where('time_id', $time)->order('sort desc')->page($page, $limit)->select()->toArray();
  86. } else {
  87. $list = StoreSeckill::where('is_del', 0)->where('status', 1)->where('start_time', '<=', $startTime)->where('stop_time', '>=', $startTime)->where('time_id', $time)->order('sort desc')->select()->toArray();
  88. }
  89. // if ($list_today) $list_today->hidden(['cost', 'add_time', 'is_del'])->toArray();
  90. // if ($list_yesterday) $list_yesterday->hidden(['cost', 'add_time', 'is_del'])->toArray();
  91. // if ($list_tomorrow) $list_tomorrow->hidden(['cost', 'add_time', 'is_del'])->toArray();\
  92. return $list;
  93. }
  94. /**
  95. * 获取所有秒杀产品
  96. * @param string $field
  97. * @return array
  98. */
  99. public static function getListAll($offset = 0, $limit = 10, $field = 'id,product_id,image,title,price,ot_price,start_time,stop_time,stock,sales')
  100. {
  101. $time = time();
  102. $model = self::where('is_del', 0)->where('status', 1)->where('stock', '>', 0)->field($field)
  103. ->where('start_time', '<', $time)->where('stop_time', '>', $time)->order('sort DESC,add_time DESC');
  104. $model = $model->limit($offset, $limit);
  105. $list = $model->select();
  106. if ($list) return $list->toArray();
  107. else return [];
  108. }
  109. /**
  110. * 获取热门推荐的秒杀产品
  111. * @param int $limit
  112. * @param string $field
  113. * @return array
  114. */
  115. public static function getHotList($limit = 0, $field = 'id,product_id,image,title,price,ot_price,start_time,stop_time,stock')
  116. {
  117. $time = time();
  118. $model = self::where('is_hot', 1)->where('is_del', 0)->where('status', 1)->where('stock', '>', 0)->field($field)
  119. ->where('start_time', '<', $time)->where('stop_time', '>', $time)->order('sort DESC,add_time DESC');
  120. if ($limit) $model->limit($limit);
  121. $list = $model->select();
  122. if ($list) return $list->toArray();
  123. else return [];
  124. }
  125. /**
  126. * 获取一条秒杀产品
  127. * @param $id
  128. * @param string $field
  129. * @return array|false|\PDOStatement|string|\think\Model
  130. */
  131. public static function getValidProduct($id, $field = '*')
  132. {
  133. $time = time();
  134. // $info = self::alias('n')->join('store_product c', 'c.id=n.product_id')->where('n.id', $id)->where('n.is_del', 0)->where('n.status', 1)->where('n.start_time', '<', $time)->where('n.stop_time', '>', $time)->field('n.*,SUM(c.sales+c.ficti) as total')->find();
  135. $info = self::alias('n')->join('store_product c', 'c.id=n.product_id')->where('n.id', $id)->where('n.is_del', 0)->where('n.status', 1)->field('n.*,SUM(c.sales+c.ficti) as total')->find();
  136. return $info;
  137. }
  138. /**
  139. * 获取秒杀是否有开启
  140. * @return bool
  141. */
  142. public static function getSeckillContStatus()
  143. {
  144. $time = time();
  145. $count = self::where('is_del', 0)->where('status', 1)->where('start_time', '<', $time)->where('stop_time', '>', $time)->count();
  146. return $count ? true : false;
  147. }
  148. public static function initFailSeckill()
  149. {
  150. self::where('is_hot', 1)->where('is_del', 0)->where('status', '<>', 1)->where('stop_time', '<', time())->update(['status' => '-1']);
  151. }
  152. public static function idBySimilaritySeckill($id, $limit = 4, $field = '*')
  153. {
  154. $time = time();
  155. $list = [];
  156. $productId = self::where('id', $id)->value('product_id');
  157. if ($productId) {
  158. $list = array_merge($list, self::where('product_id', $productId)->where('id', '<>', $id)
  159. ->where('is_del', 0)->where('status', 1)->where('stock', '>', 0)
  160. ->field($field)->where('start_time', '<', $time)->where('stop_time', '>', $time)
  161. ->order('sort DESC,add_time DESC')->limit($limit)->select()->toArray());
  162. }
  163. $limit = $limit - count($list);
  164. if ($limit) {
  165. $list = array_merge($list, self::getHotList($limit, $field));
  166. }
  167. return $list;
  168. }
  169. /** 获取秒杀产品库存
  170. * @param $id
  171. * @return mixed
  172. */
  173. public static function getProductStock($id)
  174. {
  175. return self::where('id', $id)->value('stock');
  176. }
  177. /**
  178. * 获取字段值
  179. * @param $id
  180. * @param string $field
  181. * @return mixed
  182. */
  183. public static function getProductField($id, $field = 'title')
  184. {
  185. return self::where('id', $id)->value($field);
  186. }
  187. /**
  188. * 修改秒杀库存
  189. * @param int $num
  190. * @param int $seckillId
  191. * @return bool
  192. */
  193. public static function decSeckillStock($num = 0, $seckillId = 0, $unique = '')
  194. {
  195. $product_id = self::where('id', $seckillId)->value('product_id');
  196. if ($unique) {
  197. $res = false !== StoreProductAttrValue::decProductAttrStock($seckillId, $unique, $num, 1);
  198. $res = $res && self::where('id', $seckillId)->dec('stock', $num)->inc('sales', $num)->update();
  199. $sku = StoreProductAttrValue::where('product_id', $seckillId)->where('unique', $unique)->where('type', 1)->value('suk');
  200. $res = $res && StoreProductAttrValue::where('product_id', $product_id)->where('suk', $sku)->where('type', 0)->dec('stock', $num)->inc('sales', $num)->update();
  201. } else {
  202. $res = false !== self::where('id', $seckillId)->dec('stock', $num)->inc('sales', $num)->update();
  203. }
  204. $res = $res && StoreProduct::where('id', $product_id)->dec('stock', $num)->inc('sales', $num)->update();
  205. return $res;
  206. }
  207. /**
  208. * 增加库存较少销量
  209. * @param int $num
  210. * @param int $seckillId
  211. * @return bool
  212. */
  213. public static function incSeckillStock($num = 0, $seckillId = 0)
  214. {
  215. $seckill = self::where('id', $seckillId)->field(['stock', 'sales'])->find();
  216. if (!$seckill) return true;
  217. if ($seckill->sales > 0) $seckill->sales = bcsub($seckill->sales, $num, 0);
  218. if ($seckill->sales < 0) $seckill->sales = 0;
  219. $seckill->stock = bcadd($seckill->stock, $num, 0);
  220. return $seckill->save();
  221. }
  222. }