|
- <?php
- /**
- * 车辆设置通用函数
- * ============================================================================
- * * 版权所有 蜘蛛出行 * *
- * 网站地址: http://www.zhizhuchuxing.com
- * ----------------------------------------------------------------------------
- * 这不是一个自由软件!您只能在不用于商业目的的前提下对程序代码进行修改和
- * 使用;不允许对程序代码以任何形式任何目的的再发布。
- * ============================================================================
- * Author By: 倪宗锋
- * PhpStorm BusService.php
- * Create By 2017/10/11 10:35 $
- */
-
- namespace backend\modules\zzcs\logic;
-
-
- use backend\modules\api\logic\ChangeSeat;
- use backend\modules\api\models\OrderMain;
- use backend\modules\zzcs\models\BaseBus;
- use backend\modules\zzcs\models\BaseResourceMatrix;
- use backend\modules\zzcs\models\BusSeat;
- use backend\modules\zzcs\models\BusSeatMatrix;
- use backend\modules\zzcs\models\RunBus;
- use backend\modules\zzcs\models\RunStock;
- use backend\modules\zzcs\models\RunX;
- use backend\modules\zzcs\util\Util;
- use yii\db\Exception;
-
- class BusService
- {
- private static $_service = null;
-
- //私有构造函数,防止外界实例化对象
- private function __construct()
- {
- }
-
- //私有克隆函数,防止外办克隆对象
- private function __clone()
- {
- }
-
- /**
- * Des:静态方法,单例统一访问入口
- * Name: init
- * @return BusService|null
- * @author 倪宗锋
- */
- static public function init()
- {
- if (static::$_service == null) {
- static::$_service = new static();
- }
- return self::$_service;
- }
-
- /**
- * Des:车辆变更
- * Name: changeBusForRunBus
- * @param $user_id int 用户ID
- * @param $bus_id int 车辆ID
- * @param $run_bus_id int run_bus_id 班次车次表run_bus 的ID 原班次安排信息
- * @return array
- * @author 倪宗锋
- */
- public function changeBusForRunBus($user_id, $bus_id, $run_bus_id)
- {
- if (intval($user_id) == 0 || intval($bus_id) == 0 || intval($run_bus_id) == 0) {
- return Util::returnArrEr('参数错误!');
- }
- /**--------------获取原派车班次信息**/
- /**获取原派车班次信息**/
- $run_bus = new RunBus();
- $getRunBusInfo = $run_bus->getRunBusById($run_bus_id);
- if (empty($getRunBusInfo['run_id'])) {
- return Util::returnArrEr('车辆班次信息获取失败!');
- }
- //获取座位使用情况
- $run_x = new RunX($getRunBusInfo['run_x_tab']);
- $getSeatOccupy = $run_x->getSeatOccupyMax($getRunBusInfo['run_id'], $getRunBusInfo['bus_order_id']);//获取座位使用情况
- /**--------------获取新派车辆的信息**/
- $base_bus = new BaseBus();
- $newBusInfo = $base_bus->getInfoByBusId($bus_id);
- if (empty($newBusInfo['BUS_ID'])) {
- return Util::returnArrEr('新派车辆的信息获取失败!');
- }
- //获取所派车辆总座位数
- $getBusSeat = $this->getBusSeatCount($newBusInfo);
- if ($getBusSeat['flag'] == false) {
- return $getBusSeat;
- }
-
- /**--------------判断是否可以派车**/
- if (!in_array($getRunBusInfo['run_bus_status'], [138, 139])) {
- return Util::returnArrEr('非售卖中和出行中的车次不能进行调派!');
- }
- if ($getSeatOccupy > $newBusInfo['SEAT_COUNT']) {
- return Util::returnArrEr('有乘客已经预定了序号「' . $getSeatOccupy . '」的座位!所派车辆座位数量不支持!');
- }
- if ($getBusSeat == 0) {
- return Util::returnArrEr('未找到所派车辆的车型座位图!');
- }
- $transaction = \Yii::$app->db->beginTransaction();
- /**--------------变更数据库数据**/
- try {
- //4.更新时间派车信息
- $upRunBusFlag = $run_bus->changeBus($bus_id, $newBusInfo, $run_bus_id);
- if ($upRunBusFlag == false) {
- $transaction->rollBack();
- return Util::returnArrEr('变更车次与车辆关系失败!');
- }
- //5.更新新派车辆的状态信息
- $run_bus->changeStatus($run_bus_id);
- //6.由于班次中当前车次车辆变更了,所以需要将班次总库存更新
- $run_stock = new RunStock();
- $run_stock->editSeatCountByRunId($getRunBusInfo['run_id']);
- //7.更新车辆座位图
- $updateRunX = $this->updateRunXForNewBus($getRunBusInfo, $newBusInfo, $user_id);
- if ($updateRunX['flag'] == false) {
- $transaction->rollBack();
- return $updateRunX;
- }
- //8.更新order_main表座位名称
- $updateOrderMain = $this->updateOrderMainForNewBus($getRunBusInfo, $newBusInfo, $user_id);
- if ($updateOrderMain['flag'] == false) {
- $transaction->rollBack();
- return $updateOrderMain;
- }
- //设置完成提交
- $transaction->commit();
- return Util::returnArrSu();
- } catch (Exception $e) {
- $transaction->rollBack();
- return Util::returnArrEr('设置失败,请联系管理员!');
- }
-
- }
-
- /**
- * Des:重置巴士run_x表座位图信息
- * Name: updateRunXForNewBus
- * @param $getRunBusInfo
- * @param $newBusInfo
- * @param $user_id
- * @return array
- * @author 倪宗锋
- */
- private function updateRunXForNewBus($getRunBusInfo, $newBusInfo, $user_id)
- {
- try {
- $run_x = new RunX($getRunBusInfo['run_x_tab']);//初始化run_x表
- if ($newBusInfo['SEAT_MATRIX_ID'] == 0) {//使用的默认座位图
- $run_x->updateRunXForNewBusByOldMatrix($getRunBusInfo['run_id'], $getRunBusInfo['bus_order_id'], $newBusInfo['bus_type_res_id'], $newBusInfo['SEAT_COUNT'], $user_id);
- } else {//使用的新版座位图
- $run_x->updateRunXForNewBusByNewMatrix($getRunBusInfo['run_id'], $getRunBusInfo['bus_order_id'], $newBusInfo['SEAT_MATRIX_ID'], $newBusInfo['SEAT_COUNT'], $user_id);
- }
- return Util::returnArrSu();
- } catch (Exception $e) {
- return Util::returnArrEr('设置run_x座位失败!');
- }
- }
-
- /**
- * Des:换车更新order_main表座位图信息
- * Name: updateOrderMainForNewBus
- * @param $getRunBusInfo
- * @param $newBusInfo
- * @param $user_id
- * @return array
- * @author 倪宗锋
- */
- private function updateOrderMainForNewBus($getRunBusInfo, $newBusInfo, $user_id)
- {
- try {
- $orderMain = new OrderMain();//初始化run_x表
- if ($newBusInfo['SEAT_MATRIX_ID'] == 0) {//使用的默认座位图
- $orderMain->updateOrderMainForNewBusByOldMatrix($getRunBusInfo['run_id'], $getRunBusInfo['bus_order_id'], $newBusInfo['bus_type_res_id'], $user_id);
- } else {//使用的新版座位图
- $orderMain->updateOrderMainForNewBusByNewMatrix($getRunBusInfo['run_id'], $getRunBusInfo['bus_order_id'], $newBusInfo['SEAT_MATRIX_ID'], $user_id);
- }
- return Util::returnArrSu();
- } catch (Exception $e) {
- return Util::returnArrEr('更新order_main座位名称失败!');
- }
- }
-
- /**
- * Des:获取车的座位总数
- * Name: getBusSeatCount
- * @param $busInfo
- * @return array
- * @author 倪宗锋
- */
- private function getBusSeatCount($busInfo)
- {
- if ($busInfo['SEAT_MATRIX_ID'] != 0) {
- $busSeatTab = new BusSeatMatrix();
- $seatCount = $busSeatTab->getSeatCount($busInfo['SEAT_MATRIX_ID']);
- } elseif ($busInfo['bus_type_res_id'] != 0) {
- $busSeatTab = new BaseResourceMatrix();
- $seatCount = $busSeatTab->getSeatCount($busInfo['bus_type_res_id']);
- } else {
- return Util::returnArrEr('车辆没有座位图!');
- }
- return Util::returnArrSu('', ['seatCount' => $seatCount]);
- }
-
- /*************选择座位************************/
- /**
- * Des:选择座位
- * Name: chooseSeat
- * @param $order_id
- * @param $seq_id
- * @return array
- * @author 倪宗锋
- */
- public function chooseSeat($order_id, $seq_id)
- {
- //1.参数校验正确性
- if (preg_match('/^[1-9][0-9]{0,10}$/', $order_id) == false) {
- return Util::returnArrEr('参数错误![order_id]');
- }
- if (preg_match('/^([1-9][0-9]{0,10},?){1,}$/', $seq_id) == false) {
- return Util::returnArrEr('参数错误![seq_id]');
- }
- $seq_id_arr = explode(',', $seq_id);//座位图序号ID
- //2.判断可选座位数是否和选择的座位数一致
- $order_main = new OrderMain();
- $orderInfo = $order_main->getOrderInfoForGetSeat($order_id);//获取子订单
- if ($orderInfo['cnt'] != count($seq_id_arr)) {
- return Util::returnArrEr('需选择' . $orderInfo['cnt'] . '个座位!');
- }
- $order_array = explode(',', $orderInfo['son_order']);//订单数组
- //3、判断订单是否可以选择
- $change_seat = new ChangeSeat();
- $getSeatInfo = $change_seat->getSeatInfo($order_id);//获取订单座位图表
- if (!isset($getSeatInfo['code']) || $getSeatInfo['code'] != 0) {
- $msg = empty($getSeatInfo['info']) ? '该线路不支持选座![10003]' : $getSeatInfo['info'];
- return Util::returnArrEr($msg);
- }
- $seat_list = $getSeatInfo['list'];//座位图列表
- foreach ($seq_id_arr as $val) {//循环判断 选择的位置是否可以选择
- if (isset($seat_list[$val]['seat_status']) == false || $seat_list[$val]['seat_status'] == 2) {
- $seat_name = $seat_list[$val]['seat_name'];
- return Util::returnArrEr("座位「{$seat_name}」已经被他人选择,请选择其他座位!", ['seq_id' => $val]);
- }
- }
- //4.更换座位
- $transaction = \Yii::$app->db->beginTransaction();
- try {
- foreach ($seq_id_arr as $key => $seq_id) {
- if (empty($order_array[$key])) {
- $transaction->rollBack();
- return Util::returnArrEr('操作失败,请稍后再试![10001]');//车票数量和座位数量不一致!
- }
- $sun_order_id = $order_array[$key];//订单ID
- $run_bus_order_id = $orderInfo['run_bus_order_id'];//班次车号ID
- $run_id = $orderInfo['run_id'];//班次ID
- $run_x = new RunX($orderInfo['run_x']);//初始化run_x表
- //-1- 修改order_main座位信息
- $run_x->editOrderMainForChangeSeat($sun_order_id, $run_bus_order_id, $run_id, $seq_id);
- //-2- 修改run_x表座位位置
- $run_x->chooseSeat($sun_order_id, $run_bus_order_id, $run_id, $seq_id);
- }
- $transaction->commit();
- return Util::returnArrSu();
- } catch (\yii\base\Exception $e) {
- $transaction->rollBack();
- return Util::returnArrEr('操作失败,请稍后再试![10000]');
- }
- }
-
- /**
- * Des:初始化选择页面接口
- * Name: initChooseSeat
- * @param $order_id
- * @return array
- * @author 倪宗锋
- */
- public function initChooseSeat($order_id)
- {
- $order_main = new OrderMain();
- $orderInfo = $order_main->getOrderInfoForGetSeat($order_id);//获取子订单
- //3、判断订单是否可以选择
- $change_seat = new ChangeSeat();
- $getSeatInfo = $change_seat->getSeatInfo($order_id);//获取订单座位图表
- if (!isset($getSeatInfo['code']) || $getSeatInfo['code'] != 0) {
- $msg = empty($getSeatInfo['info']) ? '该线路不支持选座![10003]' : $getSeatInfo['info'];
- return Util::returnArrEr($msg);
- }
- $return['seat_array'] = array_values($getSeatInfo['list']);
- $busSeatMi = new BusSeatMatrix();
- $getList = $busSeatMi->getListForShow($getSeatInfo['SEAT_MATRIX_ID']);
- $return['seat_list'] = $getList;
- $return['person_cnt'] = $orderInfo['cnt'];
- return Util::returnArrSu('', $return);
-
- }
- }
|