hrjy 2 years ago
parent
commit
3b11a39a27

+ 2 - 0
app/api/controller/store/StoreProductController.php

@@ -5,6 +5,7 @@ namespace app\api\controller\store;
 use app\admin\model\store\StoreDescription;
 use app\admin\model\system\SystemAttachment;
 use app\models\routine\RoutineCode;
+use app\models\store\StoreCoupon;
 use app\models\store\StoreOrder;
 use app\models\store\StoreVisit;
 use app\models\system\SystemStore;
@@ -177,6 +178,7 @@ class StoreProductController
         $data['mapKey'] = sys_config('tengxun_map_key');
         $data['store_self_mention'] = (int)sys_config('store_self_mention') ?? 0;//门店自提是否开启
         $data['activity'] = StoreProduct::activity($data['storeInfo']['id'], false);
+        $data['coupon_list'] = StoreCoupon::where('id', 'in', $storeInfo['coupon'])->select()->toArray();
         return app('json')->successful($data);
     }
 

+ 44 - 0
app/common.php

@@ -606,3 +606,47 @@ if (!function_exists('pr')) {
 
     }
 }
+
+
+function generate_promotion_code($no_of_codes, $exclude_codes_array = '', $code_length = 11) {
+    $characters = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
+    $promotion_codes = ''; //这个数组用来接收生成的优惠码
+    for ($j = 0; $j < $no_of_codes; $j++) {
+        $code = "";
+        for ($i = 0; $i < $code_length; $i++) {
+            $code.= $characters[mt_rand(0, strlen($characters) - 1) ];
+        }
+        $codes = \app\models\store\StoreCouponUser::where('code', $code);
+        if (!$codes) generate_promotion_code(1);
+
+        $promotion_codes = $code;
+    }
+    return $promotion_codes;
+}
+
+
+if (!function_exists('getDataFind')){
+    function getDataFind($table,$where=[]){
+        $data = \think\facade\Db::name($table)->where($where)->find();
+        return $data;
+    }
+}
+
+if (!function_exists('getParent')){
+    //查找所有创业合伙人
+    function getParent($uid){
+        //当前用户
+        static $arr=[];
+        //当前用户
+        $member = getDataFind('user',array('uid'=>$uid));
+        if ($member['spread_uid'] > 0){
+            $parent = getDataFind('user',array('uid'=>$member['spread_uid']));
+            if ($parent['level'] >= 2){
+                $arr[]= $parent['uid'];
+            }
+            getParent($parent['uid']);
+        }
+        return $arr;
+
+    }
+}

+ 1 - 1
app/models/store/StoreCouponUser.php

@@ -219,7 +219,7 @@ class StoreCouponUser extends BaseModel
 
     public static function checkInvalidCoupon()
     {
-        self::where('end_time', '<', time())->where('status', 0)->update(['status' => 2]);
+        self::where('end_time', '<', time())->where('end_time', '>', 0)->where('status', 0)->update(['status' => 2]);
     }
 
     public static function tidyCouponList($couponList)

+ 115 - 1
app/models/store/StoreOrder.php

@@ -769,18 +769,132 @@ class StoreOrder extends BaseModel
     {
         $order = self::where('order_id', $orderId)->find();
         $resPink = true;
-        $res1 = self::where('order_id', $orderId)->update(['paid' => 1, 'pay_type' => $paytype, 'pay_time' => time(), 'is_del' => 0, 'is_system_del' => 0, 'mark' => '']);//订单改为支付
+        $res1 = self::where('order_id', $orderId)->update(['paid' => 1, 'pay_type' => $paytype, 'pay_time' => time(), 'is_del' => 0, 'is_system_del' => 0, 'mark' => '', 'status' => 2]);//订单改为支付
         if ($order->combination_id && $res1 && !$order->refund_status) $resPink = StorePink::createPink($order);//创建拼团
         $oid = self::where('order_id', $orderId)->value('id');
         StoreOrderStatus::status($oid, 'pay_success', '用户付款成功');
         $now_money = User::where('uid', $order['uid'])->value('now_money');
         UserBill::expend('购买商品', $order['uid'], 'now_money', 'pay_money', $order['pay_price'], $order['id'], $now_money, '支付' . floatval($order['pay_price']) . '元购买商品');
         //支付成功后
+        self::operation($order['uid'], $orderId); // 发放优惠券
         event('OrderPaySuccess', [$order, $formId]);
         $res = $res1 && $resPink && UserSpread::setSpreadSure($order['uid']) && User::backOrderBrokerage($order);
         return false !== $res;
     }
 
