OrderController.php
6.9 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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
<?php
/**
* Created by PhpStorm.
* User: yhbr
* Date: 2018/9/1
* Time: 9:26
*/
namespace app\admin\controller;
use Think\Db;
use cmf\controller\AdminBaseController;
class OrderController extends AdminBaseController
{
public function index()
{
$where = [];
$order_sn = request()->param('order_sn');
if ($order_sn != null) {
$where['order_sn'] = ['like', "%$order_sn%"];
}
$status = request()->param('status');
if ($status != null) {
$where['status'] = ['eq', $status];
}
$res = Db::name('order_info')->alias('o')
->field('o.order_sn,o.id as oid,a.name,s.start_time,s.end_time,o.status,u.user_nickname')
->join('activity a', 'a.id=o.activity_id')
->join('activity_schedule s', 's.id=o.schedule_id')
->join('user u', 'u.id=o.user_id')
->where($where)
->order('add_time DESC')
->paginate(20, false, ['query' => request()->param()]);
foreach ($res as $k => $v) {
$v['status'] = getOrderStatusText($v['status']);
$res[$k] = $v;
}
return $this->fetch('', [
'posts' => $res,
'page' => $res->render(),
'order_sn' => $order_sn,
'status' => $status
]);
}
public function detail()
{
$id = request()->param('id');
if (request()->isPost()) {
$act = request()->param('act');
$status = 0;
if ($act == 'down_payment') {
$status = 2;
} elseif ($act == 'final_payment') {
$status = 4;
} else {
$this->error('操作失败');
}
$data = [
'id' => $id,
'status' => $status
];
if (Db::name('order_info')->update($data)) {
$this->success('操作成功');
} else {
$this->error('操作失败');
}
} else {
//订单基本信息
$orderInfo = Db::name('order_info')->alias('o')
->field('o.*,u.user_nickname,a.name,s.start_time,s.end_time')
->join('activity a ', 'a.id=o.activity_id')
->join('user u', 'u.id=o.user_id')
->join('activity_schedule s', 's.id=o.schedule_id')
->where(['o.id' => $id])
->find();
if ($orderInfo['is_use_discount_coupon'] == 1 && !empty($orderInfo['discount_coupon_id'])) {
$orderInfo['coupons'] = Db::name('discount_coupon')->where(['id' => $orderInfo['discount_coupon_id']])->value('discount_coupon_name');
} else {
$orderInfo['coupons'] = '未使用';
}
$orderInfo['status_text'] = getOrderStatusText($orderInfo['status']);
//订单详情信息
$orderDetail = Db::name('order_detail')->alias('d')
->field('d.*,e.name,e.tel,e.sex,e.identity,e.wechat')
->join('escort e', 'e.id=d.escort_id')
->where(['oid' => $id])
->select();
foreach ($orderDetail as $k => $v) {
if (!empty($v['refund_time'])) {
$v['refund_time'] = date('Y-m-d', $v['refund_time']);
}
$orderDetail[$k] = $v;
}
//订单金额信息
$orderLog = Db::name('order_log')->field('order_amount,type')->where(['oid' => $id])->select();
return $this->fetch('', [
'order_info' => $orderInfo,
'order_detail' => $orderDetail,
'order_log' => $orderLog
]);
}
}
/**
* 同意退款
* @return mixed
* @throws \think\Exception
* @throws \think\exception\PDOException
*/
public function agree()
{
$request = request();
if ($request->isPost()) {
$refund_amount = $request->param('refund_amount');
$id = $request->param('id');
if ((Db::name('order_detail')->where(['id' => $id])->value('status')) != 1) {
$this->error('该退款需求已经处理');
} else {
//改变此条状态->给支付人返还指定金额->我的钱包记录退款
Db::startTrans();
$orderDetail = [
'id' => $id,
'status' => 2,
'refund_amount' => $request->param('refund_amount'),
'refund_time' => time()
];
if (Db::name('order_detail')->update($orderDetail)) {
$userId = Db::name('order_detail')->alias('s')
->join('order_info o', 'o.id=s.oid')
->value('user_id');
if (Db::name('user')->where(['id' => 2])->setInc('balance', $refund_amount)) {
$log = [
'user_id' => $userId,
'type' => 2,
'cost' => $refund_amount,
'create_time' => time()
];
if (Db::name('my_wallet')->insert($log)) {
Db::commit();
$this->success('退款成功', url('Order/detail', ['id' => Db::name('order_detail')->where(['id' => $id])->value('oid')]));
} else {
Db::rollback();
$this->error('未知错误');
}
} else {
Db::rollback();
$this->error('未知错误');
}
} else {
Db::rollback();
$this->error('未知错误');
}
}
} else {
return $this->fetch('', [
'id' => $request->param('id')
]);
}
}
public function disagree()
{
$request = request();
$id = $request->param('id');
if ($request->isPost()) {
if ((Db::name('order_detail')->where(['id' => $id])->value('status')) != 1) {
$this->error('该退款需求已经处理');
} else {
$data = [
'id' => $id,
'status' => 3,
'reason' => $request->param('reason')
];
if (Db::name('order_detail')->update($data)) {
$this->success('已拒绝退款', url('Order/detail', ['id' => Db::name('order_detail')->where(['id' => $id])->value('oid')]));
} else {
$this->error('未知错误');
}
}
} else {
return $this->fetch('', [
'id' => $request->param('id')
]);
}
}
}