Explorar el Código

下单接口优化

wufeng hace 2 años
padre
commit
7fe214aff4

+ 1 - 15
app/mobile/controller/Order.php

@@ -4,25 +4,11 @@ namespace app\mobile\controller;
 
 use app\BaseController;
 use app\mobile\logic\OrderLogic;
-use think\exception\ValidateException;
-use think\facade\Config;
-use think\facade\Validate;
+
 
 //【订单】
 class Order extends BaseController
 {
-    //兑换商品下单
-    public function add(){
-
-        $param = $this->request->only(['good_id','addr_id','num','remark'],'post');
-
-        $val = Validate::rule(Config::get('validate_rules.ExchangeOrderAdd'));
-
-        if(!$val->check($param)) throw new ValidateException($val->getError());
-
-        return OrderLogic::add($param);
-
-    }
 
     //订单列表
     public function list(){

+ 6 - 5
app/mobile/controller/Pay.php

@@ -5,19 +5,20 @@ namespace app\mobile\controller;
 use app\BaseController;
 use app\mobile\logic\PayLogic;
 use app\model\CommonModel;
+use app\model\MasterOrderModel;
 use think\exception\ValidateException;
 use think\facade\Validate;
 
 //支付相关
 class Pay extends BaseController
 {
-    //微信支付预下单
+    //预下单
     public function getPrepayId()
     {
         $param = $this->request->only(['type', 'list', 'addr_id'], 'post');
 
         $val = Validate::rule([
-            'type|购买类型' => 'require|number|in:' . CommonModel::$pay_type_service . ',' . CommonModel::$pay_type_shopping_good,
+            'type|购买类型' => 'require|number|in:' . MasterOrderModel::$type_exchange_good . ',' . MasterOrderModel::$type_shopping_good . ',' . MasterOrderModel::$type_service,
             'list|购买列表' => 'require|array|max:100',
             'addr_id|收货地址' => 'require|number|gt:0'
         ]);
@@ -45,11 +46,11 @@ class Pay extends BaseController
     public function checkPayResult()
     {
 
-        $param = $this->request->only(['pay_code'],'post');
+        $param = $this->request->only(['pay_code'], 'post');
 
-        $val = Validate::rule(['pay_code|支付编码'=>'require|length:18']);
+        $val = Validate::rule(['pay_code|支付编码' => 'require|length:18']);
 
-        if(!$val->check($param)) throw new ValidateException($val->getError());
+        if (!$val->check($param)) throw new ValidateException($val->getError());
 
         return PayLogic::checkPayResult($param['pay_code']);
 

+ 0 - 94
app/mobile/logic/OrderLogic.php

@@ -1,108 +1,14 @@
 <?php
 
-
 namespace app\mobile\logic;
 
-
-use app\model\AddrModel;
 use app\model\CommonModel;
-use app\model\CompanyGoodModel;
-use app\model\GoodModel;
-use app\model\InventoryExchangeModel;
-use app\model\MasterOrderModel;
 use app\model\OrderModel;
-use think\facade\Db;
 use think\response\Json;
 
 class OrderLogic extends BaseLogic
 {
 
-    //兑换商品下单
-    public static function add(array $data = []): Json
-    {
-        Db::startTrans();
-
-        try {
-
-            //判断收货地址
-
-            $rs = AddrModel::field('id')
-                ->where(['is_del' => CommonModel::$del_normal, 'id' => $data['addr_id'], 'uid' => self::$aid])
-                ->findOrEmpty()
-                ->isEmpty();
-            if ($rs) throw new Exception('该地址不存在');
-
-
-            //判断商品
-            $rs = CompanyGoodModel::alias('a')
-                ->field('a.id,a.good_id,b.good_code,b.good_name,b.status,b.moq,c.id inventory_id,c.inventory')
-                ->leftJoin('good b', 'b.id=a.good_id AND b.is_del=' . CommonModel::$del_normal)
-                ->leftJoin('inventory_exchange c', 'c.account_id=' . self::$aid . ' AND c.good_id=a.good_id')
-                ->where(['a.is_del' => CommonModel::$del_normal, 'a.group_id' => self::$group_id, 'a.good_id' => $data['good_id']])
-                ->findOrEmpty();
-
-            if ($rs->isEmpty()) throw new Exception('该商品不存在或不允许兑换');
-            if ($rs->status != CommonModel::$status_normal) throw new Exception('该商品已被禁用');
-            if ($rs->moq > $data['num']) throw new Exception('不满足该商品的起订量,' . $rs->moq);
-            if ($rs->inventory < $data['num']) throw new Exception('库存数不足');
-
-            $date = date('Y-m-d H:i:s');
-
-            $after_inventory = bcsub($rs->inventory, $data['num']);
-
-            //添加库存变动记录
-            Db::name('inventory_exchange_log')
-                ->insert([
-                    'inventory_exchange_id' => $rs->inventory_id,
-                    'before_inventory' => $rs->inventory,
-                    'after_inventory' => $after_inventory,
-                    'flag' => '-1',
-                    'good_id' => $rs->good_id,
-                    'good_code' => $rs->good_code,
-                    'good_name' => $rs->good_name,
-                    'source' => CommonModel::$source_account,
-                    'createrid' => self::$aid,
-                    'creater' => self::$aname,
-                    'addtime' => $date
-                ]);
-
-            //扣减库存
-            InventoryExchangeModel::where('id', $rs->inventory_id)
-                ->save(['inventory' => $after_inventory, 'updaterid' => self::$aid, 'updater' => self::$aname, 'updatetime' => $date]);
-
-            //生成兑换订单
-            OrderModel::create([
-                'orderCode' => make_no('QR'),
-                'company_id' => self::$company_id,
-                'card_id' => self::$card_id,
-                'uid' => self::$aid,
-                'good_id' => $rs->good_id,
-                'num' => $data['num'],
-                'price' => 0,
-                'total_price' => 0,
-                'addr_id' => $data['addr_id'],
-                'type' => GoodModel::$type_exchange,
-                'remark' => $data['remark'],
-                'status' => MasterOrderModel::$order_status_not_deliver,
-                'is_del' => CommonModel::$del_normal,
-                'creater' => self::$aname,
-                'createrid' => self::$aid,
-                'addtime' => $date,
-                'updater' => self::$aname,
-                'updaterid' => self::$aid,
-                'updatetime' => $date,
-            ])->save();
-
-            Db::commit();
-
-            return json_show(CommonModel::$success, '兑换商品成功');
-        } catch (Exception $exception) {
-            Db::rollback();
-            return json_show(CommonModel::$error_param, '兑换失败,' . $exception->getMessage());
-        }
-
-    }
-
     //订单列表
     public static function list(array $data = []): Json
     {

+ 210 - 82
app/mobile/logic/PayLogic.php

@@ -6,22 +6,20 @@ use app\model\AccountModel;
 use app\model\AddrModel;
 use app\model\CommonModel;
 use app\model\GoodModel;
+use app\model\InventoryExchangeModel;
 use app\model\MasterOrderModel;
 use app\model\PayInfoModel;
 use app\model\ServiceModel;
+use think\Exception;
 use think\facade\Db;
 use think\response\Json;
 
 class PayLogic extends BaseLogic
 {
 
-    //微信支付预下单
+    //下单
     public static function getPrepayId(array $data = []): json
     {
-        $openId = AccountModel::where(['id' => self::$aid, 'is_del' => CommonModel::$del_normal])
-            ->value('wx_openId', '');
-
-        if ($openId == '') return json_show(CommonModel::$error_token, '获取账户openId失败,请重新登录');
 
         Db::startTrans();
 
@@ -37,75 +35,37 @@ class PayLogic extends BaseLogic
             $num = array_column($data['list'], 'num', 'id');//
             $date = date('Y-m-d H:i:s');
 
-            $pay_code = make_no('ZF');
+            $masterOrderCode = make_no('MS');
 
-            //支付信息表
-            $pay_info_id = Db::name('pay_info')->insertGetId([
-                'pay_code' => $pay_code,
-                'wx_openId' => $openId,
+            //主订单数据
+            $insert_master_order = [
+                'masterOrderCode' => $masterOrderCode,
                 'uid' => self::$aid,
                 'type' => $data['type'],
-                'ids' => json_encode($data['list']),
-                'status' => MasterOrderModel::$order_status_wait_pay,
                 'createrid' => self::$aid,
                 'creater' => self::$aname,
                 'addtime' => $date,
                 'updaterid' => self::$aid,
                 'updater' => self::$aname,
                 'updatetime' => $date,
-            ]);
+            ];
 
+            //子订单数据
+            $insert_order = [];
 
-            if ($data['type'] == CommonModel::$pay_type_service) {
-                //购买服务
-                $rs = ServiceModel::field('id,original_price,activity_price,title,activity_status')
-                    ->whereIn('id', array_column($data['list'], 'id'))
-                    ->where(['is_del' => CommonModel::$del_normal, 'status' => CommonModel::$status_normal])
-                    ->select()
-                    ->toArray();
-                if (empty($rs)) throw new Exception('购买的服务不存在或已下架');
+            //库存变动记录数据
+            $insert_inventory_exchange_log = [];
 
-                $insert_service = [];
-                foreach ($rs as $item) {
-                    //单价
-                    if ($item['activity_status'] == ServiceModel::$activity_status_ing) $price = $rs['activity_price'];
-                    else $price = $rs['original_price'];
 
-                    $total_price = bcmul($price, $num[$item['id']] ?? 0, 2);//总价
-                    $amount = bcadd($amount, $total_price, 2);//总金额
+            //兑换商品
+            if ($data['type'] == MasterOrderModel::$type_exchange_good) {
 
-                    $insert_service[] = [
-                        'orderCode' => make_no('FW'),
-                        'pay_info_id' => $pay_info_id,
-                        'uid' => self::$aid,
-                        'service_id' => $item['id'],
-                        'num' => $num[$item['id']] ?? 0,
-                        'addr_id' => $data['addr_id'],
-                        'price' => $price,
-                        'total_price' => $total_price,
-                        'status' => MasterOrderModel::$order_status_wait_pay,
-                        'is_del' => CommonModel::$del_normal,
-                        'createrid' => self::$aid,
-                        'creater' => self::$aname,
-                        'addtime' => $date,
-                        'updaterid' => self::$aid,
-                        'updater' => self::$aname,
-                        'updatetime' => $date,
-                    ];
-
-                }
-
-                //向服务订单表写数据
-                Db::name('order_service')->insertAll($insert_service);
+                $rs = GoodModel::alias('a')
+                    ->leftJoin('inventory_exchange c', 'c.account_id=' . self::$aid . ' AND c.good_id=a.id')
+                    ->whereIn('a.id', array_column($data['list'], 'id'))
+                    ->where(['a.is_del' => CommonModel::$del_normal, 'a.status' => CommonModel::$status_normal, 'type' => GoodModel::$type_exchange])
+                    ->column('a.id,a.good_code,a.good_name,a.moq,c.inventory,c.id inventory_id', 'a.id');
 
-            } else {
-                //购买商城商品
-                $order_insert = [];
-                $rs = GoodModel::field('id,price,good_name')
-                    ->whereIn('id', array_column($data['list'], 'id'))
-                    ->where(['is_del' => CommonModel::$del_normal, 'status' => CommonModel::$status_normal])
-                    ->select()
-                    ->toArray();
                 if (empty($rs)) throw new Exception('购买的商品不存在或已禁用');
 
                 $i = 0;
@@ -113,20 +73,22 @@ class PayLogic extends BaseLogic
                 $orderCode = substr($orderCode, 0, -2) . str_pad($i, 2, '0', STR_PAD_LEFT);
                 foreach ($rs as $item) {
 
-                    $total_price = bcmul($item['price'], $num[$item['id']] ?? 0, 2);//总价
-                    $order_insert[] = [
+                    if ($item['moq'] > $num[$item['id']]) throw new Exception($item['good_name'] . '不满足起订量,' . $item['moq']);
+                    if ($item['inventory'] < $num[$item['id']]) throw new Exception($item['good_name'] . '的库存数不足');
+
+                    $insert_order[] = [
+                        'masterOrderCode' => $masterOrderCode,
                         'orderCode' => $orderCode,
-                        'pay_info_id' => $pay_info_id,
                         'company_id' => self::$company_id,
                         'card_id' => self::$card_id,
                         'uid' => self::$aid,
-                        'good_id' => $item['id'],
+                        'child_id' => $item['id'],
                         'num' => $num[$item['id']],
-                        'price' => $item['price'],
-                        'total_price' => $total_price,
+                        'price' => 0,
+                        'total_price' => 0,
                         'addr_id' => $data['addr_id'],
-                        'type' => MasterOrderModel::$pay_type_shopping_good,
-                        'status' => MasterOrderModel::$order_status_wait_pay,
+                        'type' => $data['type'],
+                        'status' => MasterOrderModel::$order_status_not_deliver,
                         'is_del' => CommonModel::$del_normal,
                         'createrid' => self::$aid,
                         'creater' => self::$aname,
@@ -135,38 +97,204 @@ class PayLogic extends BaseLogic
                         'updater' => self::$aname,
                         'updatetime' => $date,
                     ];
-                    $amount = bcadd($amount, $total_price, 2);
+
+                    //扣减库存
+                    $after_inventory = bcsub($item['inventory'], $num[$item['id']]);
+                    //添加库存变动记录
+                    $insert_inventory_exchange_log[] = [
+                        'inventory_exchange_id' => $item['inventory_id'],
+                        'before_inventory' => $item['inventory'],
+                        'after_inventory' => $after_inventory,
+                        'flag' => '-1',
+                        'good_id' => $item['id'],
+                        'good_code' => $item['good_code'],
+                        'good_name' => $item['good_name'],
+                        'source' => CommonModel::$source_account,
+                        'createrid' => self::$aid,
+                        'creater' => self::$aname,
+                        'addtime' => $date
+                    ];
+
+                    //扣减库存
+                    InventoryExchangeModel::where('id', $item['inventory_id'])
+                        ->save(['inventory' => $after_inventory, 'updaterid' => self::$aid, 'updater' => self::$aname, 'updatetime' => $date]);
 
                     $i++;
                 }
 
+                //更新主订单表数据
+                $insert_master_order = array_merge($insert_master_order, [
+                    'wx_openId' => '',
+                    'prepay_info' => '',
+                    'amount' => $amount,
+                    'transaction_id' => '',
+                    'trade_type' => '',
+                    'notify_result' => '',
+                    'expiretime' => $date,
+                    'status' => MasterOrderModel::$order_status_not_deliver,
+                ]);
 
-            }
+                $res = ['is_need_pay' => 0, 'result' => ''];
 
-            //获取预支付信息,
-            //@todo body要写什么????????
-            $result = WechatLogic::getPrepayId($openId, '', $pay_code, $amount);
+            } else {
 
+                $openId = AccountModel::where(['id' => self::$aid, 'is_del' => CommonModel::$del_normal])
+                    ->value('wx_openId', '');
 
-            //更新pay_info表
-            Db::name('pay_info')
-                ->where('id', $pay_info_id)
-                ->update([
-                    'prepay_info' => $result,
+                if ($openId == '') return json_show(CommonModel::$error_token, '获取账户openId失败,请重新登录');
+
+                switch ($data['type']) {
+
+                    //服务订单
+                    case MasterOrderModel::$type_service:
+                        $rs = ServiceModel::field('id,original_price,activity_price,title,activity_status')
+                            ->whereIn('id', array_column($data['list'], 'id'))
+                            ->where(['is_del' => CommonModel::$del_normal, 'status' => CommonModel::$status_normal])
+                            ->select()
+                            ->toArray();
+                        if (empty($rs)) throw new Exception('购买的服务不存在或已下架');
+
+                        $i = 0;
+                        $orderCode = make_no('QR');
+                        $orderCode = substr($orderCode, 0, -2) . str_pad($i, 2, '0', STR_PAD_LEFT);
+                        foreach ($rs as $item) {
+                            //单价
+                            if ($item['activity_status'] == ServiceModel::$activity_status_ing) $price = $rs['activity_price'];
+                            else $price = $rs['original_price'];
+
+                            $total_price = bcmul($price, $num[$item['id']] ?? 0, 2);//总价
+                            $amount = bcadd($amount, $total_price, 2);//总金额
+
+                            $insert_order[] = [
+                                'masterOrderCode' => $masterOrderCode,
+                                'orderCode' => $orderCode,
+                                'company_id' => self::$company_id,
+                                'card_id' => self::$card_id,
+                                'uid' => self::$aid,
+                                'child_id' => $item['id'],
+                                'num' => $num[$item['id']] ?? 0,
+                                'price' => $price,
+                                'total_price' => $total_price,
+                                'addr_id' => $data['addr_id'],
+                                'type' => $data['type'],
+                                'status' => MasterOrderModel::$order_status_wait_pay,
+                                'is_del' => CommonModel::$del_normal,
+                                'createrid' => self::$aid,
+                                'creater' => self::$aname,
+                                'addtime' => $date,
+                                'updaterid' => self::$aid,
+                                'updater' => self::$aname,
+                                'updatetime' => $date,
+                            ];
+                            $i++;
+                        }
+
+
+                        break;
+
+                    //商城商品订单
+                    case MasterOrderModel::$type_shopping_good:
+                        $rs = GoodModel::alias('a')
+                            ->leftJoin('inventory_exchange c', 'c.account_id=' . self::$aid . ' AND c.good_id=a.id')
+                            ->whereIn('a.id', array_column($data['list'], 'id'))
+                            ->where(['a.is_del' => CommonModel::$del_normal, 'a.status' => CommonModel::$status_normal, 'type' => GoodModel::$type_shopping])
+                            ->column('a.id,a.good_code,a.good_name,a.moq,a.price,c.inventory,c.id inventory_id', 'a.id');
+
+                        if (empty($rs)) throw new Exception('购买的商品不存在或已禁用');
+
+                        $i = 0;
+                        $orderCode = make_no('QR');
+                        $orderCode = substr($orderCode, 0, -2) . str_pad($i, 2, '0', STR_PAD_LEFT);
+                        foreach ($rs as $item) {
+
+                            if ($item['moq'] > $num[$item['id']]) throw new Exception('不满足该商品的起订量,' . $item['moq']);
+                            if ($item['inventory'] < $num[$item['id']]) throw new Exception('库存数不足');
+
+                            $total_price = bcmul($rs['price'], $num[$item['id']] ?? 0, 2);//总价
+                            $amount = bcadd($amount, $total_price, 2);//总金额
+
+                            $order_insert[] = [
+                                'masterOrderCode' => $masterOrderCode,
+                                'orderCode' => $orderCode,
+                                'company_id' => self::$company_id,
+                                'card_id' => self::$card_id,
+                                'uid' => self::$aid,
+                                'child_id' => $item['id'],
+                                'num' => $num[$item['id']],
+                                'price' => $rs['price'],
+                                'total_price' => $total_price,
+                                'addr_id' => $data['addr_id'],
+                                'type' => $data['type'],
+                                'status' => MasterOrderModel::$order_status_wait_pay,
+                                'is_del' => CommonModel::$del_normal,
+                                'createrid' => self::$aid,
+                                'creater' => self::$aname,
+                                'addtime' => $date,
+                                'updaterid' => self::$aid,
+                                'updater' => self::$aname,
+                                'updatetime' => $date,
+                            ];
+
+
+                            //扣减库存
+                            $after_inventory = bcsub($item['inventory'], $num[$item['id']]);
+                            //添加库存变动记录
+                            $insert_inventory_exchange_log[] = [
+                                'inventory_exchange_id' => $item['inventory_id'],
+                                'before_inventory' => $item['inventory'],
+                                'after_inventory' => $after_inventory,
+                                'flag' => '-1',
+                                'good_id' => $item['id'],
+                                'good_code' => $item['good_code'],
+                                'good_name' => $item['good_name'],
+                                'source' => CommonModel::$source_account,
+                                'createrid' => self::$aid,
+                                'creater' => self::$aname,
+                                'addtime' => $date
+                            ];
+
+                            //扣减库存
+                            InventoryExchangeModel::where('id', $item['inventory_id'])
+                                ->save(['inventory' => $after_inventory, 'updaterid' => self::$aid, 'updater' => self::$aname, 'updatetime' => $date]);
+
+                            $i++;
+                        }
+
+                        break;
+                }
+
+                //获取预支付信息,
+                //@todo body要写什么????????
+                $result = WechatLogic::getPrepayId($openId, '', $masterOrderCode, $amount);
+
+
+                //更新主订单表数据
+                $insert_master_order = array_merge($insert_master_order, [
+                    'wx_openId' => $openId,
+                    'prepay_info' => '',//预支付信息
                     'amount' => $amount,
-                    'expiretime' => date('Y-m-d H:i:s', time() + 5 * 60),
+                    'transaction_id' => '',
+                    'trade_type' => '',
+                    'notify_result' => '',
+                    'expiretime' => $date,
+                    'status' => MasterOrderModel::$order_status_wait_pay,
                 ]);
 
+                $res = ['is_need_pay' => 1, 'result' => $result];
+
+            }
 
-            //@todo 要给前端返回什么信息?
-            $res = ['pay_code' => $pay_code];
 
+            //处理数据
+            MasterOrderModel::create($insert_master_order)->save();
+            if ($insert_inventory_exchange_log) Db::name('inventory_exchange_log')->insertAll($insert_inventory_exchange_log);
+            if ($insert_order) Db::name('order')->insertAll($insert_order);
 
             Db::commit();
-            return json_show(CommonModel::$success, '微信支付预下单成功', $res);
+            return json_show(CommonModel::$success, '下单成功', $res);
         } catch (Exception $exception) {
             Db::rollback();
-            return json_show(CommonModel::$success, '微信支付预下单失败' . $exception->getMessage());
+            return json_show(CommonModel::$success, '下单失败,' . $exception->getMessage());
         }
 
 

+ 1 - 1
app/model/MasterOrderModel.php

@@ -8,7 +8,7 @@ use think\Model;
 class MasterOrderModel extends Model
 {
 
-    protected $table='master_order';
+    protected $table='fc_master_order';
     protected $pk='id';
 
     //订单类型,1兑换商品,2商城商品,3服务