OrderModel.php
4.0 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
<?php
/**
* Created by PhpStorm.
* User: yhbr
* Date: 2018/9/5
* Time: 15:03
*/
namespace app\order\model;
use think\Model;
use think\Db;
class OrderModel extends Model
{
//暂定
public function getOrderListByUserId($status, $orderSn, $userId)
{
$where['user_id'] = ['eq', $userId];
if ($orderSn != null) {
$where['order_sn'] = ['like', "%$orderSn%"];
}
if ($status != null) {
$where['status'] = ['eq', $status];
}else {
$where['status'] = ['neq', 7];
}
$res = Db::name('order_info')->alias('o')
->field('o.id as oid,o.order_sn,a.name,a.thumb,o.status,a.down_price,s.price')
->join('activity a', 'a.id=o.activity_id')
->join('activity_schedule s', 's.id=o.schedule_id')
->where($where)
->order('add_time DESC')
->select()
->toArray();
foreach ($res as $k => $v) {
$v['status_text'] = getOrderStatusText($v['status']);
$v['count'] = Db::name('order_detail')->where(['oid' => $v['oid']])->count();
$res[$k] = $v;
}
return $res;
}
//通用订单回调类(未完待续。。。)
//余额扣除(如果余额支付->钱包记录)->优惠券变为已使用(如果使用优惠券)->改变订单状态(必然)->相应批次库存减少(必然)
public function orderCallBack($oid, $order_amount = null)
{
$orderInfo = Db::name('order_info')->alias('o')
->field('o.user_id,o.is_use_discount_coupon,discount_coupon_id,o.status,o.payment,o.order_type,o.activity_id,o.schedule_id,l.order_amount')
->join('order_log l', 'l.oid=o.id')
->where(['o.id' => $oid])
->find();
Db::startTrans();
$go = 'rollback';
if ($orderInfo['payment'] == 0) {
$amount = ($order_amount == null) ? $order_amount['order_amount'] : $order_amount;
if (Db::name('user')->where(['id' => $orderInfo['user_id']])->setDec('balance', $amount)) {
$wallet = [
'user_id' => $orderInfo['user_id'],
'type' => 0,
'cost' => $orderInfo['order_amount'],
'create_time' => time()
];
if (Db::name('my_wallet')->insert($wallet)) {
$go = 'commit';
} else {
return false;
}
} else {
return false;
}
}
//订单状态无定金直接变为4,有定金变为2
if ($orderInfo['status'] == 1) {
if ($orderInfo['order_type'] == 0) {
$status = 4;
} elseif ($orderInfo['order_type'] == 1) {
$status = 2;
} else {
$status = 0;
}
$order = [
'id' => $oid,
'status' => $status
];
if (Db::name('order_info')->update($order)) {
//相应批次报名数增加
$dec = Db::name('order_detail')->where(['oid' => $oid])->count();
if (Db::name('activity_schedule')->where(['id' => $orderInfo['schedule_id']])->setInc('real_join_num', $dec)) {
$go = 'commit';
} else {
return false;
}
} else {
return false;
}
}
//待付尾款订单
if($orderInfo['status'] == 3) {
$status = 4;
$order = [
'id' => $oid,
'status' => $status
];
if(Db::name('order_info')->update($order)) {
$go = 'commit';
}else {
return false;
}
}
if ($go == 'commit') {
Db::commit();
return true;
} else {
Db::rollback();
return false;
}
}
}