Browse Source

双十一活动

gorden 4 months ago
parent
commit
cc2bec5a4d

+ 35 - 14
app/admin/controller/coupon/CouponDetailController.php

@@ -316,31 +316,52 @@ class CouponDetailController extends Curd
     public function promotionCoupon(Request $request)
     {
         $goods = $request->get('goods', []);
-        $money = $request->get('money', []);
         $goodsIds = array_column($goods, 'goods_id');
-        $couponIds = CouponGoods::whereIn('join_coupon_goods_id', $goodsIds)->pluck('join_goods_coupon_id')->toArray();
+        $goods = array_column($goods, null, 'goods_id');
 
         $coupons = Coupon::where('coupon_category', 'PROMOTION')
             ->where('coupon_status', 'ACTIVED')
-            ->whereIn('coupon_id', $couponIds)
-            ->where(function ($query) use ($money) {
-                $query->where('coupon_minimum_limit', 0)
-                    ->orWhere('coupon_minimum_limit', '<=', $money);
-            })->select('coupon.coupon_id', 'coupon.coupon_name', 'coupon.coupon_minimum_limit', 'coupon.coupon_classify', 'coupon.coupon_value', 'coupon.coupon_category')
+            ->select('coupon_id')
             ->get()
             ->toArray();
         $data = [];
-        foreach ($coupons as $coupon){
-            $classify = CouponService::couponClassifyInfo($coupon['coupon_classify'], $coupon['coupon_category'], $coupon['coupon_value'], $coupon['coupon_minimum_limit']);
+        foreach ($coupons as $coupon) {
+            $couponGoods = CouponGoods::whereIn('join_coupon_goods_id', $goodsIds)
+                ->where('join_goods_coupon_id', $coupon['coupon_id'])
+                ->select('join_goods_coupon_id', 'join_coupon_goods_id')
+                ->get()
+                ->toArray();
+            $money = 0;
+            $moneyGoodsIds = [];
+            foreach ($couponGoods as $couponGood) {
+                if (isset($goods[$couponGood['join_coupon_goods_id']]) && !in_array($couponGood['join_coupon_goods_id'], $moneyGoodsIds)) {
+                    $good = $goods[$couponGood['join_coupon_goods_id']];
+                    $money = $money + ($good['price'] * $good['nbr']);
+                    $moneyGoodsIds[] = $couponGood['join_coupon_goods_id'];
+                }
+            }
+
+            $promotionCoupon = Coupon::where('coupon_id', $coupon['coupon_id'])
+                ->where(function ($query) use ($money) {
+                    $query->where('coupon_minimum_limit', 0)
+                        ->orWhere('coupon_minimum_limit', '<=', $money);
+                })->select('coupon_id', 'coupon_name', 'coupon_minimum_limit', 'coupon_classify', 'coupon_value', 'coupon_category')
+                ->first();
+            if (empty($promotionCoupon)){
+                continue;
+            }
+            $promotionCoupon = $promotionCoupon->toArray();
+            $classify = CouponService::couponClassifyInfo($promotionCoupon['coupon_classify'], $promotionCoupon['coupon_category'], $promotionCoupon['coupon_value'], $promotionCoupon['coupon_minimum_limit']);
             $data[] = [
-                'coupon_id' => $coupon['coupon_id'],
-                'coupon_name' => $coupon['coupon_name'],
+                'coupon_id' => $promotionCoupon['coupon_id'],
+                'coupon_name' => $promotionCoupon['coupon_name'],
                 'classify' => $classify,
                 'count' => 1,
-                'coupon_classify' => $coupon['coupon_classify'],
-                'coupon_value' => $coupon['coupon_value'] ?? '',
-                'coupon_minimum_limit' => $coupon['coupon_minimum_limit'] ?? '',
+                'coupon_classify' => $promotionCoupon['coupon_classify'],
+                'coupon_value' => $promotionCoupon['coupon_value'] ?? '',
+                'coupon_minimum_limit' => $promotionCoupon['coupon_minimum_limit'] ?? '',
             ];
+
         }
 
         return json_success('', $data);

+ 23 - 0
app/admin/controller/order/WholeController.php

@@ -2,6 +2,7 @@
 
 namespace app\admin\controller\order;
 
+use app\admin\service\coupon\CouponDetailService;
 use app\admin\service\coupon\CouponService;
 use app\admin\service\member\MemberService;
 use app\admin\service\order\OrderService;
@@ -5339,6 +5340,28 @@ class WholeController extends Curd
                 return json_fail("参数异常");
             }
         }
