Jelajahi Sumber

美团对接-线下派单

whitefang 1 tahun lalu
induk
melakukan
01eb9fbc2d

+ 98 - 0
app/adminapi/validate/third/ThirdGoodsValidate.php

@@ -0,0 +1,98 @@
+<?php
+// +----------------------------------------------------------------------
+// | likeadmin快速开发前后端分离管理后台(PHP版)
+// +----------------------------------------------------------------------
+// | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
+// | 开源版本可自由商用,可去除界面版权logo
+// | gitee下载:https://gitee.com/likeshop_gitee/likeadmin
+// | github下载:https://github.com/likeshop-github/likeadmin
+// | 访问官网:https://www.likeadmin.cn
+// | likeadmin团队 版权所有 拥有最终解释权
+// +----------------------------------------------------------------------
+// | author: likeadminTeam
+// +----------------------------------------------------------------------
+
+namespace app\adminapi\validate\third;
+
+
+use app\common\validate\BaseValidate;
+
+
+/**
+ * ThirdGoods验证器
+ * Class ThirdGoodsValidate
+ * @package app\adminapi\validate\third
+ */
+class ThirdGoodsValidate extends BaseValidate
+{
+
+     /**
+      * 设置校验规则
+      * @var string[]
+      */
+    protected $rule = [
+        'id' => 'require',
+        'third_type' => 'require',
+        'goods_id' => 'require',

+    ];
+
+
+    /**
+     * 参数描述
+     * @var string[]
+     */
+    protected $field = [
+        'id' => 'id',
+        'third_type' => '平台名称',
+        'goods_id' => '商品ID',

+    ];
+
+
+    /**
+     * @notes 添加场景
+     * @return ThirdGoodsValidate
+     * @author likeadmin
+     * @date 2025/03/06 16:57
+     */
+    public function sceneAdd()
+    {
+        return $this->only(['third_type','goods_id']);
+    }
+
+
+    /**
+     * @notes 编辑场景
+     * @return ThirdGoodsValidate
+     * @author likeadmin
+     * @date 2025/03/06 16:57
+     */
+    public function sceneEdit()
+    {
+        return $this->only(['id','third_type','goods_id']);
+    }
+
+
+    /**
+     * @notes 删除场景
+     * @return ThirdGoodsValidate
+     * @author likeadmin
+     * @date 2025/03/06 16:57
+     */
+    public function sceneDelete()
+    {
+        return $this->only(['id']);
+    }
+
+
+    /**
+     * @notes 详情场景
+     * @return ThirdGoodsValidate
+     * @author likeadmin
+     * @date 2025/03/06 16:57
+     */
+    public function sceneDetail()
+    {
+        return $this->only(['id']);
+    }
+
+}

+ 100 - 0
app/adminapi/validate/third/ThirdOrdersValidate.php

@@ -0,0 +1,100 @@
+<?php
+// +----------------------------------------------------------------------
+// | likeadmin快速开发前后端分离管理后台(PHP版)
+// +----------------------------------------------------------------------
+// | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
+// | 开源版本可自由商用,可去除界面版权logo
+// | gitee下载:https://gitee.com/likeshop_gitee/likeadmin
+// | github下载:https://github.com/likeshop-github/likeadmin
+// | 访问官网:https://www.likeadmin.cn
+// | likeadmin团队 版权所有 拥有最终解释权
+// +----------------------------------------------------------------------
+// | author: likeadminTeam
+// +----------------------------------------------------------------------
+
+namespace app\adminapi\validate\third;
+
+
+use app\common\validate\BaseValidate;
+
+
+/**
+ * ThirdOrders验证器
+ * Class ThirdOrdersValidate
+ * @package app\adminapi\validate\third
+ */
+class ThirdOrdersValidate extends BaseValidate
+{
+
+     /**
+      * 设置校验规则
+      * @var string[]
+      */
+    protected $rule = [
+        'id' => 'require',
+        'orderId' => 'require',
+        'productId' => 'require',
+        'third_type' => 'require',

+    ];
+
+
+    /**
+     * 参数描述
+     * @var string[]
+     */
+    protected $field = [
+        'id' => 'id',
+        'orderId' => '预约订单ID',
+        'productId' => '产品ID',
+        'third_type' => '订单来源',

+    ];
+
+
+    /**
+     * @notes 添加场景
+     * @return ThirdOrdersValidate
+     * @author likeadmin
+     * @date 2025/03/06 16:57
+     */
+    public function sceneAdd()
+    {
+        return $this->only(['orderId','productId','third_type']);
+    }
+
+
+    /**
+     * @notes 编辑场景
+     * @return ThirdOrdersValidate
+     * @author likeadmin
+     * @date 2025/03/06 16:57
+     */
+    public function sceneEdit()
+    {
+        return $this->only(['id','orderId','productId','third_type']);
+    }
+
+
+    /**
+     * @notes 删除场景
+     * @return ThirdOrdersValidate
+     * @author likeadmin
+     * @date 2025/03/06 16:57
+     */
+    public function sceneDelete()
+    {
+        return $this->only(['id']);
+    }
+
+
+    /**
+     * @notes 详情场景
+     * @return ThirdOrdersValidate
+     * @author likeadmin
+     * @date 2025/03/06 16:57
+     */
+    public function sceneDetail()
+    {
+        return $this->only(['id']);
+    }
+
+}

