Browse Source

预下单接口优化,订单状态优化

wufeng 2 years ago
parent
commit
2faa6bc592

+ 10 - 4
app/admin/logic/OrderLogic.php

@@ -90,7 +90,13 @@ class OrderLogic extends BaseLogic
         if (($data['start_date'] != '') && ($data['end_date'] != '')) $db->whereBetween('a.addtime', [$data['start_date'], $data['end_date']]);
         if ($data['type'] != '') $db->where('a.type', $data['type']);
 
-        $status = [CommonModel::$status_not_deliver => '待发货', CommonModel::$status_deliver => '已发货', CommonModel::$status_receipt => '已收货'];
+        $status = [
+            CommonModel::$order_status_wait_pay => '待支付',
+            CommonModel::$order_status_pay_fail => '支付失败',
+            CommonModel::$order_status_not_deliver => '待发货',
+            CommonModel::$order_status_deliver => '已发货',
+            CommonModel::$order_status_receipt => '已收货',
+        ];
         $type = [GoodModel::$type_shopping => '商城', GoodModel::$type_exchange => '兑换'];
         $list = $db
             ->field('a.id 订单ID,a.orderCode 订单编号,a.type,b.title 业务公司,c.title 卡类型,d.name 购买用户名,d.username 购买账号,a.num 购买数量,e.contactor 收货联系人,e.mobile 收货联系电话,e.addr_code,a.status 状态,a.post_name 物流公司,a.post_code 物流单号,a.addtime 创建时间,e.addr,f.good_code 商品编码,f.good_name 商品名称')
@@ -126,7 +132,7 @@ class OrderLogic extends BaseLogic
         $temp = $db
             ->field('id,orderCode')
             ->where(['is_del' => CommonModel::$del_normal])
-            ->where('status', '<>', CommonModel::$status_not_deliver)
+            ->where('status', '<>', CommonModel::$order_status_not_deliver)
             ->whereIn('orderCode', array_column($list, 'orderCode'))
             ->findOrEmpty();
 
