截流自动化的商城平台
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

SeckillGoodsLogic.php 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. <?php
  2. namespace app\admin\logic\seckill;
  3. use app\common\basics\Logic;
  4. use app\common\model\seckill\SeckillGoods;
  5. use app\common\model\seckill\SeckillTime;
  6. use app\common\model\goods\Goods;
  7. use app\common\server\UrlServer;
  8. use think\facade\Db;
  9. class SeckillGoodsLogic extends Logic
  10. {
  11. /**
  12. * 统计
  13. */
  14. public static function statistics()
  15. {
  16. // 秒杀中商品
  17. $where = [
  18. ['sg.del', '=', 0],
  19. ['sg.review_status', '=', 1],
  20. ];
  21. $lists = SeckillGoods::alias('sg')
  22. ->leftJoin('seckill_time st', 'st.id=sg.seckill_id')
  23. ->field('sg.goods_id,sg.start_date,sg.end_date,st.start_time,st.end_time')
  24. ->where($where)
  25. ->group('sg.goods_id,sg.start_date,sg.end_date,st.start_time,st.end_time')
  26. ->select()
  27. ->toArray();
  28. $now = time();
  29. $now_date = date('Y-m-d', $now);
  30. $unSeckillCount = 0;
  31. foreach($lists as $key => $item) { // 检验是否在秒杀中
  32. $start_date_time = strtotime($item['start_date']. ' 00:00:00');
  33. $end_date_time = strtotime($item['end_date']. ' 23:59:59');
  34. // 日期校验
  35. if($now >= $start_date_time && $now <= $end_date_time) {
  36. $start_time = strtotime($now_date. ' '. $item['start_time']);
  37. $end_time = strtotime($now_date. ' '. $item['end_time']);
  38. if($now >= $start_time && $now <= $end_time) {
  39. // 秒杀中的时段,无需处理
  40. }else{
  41. unset($lists[$key]); // 未在秒杀时段
  42. ++$unSeckillCount ;
  43. }
  44. }else{
  45. unset($lists[$key]); // 未在秒杀时间日期
  46. ++$unSeckillCount;
  47. }
  48. }
  49. $seckillCount = count($lists);
  50. // 待审核
  51. $waitReview = SeckillGoods::where(['del'=>0, 'review_status'=>0])->group('seckill_id,goods_id,start_date,end_date')->count();
  52. // 审核拒绝
  53. $refuseReview = SeckillGoods::where(['del'=>0, 'review_status'=>2])->group('seckill_id,goods_id,start_date,end_date')->count();
  54. //
  55. return [
  56. 'unSeckillCount' => $unSeckillCount,
  57. 'seckillCount' => $seckillCount,
  58. 'waitReview' => $waitReview,
  59. 'refuseReview' => $refuseReview,
  60. ];
  61. }
  62. public static function getTimeAll(){
  63. $time_list = SeckillTime::where(['del'=>0])->order('start_time asc')->select()->toArray();
  64. foreach ($time_list as &$item){
  65. $item['time'] = $item['start_time'].' ~ '.$item['end_time'];
  66. }
  67. return $time_list;
  68. }
  69. public static function goodsList($get)
  70. {
  71. $where = [
  72. ['sg.del', '=', 0],
  73. ];
  74. // 商品名称
  75. if(isset($get['name']) && !($get['name'] == '')) {
  76. $where[] = ['g.name', 'like', '%'.trim($get['name']).'%'];
  77. }
  78. // 参与日期
  79. if(isset($get['start_end']) && !empty($get['start_end'])) {
  80. $start_end = explode('~', $get['start_end']);
  81. $where[] = ['sg.start_date', '=', trim($start_end[0])];
  82. $where[] = ['sg.end_date', '=', trim($start_end[1])];
  83. }
  84. // 参与时段
  85. if(isset($get['seckill_id']) && !empty($get['seckill_id'])) {
  86. $where[] = ['sg.seckill_id', '=', $get['seckill_id']];
  87. }
  88. $lists = SeckillGoods::alias('sg')
  89. ->leftJoin('seckill_time st', 'st.id=sg.seckill_id')
  90. ->leftJoin('goods g', 'sg.goods_id=g.id')
  91. ->leftJoin('shop s', 's.id=sg.shop_id')
  92. ->field('sg.seckill_id,sg.goods_id,review_status,review_desc,start_date,end_date,start_time,end_time,g.name,g.image,g.min_price,g.max_price,s.id as shop_id,s.name as shop_name,s.type as shop_type,s.logo as shop_logo')
  93. ->where($where)
  94. ->group('sg.seckill_id,sg.goods_id,review_status,review_desc,start_date,end_date,start_time,end_time,g.name,g.image,g.min_price,g.max_price,s.id,s.name,s.type,s.logo')
  95. ->order(['sg.id' => 'desc'])
  96. ->select()
  97. ->toArray();
  98. // 按类型提取数据
  99. $unSeckill = [];
  100. $seckill = [];
  101. $waitReview = [];
  102. $refuseReview = [];
  103. $now = time();
  104. $now_date = date('Y-m-d', $now);
  105. foreach($lists as $key => $item) {
  106. if($item['review_status'] == 0) { // 待审核
  107. $waitReview[] = $item;
  108. continue;
  109. }else if($item['review_status'] == 2){ // 审核拒绝
  110. $refuseReview[] = $item;
  111. continue;
  112. }else if($item['review_status'] == 1) { // 审核通过
  113. $start_date_time = strtotime($item['start_date']. ' 00:00:00');
  114. $end_date_time = strtotime($item['end_date']. ' 23:59:59');
  115. // 日期校验
  116. if($now >= $start_date_time && $now <= $end_date_time) {
  117. $start_time = strtotime($now_date. ' '. $item['start_time']);
  118. $end_time = strtotime($now_date. ' '. $item['end_time']);
  119. if($now >= $start_time && $now <= $end_time) {
  120. $seckill[] = $item;
  121. continue;
  122. }else{
  123. // 未在秒杀时段
  124. $unSeckill[] = $item;
  125. continue;
  126. }
  127. }else{
  128. // 未在秒杀时间日期
  129. $unSeckill[] = $item;
  130. continue;
  131. }
  132. }
  133. }
  134. switch($get['type']) {
  135. case 'seckill':
  136. $lists = $seckill;
  137. break;
  138. case 'un_seckill':
  139. $lists = $unSeckill;
  140. break;
  141. case 'wait_review':
  142. $lists = $waitReview;
  143. break;
  144. case 'refuse_review':
  145. $lists = $refuseReview;
  146. break;
  147. }
  148. // 组装信息
  149. $review_status_desc = ['待审核','审核通过','审核拒绝'];
  150. $shop_type_desc = [1=>'官方自营', 2=>'入驻商家'];
  151. foreach($lists as &$item) {
  152. $item['shop_logo'] = empty($item['shop_logo']) ? '' : UrlServer::getFileUrl($item['shop_logo']);
  153. // 秒杀价格
  154. $price = SeckillGoods::where([
  155. 'del' => 0,
  156. 'seckill_id' => $item['seckill_id'],
  157. 'goods_id' => $item['goods_id'],
  158. 'start_date' => $item['start_date'],
  159. 'end_date' => $item['end_date'],
  160. ])->column('price', 'id');
  161. $seckill_min_price = min($price);
  162. $seckill_max_price = max($price);
  163. $item['seckill_price'] = $seckill_min_price == $seckill_max_price ? '¥ ' .$seckill_min_price : '¥ '. $seckill_min_price . ' ~ ¥ ' . $seckill_max_price;
  164. // 商品价格
  165. $item['goods_price'] = $item['min_price'] == $item['max_price'] ? '¥ ' .$item['min_price'] : '¥ '. $item['min_price'] .' ~ ¥ '. $item['max_price'];
  166. // 参与日期
  167. $item['date'] = $item['start_date'] . ' ~ ' . $item['end_date'];
  168. // 参与时段
  169. $item['time'] = $item['start_time'] . ' ~ ' . $item['end_time'];
  170. // 审核状态
  171. $item['review_status_desc'] = $review_status_desc[$item['review_status']];
  172. // 商家类型
  173. $item['shop_type_desc'] = $shop_type_desc[$item['shop_type']];
  174. }
  175. // 分页
  176. $count = count($lists);
  177. $index = ($get['page'] -1) * $get['limit'];
  178. $lists = array_slice($lists, $index, $get['limit']);
  179. // 返回
  180. return [
  181. 'count' => $count,
  182. 'lists' => $lists,
  183. ];
  184. }
  185. public static function getSeckillGoods($id,$seckill_id, $start_date, $end_date){
  186. $skill_goods = SeckillGoods::alias('sg')
  187. ->join('goods_item gi','sg.item_id = gi.id')
  188. ->join('shop s', 's.id=sg.shop_id')
  189. ->where(['sg.del'=>0,'sg.goods_id'=>$id,'sg.seckill_id'=>$seckill_id, 'sg.start_date'=>$start_date,'sg.end_date'=>$end_date])
  190. ->field('sg.*,gi.image,gi.spec_value_str,gi.price as goods_price,s.name as shop_name')
  191. ->select()
  192. ->toArray();
  193. $goods_id = $skill_goods[0]['goods_id'];
  194. $goods = Goods::where(['del'=>0,'id'=>$goods_id])->field('image,name')->find()->toArray();
  195. foreach ($skill_goods as &$item){
  196. $item['name'] = $goods['name'];
  197. if(!$item['image']){
  198. $item['image'] = $goods['image'];
  199. }
  200. $item['date'] = $item['start_date'] . ' ~ ' . $item['end_date'];
  201. }
  202. return $skill_goods;
  203. }
  204. public static function reAudit($post)
  205. {
  206. try{
  207. $updateData = [
  208. 'review_status' => 2,
  209. 'review_desc' => $post['reason'],
  210. 'update_time' => time()
  211. ];
  212. $where = [
  213. 'del' => 0,
  214. 'goods_id' => $post['goods_id'],
  215. 'seckill_id' => $post['seckill_id'],
  216. 'start_date' => $post['start_date'],
  217. 'end_date' => $post['end_date'],
  218. ];
  219. SeckillGoods::where($where)->update($updateData);
  220. return true;
  221. }catch(\Exception $e) {
  222. self::$error = $e->getMessage();
  223. return false;
  224. }
  225. }
  226. public static function audit($post)
  227. {
  228. try{
  229. $updateData = [
  230. 'review_status' => $post['audit_status'],
  231. 'review_desc' => $post['audit_remark'],
  232. 'update_time' => time()
  233. ];
  234. $where = [
  235. 'del' => 0,
  236. 'goods_id' => $post['goods_id'],
  237. 'seckill_id' => $post['seckill_id'],
  238. 'start_date' => $post['start_date'],
  239. 'end_date' => $post['end_date'],
  240. ];
  241. SeckillGoods::where($where)->update($updateData);
  242. return true;
  243. }catch(\Exception $e) {
  244. self::$error = $e->getMessage();
  245. return false;
  246. }
  247. }
  248. }