+ 315 - 0
app/api/logic/ThirdOrderLogic.php

@@ -0,0 +1,315 @@
+<?php
+namespace app\api\logic;
+
+
+use app\common\{enum\GoodsEnum,
+    enum\PayEnum,
+    enum\WorkEnum,
+    logic\BaseLogic,
+    model\goods\Goods,
+    model\orders\RechargeOrder,
+    model\recharge\OrderGoods,
+    model\setting\PostageRegion,
+    model\third\ThirdGoods,
+    model\third\ThirdOrders,
+    model\user\User,
+    model\works\ServiceWork};
+use think\facade\Db;
+
+/**
+ * 第三方平台下单逻辑层
+ * Class UserLogic
+ * @package app\shopapi\logic
+ */
+class ThirdOrderLogic extends BaseLogic
+{
+
+    /**
+     * 处理订单
+     * @param array $order
+     * @return bool
+     * @throws \think\db\exception\DataNotFoundException
+     * @throws \think\db\exception\DbException
+     * @throws \think\db\exception\ModelNotFoundException
+     */
+    public static function orderhandle(array $order)
+    {
+        $message = json_decode($order['message'], true);
+        //查询用户是否存在
+        $userName = $message['userName'];
+        $mobile = $message['mobile'];
+        $userAddress = json_decode($message['userAddress']);
+        $user = User::where(['mobile' =>$mobile])->findOrEmpty();
+        $product_message = json_decode($message['products'],true);
+        if($user->isEmpty()){
+            $user = LoginLogic::register([
+                'password'=>'123456',
+                'account'=>$userName,
+                'mobile'=>$mobile,
+                'channel'=>1,
+                'user_type'=>0
+            ]);
+        }
+
+        //查询美团商品和平台商品的对应关系
+        $productId = $message['productId'];
+        $product = ThirdGoods::where(['third_type'=>1,'product_id'=>$productId])->findOrEmpty();
+
+        //生成第三方订单信息
+        $orderData = [
+            'orderId'=>$message['orderId'],
+            'uni_order_id'=>$product_message[0]['uni_order_id'],
+            'productId'=>$message['productId'],
+            'productName'=>$message['productName'],
+            'request_msg'=>json_encode($order,JSON_UNESCAPED_UNICODE)
+        ];
+
+        //判断美团的预约订单是否生成
+        if(!$product->isEmpty()){
+            //查询美团预约订单是否生成
+            $thirdOrder = ThirdOrders::where(['uni_order_id'=>$product_message[0]['uni_order_id']])->findOrEmpty();
+            if(!$thirdOrder->isEmpty() and $thirdOrder['work_id'] != 0){
+                //生成第三方订单信息
+                $orderData['sync_status'] = 1;
+                $orderData['work_id'] = $thirdOrder['work_id'];
+            }
+        }
+        $thirdOrders = ThirdOrders::where('orderId',$message['orderId'])->findOrEmpty();
+        //生成未同步预约订单信息
+        if($thirdOrders->isEmpty()){
+            $thirdOrders = ThirdOrders::create($orderData);
+
+        }
+        if(!$thirdOrders->isEmpty()){
+            $orderData['work_id'] = $thirdOrders->work_id;
+        }
+        if(empty($orderData['work_id']) and !$product->isEmpty()){
+            $goods = Goods::findOrEmpty($product['goods_id']);
+            $pattern = '/(?:省|市)([\w\s]+市)/u';
+            Db::startTrans();
+            try {
+                if (preg_match($pattern, $userAddress->receiver_address, $matches)) {
+                    $city = $matches[1];
+                }
+                $area_city = PostageRegion::where(['level'=>2,'name'=>$city])->value('id');
+
+                //生成服务工单
+                $work_data = [
+                    'work_sn' => generate_sn(ServiceWork::class, 'work_sn'),
+                    'real_name' => $userName,
+                    'mobile' => $mobile,
+                    'address' => $userAddress->receiver_address,
+                    'province' => 0,
+                    'city' => $area_city??0,
+                    'area_name' => $area_city??'',
+                    'title' => $goods->goods_name,
+                    'category_type' => $goods['category_type'],
+                    'goods_category_ids' => $goods['goods_category_ids'],
+                    'goods_category_id' => $goods['goods_category_id'],
+                    'base_service_fee' => $goods['base_service_fee'],
+                    'service_fee' => $message['amount'],
+                    'work_pay_status'=>WorkEnum::IS_PAY_STATUS,
+                    'appointment_time' => strtotime($message['begintime']),
+                    'user_id'=>$user['id'],
+                    'lon'=>!empty($userAddress->longitude)?$userAddress->longitude:0,
+                    'lat'=>!empty($userAddress->latitude)?$userAddress->latitude:0,
+                    'property_activity_id'=>0,
+                    'user_equity_id'=>0,
+                    'third_type'=>1,
+                    'work_total'=>$goods['base_service_fee'],
+                    'work_amount'=>$goods['base_service_fee']
+                ];
+                $service_work = ServiceWork::create($work_data);
+
+                //生成服务订单
+                $data = [
+                    'work_id'=> $service_work['id'],
+                    'sn' => generate_sn(RechargeOrder::class, 'sn'),
+                    'order_type'=>0,//服务订单
+                    'order_terminal' => 1,
+                    'payment_type'=>$goods['goods_payment_type']==GoodsEnum::ISGOODS_PAYMENT_TYPE?0:1,
+                    'user_id' => $user['id'],
+                    'pay_status' => PayEnum::ISPAID,
+                    'coupon_id'=>0,
+                    'coupon_price'=>0,
+                    'pay_way' => 4,
+                    'order_total' => $message['amount'],
+                    'order_amount' => $message['amount'],
+                ];
+                $order = RechargeOrder::create($data);
+                //生成订单服务详情
+                OrderGoods::create([
+                    'sn' => $order['sn'],
+                    'goods_id' => $goods['id'],
+                    'category_type' => $goods['category_type'],
+                    'goods_category_ids' => $goods['goods_category_ids'],
+                    'goods_category_id' => $goods['goods_category_id'],
+                    'goods_name' => $goods['goods_name'],
+                    'goods_image' => $goods['goods_image'],
+                    'goods_video' => $goods['goods_video'],
+                    'goods_number' => $goods['goods_number'],
+                    'good_unit' => $goods['good_unit'],
+                    'goods_size' => $goods['goods_size'],
+                    'goods_type' => $goods['goods_type'],
+                    'goods_brand' => $goods['goods_brand'],
+                    'install_guide' => $goods['install_guide'],
+                    'goods_payment_type'=>$goods['goods_payment_type'],
+                    'base_service_fee' => $goods['base_service_fee'],
+                    'service_total' => $goods['service_total'],
+                    'service_fee' => $message['amount'],
+                    'service_image' => $goods['service_image'],
+                    'warranty_period'=>$goods['warranty_period'],
+                    'fee_schedule' => $goods['fee_schedule'],
+                    'goods_status' => $goods['goods_status'],
+                ]);
+
+                //判断是否是一口价预支付订单,支付成功后生成尾款订单
+                $order_goods = OrderGoods::where('sn',$order->sn)->findOrEmpty();
+                if(!$goods->isEmpty() and $goods->goods_payment_type == 3 and bcsub($goods->base_service_fee,$goods->service_fee,2)>=0){
+                    //新增待支付尾款
+                    $order_weikuan_data = [
+                        'order_type'=>$order['order_type'],
+                        'sn'=>generate_sn(\app\common\model\orders\RechargeOrder::class, 'sn'),
+                        'work_id'=>$order['work_id'],
+                        'user_id'=>$order['user_id'],
+                        'payment_type'=>2,
+                        'order_total'=>bcsub($goods->base_service_fee,$message['amount'],2),
+                        'order_amount'=>bcsub($goods->base_service_fee,$message['amount'],2),
+                        'order_terminal'=>$order['order_terminal']
+                    ];
+                    \app\common\model\recharge\RechargeOrder::create($order_weikuan_data);
+                }
+
+                $thirdOrders->work_id = $service_work->id;
+                $thirdOrders->save();
+                Db::commit();
+            } catch (\Exception $e) {
+                Db::rollback();
+                self::setError($e->getMessage());
+                return false;
+            }
+        }
+        return true;
+    }
+
+
+    //手动添加订单
+    public static function submitOrders(array $message)
+    {
+        //查询用户是否存在
+        $userName = $message['userName'];
+        $mobile = $message['mobile'];
+        $user = User::where(['mobile' =>$mobile])->findOrEmpty();
+        if($user->isEmpty()){
+            $user = LoginLogic::register([
+                'password'=>'123456',
+                'account'=>$userName,
+                'mobile'=>$mobile,
+                'channel'=>1,
+                'user_type'=>0,
+            ]);
+        }
+        $goods = Goods::findOrEmpty($message['goods_id']);
+        $pattern = '/(?:省|市)([\w\s]+市)/u';
+        Db::startTrans();
+        try {
+            if (preg_match($pattern, $message['userAddress'], $matches)) {
+                $city = $matches[1];
+            }
+            $area_city = PostageRegion::where(['level'=>2,'name'=>$city])->value('id');
+
+            //生成服务工单
+            $work_data = [
+                'work_sn' => generate_sn(ServiceWork::class, 'work_sn'),
+                'real_name' => $userName,
+                'mobile' => $mobile,
+                'address' => $message['userAddress'],
+                'province' => 0,
+                'city' => $area_city??0,
+                'area_name' => $area_city??'',
+                'title' => $goods->goods_name,
+                'category_type' => $goods['category_type'],
+                'goods_category_ids' => $goods['goods_category_ids'],
+                'goods_category_id' => $goods['goods_category_id'],
+                'base_service_fee' => $goods['base_service_fee'],
+                'service_fee' => $message['amount'],
+                'work_pay_status'=>WorkEnum::IS_PAY_STATUS,
+                'appointment_time' => strtotime($message['appointment_time']),
+                'user_id'=>$user['id'],
+                'lon'=>$message['lon'],
+                'lat'=>$message['lat'],
+                'property_activity_id'=>0,
+                'user_equity_id'=>0,
+//                'third_type'=>1,
+                'work_total'=>$goods['base_service_fee'],
+                'work_amount'=>$goods['base_service_fee']
+            ];
+            $service_work = ServiceWork::create($work_data);
+
+            //生成服务订单
+            $data = [
+                'work_id'=> $service_work['id'],
+                'sn' => generate_sn(RechargeOrder::class, 'sn'),
+                'order_type'=>0,//服务订单
+                'order_terminal' => 1,
+                'payment_type'=>$goods['goods_payment_type']==GoodsEnum::ISGOODS_PAYMENT_TYPE?0:1,
+                'user_id' => $user['id'],
+                'pay_status' => PayEnum::ISPAID,
+                'coupon_id'=>0,
+                'coupon_price'=>0,
+                'pay_way' => 4,
+                'order_total' => $message['amount'],
+                'order_amount' => $message['amount'],
+            ];
+            $order = RechargeOrder::create($data);
+            //生成订单服务详情
+            OrderGoods::create([
+                'sn' => $order['sn'],
+                'goods_id' => $goods['id'],
+                'category_type' => $goods['category_type'],
+                'goods_category_ids' => $goods['goods_category_ids'],
+                'goods_category_id' => $goods['goods_category_id'],
+                'goods_name' => $goods['goods_name'],
+                'goods_image' => $goods['goods_image'],
+                'goods_video' => $goods['goods_video'],
+                'goods_number' => $goods['goods_number'],
+                'good_unit' => $goods['good_unit'],
+                'goods_size' => $goods['goods_size'],
+                'goods_type' => $goods['goods_type'],
+                'goods_brand' => $goods['goods_brand'],
+                'install_guide' => $goods['install_guide'],
+                'goods_payment_type'=>$goods['goods_payment_type'],
+                'base_service_fee' => $goods['base_service_fee'],
+                'service_total' => $goods['service_total'],
+                'service_fee' => $message['amount'],
+                'service_image' => $goods['service_image'],
+                'warranty_period'=>$goods['warranty_period'],
+                'fee_schedule' => $goods['fee_schedule'],
+                'goods_status' => $goods['goods_status'],
+            ]);
+
+            //判断是否是一口价预支付订单,支付成功后生成尾款订单
+            if(!$goods->isEmpty() and $goods->goods_payment_type == 3 and bcsub($goods->base_service_fee,$goods->service_fee,2)>=0){
+                //新增待支付尾款
+                $order_weikuan_data = [
+                    'order_type'=>$order['order_type'],
+                    'sn'=>generate_sn(\app\common\model\orders\RechargeOrder::class, 'sn'),
+                    'work_id'=>$order['work_id'],
+                    'user_id'=>$order['user_id'],
+                    'payment_type'=>2,
+                    'order_total'=>bcsub($goods->base_service_fee,$message['amount'],2),
+                    'order_amount'=>bcsub($goods->base_service_fee,$message['amount'],2),
+                    'order_terminal'=>$order['order_terminal']
+                ];
+                \app\common\model\recharge\RechargeOrder::create($order_weikuan_data);
+            }
+            Db::commit();
+        } catch (\Exception $e) {
+            Db::rollback();
+            self::setError($e->getMessage());
+            return false;
+        }
+        return true;
+    }
+
+}