DefaultController.php
8.53 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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
<?php
namespace app\wx\modules\order\controllers;
use Yii;
use yii\helpers\HtmlPurifier;
use yii\base\Exception;
use domain\order\RepairOrder;
use domain\order\RepairOrderImages;
use domain\order\RepairOrderRepository;
use domain\order\RepairOrderStatus;
use domain\order\RepairFinishImages;
use domain\order\RepairOrderRepairPlan;
use stdClass;
/**
* 控制器
*/
class DefaultController extends BaseController
{
/**
* 首页
*/
public function actionIndex()
{
return $this->render('index');
}
/**
* @return string
*/
public function actionSubmit()
{
$e = new stdClass();
$e->success = false;
$e->message = 'ok';
$userId = $this->getUserId();
$req = Yii::$app->request;
$carNo = HtmlPurifier::process($req->post('carNo'), ['HTML.Allowed' => '']);
$carModel = HtmlPurifier::process($req->post('carModel'), ['HTML.Allowed' => '']);
$customer = HtmlPurifier::process($req->post('customer'), ['HTML.Allowed' => '']);
$phone = HtmlPurifier::process($req->post('phone'), ['HTML.Allowed' => '']);
$preRepair = HtmlPurifier::process($req->post('preRepair'), ['HTML.Allowed' => '']);
$predictPrice = HtmlPurifier::process($req->post('repairPrice'), ['HTML.Allowed' => '']);
$finishDate = HtmlPurifier::process($req->post('finishDate'), ['HTML.Allowed' => '']);
$images = $req->post('images');
if (empty($carNo)) {
$e->message = '车牌号码未填';
return $this->renderJson($e);
}
if (empty($carModel)) {
$e->message = '车辆型号未填';
return $this->renderJson($e);
}
if (empty($phone)) {
$e->message = '联系电话未填';
return $this->renderJson($e);
}
$data = [
'user_id' => $userId,
'car_no' => $carNo,
'car_model' => $carModel,
'customer' => $customer,
'contact_mobile' => $phone,
'predict_fault' => $preRepair,
'predict_price' => $predictPrice,
'predict_finish_time' => strtotime($finishDate.':00:00'),
'status' => RepairOrderStatus::REPAIRING,
];
$tran = Yii::$app->db->beginTransaction();
try {
$repairOrder = RepairOrder::create($data);
$uuid = $repairOrder->uuid;
RepairOrderImages::createBrokenImages($repairOrder->id, $uuid, $images);
$tran->commit();
RepairOrderImages::mvBrokenImages($uuid, $images);
$e->orderId = $uuid;
$e->success = true;
} catch(Exception $ex) {
$tran->rollBack();
$e->orderId = '';
$e->message = '提交失败';
}
return $this->renderJson($e);
}
/**
* @return string
*/
public function actionOrderList()
{
$e = new stdClass();
$e->success = false;
$e->page = null;
$e->page_count = null;
$e->items = [];
$page = $this->request->get('page');
$status = $this->request->get('status');
if (empty($page)) {
$page = 1;
}
$allItemList = [0, 0, 0];
if ('working' == $status || '' == $status) {
$allItemList = $this->getOrderList($page, 1);
} else {
$allItemList = $this->getOrderList($page, 0);
}
$e->page_count = $allItemList[1];
$e->page = $page ;
$e->items = $allItemList[2];
$e->success = true;
return $this->renderJson($e);
}
/**
* @param $page
* @param $status
*/
private function getOrderList($page, $status)
{
$pageSize = 5 ;
$pager_offset = ($page -1) * $pageSize;
$limitPage = $pageSize;
$count = 15;
$items = [];
$pageCount = ceil(($count*1) / $pageSize);
$carModelList = ['宝马X3','宝马X4','宝马X5','宝马X6','奔驰c200l','奔驰E260','奔驰E260','奔驰E300'];
$customerList = ['张先生','李先生','王先生','赵先生','刘先生','宝先生','好先生','费先生'];
for ($i = $pager_offset; $i < ($pager_offset+$limitPage); $i++ ) {
$carModel = $carModelList[mt_rand(0,7)];
$customer = $customerList[mt_rand(0,7)];
$items[] = ['id'=> $i ,'carNo' => 'XA'.mt_rand(10000,99999), 'carModel' => $carModel, 'status' => $status, 'customer' => $customer ,'createdTime' => date('Y-m-d H:i')];
}
return [$count, $pageCount, $items];
}
/**
* @return string
*/
public function actionOrderDetails()
{
$e = new stdClass();
$e->success = false;
$e->message = 'ok';
$orderUUId = $this->request->post('id');
$userId = $this->getUserId();
$orderModel = RepairOrderRepository::findOne(['uuid' => $orderUUId, 'user_id' => $userId]);
if (empty($orderModel)) {
$e->message = '找不到该订单';
return $this->renderJson($e);
}
$brokenImageModels = RepairOrderRepository::findOrderImageAll(['repair_order_id' => $orderModel->id]);
$brokenImages = [];
$baseURL = Yii::$app->request->getHostInfo();
foreach($brokenImageModels as $k => $v) {
$brokenImages[] = $baseURL.'/'.$v->image_path;
}
$totalPrice = $orderModel->order_price;
$rPlans = RepairOrderRepository::findOrderPlansAll(['repair_order_id' => $orderModel->id]);
$repairPlans = [];
foreach($rPlans as $k => $plan) {
$repairPlans[] = ['plan' => $plan['repair_plan'] ,'price' => $plan['price']];
}
$fImages = RepairOrderRepository::findOrderFinishImageAll(['repair_order_id' => $orderModel->id]);
$finishImages = [];
foreach($fImages as $k => $image) {
$finishImages[] = $baseURL.'/'.$image['image_path'];
}
$hasComment = false;
$e->success = true;
$e->item = [
'carNo' => $orderModel->car_no,
'carModel' => $orderModel->car_model,
'customer' => $orderModel->customer,
'mobile' => $orderModel->contact_mobile,
'preRepair' => $orderModel->predict_fault,
'repairPrice' => $orderModel->predict_price.'元',
'preFinishDate' => date('Y-m-d H:00', $orderModel->predict_finish_time),
'orderDateTime' => date('Y-m-d H:00', $orderModel->created_at),
'status' => RepairOrderStatus::getEnLabel($orderModel->status),
'hasComment' => $hasComment,
'brokenImages' => $brokenImages,
'repairPlans' => $repairPlans,
'totalPrice' => $totalPrice,
'finishImages' => $finishImages
];
return $this->renderJson($e);
}
/**
* @return string
*/
public function actionSubmitRepairPlans()
{
$e = new stdClass();
$e->success = false;
$e->message = 'ok';
$repairPlans = $this->request->post('plans');
$images = $this->request->post('images');
$orderUUId = $this->request->post('id');
$userId = $this->getUserId();
$orderModel = RepairOrderRepository::findOne(['uuid' => $orderUUId, 'user_id' => $userId]);
if (empty($orderModel)) {
$e->message = '找不到订单';
return $this->renderJson($e);
}
if (RepairOrderStatus::FINISH == $orderModel->status) {
$e->message = '订单已完成,不能编辑';
return $this->renderJson($e);
}
$orderId = $orderModel->id;
$tran = Yii::$app->db->beginTransaction();
try {
$totalPrice = RepairOrderRepairPlan::batchCreate($orderId, $repairPlans);
RepairFinishImages::createFinishImages($orderId, $orderModel->uuid, $images);
$orderModel->status = RepairOrderStatus::FINISH;
$orderModel->finish_at = time();
$orderModel->order_price = $totalPrice;
$orderModel->save();
$tran->commit();
RepairFinishImages::mvFinishImages($orderModel->uuid, $images);
$e->success = true;
} catch (Exception $ex) {
$tran->rollBack();
$e->success = false;
$e->message = '提交失败';
$e->innerMEssage = $ex->getMessage();
}
return $this->renderJson($e);
}
}