@@ -150,9 +156,9 @@ class OrderLogic extends BaseLogic
                 $db->where([
                     'is_del' => CommonModel::$del_normal,
                     'orderCode' => $order['orderCode'],
-                    'status' => CommonModel::$status_not_deliver,
+                    'status' => CommonModel::$order_status_not_deliver,
                 ])->save([
-                    'status' => CommonModel::$status_deliver,
+                    'status' => CommonModel::$order_status_deliver,
                     'post_name' => $order['post_name'],
                     'post_code' => $order['post_code'],
                     'updater' => self::$uname,

+ 10 - 4
app/admin/logic/OrderServiceLogic.php

@@ -98,7 +98,13 @@ class OrderServiceLogic extends BaseLogic
             ->order('a.addtime desc')
             ->cursor();
 
-        $status = [CommonModel::$status_not_deliver => '待发货', CommonModel::$status_deliver => '已发货', CommonModel::$status_receipt => '已收货'];
+        $status =  $status = [
+            CommonModel::$order_status_wait_pay => '待支付',
+            CommonModel::$order_status_pay_fail => '支付失败',
+            CommonModel::$order_status_not_deliver => '待发货',
+            CommonModel::$order_status_deliver => '已发货',
+            CommonModel::$order_status_receipt => '已收货',
+        ];
 
         $da = [];
         foreach ($list as $v) {
@@ -128,7 +134,7 @@ class OrderServiceLogic extends BaseLogic
         $temp = $db
             ->field('id,orderCode')
             ->where(['is_del' => CommonModel::$del_normal])
-            ->where('status', '<>', CommonModel::$status_not_deliver)
+            ->where('status', '<>', CommonModel::$order_status_not_deliver)
             ->whereIn('orderCode', array_column($list, 'orderCode'))
             ->findOrEmpty();
 
@@ -152,9 +158,9 @@ class OrderServiceLogic extends BaseLogic
                 $db->where([
                     'is_del' => CommonModel::$del_normal,
                     'orderCode' => $order['orderCode'],
-                    'status' => CommonModel::$status_not_deliver,
+                    'status' => CommonModel::$order_status_not_deliver,
                 ])->save([
-                    'status' => CommonModel::$status_deliver,
+                    'status' => CommonModel::$order_status_deliver,
                     'post_name' => $order['post_name'],
                     'post_code' => $order['post_code'],
                     'updater' => self::$uname,

+ 10 - 9
app/mobile/controller/Common.php

@@ -36,23 +36,24 @@ class Common extends BaseController
     }
 
     //微信支付预下单
-    public function getPrepayId(){
-
-        $param = $this->request->only(['type','list'],'post');
+    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,
-            'list|购买列表'=>'require|array|max:100',
+            'type|购买类型' => 'require|number|in:' . CommonModel::$pay_type_service . ',' . CommonModel::$pay_type_shopping_good,
+            'list|购买列表' => 'require|array|max:100',
+            'addr_id|收货地址' => 'require|number|gt:0'
         ]);
         if (!$val->check($param)) throw new ValidateException($val->getError());
 
         $val2 = Validate::rule([
-            'id'=>'require|number|gt:0',
-            'num|购买数量'=>'require|number|max:99999999',
+            'id' => 'require|number|gt:0',
+            'num|购买数量' => 'require|number|max:99999999',
         ]);
 
-        foreach ($param['list'] as $list){
-            if(!$val2->check($list)) throw new ValidateException($val2->getError());
+        foreach ($param['list'] as $list) {
+            if (!$val2->check($list)) throw new ValidateException($val2->getError());
         }
 
         return CommonLogic::getPrepayId($param);

+ 94 - 23
app/mobile/logic/CommonLogic.php

@@ -6,6 +6,7 @@ use app\model\AccountModel;
 use app\model\CommonModel;
 use app\model\GoodModel;
 use app\model\GroupModel;
+use app\model\OrderModel;
 use app\model\PayInfoModel;
 use app\model\ServiceModel;
 use app\model\ThemeModel;
@@ -86,6 +87,24 @@ class CommonLogic extends BaseLogic
             $num = array_column($data['list'], 'num', 'id');//
             $date = date('Y-m-d H:i:s');
 
+            $code = make_no('ZF');
+
+            //支付信息表
+            $pay_info_id = Db::name('pay_info')->insertGetId([
+                'code' => $code,
+                'wx_openId' => $openId,
+                'type' => $data['type'],
+                'ids' => json_encode($data['list']),
+                'status' => CommonModel::$order_status_wait_pay,
+                'createrid' => self::$aid,
+                'creater' => self::$aname,
+                'addtime' => $date,
+                'updaterid' => self::$aid,
+                'updater' => self::$aname,
+                'updatetime' => $date,
+            ]);
+
+
             if ($data['type'] == CommonModel::$pay_type_service) {
                 //购买服务
                 $rs = ServiceModel::field('id,original_price,activity_price,title,activity_status')
@@ -95,12 +114,39 @@ class CommonLogic extends BaseLogic
                     ->toArray();
                 if (empty($rs)) throw new Exception('购买的服务不存在或已下架');
 
+                $insert_service = [];
                 foreach ($rs as $item) {
-                    if ($item['activity_status'] == ServiceModel::$activity_status_ing) $amount = bcadd($amount, bcmul($rs['activity_price'], $num[$item['id']] ?? 0), 2);
-                    else $amount = bcadd($amount, bcmul($rs['original_price'], $num[$item['id']] ?? 0), 2);
+                    if ($item['activity_status'] == ServiceModel::$activity_status_ing) $total = bcmul($rs['activity_price'], $num[$item['id']] ?? 0, 2);
+                    else $total = bcmul($rs['original_price'], $num[$item['id']] ?? 0, 2);
+
+                    $amount = bcadd($amount, $total, 2);//总金额
+
+                    $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'],
+                        'amount' => $total,
+                        'status' => CommonModel::$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);
+
             } 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])
@@ -108,34 +154,59 @@ class CommonLogic extends BaseLogic
                     ->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) {
-                    $amount = bcadd($amount, bcmul($rs['price'], $num[$item['id']] ?? 0), 2);
+
+                    $total_price = bcmul($item['price'], $num[$item['id']] ?? 0, 2);//总价
+                    $order_insert[] = [
+                        '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'],
+                        'num' => $num[$item['id']],
+                        'price' => $item['price'],
+                        'total_price' => $total_price,
+                        'addr_id' => $data['addr_id'],
+                        'type' => CommonModel::$pay_type_shopping_good,
+                        'status' => CommonModel::$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,
+                    ];
+                    $amount = bcadd($amount, $total_price, 2);
+
+                    $i++;
                 }
 
