Kaynağa Gözat

套餐卡券类型

zhangxiaobin 1 yıl önce
ebeveyn
işleme
7e228e9356

+ 164 - 27
application/api/controller/company/Package.php

@@ -4,6 +4,7 @@ namespace app\api\controller\company;
 
 use app\common\controller\Apic;
 use think\Db;
+use think\Exception;
 
 /**
  * 套餐管理
@@ -32,6 +33,25 @@ class Package extends Apic
             ->field('p.*,type.title as servicetype_title')
             ->join('servicetype type','p.servicetype_id = type.id','LEFT')
             ->where($where)->order('p.id desc')->autopage()->select();
+        //追加赠送
+        if(!empty($list)){
+            $package_ids = array_column($list,'id');
+            $gift = Db::name('package_gift')->alias('gift')
+                ->field('gift.*,coupons.name,coupons.info,coupons.days')
+                ->join('coupons','gift.coupon_id = coupons.id','LEFT')
+                ->where('gift.package_id','IN',$package_ids)
+                ->where('coupons.status',1)
+                ->select();
+
+            foreach($list as $key => &$val){
+                $val['gift'] = [];
+                foreach($gift as $k => $v){
+                    if($val['id'] == $v['package_id']){
+                        $val['gift'][] = $v;
+                    }
+                }
+            }
+        }
         $list = list_domain_image($list,['images','content_images']);
         $this->success(1,$list);
 
@@ -39,20 +59,52 @@ class Package extends Apic
 
     //新增
     public function add(){
-        //验证
-        if($this->auth->type != 1){
-            $this->error('只有门店老板才能设置');
-        }
-        $field = ['title','info','servicetype_id','images','price','oldprice','num','content','content_images'];
-        $data = request_post_hub($field);
+        Db::startTrans();
+        try {
+            //验证
+            if($this->auth->type != 1){
+                throw new Exception('只有门店老板才能设置');
+            }
+            $field = ['title','info','servicetype_id','images','price','oldprice','num','content','content_images','type'];
+            $data = request_post_hub($field);
 
-        $data['company_id'] = $this->auth->company_id;
-        $data['createtime'] = time();
-        $data['updatetime'] = time();
-        $data['status'] = 1;
+            $data['company_id'] = $this->auth->company_id;
+            $data['createtime'] = time();
+            $data['updatetime'] = time();
+            $data['status'] = 1;
 
-        Db::name('package')->insertGetId($data);
-        $this->success('添加成功');
+            $package_id = Db::name('package')->insertGetId($data);
+            if (!$package_id) {
+                throw new Exception('添加套餐失败');
+            }
+            if (isset($data['type']) && $data['type'] == 2) {
+                $gift_data = input('gift_data','','trim');
+                $gift_data = json_decode(htmlspecialchars_decode($gift_data),true);
+
+                if(is_array($gift_data) && !empty($gift_data)){
+                    $package_gift = [];
+                    foreach($gift_data as $key => $val){
+                        $package_gift[] = [
+                            'package_id' => $package_id,
+                            'coupon_id' => $val['coupon_id'],
+                            'number'    => $val['number'],
+                        ];
+                    }
+                    if(!empty($package_gift)){
+                        $rs_gift = Db::name('package_gift')->insertAll($package_gift);
+                        if($rs_gift === false){
+                            Db::rollback();
+                            $this->error('添加失败');
+                        }
+                    }
+                }
+            }
+            Db::commit();
+            $this->success('添加成功');
+        } catch (Exception $e) {
+            Db::rollback();
+            $this->error($e->getMessage());
+        }
     }
 
     //上下架
@@ -77,30 +129,114 @@ class Package extends Apic
             ->field('p.*,type.title as servicetype_title')
             ->join('servicetype type','p.servicetype_id = type.id','LEFT')
             ->where('p.id',$id)->find();
+        //追加赠送
+        if(!empty($info)){
+            $gift = Db::name('package_gift')->alias('gift')
+                ->field('gift.*,coupons.name,coupons.info,coupons.days')
+                ->join('coupons','gift.coupon_id = coupons.id','LEFT')
+                ->where('gift.package_id',$id)
+                ->where('coupons.status',1)
+                ->select();
+
+            $info['gift'] = [];
+            if (!empty($gift)) {
+                foreach($gift as $k => $v){
+                    $info['gift'][] = $v;
+                }
+            }
+        }
         $info = info_domain_image($info,['images','content_images']);
         $this->success(1,$info);
     }
 
     //编辑
     public function edit(){
-        //验证
-        if($this->auth->type != 1){
-            $this->error('只有门店老板才能设置');
-        }
-        $id = input('id','');
+        Db::startTrans();
+        try {
+            //验证
+            if($this->auth->type != 1){
+                throw new Exception('只有门店老板才能设置');
+            }
+            $id = input('id','');
 
-        $check = Db::name('package')->where('id',$id)->where('company_id',$this->auth->company_id)->lock(true)->find();
-        if(empty($check)){
-            $this->error('不存在的套餐');
-        }
+            $check = Db::name('package')->where('id',$id)->where('company_id',$this->auth->company_id)->lock(true)->find();
+            if(empty($check)){
+                throw new Exception('不存在的套餐');
+            }
+
+            //
+            $field = ['title','info','servicetype_id','images','price','oldprice','num','content','content_images'];
+            $data = request_post_hub($field);
+            $data['updatetime'] = time();
 
-        //
-        $field = ['title','info','servicetype_id','images','price','oldprice','num','content','content_images'];
-        $data = request_post_hub($field);
-        $data['updatetime'] = time();
+            $packageRes = Db::name('package')->where('id',$id)->update($data);
+            if (!$packageRes) {
+                throw new Exception('套餐编辑失败');
+            }
+            if ($check['type'] == 2) {
+                $packageGiftWhere['package_id'] = $id;
+                $packageGiftData = Db::name('package_gift')->where($packageGiftWhere)->select();
 
-        Db::name('package')->where('id',$id)->update($data);
-        $this->success('编辑成功');
+                $gift_data = input('gift_data','','trim');
+                $gift_data = json_decode(htmlspecialchars_decode($gift_data),true);
+
+                if(is_array($gift_data) && !empty($gift_data)) {
+                    $packageGiftCouponIds = array_column($packageGiftData,'coupon_id');
+                    $postCouponIds = array_column($gift_data,'coupon_id');
+                    $package_gift = [];
+                    $same = [];
+                    $different = [];
+                    foreach ($gift_data as $key => $val) {
+                        if (in_array($val['coupon_id'], $packageGiftCouponIds)) {
+                            $same[] = [
+                                'package_id' => $id,
+                                'coupon_id'  => $val['coupon_id'],
+                                'number'     => $val['number'],
+                            ];
+                        } else {
+                            $different[] = [
+                                'package_id' => $id,
+                                'coupon_id'  => $val['coupon_id'],
+                                'number'     => $val['number'],
+                            ];
+                        }
+                    }
+                    if (!empty($same)) {//相同的更新
+                        foreach ($same as $sameKey => $sameVal) {
+                            $sameWhere['package_id'] = $sameVal['package_id'];
+                            $sameWhere['coupon_id'] = $sameVal['coupon_id'];
+                            $sameData['number'] = $sameVal['number'];
+                            Db::name('package_gift')->where($sameWhere)->update($sameData);
+                        }
+                    }
+                    if (!empty($different)) {//新增的添加
+                        $packageGiftRes = Db::name('package_gift')->where($sameWhere)->insertAll($different);
+                        if (!$packageGiftRes) {
+                            throw new Exception('套餐卡券添加失败');
+                        }
+                    }
+                    //多余的删除
+                    $delIds = array_diff($packageGiftCouponIds,$postCouponIds);
+                    if (!empty($delIds)) {
+                        $packageGiftDelWhere['package_id'] = $id;
+                        $packageGiftDelWhere['coupon_id'] = ['in',$delIds];
+                        Db::name('package_gift')->where($packageGiftDelWhere)->delete();
+                    }
+                } else {
+                    if (!empty($packageGiftData)) {
+                        $packageGiftDelRes = Db::name('package_gift')->where($packageGiftWhere)->delete();
+                        if (!$packageGiftDelRes) {
+                            throw new Exception('删除卡券失败');
+                        }
+                    }
+                }
+            }
+            Db::commit();
+            $this->success('编辑成功');
+        } catch (Exception $e) {
+            Db::rollback();
+            $this->error($e->getMessage());
+        }
     }
 
     //删除
@@ -117,6 +253,7 @@ class Package extends Apic
         }
 
         Db::name('package')->where('id',$id)->delete();
+        Db::name('package_gift')->where('package_id',$id)->delete();
         $this->success('删除成功');
     }
 

+ 3 - 0
application/common/service/OrderService.php

@@ -210,6 +210,7 @@ class OrderService
             }
             if ($package['type'] == 2) {//卡券类型直接已完成
                 $orderStatus = 3;
+                $finishTime = $time;
                 $userCouponsParams = [
                     'package_id' => $packageId,
                     'company_id' => $companyId,
@@ -222,6 +223,7 @@ class OrderService
                 }
             } else {
                 $orderStatus = 1;
+                $finishTime = 0;
             }
             $orderData = [
                 'check_code'      => $checkCodeStr,
@@ -245,6 +247,7 @@ class OrderService
                 'total_fee'       => isset($package['price']) ? $package['price'] : 0.00,//支付总额
                 'createtime'      => $time,//下单时间
                 'pay_order_id'    => $payOrderId,//支付ID
+                'finish_time'     => $finishTime,//完成时间
             ];
             $orderRes = Db::name('order')->insertGetId($orderData);
             if (!$orderRes) {