301 lines
12 KiB

  1. <?php
  2. /**
  3. * 车辆设置通用函数
  4. * ============================================================================
  5. * * 版权所有 蜘蛛出行 * *
  6. * 网站地址: http://www.zhizhuchuxing.com
  7. * ----------------------------------------------------------------------------
  8. * 这不是一个自由软件!您只能在不用于商业目的的前提下对程序代码进行修改和
  9. * 使用;不允许对程序代码以任何形式任何目的的再发布。
  10. * ============================================================================
  11. * Author By: 倪宗锋
  12. * PhpStorm BusService.php
  13. * Create By 2017/10/11 10:35 $
  14. */
  15. namespace backend\modules\zzcs\logic;
  16. use backend\modules\api\logic\ChangeSeat;
  17. use backend\modules\api\models\OrderMain;
  18. use backend\modules\zzcs\models\BaseBus;
  19. use backend\modules\zzcs\models\BaseResourceMatrix;
  20. use backend\modules\zzcs\models\BusSeat;
  21. use backend\modules\zzcs\models\BusSeatMatrix;
  22. use backend\modules\zzcs\models\RunBus;
  23. use backend\modules\zzcs\models\RunStock;
  24. use backend\modules\zzcs\models\RunX;
  25. use backend\modules\zzcs\util\Util;
  26. use yii\db\Exception;
  27. class BusService
  28. {
  29. private static $_service = null;
  30. //私有构造函数,防止外界实例化对象
  31. private function __construct()
  32. {
  33. }
  34. //私有克隆函数,防止外办克隆对象
  35. private function __clone()
  36. {
  37. }
  38. /**
  39. * Des:静态方法,单例统一访问入口
  40. * Name: init
  41. * @return BusService|null
  42. * @author 倪宗锋
  43. */
  44. static public function init()
  45. {
  46. if (static::$_service == null) {
  47. static::$_service = new static();
  48. }
  49. return self::$_service;
  50. }
  51. /**
  52. * Des:车辆变更
  53. * Name: changeBusForRunBus
  54. * @param $user_id int 用户ID
  55. * @param $bus_id int 车辆ID
  56. * @param $run_bus_id int run_bus_id 班次车次表run_bus 的ID 原班次安排信息
  57. * @return array
  58. * @author 倪宗锋
  59. */
  60. public function changeBusForRunBus($user_id, $bus_id, $run_bus_id)
  61. {
  62. if (intval($user_id) == 0 || intval($bus_id) == 0 || intval($run_bus_id) == 0) {
  63. return Util::returnArrEr('参数错误!');
  64. }
  65. /**--------------获取原派车班次信息**/
  66. /**获取原派车班次信息**/
  67. $run_bus = new RunBus();
  68. $getRunBusInfo = $run_bus->getRunBusById($run_bus_id);
  69. if (empty($getRunBusInfo['run_id'])) {
  70. return Util::returnArrEr('车辆班次信息获取失败!');
  71. }
  72. //获取座位使用情况
  73. $run_x = new RunX($getRunBusInfo['run_x_tab']);
  74. $getSeatOccupy = $run_x->getSeatOccupyMax($getRunBusInfo['run_id'], $getRunBusInfo['bus_order_id']);//获取座位使用情况
  75. /**--------------获取新派车辆的信息**/
  76. $base_bus = new BaseBus();
  77. $newBusInfo = $base_bus->getInfoByBusId($bus_id);
  78. if (empty($newBusInfo['BUS_ID'])) {
  79. return Util::returnArrEr('新派车辆的信息获取失败!');
  80. }
  81. //获取所派车辆总座位数
  82. $getBusSeat = $this->getBusSeatCount($newBusInfo);
  83. if ($getBusSeat['flag'] == false) {
  84. return $getBusSeat;
  85. }
  86. /**--------------判断是否可以派车**/
  87. if (!in_array($getRunBusInfo['run_bus_status'], [138, 139])) {
  88. return Util::returnArrEr('非售卖中和出行中的车次不能进行调派!');
  89. }
  90. if ($getSeatOccupy > $newBusInfo['SEAT_COUNT']) {
  91. return Util::returnArrEr('有乘客已经预定了序号「' . $getSeatOccupy . '」的座位!所派车辆座位数量不支持!');
  92. }
  93. if ($getBusSeat == 0) {
  94. return Util::returnArrEr('未找到所派车辆的车型座位图!');
  95. }
  96. $transaction = \Yii::$app->db->beginTransaction();
  97. /**--------------变更数据库数据**/
  98. try {
  99. //4.更新时间派车信息
  100. $upRunBusFlag = $run_bus->changeBus($bus_id, $newBusInfo, $run_bus_id);
  101. if ($upRunBusFlag == false) {
  102. $transaction->rollBack();
  103. return Util::returnArrEr('变更车次与车辆关系失败!');
  104. }
  105. //5.更新新派车辆的状态信息
  106. $run_bus->changeStatus($run_bus_id);
  107. //6.由于班次中当前车次车辆变更了,所以需要将班次总库存更新
  108. $run_stock = new RunStock();
  109. $run_stock->editSeatCountByRunId($getRunBusInfo['run_id']);
  110. //7.更新车辆座位图
  111. $updateRunX = $this->updateRunXForNewBus($getRunBusInfo, $newBusInfo, $user_id);
  112. if ($updateRunX['flag'] == false) {
  113. $transaction->rollBack();
  114. return $updateRunX;
  115. }
  116. //8.更新order_main表座位名称
  117. $updateOrderMain = $this->updateOrderMainForNewBus($getRunBusInfo, $newBusInfo, $user_id);
  118. if ($updateOrderMain['flag'] == false) {
  119. $transaction->rollBack();
  120. return $updateOrderMain;
  121. }
  122. //设置完成提交
  123. $transaction->commit();
  124. return Util::returnArrSu();
  125. } catch (Exception $e) {
  126. $transaction->rollBack();
  127. return Util::returnArrEr('设置失败,请联系管理员!');
  128. }
  129. }
  130. /**
  131. * Des:重置巴士run_x表座位图信息
  132. * Name: updateRunXForNewBus
  133. * @param $getRunBusInfo
  134. * @param $newBusInfo
  135. * @param $user_id
  136. * @return array
  137. * @author 倪宗锋
  138. */
  139. private function updateRunXForNewBus($getRunBusInfo, $newBusInfo, $user_id)
  140. {
  141. try {
  142. $run_x = new RunX($getRunBusInfo['run_x_tab']);//初始化run_x表
  143. if ($newBusInfo['SEAT_MATRIX_ID'] == 0) {//使用的默认座位图
  144. $run_x->updateRunXForNewBusByOldMatrix($getRunBusInfo['run_id'], $getRunBusInfo['bus_order_id'], $newBusInfo['bus_type_res_id'], $newBusInfo['SEAT_COUNT'], $user_id);
  145. } else {//使用的新版座位图
  146. $run_x->updateRunXForNewBusByNewMatrix($getRunBusInfo['run_id'], $getRunBusInfo['bus_order_id'], $newBusInfo['SEAT_MATRIX_ID'], $newBusInfo['SEAT_COUNT'], $user_id);
  147. }
  148. return Util::returnArrSu();
  149. } catch (Exception $e) {
  150. return Util::returnArrEr('设置run_x座位失败!');
  151. }
  152. }
  153. /**
  154. * Des:换车更新order_main表座位图信息
  155. * Name: updateOrderMainForNewBus
  156. * @param $getRunBusInfo
  157. * @param $newBusInfo
  158. * @param $user_id
  159. * @return array
  160. * @author 倪宗锋
  161. */
  162. private function updateOrderMainForNewBus($getRunBusInfo, $newBusInfo, $user_id)
  163. {
  164. try {
  165. $orderMain = new OrderMain();//初始化run_x表
  166. if ($newBusInfo['SEAT_MATRIX_ID'] == 0) {//使用的默认座位图
  167. $orderMain->updateOrderMainForNewBusByOldMatrix($getRunBusInfo['run_id'], $getRunBusInfo['bus_order_id'], $newBusInfo['bus_type_res_id'], $user_id);
  168. } else {//使用的新版座位图
  169. $orderMain->updateOrderMainForNewBusByNewMatrix($getRunBusInfo['run_id'], $getRunBusInfo['bus_order_id'], $newBusInfo['SEAT_MATRIX_ID'], $user_id);
  170. }
  171. return Util::returnArrSu();
  172. } catch (Exception $e) {
  173. return Util::returnArrEr('更新order_main座位名称失败!');
  174. }
  175. }
  176. /**
  177. * Des:获取车的座位总数
  178. * Name: getBusSeatCount
  179. * @param $busInfo
  180. * @return array
  181. * @author 倪宗锋
  182. */
  183. private function getBusSeatCount($busInfo)
  184. {
  185. if ($busInfo['SEAT_MATRIX_ID'] != 0) {
  186. $busSeatTab = new BusSeatMatrix();
  187. $seatCount = $busSeatTab->getSeatCount($busInfo['SEAT_MATRIX_ID']);
  188. } elseif ($busInfo['bus_type_res_id'] != 0) {
  189. $busSeatTab = new BaseResourceMatrix();
  190. $seatCount = $busSeatTab->getSeatCount($busInfo['bus_type_res_id']);
  191. } else {
  192. return Util::returnArrEr('车辆没有座位图!');
  193. }
  194. return Util::returnArrSu('', ['seatCount' => $seatCount]);
  195. }
  196. /*************选择座位************************/
  197. /**
  198. * Des:选择座位
  199. * Name: chooseSeat
  200. * @param $order_id
  201. * @param $seq_id
  202. * @return array
  203. * @author 倪宗锋
  204. */
  205. public function chooseSeat($order_id, $seq_id)
  206. {
  207. //1.参数校验正确性
  208. if (preg_match('/^[1-9][0-9]{0,10}$/', $order_id) == false) {
  209. return Util::returnArrEr('参数错误![order_id]');
  210. }
  211. if (preg_match('/^([1-9][0-9]{0,10},?){1,}$/', $seq_id) == false) {
  212. return Util::returnArrEr('参数错误![seq_id]');
  213. }
  214. $seq_id_arr = explode(',', $seq_id);//座位图序号ID
  215. //2.判断可选座位数是否和选择的座位数一致
  216. $order_main = new OrderMain();
  217. $orderInfo = $order_main->getOrderInfoForGetSeat($order_id);//获取子订单
  218. if ($orderInfo['cnt'] != count($seq_id_arr)) {
  219. return Util::returnArrEr('需选择' . $orderInfo['cnt'] . '个座位!');
  220. }
  221. $order_array = explode(',', $orderInfo['son_order']);//订单数组
  222. //3、判断订单是否可以选择
  223. $change_seat = new ChangeSeat();
  224. $getSeatInfo = $change_seat->getSeatInfo($order_id);//获取订单座位图表
  225. if (!isset($getSeatInfo['code']) || $getSeatInfo['code'] != 0) {
  226. $msg = empty($getSeatInfo['info']) ? '该线路不支持选座![10003]' : $getSeatInfo['info'];
  227. return Util::returnArrEr($msg);
  228. }
  229. $seat_list = $getSeatInfo['list'];//座位图列表
  230. foreach ($seq_id_arr as $val) {//循环判断 选择的位置是否可以选择
  231. if (isset($seat_list[$val]['seat_status']) == false || $seat_list[$val]['seat_status'] == 2) {
  232. $seat_name = $seat_list[$val]['seat_name'];
  233. return Util::returnArrEr("座位「{$seat_name}」已经被他人选择,请选择其他座位!", ['seq_id' => $val]);
  234. }
  235. }
  236. //4.更换座位
  237. $transaction = \Yii::$app->db->beginTransaction();
  238. try {
  239. foreach ($seq_id_arr as $key => $seq_id) {
  240. if (empty($order_array[$key])) {
  241. $transaction->rollBack();
  242. return Util::returnArrEr('操作失败,请稍后再试![10001]');//车票数量和座位数量不一致!
  243. }
  244. $sun_order_id = $order_array[$key];//订单ID
  245. $run_bus_order_id = $orderInfo['run_bus_order_id'];//班次车号ID
  246. $run_id = $orderInfo['run_id'];//班次ID
  247. $run_x = new RunX($orderInfo['run_x']);//初始化run_x表
  248. //-1- 修改order_main座位信息
  249. $run_x->editOrderMainForChangeSeat($sun_order_id, $run_bus_order_id, $run_id, $seq_id);
  250. //-2- 修改run_x表座位位置
  251. $run_x->chooseSeat($sun_order_id, $run_bus_order_id, $run_id, $seq_id);
  252. }
  253. $transaction->commit();
  254. return Util::returnArrSu();
  255. } catch (\yii\base\Exception $e) {
  256. $transaction->rollBack();
  257. return Util::returnArrEr('操作失败,请稍后再试![10000]');
  258. }
  259. }
  260. /**
  261. * Des:初始化选择页面接口
  262. * Name: initChooseSeat
  263. * @param $order_id
  264. * @return array
  265. * @author 倪宗锋
  266. */
  267. public function initChooseSeat($order_id)
  268. {
  269. $order_main = new OrderMain();
  270. $orderInfo = $order_main->getOrderInfoForGetSeat($order_id);//获取子订单
  271. //3、判断订单是否可以选择
  272. $change_seat = new ChangeSeat();
  273. $getSeatInfo = $change_seat->getSeatInfo($order_id);//获取订单座位图表
  274. if (!isset($getSeatInfo['code']) || $getSeatInfo['code'] != 0) {
  275. $msg = empty($getSeatInfo['info']) ? '该线路不支持选座![10003]' : $getSeatInfo['info'];
  276. return Util::returnArrEr($msg);
  277. }
  278. $return['seat_array'] = array_values($getSeatInfo['list']);
  279. $busSeatMi = new BusSeatMatrix();
  280. $getList = $busSeatMi->getListForShow($getSeatInfo['SEAT_MATRIX_ID']);
  281. $return['seat_list'] = $getList;
  282. $return['person_cnt'] = $orderInfo['cnt'];
  283. return Util::returnArrSu('', $return);
  284. }
  285. }