You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

349 lines
12 KiB

  1. <?php
  2. namespace backend\modules\motorcade\controllers;
  3. use backend\modules\api\models\BaseResource;
  4. use backend\modules\api\models\OrderMain;
  5. use backend\modules\motorcade\models\BusItinerary;
  6. use backend\modules\motorcade\models\BusOrder;
  7. use backend\modules\zzcs\models\RunBus;
  8. use common\components\zHttp;
  9. use Yii;
  10. use backend\modules\motorcade\models\ZizaiOrderTemp;
  11. use yii\data\ActiveDataProvider;
  12. use yii\web\NotFoundHttpException;
  13. use yii\filters\VerbFilter;
  14. /**
  15. * ZizaiController implements the CRUD actions for ZizaiOrderTemp model.
  16. */
  17. class ZizaiController extends BaseController
  18. {
  19. public $layout = "@backend/modules/motorcade/views/layouts/iframe";
  20. /**
  21. * @inheritdoc
  22. */
  23. public function behaviors()
  24. {
  25. return [
  26. 'verbs' => [
  27. 'class' => VerbFilter::className(),
  28. 'actions' => [
  29. 'delete' => ['POST'],
  30. ],
  31. ],
  32. ];
  33. }
  34. /**
  35. * Lists all ZizaiOrderTemp models.
  36. * @return mixed
  37. */
  38. public function actionIndex()
  39. {
  40. $dataProvider = new ActiveDataProvider([
  41. 'query' => ZizaiOrderTemp::find()->orderBy(['run_bus_id'=>'desc','seq'=>'asc']),
  42. ]);
  43. return $this->render('index', [
  44. 'dataProvider' => $dataProvider,
  45. ]);
  46. }
  47. /**
  48. * Displays a single ZizaiOrderTemp model.
  49. * @param integer $id
  50. * @return mixed
  51. */
  52. public function actionView($id)
  53. {
  54. return $this->render('view', [
  55. 'model' => $this->findModel($id),
  56. ]);
  57. }
  58. /**
  59. * Creates a new ZizaiOrderTemp model.
  60. * If creation is successful, the browser will be redirected to the 'view' page.
  61. * @return mixed
  62. */
  63. public function actionCreate()
  64. {
  65. $model = new ZizaiOrderTemp();
  66. $model->end_time = date('Y-m-d H:i');
  67. if ($model->load(Yii::$app->request->post()) && $model->save()) {
  68. //下单成功,调用计算最优算法
  69. $result = $this->sendOrder($model);
  70. if ($result['code'] == 0) {
  71. $order_id = $result['data']['order_id'];
  72. $data = $result['data'];
  73. $model->is_abnormal = 0;
  74. $model->abnormal_msg = '';
  75. $model->setAttributes($result['data'], false);
  76. $model->order_status = ZizaiOrderTemp::STATUS_BOOKED;
  77. $model->save(false);
  78. $model->orderResort($result['data']['order_list'], $result['data']['order_id']);
  79. //如果订单数等于3个,或总人数大于19,直接生成出车任务
  80. if ($model->checkDone()) {
  81. if (!$this->createBusOrder($data, $order_id)) {
  82. Yii::$app->session->setFlash('error', '生成出车任务失败,请联系技术人员!');
  83. }
  84. }
  85. Yii::$app->session->setFlash('success', '下单成功!');
  86. } else {
  87. $model->is_abnormal = 1;
  88. $model->abnormal_msg = $result['info'];
  89. $model->save(false);
  90. Yii::$app->session->setFlash('error', '保存成功,CS下单失败!');
  91. }
  92. return $this->redirect(['view', 'id' => $model->ID]);
  93. } else {
  94. return $this->render('create', [
  95. 'model' => $model,
  96. ]);
  97. }
  98. }
  99. /**
  100. * Updates an existing ZizaiOrderTemp model.
  101. * If update is successful, the browser will be redirected to the 'view' page.
  102. * @param integer $id
  103. * @return mixed
  104. */
  105. public function actionUpdate($id)
  106. {
  107. $model = $this->findModel($id);
  108. if ($model->load(Yii::$app->request->post()) && $model->save()) {
  109. //异常订单修改,可以重新检查是否可下单
  110. if ($model->is_abnormal == 1) {
  111. $result = $this->sendOrder($model);
  112. if ($result['code'] == 0) {
  113. $order_id = $result['data']['order_id'];
  114. $data = $result['data'];
  115. $model->is_abnormal = 0;
  116. $model->abnormal_msg = '';
  117. $model->setAttributes($result['data'], false);
  118. $model->order_status = ZizaiOrderTemp::STATUS_BOOKED;
  119. $model->save(false);
  120. $model->orderResort($result['data']['order_list'], $result['data']['order_id']);
  121. //如果订单数等于3个,或总人数大于19,直接生成出车任务
  122. if ($model->checkDone()) {
  123. if (!$this->createBusOrder($data, $order_id)) {
  124. Yii::$app->session->setFlash('error', '生成出车任务失败,请联系技术人员!');
  125. }
  126. }
  127. Yii::$app->session->setFlash('success', '下单成功!');
  128. } else {
  129. $model->is_abnormal = 1;
  130. $model->abnormal_msg = $result['info'];
  131. $model->save(false);
  132. Yii::$app->session->setFlash('error', '保存成功,CS下单失败!');
  133. }
  134. }
  135. return $this->redirect(['view', 'id' => $model->ID]);
  136. } else {
  137. return $this->render('update', [
  138. 'model' => $model,
  139. ]);
  140. }
  141. }
  142. /**
  143. * Deletes an existing ZizaiOrderTemp model.
  144. * If deletion is successful, the browser will be redirected to the 'index' page.
  145. * @param integer $id
  146. * @return mixed
  147. */
  148. public function actionDelete($id)
  149. {
  150. $this->findModel($id)->delete();
  151. return $this->redirect(['index']);
  152. }
  153. /**
  154. * Finds the ZizaiOrderTemp model based on its primary key value.
  155. * If the model is not found, a 404 HTTP exception will be thrown.
  156. * @param integer $id
  157. * @return ZizaiOrderTemp the loaded model
  158. * @throws NotFoundHttpException if the model cannot be found
  159. */
  160. protected function findModel($id)
  161. {
  162. if (($model = ZizaiOrderTemp::findOne($id)) !== null) {
  163. return $model;
  164. } else {
  165. throw new NotFoundHttpException('The requested page does not exist.');
  166. }
  167. }
  168. /**
  169. * User: wangxj
  170. *
  171. * 在cs系统下订单
  172. *
  173. * @param $model ZizaiOrderTemp
  174. * @return array
  175. *
  176. */
  177. protected function sendOrder($model)
  178. {
  179. $result = zHttp::httpRequest('http://zzdz.' . DOMAIN . '/api/index.php', [
  180. 'tp' => 'dispatch_calRoute',
  181. 'num' => $model->num,
  182. 'longitude' => $model->longitude,
  183. 'latitude' => $model->latitude,
  184. 'end_time' => $model->end_time,
  185. 'zizai_order_temp' => json_encode($model->attributes)]);
  186. // return $result;
  187. $result = json_decode($result, true);
  188. return $result;
  189. }
  190. /**
  191. * User: wangxj
  192. *
  193. * 最后一站黄浦集散站的数据填充
  194. *
  195. * @param $data array 新订单录入的返回结果集
  196. * @param $key integer 最后一站是第几站
  197. * @return BusItinerary
  198. */
  199. protected function getEndStation($data, $key)
  200. {
  201. $BusItinerary = new BusItinerary();
  202. $BusItinerary->station_seq_id = $key + 1;
  203. $BusItinerary->station_name = '黄浦集散站';
  204. $BusItinerary->station_address = '黄浦集散站';
  205. $BusItinerary->inout_type = 110; //仅下客
  206. $BusItinerary->day_seq_id = 1;
  207. $BusItinerary->start_time = substr($data['down_time'], 11);
  208. return $BusItinerary;
  209. }
  210. /**
  211. * User: wangxj
  212. *
  213. * 生成出车任务,要影响orderMain表的站点字段,prod_start_station_res_id prod_start_station_res_name prod_start_station_seq_id prod_end_station_seq_id
  214. * @param $data array
  215. * @param $order_id
  216. *
  217. * @return bool
  218. *
  219. */
  220. protected function createBusOrder($data, $order_id)
  221. {
  222. $stations = ZizaiOrderTemp::find()->where(['run_bus_id' => $data['run_bus_id'], 'cancel_flag' => 0])
  223. ->orderBy('seq')->all();
  224. $bus_order = new BusOrder();
  225. $bus_order->run_date = substr($data['down_time'], 0, 10);
  226. $seat_count = BaseResource::findOne(['parent_id' => 72, 'res_name' => $data['seat_count'] . '座']);
  227. $bus_order->bus_type_res_id = $seat_count->RES_ID;
  228. $bus_order->day_num = 1;
  229. $bus_order->line_type = BusOrder::LINE_TYPE_CITY;
  230. $bus_order->task_type = BusOrder::TASK_TYPE_CUSTOM;
  231. //线路名称
  232. $order = OrderMain::findOne(['order_id' => $order_id]);
  233. $bus_order->itinerary_name = $order->PARENT_PROD_NAME;
  234. $bus_order->line_id = $order->PARENT_PROD_ID;
  235. //用车单位
  236. $bus_order->use_bus_org_id = 517;
  237. //车价、承运车队
  238. $run_bus = RunBus::findOne(['id' => $data['run_bus_id']]);
  239. $run_bus->COST_MOTORCADE_ID = 628;
  240. $bus_order->bus_cost = $run_bus->COST_PRICE;
  241. $bus_order->bus_cost_type = $run_bus->COST_TYPE;
  242. $bus_order->motorcade_id = $run_bus->COST_MOTORCADE_ID;
  243. $bus_order->setAttributes($data, false);
  244. switch ($run_bus->COST_MOTORCADE_ID) {
  245. case 628: //恒栋车队
  246. $user_id = 487; //admin_007:恒栋管理员
  247. break;
  248. case 403: //千旅车队
  249. $user_id = 559; //admin_004:千旅管理员
  250. break;
  251. case 1284: //黄山长三角车队
  252. $user_id = 579; //admin_011:郝贵福
  253. break;
  254. case 1288: //苏州舟游车队
  255. $user_id = 582; //周慧
  256. break;
  257. default:
  258. $user_id = 0;
  259. }
  260. $bus_order->create_user_id = $user_id;
  261. $key = 0;
  262. foreach ($stations as $zz_model) {
  263. $BusItinerary = new BusItinerary();
  264. $BusItinerary->station_seq_id = $key + 1;
  265. $BusItinerary->station_name = $zz_model['start_station'];
  266. $BusItinerary->station_address = $zz_model['start_station'];
  267. $BusItinerary->inout_type = 108; //仅上客
  268. $BusItinerary->day_seq_id = 1;
  269. $BusItinerary->Longitude = $zz_model['longitude'];
  270. $BusItinerary->Latitude = $zz_model['latitude'];
  271. $BusItinerary->start_time = $zz_model['start_time'];
  272. $bus_order->itinerary_list[] = $BusItinerary;
  273. $key++;
  274. }
  275. //黄浦集散站
  276. $bus_order->itinerary_list[] = $this->getEndStation($data, $key);
  277. if ($bus_order->addBus()) {
  278. $key = 0;
  279. $arr = $bus_order->itinerary;
  280. $model = $stations[0];
  281. foreach ($arr as $res) {
  282. $res_id = $res->id;
  283. $end_seq_id = count($stations);
  284. if ($key < count($arr) - 1) {
  285. $model = $stations[$key];
  286. ZizaiOrderTemp::updateOrderMain($model, $res_id, $key + 1, $end_seq_id + 1);
  287. } else {
  288. OrderMain::updateAll([
  289. 'prod_end_station_res_id' => $res_id,
  290. ],
  291. ['run_id' => $model->run_id, 'RUN_BUS_ORDER_ID' => $model->bus_order_id]);
  292. }
  293. $res->station_res_id = $res_id;
  294. $res->save(false);
  295. $key++;
  296. }
  297. ZizaiOrderTemp::updateAll(['order_status' => ZizaiOrderTemp::STATUS_SYNC], ['run_bus_id'=> $model->run_bus_id]);
  298. return true;
  299. } else {
  300. return false;
  301. }
  302. }
  303. /**
  304. * User: wangxj
  305. *
  306. * 定时任务,自动生成出车任务,不考虑订单数量和人数
  307. *
  308. */
  309. public function actionAutoOrder()
  310. {
  311. // ::todo 定时
  312. $sql = "SELECT seat_count,min(end_time) AS down_time,sum(num) AS saled_count,t.* FROM zizai_order_temp t LEFT JOIN run_bus rb ON rb.id = t.run_bus_id WHERE t.cancel_flag = 0 AND is_abnormal = 0 AND order_status=" . ZizaiOrderTemp::STATUS_BOOKED . ' GROUP BY run_bus_id';
  313. $list = Yii::$app->db->createCommand($sql)
  314. ->queryAll();
  315. foreach ($list as $item) {
  316. $this->createBusOrder($item, $item['order_id']);
  317. }
  318. }
  319. }