+
+    /**
+     * 发放优惠券
+     * @param $uid
+     * @param $orderId
+     * @return void
+     * @throws DataNotFoundException
+     * @throws DbException
+     * @throws ModelNotFoundException
+     */
+    public static function operation($uid, $orderId)
+    {
+        $order = self::where('order_id', $orderId)->find();
+        $info = StoreOrderCartInfo::where('oid', $order['id'])->select();
+        $user = User::where('uid', $uid)->find();
+        foreach ($info as $v){
+            $product = StoreProduct::where('id', $v['product_id'])->find();
+            $cart_info = $v['cart_info'];
+            $coupon = StoreCoupon::where('id', 'in', $product['coupon'])->select();
+            if ($coupon){
+                for ($i = 0;$i < $cart_info['cart_num']; $i++){
+                    foreach ($coupon as $value){
+                        $data = [
+                            'cid' => $value['id'],
+                            'uid' => $uid,
+                            'coupon_title' => $value['title'],
+                            'coupon_price' => $value['coupon_price'],
+                            'use_min_price' => $value['use_min_price'],
+                            'add_time' => $value['start_time'] == 0? 0 : $value['start_time'],
+                            'end_time' => $value['end_time'] == 0? 0 : $value['end_time'],
+                            'code' => generate_promotion_code(1),
+                            'store_id' => $value['store_id'],
+                        ];
+                        StoreCouponUser::create($data);
+                    }
+                }
+            }
+            if ($product['id'] == 1){
+                // 如果是2999礼包
+                if ($user['spread_uid']) self::superior_push($user['spread_uid']);
+                if ($user['level'] < 2) $user['level'] = 2;
+            }else if ($product['id'] = 2){
+                if ($user['spread_uid']) self::superior($user['spread_uid']);
+                if ($user['level'] < 1) $user['level'] = 1;
+            }
+            $user->save();
+        }
+    }
+
+    /**
+     * 推广2999礼包奖励
+     * @param $uid
+     * @return void
+     * @throws DataNotFoundException
+     * @throws DbException
+     * @throws ModelNotFoundException
+     */
+    public static function superior_push($uid)
+    {
+        $top1 = User::where('uid', $uid)->find(); // 一级上级
+
+        $top2 = User::where('uid', $top1['spread_uid'])->find(); // 二级上级
+        $top1['brokerage_price'] += 1200;//直推
+        $top2['brokerage_price'] += 600;//间推
+        if ($top1['level'] == 2){
+            $top1->save();
+            UserBill::income('佣金', $top1['uid'], 'now_money', 'brokerage', 1200, 0, $top1['brokerage_price'], '直推2999礼包奖励');
+        }
+        if ($top2['level'] == 2){
+            $top2->save();
+            UserBill::income('佣金', $top2['uid'], 'now_money', 'brokerage', 600, 0, $top2['brokerage_price'], '间推2999礼包奖励');
+        }
+
+    }
+
+    /**
+     * 推广399礼包奖励
+     * @param $uid
+     * @return void
+     * @throws DataNotFoundException
+     * @throws DbException
+     * @throws ModelNotFoundException
+     */
+    public static function superior($uid)
+    {
+        $top1 = User::where('uid', $uid)->find(); // 一级上级
+        $price = 0;
+        if ($top1['level'] == 1){
+            $top1['brokerage_price'] += 40;
+            $price = 40;
+
+        }elseif ($top1['level'] == 2){
+            $top1['brokerage_price'] += 130;
+            $price = 130;
+        }
+        if ($top1['spread_uid']){
+            $user = getParent($top1['uid']);
+            if ($user){
+                foreach ($user as $v){
+                    $top =   User::where('uid', $v)->find();
+                    $top['brokerage_price'] += 90;
+                    $top->save();
+                    UserBill::income('佣金', $top1['uid'], 'now_money', 'brokerage', 90, 0, $top['brokerage_price'], '间推399礼包奖励');
+                }
+            }
+        }
+        $top1->save();
+        UserBill::income('佣金', $top1['uid'], 'now_money', 'brokerage', $price, 0, $top1['brokerage_price'], '直推399礼包奖励');
+
+    }
+
+
+
     /*
      * 线下支付消息通知
      * 待完善

+ 1 - 1
app/models/store/StoreProduct.php

@@ -58,7 +58,7 @@ class StoreProduct extends BaseModel
         return htmlspecialchars_decode($value);
     }
 
-    public static function getValidProduct($productId, $field = 'add_time,browse,cate_id,code_path,cost,ficti,give_integral,id,image,is_sub,is_bargain,is_benefit,is_best,is_del,is_hot,is_new,is_postage,is_seckill,is_show,keyword,mer_id,mer_use,ot_price,postage,price,sales,slider_image,sort,stock,store_info,store_name,unit_name,vip_price,spec_type,IFNULL(sales,0) + IFNULL(ficti,0) as fsales,video_link')
+    public static function getValidProduct($productId, $field = 'add_time,browse,cate_id,code_path,cost,ficti,give_integral,id,image,is_sub,is_bargain,is_benefit,is_best,is_del,is_hot,is_new,is_postage,is_seckill,is_show,keyword,mer_id,mer_use,ot_price,postage,price,sales,slider_image,sort,stock,store_info,store_name,unit_name,vip_price,spec_type,IFNULL(sales,0) + IFNULL(ficti,0) as fsales,video_link,coupon')
     {
         $Product = self::where('is_del', 0)->where('is_show', 1)->where('id', $productId)->field($field)->find();
         if ($Product) return $Product->toArray();