+        // 是否是推广券
+        $promotionCoupon = Coupon::whereIn('coupon_id', $coupon)
+            ->where('coupon_category', 'PROMOTION')
+            ->select('coupon_id')
+            ->first();
+        if (!empty($promotionCoupon)) {
+            // 是否已有
+            $couponDetail = CouponDetail::whereIn('join_detail_coupon_id', $coupon)
+                ->where('join_coupon_detail_member_id', $memberId)
+                ->whereIn('coupon_detail_status', ['INIT', 'PENDING', 'ACTIVED'])
+                ->first();
+            if (empty($couponDetail)) {
+                $chooseCoupons[] = [
+                    'id' => $coupon[0],
+                    'nbr' => 1,
+                    'gain_datetime' => date('Y-m-d H:i:s')
+                ];
+
+                CouponDetailService::customSendService($chooseCoupons, '', [$memberId], '');
+            }
+        }
+
         $amountBalance = [
             'pay_amount' => $payAmount,
             'welfare_balance' => 0,

+ 86 - 0
app/admin/service/coupon/CouponDetailService.php

@@ -4,6 +4,7 @@ namespace app\admin\service\coupon;
 
 use app\model\Coupon;
 use app\model\CouponDetail;
+use app\model\Member;
 use app\model\SysSerial;
 use support\Db;
 use support\exception\BusinessException;
@@ -287,4 +288,89 @@ class CouponDetailService
 
         return $params;
     }