-            }
 
-            $code = make_no('ZF');
+            }
 
-            //获取预支付信息
+            //获取预支付信息,
+            //@todo body要写什么????????
             $result = WechatLogic::getPrepayId($openId, '', $code, $amount);
 
-            PayInfoModel::create([
-                'code' => $code,
-                'wx_openId' => $openId,
-                'type' => $data['type'],
-                'ids' => json_encode($data['list']),
-                'prepay_info' => $result,
-                'amount' => $amount,
-                '' => '',//流水号
-                'expiretime' => date('Y-m-d H:i:s', time() + 5 * 60),
-                'status' => PayInfoModel::$pay_wait,
-                'createrid' => self::$aid,
-                'creater' => self::$aname,
-                'addtime' => $date,
-                'updaterid' => self::$aid,
-                'updater' => self::$aname,
-                'updatetime' => $date,
-            ])->save();
+
+            //更新pay_info表
+            Db::name('pay_info')
+                ->where('id', $pay_info_id)
+                ->update([
+                    'prepay_info' => $result,
+                    'amount' => $amount,
+                    'expiretime' => date('Y-m-d H:i:s', time() + 5 * 60),
+                ]);
+
+
+            //@todo 要给前端返回什么信息?
+            $res = '';
+
 
             Db::commit();
             return json_show(CommonModel::$success, '微信支付预下单成功', $res);

+ 4 - 1
app/mobile/logic/ExchangeLogic.php

@@ -121,9 +121,12 @@ class ExchangeLogic extends BaseLogic
                 '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' => CommonModel::$status_not_deliver,
+                'status' => CommonModel::$order_status_not_deliver,
                 'is_del' => CommonModel::$del_normal,
                 'creater' => self::$aname,
                 'createrid' => self::$aid,

+ 7 - 4
app/model/CommonModel.php

@@ -33,10 +33,13 @@ class CommonModel
     public static $source_admin = 1 ;//后台操作
     public static $source_account = 2 ;//客户操作
 
-    //状态:0未发货,1已发货,2已收货
-    public static $status_not_deliver = 0;
-    public static $status_deliver = 1;
-    public static $status_receipt = 2;
+    //订单状态:0待支付,1支付失败,2支付成功待发货,3已发货,4已收货
+    public static $order_status_wait_pay = 0;
+    public static $order_status_pay_fail = 1;
+    public static $order_status_not_deliver = 2;
+    public static $order_status_deliver = 3;
+    public static $order_status_receipt = 4;
+
 
     //购买的类型,1购买服务,2购买商城商品
     public static $pay_type_service = 1;

+ 0 - 4
app/model/PayInfoModel.php

@@ -11,9 +11,5 @@ class PayInfoModel extends Model
     protected $table='fc_pay_info';
     protected $pk='id';
 
-    //支付状态,0待支付,1支付成功,2支付失败
-    public static $pay_wait=0;
-    public static $pay_success=1;
-    public static $pay_fail=2;
 
 }