StoreSeckill.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  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\store\StoreProductAttrValue as StoreProductAttrValueModel;
  9. use crmeb\basic\BaseModel;
  10. use app\admin\model\store\StoreProductAttrValue;
  11. /**
  12. * TODO 秒杀产品Model
  13. * Class StoreSeckill
  14. * @package app\models\store
  15. */
  16. class StoreSeckill extends BaseModel
  17. {
  18. /**
  19. * 数据表主键
  20. * @var string
  21. */
  22. protected $pk = 'id';
  23. /**
  24. * 模型名称
  25. * @var string
  26. */
  27. protected $name = 'store_seckill';
  28. protected function getImagesAttr($value)
  29. {
  30. return json_decode($value, true) ?: [];
  31. }
  32. public function getDescriptionAttr($value)
  33. {
  34. return htmlspecialchars_decode($value);
  35. }
  36. public static function getSeckillCount()
  37. {
  38. $seckillTime = sys_data('routine_seckill_time') ?: [];//秒杀时间段
  39. $timeInfo = ['time' => 0, 'continued' => 0];
  40. foreach ($seckillTime as $key => $value) {
  41. $currentHour = date('H');
  42. $activityEndHour = bcadd((int)$value['time'], (int)$value['continued'], 0);
  43. if ($currentHour >= (int)$value['time'] && $currentHour < $activityEndHour && $activityEndHour < 24) {
  44. $timeInfo = $value;
  45. break;
  46. }
  47. }
  48. if ($timeInfo['time'] == 0) return 0;
  49. $activityEndHour = bcadd((int)$timeInfo['time'], (int)$timeInfo['continued'], 0);
  50. $startTime = bcadd(strtotime(date('Y-m-d')), bcmul($timeInfo['time'], 3600, 0));
  51. $stopTime = bcadd(strtotime(date('Y-m-d')), bcmul($activityEndHour, 3600, 0));
  52. return self::where('is_del', 0)->where('status', 1)->where('start_time', '<=', $startTime)->where('stop_time', '>=', $stopTime)->count();
  53. }
  54. /**
  55. * 获取秒杀列表
  56. * @param $time
  57. * @param int $page
  58. * @param int $limit
  59. * @return array
  60. * @throws \think\db\exception\DataNotFoundException
  61. * @throws \think\db\exception\DbException
  62. * @throws \think\db\exception\ModelNotFoundException
  63. */
  64. public static function seckillList($time, $page = 0, $limit = 20)
  65. {
  66. if ($page) $list = StoreSeckill::alias('n')->join('store_product c', 'c.id=n.product_id')->where('c.is_show', 1)->where('c.is_del', 0)->where('n.is_del', 0)->where('n.status', 1)->where('n.start_time', '<=', time())->where('n.stop_time', '>=', time() - 86400)->where('n.time_id', $time)->field('n.*')->order('n.sort desc')->page($page, $limit)->select();
  67. else $list = StoreSeckill::alias('n')->join('store_product c', 'c.id=n.product_id')->where('c.is_show', 1)->where('c.is_del', 0)->where('n.is_del', 0)->where('n.status', 1)->where('n.start_time', '<=', time())->where('n.stop_time', '>=', time() - 86400)->where('n.time_id', $time)->field('n.*')->order('sort desc')->select();
  68. if ($list) return $list->hidden(['cost', 'add_time', 'is_del'])->toArray();
  69. return [];
  70. }
  71. /**
  72. * 获取所有秒杀产品
  73. * @param string $field
  74. * @return array
  75. */
  76. public static function getListAll($offset = 0, $limit = 10, $field = 'id,product_id,image,title,price,ot_price,start_time,stop_time,stock,sales')
  77. {
  78. $time = time();
  79. $model = self::where('is_del', 0)->where('status', 1)->where('stock', '>', 0)->field($field)
  80. ->where('start_time', '<', $time)->where('stop_time', '>', $time)->order('sort DESC,add_time DESC');
  81. $model = $model->limit($offset, $limit);
  82. $list = $model->select();
  83. if ($list) return $list->toArray();
  84. else return [];
  85. }
  86. /**
  87. * 获取热门推荐的秒杀产品
  88. * @param int $limit
  89. * @param string $field
  90. * @return array
  91. */
  92. public static function getHotList($limit = 0, $field = 'id,product_id,image,title,price,ot_price,start_time,stop_time,stock')
  93. {
  94. $time = time();
  95. $model = self::where('is_hot', 1)->where('is_del', 0)->where('status', 1)->where('stock', '>', 0)->field($field)
  96. ->where('start_time', '<', $time)->where('stop_time', '>', $time)->order('sort DESC,add_time DESC');
  97. if ($limit) $model->limit($limit);
  98. $list = $model->select();
  99. if ($list) return $list->toArray();
  100. else return [];
  101. }
  102. /**
  103. * 获取一条秒杀产品
  104. * @param $id
  105. * @param string $field
  106. * @return array|false|\PDOStatement|string|\think\Model
  107. */
  108. public static function getValidProduct($id, $field = '*')
  109. {
  110. $time = time();
  111. $info = self::alias('n')->join('store_product c', 'c.id=n.product_id')->where('n.id', $id)->where('c.is_show', 1)->where('c.is_del', 0)->where('n.is_del', 0)->where('n.status', 1)->where('n.start_time', '<', $time)->where('n.stop_time', '>', $time - 86400)->field('n.*,SUM(c.sales+c.ficti) as total')->find();
  112. if ($info['id']) {
  113. return $info;
  114. } else {
  115. return [];
  116. }
  117. }
  118. /**
  119. * 获取秒杀是否有开启
  120. * @return bool
  121. */
  122. public static function getSeckillContStatus()
  123. {
  124. $time = time();
  125. $count = self::where('is_del', 0)->where('status', 1)->where('start_time', '<', $time)->where('stop_time', '>', $time)->count();
  126. return $count ? true : false;
  127. }
  128. public static function initFailSeckill()
  129. {
  130. self::where('is_hot', 1)->where('is_del', 0)->where('status', '<>', 1)->where('stop_time', '<', time())->update(['status' => '-1']);
  131. }
  132. public static function idBySimilaritySeckill($id, $limit = 4, $field = '*')
  133. {
  134. $time = time();
  135. $list = [];
  136. $productId = self::where('id', $id)->value('product_id');
  137. if ($productId) {
  138. $list = array_merge($list, self::where('product_id', $productId)->where('id', '<>', $id)
  139. ->where('is_del', 0)->where('status', 1)->where('stock', '>', 0)
  140. ->field($field)->where('start_time', '<', $time)->where('stop_time', '>', $time)
  141. ->order('sort DESC,add_time DESC')->limit($limit)->select()->toArray());
  142. }
  143. $limit = $limit - count($list);
  144. if ($limit) {
  145. $list = array_merge($list, self::getHotList($limit, $field));
  146. }
  147. return $list;
  148. }
  149. /** 获取秒杀产品库存
  150. * @param $id
  151. * @return mixed
  152. */
  153. public static function getProductStock($id)
  154. {
  155. return self::where('id', $id)->value('stock');
  156. }
  157. /**
  158. * 获取字段值
  159. * @param $id
  160. * @param string $field
  161. * @return mixed
  162. */
  163. public static function getProductField($id, $field = 'title')
  164. {
  165. return self::where('id', $id)->value($field);
  166. }
  167. /**
  168. * 修改秒杀库存
  169. * @param int $num
  170. * @param int $seckillId
  171. * @return bool
  172. */
  173. public static function decSeckillStock($num = 0, $seckillId = 0, $unique = '')
  174. {
  175. $product_id = self::where('id', $seckillId)->value('product_id');
  176. if ($unique) {
  177. $res = false !== StoreProductAttrValue::decProductAttrStock($seckillId, $unique, $num, 1);
  178. $res = $res && self::where('id', $seckillId)->dec('stock', $num)->dec('quota', $num)->inc('sales', $num)->update();
  179. $sku = StoreProductAttrValue::where('product_id', $seckillId)->where('unique', $unique)->where('type', 1)->value('suk');
  180. $res = $res && StoreProductAttrValue::where('product_id', $product_id)->where('suk', $sku)->where('type', 0)->dec('stock', $num)->inc('sales', $num)->update();
  181. } else {
  182. $res = false !== self::where('id', $seckillId)->dec('stock', $num)->inc('sales', $num)->update();
  183. }
  184. $res = $res && StoreProduct::where('id', $product_id)->dec('stock', $num)->inc('sales', $num)->update();
  185. return $res;
  186. }
  187. /**
  188. * 增加库存较少销量
  189. * @param int $num
  190. * @param int $seckillId
  191. * @return bool
  192. */
  193. public static function incSeckillStock($num = 0, $seckillId = 0, $unique = '')
  194. {
  195. $seckill = self::where('id', $seckillId)->field(['product_id', 'stock', 'sales', 'quota'])->find();
  196. if (!$seckill) return true;
  197. if ($seckill->sales > 0) $seckill->sales = bcsub($seckill->sales, $num, 0);
  198. if ($seckill->sales < 0) $seckill->sales = 0;
  199. $res = true;
  200. if ($unique) {
  201. $res = false !== StoreProductAttrValueModel::incProductAttrStock($seckillId, $unique, $num, 1);
  202. $sku = StoreProductAttrValue::where('product_id', $seckillId)->where('unique', $unique)->where('type', 1)->value('suk');
  203. $res = $res && StoreProductAttrValue::where('product_id', $seckill['product_id'])->where('suk', $sku)->where('type', 0)->inc('stock', $num)->dec('sales', $num)->update();
  204. }
  205. $seckill->stock = bcadd($seckill->stock, $num, 0);
  206. $seckill->quota = bcadd($seckill->quota, $num, 0);
  207. $res = $res && $seckill->save() && StoreProduct::where('id', $seckill['product_id'])->inc('stock', $num)->dec('sales', $num)->update();
  208. return $res;
  209. }
  210. /**
  211. * 获取秒杀是否已结束
  212. * @param $seckill_id
  213. * @return bool
  214. */
  215. public static function isSeckillEnd($seckill_id)
  216. {
  217. $time_id = self::where('id', $seckill_id)->value('time_id');
  218. //秒杀时间段
  219. $seckillTime = sys_data('routine_seckill_time') ?? [];
  220. $seckillTimeIndex = 0;
  221. $activityTime = [];
  222. if (count($seckillTime)) {
  223. foreach ($seckillTime as $key => &$value) {
  224. $currentHour = date('H');
  225. $activityEndHour = bcadd((int)$value['time'], (int)$value['continued'], 0);
  226. if ($activityEndHour > 24) {
  227. $value['time'] = strlen((int)$value['time']) == 2 ? (int)$value['time'] . ':00' : '0' . (int)$value['time'] . ':00';
  228. $value['state'] = '即将开始';
  229. $value['status'] = 2;
  230. $value['stop'] = (int)bcadd(strtotime(date('Y-m-d')), bcmul($activityEndHour, 3600, 0));
  231. } else {
  232. if ($currentHour >= (int)$value['time'] && $currentHour < $activityEndHour) {
  233. $value['time'] = strlen((int)$value['time']) == 2 ? (int)$value['time'] . ':00' : '0' . (int)$value['time'] . ':00';
  234. $value['state'] = '抢购中';
  235. $value['stop'] = (int)bcadd(strtotime(date('Y-m-d')), bcmul($activityEndHour, 3600, 0));
  236. $value['status'] = 1;
  237. if (!$seckillTimeIndex) $seckillTimeIndex = $key;
  238. } else if ($currentHour < (int)$value['time']) {
  239. $value['time'] = strlen((int)$value['time']) == 2 ? (int)$value['time'] . ':00' : '0' . (int)$value['time'] . ':00';
  240. $value['state'] = '即将开始';
  241. $value['status'] = 2;
  242. $value['stop'] = (int)bcadd(strtotime(date('Y-m-d')), bcmul($activityEndHour, 3600, 0));
  243. } else if ($currentHour >= $activityEndHour) {
  244. $value['time'] = strlen((int)$value['time']) == 2 ? (int)$value['time'] . ':00' : '0' . (int)$value['time'] . ':00';
  245. $value['state'] = '已结束';
  246. $value['status'] = 0;
  247. $value['stop'] = (int)bcadd(strtotime(date('Y-m-d')), bcmul($activityEndHour, 3600, 0));
  248. }
  249. }
  250. if ($value['id'] == $time_id) {
  251. $activityTime = $value;
  252. break;
  253. }
  254. }
  255. }
  256. if (time() < $activityTime['stop'])
  257. return true;
  258. else
  259. return false;
  260. }
  261. /**
  262. * 检查秒杀活动状态
  263. * @param $seckill_id
  264. * @return bool
  265. */
  266. public static function checkStatus($seckill_id)
  267. {
  268. $time_id = self::where('id', $seckill_id)->value('time_id');
  269. //秒杀时间段
  270. $seckillTime = sys_data('routine_seckill_time') ?? [];
  271. $seckillTimeIndex = 0;
  272. $activityTime = [];
  273. if (count($seckillTime)) {
  274. foreach ($seckillTime as $key => &$value) {
  275. $currentHour = date('H');
  276. $activityEndHour = bcadd((int)$value['time'], (int)$value['continued'], 0);
  277. if ($activityEndHour > 24) {
  278. $value['time'] = strlen((int)$value['time']) == 2 ? (int)$value['time'] . ':00' : '0' . (int)$value['time'] . ':00';
  279. $value['state'] = '即将开始';
  280. $value['status'] = 2;
  281. $value['stop'] = (int)bcadd(strtotime(date('Y-m-d')), bcmul($activityEndHour, 3600, 0));
  282. } else {
  283. if ($currentHour >= (int)$value['time'] && $currentHour < $activityEndHour) {
  284. $value['time'] = strlen((int)$value['time']) == 2 ? (int)$value['time'] . ':00' : '0' . (int)$value['time'] . ':00';
  285. $value['state'] = '抢购中';
  286. $value['stop'] = (int)bcadd(strtotime(date('Y-m-d')), bcmul($activityEndHour, 3600, 0));
  287. $value['status'] = 1;
  288. if (!$seckillTimeIndex) $seckillTimeIndex = $key;
  289. } else if ($currentHour < (int)$value['time']) {
  290. $value['time'] = strlen((int)$value['time']) == 2 ? (int)$value['time'] . ':00' : '0' . (int)$value['time'] . ':00';
  291. $value['state'] = '即将开始';
  292. $value['status'] = 2;
  293. $value['stop'] = (int)bcadd(strtotime(date('Y-m-d')), bcmul($activityEndHour, 3600, 0));
  294. } else if ($currentHour >= $activityEndHour) {
  295. $value['time'] = strlen((int)$value['time']) == 2 ? (int)$value['time'] . ':00' : '0' . (int)$value['time'] . ':00';
  296. $value['state'] = '已结束';
  297. $value['status'] = 0;
  298. $value['stop'] = (int)bcadd(strtotime(date('Y-m-d')), bcmul($activityEndHour, 3600, 0));
  299. }
  300. }
  301. if ($value['id'] == $time_id) {
  302. $activityTime = $value;
  303. break;
  304. }
  305. }
  306. }
  307. return $activityTime;
  308. }
  309. }