+
+    public static function customSendService($chooseCoupons, $couponUseMember, $memberList, $condition)
+    {
+        if ($couponUseMember == 'condition') {
+            if ($condition == 'is_employ') {
+                $memberList = Member::where('member_classify', 'EMPLOY')->pluck('member_id')->toArray();
+            } elseif ($condition == 'is_member') {
+                $memberList = Member::where('member_classify', 'MEMBER')->pluck('member_id')->toArray();
+            } elseif ($condition == 'is_vip') {
+                $memberList = Member::where('member_is_vip', 'Y')->pluck('member_id')->toArray();
+            }
+        }
+
+        $couponNbr = [];
+        Db::beginTransaction();
+        try {
+            $couponIds = array_column($chooseCoupons, 'id');
+            $coupons = Coupon::whereIn('coupon_id', $couponIds)->get()->toArray();
+            foreach ($coupons as $coupon) {
+                foreach ($chooseCoupons as $chooseCoupon) {
+                    if ($chooseCoupon['id'] == $coupon['coupon_id']) {
+                        if (empty($chooseCoupon['gain_datetime'])) {
+                            $chooseCoupon['gain_datetime'] = date('Y-m-d H:i:s');
+                        }
+                        $params['coupon_id'] = $coupon['coupon_id'];
+                        $params['coupon_detail_gain_datetime'] = date('Y-m-d H:i:s', strtotime($chooseCoupon['gain_datetime']));
+                        if (($coupon['coupon_validdate_day'] == 0 || $coupon['coupon_validdate_day'] == '') && !empty($coupon['coupon_validdate_end'])) {
+                            $params['coupon_detail_deadline_datetime'] = $coupon['coupon_validdate_end'];
+                        } elseif ($coupon['coupon_validdate_day'] > 0) {
+                            $endUnix = strtotime($chooseCoupon['gain_datetime']);
+                            $params['coupon_detail_deadline_datetime'] = date('Y-m-d H:i:s', $endUnix + ($coupon['coupon_validdate_day'] * 24 * 3600) - 1);
+                        }
+                        foreach ($memberList as $item) {
+                            $params['member_id'] = $item;
+                            if ($coupon['coupon_is_period'] == 'Y') {
+//                                if (CouponDetail::where('join_coupon_detail_member_id', $item)->where('join_detail_coupon_id', $coupon['coupon_id'])->exists()) {
+//                                    throw new BusinessException("请勿重复发放周期券");
+//                                }
+                                $params['coupon_detail_period_num'] = 1;
+                                $periodJson = json_decode($coupon['coupon_period_json'], true);
+//                                for ($i=0;$i<$periodJson['nbr'];$i++){
+                                CouponDetailService::sendPeriodCoupon($params);
+//                                }
+
+                                continue;
+                            }
+                            if ($chooseCoupon['nbr'] > 0) {
+                                // 有发行数量
+                                if ($coupon['coupon_number'] != 0) {
+                                    // 查询还有多少张没领的
+                                    $count = CouponDetail::where('join_detail_coupon_id', $coupon['coupon_id'])
+                                        ->whereIn('coupon_detail_status', ['INIT', 'PENDING'])
+                                        ->count();
+                                    if (!isset($couponNbr[$coupon['coupon_id']])) {
+                                        $couponNbr[$coupon['coupon_id']] = $count;
+                                    }
+                                    if ($couponNbr[$coupon['coupon_id']] - $chooseCoupon['nbr'] < 0) {
+                                        throw new BusinessException($coupon['coupon_name'] . "超出发行数量");
+                                    }
+                                    $couponNbr[$coupon['coupon_id']] = $couponNbr[$coupon['coupon_id']] - $chooseCoupon['nbr'];
+                                    $params['chooseCouponNbr'] = $chooseCoupon['nbr'];
+                                    // 匹配已发行的优惠券
+                                    CouponDetailService::customSendCouponHave($params);
+                                } else {
+                                    for ($i = 0; $i < $chooseCoupon['nbr']; $i++) {
+                                        CouponDetailService::customSendCoupon($params);
+                                    }
+                                }
+                            }
+                        }
+                    }
+                }
+            }
+
+            Db::commit();
+        } catch (BusinessException $e) {
+            Db::rollBack();
+            Log::error('优惠券发放失败',['msg'=>$e->getMessage()]);
+            throw new BusinessException($e->getMessage());
+        } catch (\Exception $e) {
+            Db::rollBack();
+            Log::error('优惠券发放失败',['msg'=>$e->getMessage()]);
+            throw new BusinessException('优惠券发放失败');
+        }
+    }
 }

+ 2 - 2
app/admin/service/order/OrderService.php

@@ -644,7 +644,7 @@ class OrderService
                         continue;
                     }
                     Redis::sAdd($cacheKey, $couponId);
-                    if ($couponDetail->coupon_category == 'PIECE' || ($couponDetail->coupon_category == 'NORMAL' && $countAndAmount['amount'] >= $couponDetail->coupon_minimum_limit)) {
+                    if ($couponDetail->coupon_category == 'PIECE' || $couponDetail->coupon_category == 'PROMOTION' || ($couponDetail->coupon_category == 'NORMAL' && $countAndAmount['amount'] >= $couponDetail->coupon_minimum_limit)) {
                         $zhekouAmount = round($countAndAmount['amount'] * (100 - $couponDetail->coupon_value) / 100, 2);
                         $payAmount = $payAmount - $zhekouAmount;
                         // json记录
@@ -809,7 +809,7 @@ class OrderService
                         continue;
                     }
                     Redis::sAdd($cacheKey, $couponId);
-                    if ($couponDetail->coupon_category == 'PIECE' || ($couponDetail->coupon_category == 'NORMAL' && $countAndAmount['amount'] >= $couponDetail->coupon_minimum_limit)) {
+                    if ($couponDetail->coupon_category == 'PIECE' || $couponDetail->coupon_category == 'PROMOTION' || ($couponDetail->coupon_category == 'NORMAL' && $countAndAmount['amount'] >= $couponDetail->coupon_minimum_limit)) {
                         $zhekouAmount = round($countAndAmount['amount'] * (100 - $couponDetail->coupon_value) / 100, 2);
                         $payAmount = $payAmount - $zhekouAmount;
                     }