|
- <?php
-
- namespace backend\modules\motorcade\controllers;
-
- use backend\modules\api\models\BaseResource;
- use backend\modules\api\models\OrderMain;
- use backend\modules\motorcade\models\BusItinerary;
- use backend\modules\motorcade\models\BusOrder;
- use backend\modules\zzcs\models\RunBus;
- use common\components\zHttp;
- use Yii;
- use backend\modules\motorcade\models\ZizaiOrderTemp;
- use yii\data\ActiveDataProvider;
- use yii\web\NotFoundHttpException;
- use yii\filters\VerbFilter;
-
- /**
- * ZizaiController implements the CRUD actions for ZizaiOrderTemp model.
- */
- class ZizaiController extends BaseController
- {
- public $layout = "@backend/modules/motorcade/views/layouts/iframe";
-
- /**
- * @inheritdoc
- */
- public function behaviors()
- {
- return [
- 'verbs' => [
- 'class' => VerbFilter::className(),
- 'actions' => [
- 'delete' => ['POST'],
- ],
- ],
- ];
- }
-
- /**
- * Lists all ZizaiOrderTemp models.
- * @return mixed
- */
- public function actionIndex()
- {
- $dataProvider = new ActiveDataProvider([
- 'query' => ZizaiOrderTemp::find()->orderBy(['run_bus_id'=>'desc','seq'=>'asc']),
- ]);
-
- return $this->render('index', [
- 'dataProvider' => $dataProvider,
- ]);
- }
-
- /**
- * Displays a single ZizaiOrderTemp model.
- * @param integer $id
- * @return mixed
- */
- public function actionView($id)
- {
- return $this->render('view', [
- 'model' => $this->findModel($id),
- ]);
- }
-
- /**
- * Creates a new ZizaiOrderTemp model.
- * If creation is successful, the browser will be redirected to the 'view' page.
- * @return mixed
- */
- public function actionCreate()
- {
- $model = new ZizaiOrderTemp();
-
- $model->end_time = date('Y-m-d H:i');
- if ($model->load(Yii::$app->request->post()) && $model->save()) {
-
- //下单成功,调用计算最优算法
-
- $result = $this->sendOrder($model);
- if ($result['code'] == 0) {
-
- $order_id = $result['data']['order_id'];
- $data = $result['data'];
- $model->is_abnormal = 0;
- $model->abnormal_msg = '';
- $model->setAttributes($result['data'], false);
- $model->order_status = ZizaiOrderTemp::STATUS_BOOKED;
- $model->save(false);
- $model->orderResort($result['data']['order_list'], $result['data']['order_id']);
- //如果订单数等于3个,或总人数大于19,直接生成出车任务
- if ($model->checkDone()) {
- if (!$this->createBusOrder($data, $order_id)) {
- Yii::$app->session->setFlash('error', '生成出车任务失败,请联系技术人员!');
- }
- }
- Yii::$app->session->setFlash('success', '下单成功!');
- } else {
- $model->is_abnormal = 1;
- $model->abnormal_msg = $result['info'];
- $model->save(false);
- Yii::$app->session->setFlash('error', '保存成功,CS下单失败!');
- }
- return $this->redirect(['view', 'id' => $model->ID]);
- } else {
- return $this->render('create', [
- 'model' => $model,
- ]);
- }
- }
-
- /**
- * Updates an existing ZizaiOrderTemp model.
- * If update is successful, the browser will be redirected to the 'view' page.
- * @param integer $id
- * @return mixed
- */
- public function actionUpdate($id)
- {
- $model = $this->findModel($id);
- if ($model->load(Yii::$app->request->post()) && $model->save()) {
-
- //异常订单修改,可以重新检查是否可下单
- if ($model->is_abnormal == 1) {
- $result = $this->sendOrder($model);
-
- if ($result['code'] == 0) {
-
- $order_id = $result['data']['order_id'];
- $data = $result['data'];
- $model->is_abnormal = 0;
- $model->abnormal_msg = '';
- $model->setAttributes($result['data'], false);
- $model->order_status = ZizaiOrderTemp::STATUS_BOOKED;
- $model->save(false);
- $model->orderResort($result['data']['order_list'], $result['data']['order_id']);
- //如果订单数等于3个,或总人数大于19,直接生成出车任务
- if ($model->checkDone()) {
- if (!$this->createBusOrder($data, $order_id)) {
- Yii::$app->session->setFlash('error', '生成出车任务失败,请联系技术人员!');
- }
- }
- Yii::$app->session->setFlash('success', '下单成功!');
- } else {
- $model->is_abnormal = 1;
- $model->abnormal_msg = $result['info'];
- $model->save(false);
- Yii::$app->session->setFlash('error', '保存成功,CS下单失败!');
- }
- }
-
- return $this->redirect(['view', 'id' => $model->ID]);
- } else {
- return $this->render('update', [
- 'model' => $model,
- ]);
- }
- }
-
- /**
- * Deletes an existing ZizaiOrderTemp model.
- * If deletion is successful, the browser will be redirected to the 'index' page.
- * @param integer $id
- * @return mixed
- */
- public function actionDelete($id)
- {
- $this->findModel($id)->delete();
-
- return $this->redirect(['index']);
- }
-
- /**
- * Finds the ZizaiOrderTemp model based on its primary key value.
- * If the model is not found, a 404 HTTP exception will be thrown.
- * @param integer $id
- * @return ZizaiOrderTemp the loaded model
- * @throws NotFoundHttpException if the model cannot be found
- */
- protected function findModel($id)
- {
- if (($model = ZizaiOrderTemp::findOne($id)) !== null) {
- return $model;
- } else {
- throw new NotFoundHttpException('The requested page does not exist.');
- }
- }
-
- /**
- * User: wangxj
- *
- * 在cs系统下订单
- *
- * @param $model ZizaiOrderTemp
- * @return array
- *
- */
- protected function sendOrder($model)
- {
- $result = zHttp::httpRequest('http://zzdz.' . DOMAIN . '/api/index.php', [
- 'tp' => 'dispatch_calRoute',
- 'num' => $model->num,
- 'longitude' => $model->longitude,
- 'latitude' => $model->latitude,
- 'end_time' => $model->end_time,
- 'zizai_order_temp' => json_encode($model->attributes)]);
- // return $result;
- $result = json_decode($result, true);
-
- return $result;
- }
-
- /**
- * User: wangxj
- *
- * 最后一站黄浦集散站的数据填充
- *
- * @param $data array 新订单录入的返回结果集
- * @param $key integer 最后一站是第几站
- * @return BusItinerary
- */
- protected function getEndStation($data, $key)
- {
- $BusItinerary = new BusItinerary();
- $BusItinerary->station_seq_id = $key + 1;
- $BusItinerary->station_name = '黄浦集散站';
- $BusItinerary->station_address = '黄浦集散站';
- $BusItinerary->inout_type = 110; //仅下客
- $BusItinerary->day_seq_id = 1;
- $BusItinerary->start_time = substr($data['down_time'], 11);
- return $BusItinerary;
- }
-
- /**
- * User: wangxj
- *
- * 生成出车任务,要影响orderMain表的站点字段,prod_start_station_res_id prod_start_station_res_name prod_start_station_seq_id prod_end_station_seq_id
- * @param $data array
- * @param $order_id
- *
- * @return bool
- *
- */
- protected function createBusOrder($data, $order_id)
- {
- $stations = ZizaiOrderTemp::find()->where(['run_bus_id' => $data['run_bus_id'], 'cancel_flag' => 0])
- ->orderBy('seq')->all();
- $bus_order = new BusOrder();
- $bus_order->run_date = substr($data['down_time'], 0, 10);
- $seat_count = BaseResource::findOne(['parent_id' => 72, 'res_name' => $data['seat_count'] . '座']);
- $bus_order->bus_type_res_id = $seat_count->RES_ID;
- $bus_order->day_num = 1;
- $bus_order->line_type = BusOrder::LINE_TYPE_CITY;
- $bus_order->task_type = BusOrder::TASK_TYPE_CUSTOM;
- //线路名称
- $order = OrderMain::findOne(['order_id' => $order_id]);
- $bus_order->itinerary_name = $order->PARENT_PROD_NAME;
- $bus_order->line_id = $order->PARENT_PROD_ID;
- //用车单位
- $bus_order->use_bus_org_id = 517;
-
- //车价、承运车队
- $run_bus = RunBus::findOne(['id' => $data['run_bus_id']]);
- $run_bus->COST_MOTORCADE_ID = 628;
- $bus_order->bus_cost = $run_bus->COST_PRICE;
- $bus_order->bus_cost_type = $run_bus->COST_TYPE;
- $bus_order->motorcade_id = $run_bus->COST_MOTORCADE_ID;
- $bus_order->setAttributes($data, false);
- switch ($run_bus->COST_MOTORCADE_ID) {
- case 628: //恒栋车队
- $user_id = 487; //admin_007:恒栋管理员
- break;
- case 403: //千旅车队
- $user_id = 559; //admin_004:千旅管理员
- break;
- case 1284: //黄山长三角车队
- $user_id = 579; //admin_011:郝贵福
- break;
- case 1288: //苏州舟游车队
- $user_id = 582; //周慧
- break;
- default:
- $user_id = 0;
- }
- $bus_order->create_user_id = $user_id;
- $key = 0;
-
- foreach ($stations as $zz_model) {
- $BusItinerary = new BusItinerary();
- $BusItinerary->station_seq_id = $key + 1;
- $BusItinerary->station_name = $zz_model['start_station'];
- $BusItinerary->station_address = $zz_model['start_station'];
- $BusItinerary->inout_type = 108; //仅上客
- $BusItinerary->day_seq_id = 1;
- $BusItinerary->Longitude = $zz_model['longitude'];
- $BusItinerary->Latitude = $zz_model['latitude'];
- $BusItinerary->start_time = $zz_model['start_time'];
- $bus_order->itinerary_list[] = $BusItinerary;
- $key++;
- }
- //黄浦集散站
- $bus_order->itinerary_list[] = $this->getEndStation($data, $key);
- if ($bus_order->addBus()) {
- $key = 0;
- $arr = $bus_order->itinerary;
- $model = $stations[0];
- foreach ($arr as $res) {
- $res_id = $res->id;
- $end_seq_id = count($stations);
- if ($key < count($arr) - 1) {
- $model = $stations[$key];
- ZizaiOrderTemp::updateOrderMain($model, $res_id, $key + 1, $end_seq_id + 1);
- } else {
- OrderMain::updateAll([
- 'prod_end_station_res_id' => $res_id,
- ],
- ['run_id' => $model->run_id, 'RUN_BUS_ORDER_ID' => $model->bus_order_id]);
- }
- $res->station_res_id = $res_id;
- $res->save(false);
- $key++;
- }
- ZizaiOrderTemp::updateAll(['order_status' => ZizaiOrderTemp::STATUS_SYNC], ['run_bus_id'=> $model->run_bus_id]);
- return true;
- } else {
- return false;
- }
- }
-
- /**
- * User: wangxj
- *
- * 定时任务,自动生成出车任务,不考虑订单数量和人数
- *
- */
- public function actionAutoOrder()
- {
- // ::todo 定时
- $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';
- $list = Yii::$app->db->createCommand($sql)
- ->queryAll();
- foreach ($list as $item) {
- $this->createBusOrder($item, $item['order_id']);
- }
-
- }
-
- }
|