StoreSeckill.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  1. <?php
  2. /**
  3. *
  4. * @author: xaboy<365615158@qq.com>
  5. * @day: 2017/12/18
  6. */
  7. namespace app\models\store;
  8. use app\models\system\SystemGroupDataMerchant;
  9. use crmeb\basic\BaseModel;
  10. use crmeb\services\GroupDataService;
  11. use crmeb\traits\ModelTrait;
  12. use think\db\exception\DataNotFoundException;
  13. use think\db\exception\DbException;
  14. use think\db\exception\ModelNotFoundException;
  15. /**
  16. * TODO 秒杀商品Model
  17. * Class StoreSeckill
  18. * @package app\models\store
  19. */
  20. class StoreSeckill extends BaseModel
  21. {
  22. /**
  23. * 数据表主键
  24. * @var string
  25. */
  26. protected $pk = 'id';
  27. /**
  28. * 模型名称
  29. * @var string
  30. */
  31. protected $name = 'store_seckill';
  32. use ModelTrait;
  33. public static function merSet($mer_id, $alias = '')
  34. {
  35. return $mer_id ? self::where($alias ? $alias . '.mer_id' : 'mer_id', $mer_id) : new self;
  36. }
  37. protected function getAddTimeAttr($value)
  38. {
  39. if ($value) return date('Y-m-d H:i:s', $value);
  40. return '';
  41. }
  42. protected function getImagesAttr($value)
  43. {
  44. return json_decode($value, true) ?: [];
  45. }
  46. public static function getSeckillCount($mer_id = false)
  47. {
  48. $seckillTime = sys_data('routine_seckill_time', 0, $mer_id) ?: [];//秒杀时间段
  49. $timeInfo = ['time' => 0, 'continued' => 0];
  50. foreach ($seckillTime as $key => $value) {
  51. $currentHour = date('H');
  52. $activityEndHour = bcadd((int)$value['time'], (int)$value['continued'], 0);
  53. if ($currentHour >= (int)$value['time'] && $currentHour < $activityEndHour && $activityEndHour < 24) {
  54. $timeInfo = $value;
  55. break;
  56. }
  57. }
  58. if ($timeInfo['time'] == 0) return 0;
  59. $activityEndHour = bcadd((int)$timeInfo['time'], (int)$timeInfo['continued'], 0);
  60. $startTime = bcadd(strtotime(date('Y-m-d')), bcmul($timeInfo['time'], 3600, 0));
  61. $stopTime = bcadd(strtotime(date('Y-m-d')), bcmul($activityEndHour, 3600, 0));
  62. return self::merSet($mer_id)->where('is_del', 0)->where('status', 1)->where('start_time', '<=', $startTime)->where('stop_time', '>=', $stopTime)->count();
  63. }
  64. /*
  65. * 获取秒杀列表
  66. *
  67. * */
  68. public static function seckillList($time, $page = 0, $limit = 20, $mer_id = false)
  69. {
  70. if ($page) $list = StoreSeckill::merSet($mer_id, 'n')->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();
  71. else $list = StoreSeckill::merSet($mer_id, 'n')->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();
  72. if ($list) return $list->hidden(['cost', 'add_time', 'is_del'])->toArray();
  73. return [];
  74. }
  75. /**
  76. * 获取所有秒杀商品
  77. * @param int $offset
  78. * @param int $limit
  79. * @param string $field
  80. * @return array
  81. * @throws DataNotFoundException
  82. * @throws DbException
  83. * @throws ModelNotFoundException
  84. */
  85. public static function getListAll($offset = 0, $limit = 10, $field = 'id,product_id,image,title,price,ot_price,start_time,stop_time,stock,sales')
  86. {
  87. $time = time();
  88. $model = self::where('is_del', 0)->where('status', 1)->where('stock', '>', 0)->field($field)
  89. ->where('start_time', '<', $time)->where('stop_time', '>', $time)->order('sort DESC,add_time DESC');
  90. $model = $model->limit($offset, $limit);
  91. $list = $model->select();
  92. if ($list) return $list->toArray();
  93. else return [];
  94. }
  95. /**
  96. * 获取热门推荐的秒杀商品
  97. * @param int $limit
  98. * @param string $field
  99. * @return array
  100. */
  101. public static function getHotList($limit = 0, $field = 'id,product_id,image,title,price,ot_price,start_time,stop_time,stock')
  102. {
  103. $time = time();
  104. $model = self::where('is_hot', 1)->where('is_del', 0)->where('status', 1)->where('stock', '>', 0)->field($field)
  105. ->where('start_time', '<', $time)->where('stop_time', '>', $time)->order('sort DESC,add_time DESC');
  106. if ($limit) $model->limit($limit);
  107. $list = $model->select();
  108. if ($list) return $list->toArray();
  109. else return [];
  110. }
  111. /**
  112. * 获取一条秒杀商品
  113. * @param $id
  114. * @param string $field
  115. * @return array|false|\PDOStatement|string|\think\Model
  116. */
  117. public static function getValidProduct($id, $field = '*', $mer_id = false)
  118. {
  119. $time = time();
  120. $info = self::merSet($mer_id,'n')->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();
  121. if ($info['id']) {
  122. return $info;
  123. } else {
  124. return [];
  125. }
  126. }
  127. public static function initFailSeckill()
  128. {
  129. self::where('is_hot', 1)->where('is_del', 0)->where('status', '<>', 1)->where('stop_time', '<', time())->update(['status' => '-1']);
  130. }
  131. public static function idBySimilaritySeckill($id, $limit = 4, $field = '*')
  132. {
  133. $time = time();
  134. $list = [];
  135. $productId = self::where('id', $id)->value('product_id');
  136. if ($productId) {
  137. $list = array_merge($list, self::where('product_id', $productId)->where('id', '<>', $id)
  138. ->where('is_del', 0)->where('status', 1)->where('stock', '>', 0)
  139. ->field($field)->where('start_time', '<', $time)->where('stop_time', '>', $time)
  140. ->order('sort DESC,add_time DESC')->limit($limit)->select()->toArray());
  141. }
  142. $limit = $limit - count($list);
  143. if ($limit) {
  144. $list = array_merge($list, self::getHotList($limit, $field));
  145. }
  146. return $list;
  147. }
  148. /** 获取秒杀商品库存
  149. * @param $id
  150. * @return mixed
  151. */
  152. public static function getProductStock($id)
  153. {
  154. return self::where('id', $id)->value('stock');
  155. }
  156. /**
  157. * 获取字段值
  158. * @param $id
  159. * @param string $field
  160. * @return mixed
  161. */
  162. public static function getProductField($id, $field = 'title')
  163. {
  164. return self::where('id', $id)->value($field);
  165. }
  166. /**
  167. * 修改秒杀库存
  168. * @param int $num
  169. * @param int $seckillId
  170. * @return bool
  171. */
  172. public static function decSeckillStock($num = 0, $seckillId = 0, $unique = '')
  173. {
  174. $product_id = self::where('id', $seckillId)->value('product_id');
  175. if ($unique) {
  176. $res = false !== StoreProductAttrValue::decProductAttrStock($seckillId, $unique, $num, 1);
  177. $res = $res && self::where('id', $seckillId)->dec('stock', $num)->inc('sales', $num)->update();
  178. $sku = StoreProductAttrValue::where('product_id', $seckillId)->where('unique', $unique)->where('type', 1)->value('suk');
  179. $res = $res && StoreProductAttrValue::where('product_id', $product_id)->where('suk', $sku)->where('type', 0)->dec('stock', $num)->inc('sales', $num)->update();
  180. } else {
  181. $res = false !== self::where('id', $seckillId)->dec('stock', $num)->inc('sales', $num)->update();
  182. }
  183. $res = $res && StoreProduct::where('id', $product_id)->dec('stock', $num)->inc('sales', $num)->update();
  184. return $res;
  185. }
  186. /**
  187. * 增加库存较少销量
  188. * @param int $num
  189. * @param int $seckillId
  190. * @return bool
  191. */
  192. public static function incSeckillStock($num = 0, $seckillId = 0)
  193. {
  194. $seckill = self::where('id', $seckillId)->field(['stock', 'sales'])->find();
  195. if (!$seckill) return true;
  196. if ($seckill->sales > 0) $seckill->sales = bcsub($seckill->sales, $num, 0);
  197. if ($seckill->sales < 0) $seckill->sales = 0;
  198. $seckill->stock = bcadd($seckill->stock, $num, 0);
  199. return $seckill->save();
  200. }
  201. /**
  202. * @param $where
  203. * @return array
  204. */
  205. public static function systemPage($where)
  206. {
  207. $model = new self;
  208. $model = $model->alias('s');
  209. if ($where['status'] != '') $model = $model->where('s.status', $where['status']);
  210. if ($where['store_name'] != '') $model = $model->where('s.title|s.id', 'LIKE', "%$where[store_name]%");
  211. if ($where['mer_id'] != '') $model = $model->where('mer_id', $where['mer_id']);
  212. $model = $model->page(bcmul($where['page'], $where['limit'], 0), $where['limit']);
  213. $model = $model->order('s.id desc');
  214. $model = $model->where('s.is_del', 0);
  215. return self::page($model, function ($item) {
  216. $item['store_name'] = StoreProduct::where('id', $item['product_id'])->value('store_name');
  217. if ($item['status']) {
  218. if ($item['start_time'] > time())
  219. $item['start_name'] = '活动未开始';
  220. else if (bcadd($item['stop_time'], 86400) < time())
  221. $item['start_name'] = '活动已结束';
  222. else if (bcadd($item['stop_time'], 86400) > time() && $item['start_time'] < time()) {
  223. $config = SystemGroupDataMerchant::get($item['time_id']);
  224. if ($config) {
  225. $arr = json_decode($config->value, true);
  226. $now_hour = date('H', time());
  227. $start_hour = $arr['time']['value'];
  228. $continued = $arr['continued']['value'];
  229. $end_hour = $start_hour + $continued;
  230. if ($start_hour > $now_hour) {
  231. $item['start_name'] = '活动未开始';
  232. } elseif ($end_hour < $now_hour) {
  233. $item['start_name'] = '活动已结束';
  234. } else {
  235. $item['start_name'] = '正在进行中';
  236. }
  237. } else {
  238. $item['start_name'] = '正在进行中';
  239. }
  240. }
  241. } else $item['start_name'] = '关闭';
  242. }, $where, $where['limit']);
  243. }
  244. /**
  245. * 秒杀数据
  246. * @param $where
  247. */
  248. public static function exportData($where)
  249. {
  250. $model = new self;
  251. if ($where['status'] != '') $model = $model->where('status', $where['status']);
  252. if ($where['store_name'] != '') $model = $model->where('title|id', 'LIKE', "%$where[store_name]%");
  253. if ($where['mer_id'] != '') $model = $model->where('mer_id', $where['mer_id']);
  254. $list = $model->order('id desc')->where('is_del', 0)->select();
  255. count($list) && $list = $list->toArray();
  256. foreach ($list as &$item) {
  257. $item['store_name'] = StoreProduct::where('id', $item['product_id'])->value('store_name');
  258. }
  259. return $list;
  260. }
  261. /**
  262. * 详情
  263. */
  264. public static function getOne($id)
  265. {
  266. $info = self::get($id);
  267. if ($info) {
  268. if ($info['start_time'])
  269. $start_time = date('Y-m-d', $info['start_time']);
  270. if ($info['stop_time'])
  271. $stop_time = date('Y-m-d', $info['stop_time']);
  272. if (isset($start_time) && isset($stop_time))
  273. $info['section_time'] = [$start_time, $stop_time];
  274. else
  275. $info['section_time'] = [];
  276. unset($info['start_time'], $info['stop_time']);
  277. }
  278. $info['give_integral'] = intval($info['give_integral']);
  279. $info['price'] = floatval($info['price']);
  280. $info['ot_price'] = floatval($info['ot_price']);
  281. $info['postage'] = floatval($info['postage']);
  282. $info['cost'] = floatval($info['cost']);
  283. $info['weight'] = floatval($info['weight']);
  284. $info['volume'] = floatval($info['volume']);
  285. $info['description'] = StoreDescription::getDescription($id, 1);
  286. $info['attrs'] = self::attr_list($id);
  287. return compact('info');
  288. }
  289. public static function attr_list($id)
  290. {
  291. $productId = self::where('id', $id)->value('product_id');
  292. $seckillResult = StoreProductAttrResult::where('product_id', $id)->where('type', 1)->value('result');
  293. $items = json_decode($seckillResult, true)['attr'];
  294. $productAttr = self::get_attr($items, $productId, 0);
  295. $seckillAttr = self::get_attr($items, $id, 1);
  296. foreach ($productAttr as $pk => $pv) {
  297. foreach ($seckillAttr as &$sv) {
  298. if ($pv['detail'] == $sv['detail']) {
  299. $productAttr[$pk] = $sv;
  300. }
  301. }
  302. $productAttr[$pk]['detail'] = json_decode($productAttr[$pk]['detail']);
  303. }
  304. $attrs['items'] = $items;
  305. $attrs['value'] = $productAttr;
  306. foreach ($items as $key => $item) {
  307. $header[] = ['title' => $item['value'], 'key' => 'value' . ($key + 1), 'align' => 'center', 'minWidth' => 80];
  308. }
  309. $header[] = ['title' => '图片', 'slot' => 'pic', 'align' => 'center', 'minWidth' => 120];
  310. $header[] = ['title' => '秒杀价', 'key' => 'price', 'type' => 1, 'align' => 'center', 'minWidth' => 80];
  311. $header[] = ['title' => '成本价', 'key' => 'cost', 'align' => 'center', 'minWidth' => 80];
  312. $header[] = ['title' => '原价', 'key' => 'ot_price', 'align' => 'center', 'minWidth' => 80];
  313. $header[] = ['title' => '库存', 'key' => 'stock', 'align' => 'center', 'minWidth' => 80];
  314. $header[] = ['title' => '限量', 'key' => 'quota', 'type' => 1, 'align' => 'center', 'minWidth' => 80];
  315. $header[] = ['title' => '重量(KG)', 'key' => 'weight', 'align' => 'center', 'minWidth' => 80];
  316. $header[] = ['title' => '体积(m³)', 'key' => 'volume', 'align' => 'center', 'minWidth' => 80];
  317. $header[] = ['title' => '商品编号', 'key' => 'bar_code', 'align' => 'center', 'minWidth' => 80];
  318. $attrs['header'] = $header;
  319. return $attrs;
  320. }
  321. public static function get_attr($attr, $id, $type)
  322. {
  323. $value = attr_format($attr)[1];
  324. $valueNew = [];
  325. $count = 0;
  326. foreach ($value as $key => $item) {
  327. $detail = $item['detail'];
  328. sort($item['detail'], SORT_STRING);
  329. $suk = implode(',', $item['detail']);
  330. $sukValue = StoreProductAttrValue::where('product_id', $id)->where('type', $type)->where('suk', $suk)->column('bar_code,cost,price,ot_price,stock,image as pic,weight,volume,brokerage,brokerage_two,quota', 'suk');
  331. if (count($sukValue)) {
  332. foreach (array_values($detail) as $k => $v) {
  333. $valueNew[$count]['value' . ($k + 1)] = $v;
  334. }
  335. $valueNew[$count]['detail'] = json_encode($detail);
  336. $valueNew[$count]['pic'] = $sukValue[$suk]['pic'] ?? '';
  337. $valueNew[$count]['price'] = $sukValue[$suk]['price'] ? floatval($sukValue[$suk]['price']) : 0;
  338. $valueNew[$count]['cost'] = $sukValue[$suk]['cost'] ? floatval($sukValue[$suk]['cost']) : 0;
  339. $valueNew[$count]['ot_price'] = isset($sukValue[$suk]['ot_price']) ? floatval($sukValue[$suk]['ot_price']) : 0;
  340. $valueNew[$count]['stock'] = $sukValue[$suk]['stock'] ? intval($sukValue[$suk]['stock']) : 0;
  341. $valueNew[$count]['quota'] = $sukValue[$suk]['quota'] ? intval($sukValue[$suk]['quota']) : 0;
  342. $valueNew[$count]['bar_code'] = $sukValue[$suk]['bar_code'] ?? '';
  343. $valueNew[$count]['weight'] = $sukValue[$suk]['weight'] ? floatval($sukValue[$suk]['weight']) : 0;
  344. $valueNew[$count]['volume'] = $sukValue[$suk]['volume'] ? floatval($sukValue[$suk]['volume']) : 0;
  345. $valueNew[$count]['brokerage'] = $sukValue[$suk]['brokerage'] ? floatval($sukValue[$suk]['brokerage']) : 0;
  346. $valueNew[$count]['brokerage_two'] = $sukValue[$suk]['brokerage_two'] ? floatval($sukValue[$suk]['brokerage_two']) : 0;
  347. $valueNew[$count]['_checked'] = $type != 0 ? true : false;
  348. $count++;
  349. }
  350. }
  351. return $valueNew;
  352. }
  353. /**
  354. * 获取秒杀是否已结束
  355. * @param $seckill_id
  356. * @return bool
  357. */
  358. public static function isSeckillEnd($seckill_id, $mer_id = '')
  359. {
  360. $time_id = self::where('id', $seckill_id)->value('time_id');
  361. //秒杀时间段
  362. $seckillTime = sys_data('routine_seckill_time', 0, $mer_id) ?? [];
  363. $seckillTimeIndex = 0;
  364. $activityTime = [];
  365. if (count($seckillTime)) {
  366. foreach ($seckillTime as $key => &$value) {
  367. $currentHour = date('H');
  368. $activityEndHour = bcadd((int)$value['time'], (int)$value['continued'], 0);
  369. if ($activityEndHour > 24) {
  370. $value['time'] = strlen((int)$value['time']) == 2 ? (int)$value['time'] . ':00' : '0' . (int)$value['time'] . ':00';
  371. $value['state'] = '即将开始';
  372. $value['status'] = 2;
  373. $value['stop'] = (int)bcadd(strtotime(date('Y-m-d')), bcmul($activityEndHour, 3600, 0));
  374. } else {
  375. if ($currentHour >= (int)$value['time'] && $currentHour < $activityEndHour) {
  376. $value['time'] = strlen((int)$value['time']) == 2 ? (int)$value['time'] . ':00' : '0' . (int)$value['time'] . ':00';
  377. $value['state'] = '抢购中';
  378. $value['stop'] = (int)bcadd(strtotime(date('Y-m-d')), bcmul($activityEndHour, 3600, 0));
  379. $value['status'] = 1;
  380. if (!$seckillTimeIndex) $seckillTimeIndex = $key;
  381. } else if ($currentHour < (int)$value['time']) {
  382. $value['time'] = strlen((int)$value['time']) == 2 ? (int)$value['time'] . ':00' : '0' . (int)$value['time'] . ':00';
  383. $value['state'] = '即将开始';
  384. $value['status'] = 2;
  385. $value['stop'] = (int)bcadd(strtotime(date('Y-m-d')), bcmul($activityEndHour, 3600, 0));
  386. } else if ($currentHour >= $activityEndHour) {
  387. $value['time'] = strlen((int)$value['time']) == 2 ? (int)$value['time'] . ':00' : '0' . (int)$value['time'] . ':00';
  388. $value['state'] = '已结束';
  389. $value['status'] = 0;
  390. $value['stop'] = (int)bcadd(strtotime(date('Y-m-d')), bcmul($activityEndHour, 3600, 0));
  391. }
  392. }
  393. if ($value['id'] == $time_id) {
  394. $activityTime = $value;
  395. break;
  396. }
  397. }
  398. }
  399. if (time() < $activityTime['stop'])
  400. return true;
  401. else
  402. return false;
  403. }
  404. }