截流自动化的商城平台
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

SeckillLogic.php 7.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | likeshop开源商城系统
  4. // +----------------------------------------------------------------------
  5. // | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
  6. // | gitee下载:https://gitee.com/likeshop_gitee
  7. // | github下载:https://github.com/likeshop-github
  8. // | 访问官网:https://www.likeshop.cn
  9. // | 访问社区:https://home.likeshop.cn
  10. // | 访问手册:http://doc.likeshop.cn
  11. // | 微信公众号:likeshop技术社区
  12. // | likeshop系列产品在gitee、github等公开渠道开源版本可免费商用,未经许可不能去除前后端官方版权标识
  13. // | likeshop系列产品收费版本务必购买商业授权,购买去版权授权后,方可去除前后端官方版权标识
  14. // | 禁止对系统程序代码以任何目的,任何形式的再发布
  15. // | likeshop团队版权所有并拥有最终解释权
  16. // +----------------------------------------------------------------------
  17. // | author: likeshop.cn.team
  18. // +----------------------------------------------------------------------
  19. namespace app\shop\logic;
  20. use app\common\server\UrlServer;
  21. use think\Db;
  22. class SeckillLogic{
  23. public static function goodsList($get){
  24. $where = [];
  25. $where[] = ['sg.del','=',0];
  26. $where[] = ['g.del','=',0];
  27. if(isset($get['name']) && $get['name']) {
  28. $where[] = ['name','like','%'.$get['name'].'%'];
  29. }
  30. if(isset($get['seckill_id']) && $get['seckill_id']){
  31. $where[] = ['seckill_id','=',$get['seckill_id']];
  32. }
  33. $count = Db::name('seckill_goods sg')
  34. ->join('goods g','sg.goods_id = g.id')
  35. ->where($where)
  36. ->group('goods_id,seckill_id')
  37. ->count();
  38. $list = Db::name('seckill_goods sg')
  39. ->join('goods g','sg.goods_id = g.id')
  40. ->where($where)
  41. ->group('goods_id,seckill_id')
  42. ->field('sg.id,seckill_id,goods_id,g.name,g.image,g.max_price,g.min_price,sg.sales_sum')
  43. ->page($get['page'], $get['limit'])
  44. ->order('sg.id desc')
  45. ->select();
  46. $goods_ids = array_column($list,'goods_id');
  47. $seckill_goods_list = Db::name('seckill_goods')->where(['goods_id'=>$goods_ids])->column('*','id');
  48. $seckill = Db::name('seckill_time')->where(['del'=>0])->column('*','id');
  49. foreach ($list as &$item){
  50. $price_array = [];
  51. $item['image'] = UrlServer::getFileUrl($item['image']);
  52. $item['goods_price'] = '¥'.$item['min_price'];
  53. if($item['max_price'] != $item['min_price']){
  54. $item['goods_price'] = '¥'.$item['min_price'].' ~ '.'¥'.$item['max_price'];
  55. }
  56. if(isset($seckill[$item['seckill_id']])){
  57. $item['start_time'] = $seckill[$item['seckill_id']]['start_time'];
  58. $item['end_time'] = $seckill[$item['seckill_id']]['end_time'];
  59. }
  60. //该秒杀商品的全部规格
  61. $seckill_goods_item = Db::name('seckill_goods')
  62. ->where(['seckill_id'=>$item['seckill_id'],'goods_id'=>$item['goods_id']])
  63. ->column('*','id');
  64. foreach ($seckill_goods_item as $seckill_id => $seckill_item){
  65. if(isset($seckill_goods_list[$seckill_id])){
  66. $price_array[] = $seckill_goods_list[$seckill_id]['price'];
  67. }
  68. }
  69. $max_price = max($price_array);
  70. $min_price = min($price_array);
  71. $item['seckill_price'] = '¥'.$max_price;
  72. if($max_price != $min_price){
  73. $item['seckill_price'] = '¥'.$min_price.' ~ '.'¥'.$max_price;
  74. }
  75. }
  76. return ['count' => $count, 'list' => $list];
  77. }
  78. public static function timeList($get){
  79. $where = [];
  80. $where[] = ['del','=',0];
  81. $count = Db::name('seckill_time')->where($where)->count();
  82. $list = Db::name('seckill_time')
  83. ->where($where)
  84. ->order('start_time asc')
  85. ->page($get['page'], $get['limit'])
  86. ->select();
  87. foreach ($list as &$item){
  88. $item['time'] = $item['start_time'].' ~ '.$item['end_time'];
  89. }
  90. return ['count' => $count, 'list' => $list];
  91. }
  92. public static function addTime($post){
  93. $post['create_time'] = time();
  94. $post['update_time'] = time();
  95. return Db::name('seckill_time')->insert($post);
  96. }
  97. public static function editTime($post){
  98. $post['update_time'] = time();
  99. return Db::name('seckill_time')->where(['id'=>$post['id']])->update($post);
  100. }
  101. public static function addGoods($post){
  102. $now = time();
  103. $add_data = [];
  104. foreach ($post['item'] as $item){
  105. $add_data[] =[
  106. 'seckill_id' => $post['seckill_id'],
  107. 'goods_id' => $item['goods_id'],
  108. 'item_id' => $item['item_id'],
  109. 'price' => $item['price'],
  110. 'create_time' => $now,
  111. 'sales_sum' => 0,
  112. 'update_time' => $now,
  113. 'del' => 0,
  114. ];
  115. }
  116. return Db::name('seckill_goods')->insertAll($add_data);
  117. }
  118. public static function editGoods($post){
  119. $now = time();
  120. foreach ($post['item'] as $goods){
  121. $update_data = [
  122. 'seckill_id' => $post['seckill_id'],
  123. 'price' => $goods['price'],
  124. 'update_time' => $now,
  125. ];
  126. Db::name('seckill_goods')->where(['id'=>$goods['id']])->update($update_data);
  127. }
  128. return true;
  129. }
  130. public static function delGoods($id,$seckill_id){
  131. $update_data = [
  132. 'update_time' => time(),
  133. 'del' => 1,
  134. ];
  135. return Db::name('seckill_goods')->where(['del'=>0,'goods_id'=>$id,'seckill_id'=>$seckill_id])->update($update_data);
  136. }
  137. public static function delTime($id){
  138. $update_data = [
  139. 'update_time' => time(),
  140. 'del' => 1,
  141. ];
  142. Db::name('seckill_time')->where(['id'=>$id])->update($update_data);
  143. Db::name('seckill_goods')->where(['del'=>0,'seckill_id'=>$id])->update($update_data);
  144. return true;
  145. }
  146. public static function getTime($id){
  147. return Db::name('seckill_time')->where(['del'=>0,'id'=>$id])->find();
  148. }
  149. public static function getTimeAll(){
  150. $time_list = Db::name('seckill_time')->where(['del'=>0])->order('start_time asc')->select();
  151. foreach ($time_list as &$item){
  152. $item['time'] = $item['start_time'].' ~ '.$item['end_time'];
  153. }
  154. return $time_list;
  155. }
  156. public static function getSeckillGoods($id,$seckill_id){
  157. $skill_goods = Db::name('seckill_goods sg')
  158. ->join('goods_item gi','sg.item_id = gi.id')
  159. ->where(['del'=>0,'sg.goods_id'=>$id,'sg.seckill_id'=>$seckill_id ])
  160. ->field('sg.*,gi.image,gi.spec_value_str,gi.price as goods_price')
  161. ->select();
  162. $goods_id = $skill_goods[0]['goods_id'];
  163. $goods = Db::name('goods')->where(['del'=>0,'id'=>$goods_id])->field('image,name')->find();
  164. foreach ($skill_goods as &$item){
  165. $item['name'] = $goods['name'];
  166. if(!$item['image']){
  167. $item['image'] = $goods['image'];
  168. }
  169. }
  170. return $skill_goods;
  171. }
  172. }