Index.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628
  1. <?php
  2. namespace app\api\controller\coach;
  3. use app\common\controller\Apic;
  4. use app\common\library\Sms;
  5. use think\Exception;
  6. use think\Validate;
  7. use think\Db;
  8. /**
  9. * 首页
  10. */
  11. class Index extends Apic
  12. {
  13. protected $noNeedLogin = [];
  14. protected $noNeedRight = '*';
  15. //首页
  16. public function index(){
  17. //先判断是否有进行中的,不论日期,防止有跨夜班车
  18. $map = [
  19. 'fache_status|fache_ready' => 1,
  20. 'coach_id' => $this->auth->id,
  21. ];
  22. $check = Db::name('product_chufabanci')->where($map)->find();
  23. if($check){
  24. //取详情
  25. $info = $this->banci_info($check['id']);
  26. $result = ['datetype'=>'info','info'=>$info,'list'=>[]];
  27. $this->success(1,$result);
  28. }else{
  29. //取今日列表,最靠前的一个
  30. $list = $this->index_old();
  31. $result = ['datetype'=>'list','info'=>(object)[],'list'=>$list];
  32. $this->success(1,$result);
  33. }
  34. }
  35. //原首页
  36. private function index_old(){
  37. $starttime = strtotime(date('Y-m-d'));
  38. $endtime = $starttime + 86399;
  39. //我的 今天未发车班次
  40. $map = [
  41. 'bc.fache_status' => 0,
  42. 'bc.fache_ready' => 0,
  43. 'bc.coach_id' => $this->auth->id,
  44. 'bc.chufatime' => ['BETWEEN',[$starttime,$endtime]],
  45. ];
  46. $list = Db::name('product_chufabanci')->alias('bc')
  47. ->field('bc.*,
  48. road.roadname,
  49. coach.mobile as sijimobile
  50. ')
  51. ->join('product_road road','bc.road_id = road.id','LEFT')
  52. ->join('coach','bc.coach_id = coach.id','LEFT')
  53. ->where($map)->order('chufatime asc')
  54. ->limit(1)->select();
  55. if(!empty($list)){
  56. foreach($list as $key => &$val){
  57. //起始点默认值
  58. $val['startname'] = $val['roadname'];
  59. $val['endname'] = $val['roadname'];
  60. //起始点分割,临时用这个,客户说不重要
  61. $jieduan = mb_strpos($val['roadname'],'-');
  62. if($jieduan !== false){
  63. $val['startname'] = mb_substr($val['roadname'],0,$jieduan);
  64. $val['endname'] = mb_substr($val['roadname'],$jieduan+1);
  65. }
  66. //售票数量。一个司机一天最多2班车,这里不影响
  67. $shou_num = Db::name('order_road')->alias('road')
  68. ->join('order','road.order_id = order.id','LEFT')
  69. ->where('road.chufabanci_id',$val['id'])
  70. // ->where('road.hexiao_status',0)
  71. ->where('order.status','IN',[1,2])
  72. ->sum('ticket_number');
  73. $val['shou_num'] = $shou_num;
  74. //验票数量
  75. $yan_num = Db::name('order_road')->alias('road')
  76. ->join('order','road.order_id = order.id','LEFT')
  77. ->where('road.chufabanci_id',$val['id'])
  78. ->where('road.hexiao_status',1)
  79. ->where('order.status','IN',[1,2])
  80. ->sum('ticket_number');
  81. $val['yan_num'] = $yan_num;
  82. }
  83. }
  84. return $list;
  85. }
  86. //班次列表,和上面首页接口一样的,多了接参
  87. public function banci_list(){
  88. //日期
  89. $today = date('Y-m-d');
  90. $startdate = input('date',$today);
  91. if(empty($startdate)){
  92. $startdate = $today;
  93. }
  94. $starttime = strtotime($startdate);
  95. $endtime = $starttime + 86399;
  96. //状态
  97. $fache_status = input('fache_status',1);
  98. if($fache_status == 1){
  99. $status_map = ['IN',[0,1]];
  100. }else{
  101. $status_map = 2;
  102. }
  103. $map = [
  104. 'bc.fache_status' => $status_map,
  105. 'bc.coach_id' => $this->auth->id,
  106. 'bc.chufatime' => ['BETWEEN',[$starttime,$endtime]],
  107. ];
  108. $list = Db::name('product_chufabanci')->alias('bc')
  109. ->field('bc.*,
  110. road.roadname,
  111. coach.mobile as sijimobile
  112. ')
  113. ->join('product_road road','bc.road_id = road.id','LEFT')
  114. ->join('coach','bc.coach_id = coach.id','LEFT')
  115. ->where($map)->order('chufatime asc')
  116. ->select();
  117. if(!empty($list)){
  118. $have_ready = 0;
  119. foreach($list as $key => &$val){
  120. //起始点默认值
  121. $val['startname'] = $val['roadname'];
  122. $val['endname'] = $val['roadname'];
  123. //起始点分割,临时用这个,客户说不重要
  124. $jieduan = mb_strpos($val['roadname'],'-');
  125. if($jieduan !== false){
  126. $val['startname'] = mb_substr($val['roadname'],0,$jieduan);
  127. $val['endname'] = mb_substr($val['roadname'],$jieduan+1);
  128. }
  129. //售票数量。一个司机一天最多2班车,这里不影响
  130. $shou_num = Db::name('order_road')->alias('road')
  131. ->join('order','road.order_id = order.id','LEFT')
  132. ->where('road.chufabanci_id',$val['id'])
  133. // ->where('road.hexiao_status',0)
  134. ->where('order.status','IN',[1,2])
  135. ->sum('ticket_number');
  136. $val['shou_num'] = $shou_num;
  137. //验票数量
  138. $yan_num = Db::name('order_road')->alias('road')
  139. ->join('order','road.order_id = order.id','LEFT')
  140. ->where('road.chufabanci_id',$val['id'])
  141. ->where('road.hexiao_status',1)
  142. ->where('order.status','IN',[1,2])
  143. ->sum('ticket_number');
  144. $val['yan_num'] = $yan_num;
  145. //是否有人准备或服务中
  146. if($val['fache_ready'] == 1 || $val['fache_status'] == 1){
  147. $have_ready = 1;
  148. }
  149. }
  150. //是否能点准备与继续服务
  151. $today_start = strtotime(date('Y-m-d'));
  152. $today_end = $today_start + 86399;
  153. foreach($list as $keynew => $valnew){
  154. if($valnew['chufatime'] < $today_start || $valnew['chufatime'] > $today_end){
  155. //不是今天,没按钮
  156. $valnew['fuwu_button'] = 0;
  157. }else{
  158. if($have_ready == 1){
  159. //默认不能点
  160. $valnew['fuwu_button'] = 0;
  161. //已经发车的能点
  162. if($valnew['fache_ready'] == 1 || $valnew['fache_status'] == 1){
  163. $valnew['fuwu_button'] = 2;
  164. }
  165. }else{
  166. //默认能点,按钮为发车
  167. $valnew['fuwu_button'] = 1;
  168. //到站的不能点
  169. if($valnew['fache_status'] == 2){
  170. $valnew['fuwu_button'] = 0;
  171. }
  172. }
  173. }
  174. $list[$keynew] = $valnew;
  175. }
  176. }
  177. $this->success(1,$list);
  178. }
  179. //班次详情
  180. public function banci_info($banci_id_return = false){
  181. $banci_id = input('banci_id',0);
  182. if($banci_id_return !== false){
  183. $banci_id = $banci_id_return;
  184. }
  185. $map = [
  186. 'bc.id' => $banci_id,
  187. ];
  188. $val = Db::name('product_chufabanci')->alias('bc')
  189. ->field('bc.*,
  190. road.roadname,
  191. coach.mobile as sijimobile,
  192. car.chepaihao
  193. ')
  194. ->join('product_road road','bc.road_id = road.id','LEFT')
  195. ->join('coach','bc.coach_id = coach.id','LEFT')
  196. ->join('car','bc.car_id = car.id','LEFT')
  197. ->where($map)->find();
  198. //起始点默认值
  199. $val['startname'] = $val['roadname'];
  200. $val['endname'] = $val['roadname'];
  201. //起始点分割,临时用这个,客户说不重要
  202. $jieduan = mb_strpos($val['roadname'],'-');
  203. if($jieduan !== false){
  204. $val['startname'] = mb_substr($val['roadname'],0,$jieduan);
  205. $val['endname'] = mb_substr($val['roadname'],$jieduan+1);
  206. }
  207. //售票数量。一个司机一天最多2班车,这里不影响
  208. $shou_num = Db::name('order_road')->alias('road')
  209. ->join('order','road.order_id = order.id','LEFT')
  210. ->where('road.chufabanci_id',$val['id'])
  211. // ->where('road.hexiao_status',0)
  212. ->where('order.status','IN',[1,2])
  213. ->sum('ticket_number');
  214. $val['shou_num'] = $shou_num;
  215. //验票数量
  216. $yan_num = Db::name('order_road')->alias('road')
  217. ->join('order','road.order_id = order.id','LEFT')
  218. ->where('road.chufabanci_id',$val['id'])
  219. ->where('road.hexiao_status',1)
  220. ->where('order.status','IN',[1,2])
  221. ->sum('ticket_number');
  222. $val['yan_num'] = $yan_num;
  223. //售验票情况
  224. $val['shouyan_list'] = $this->shouyan_list($banci_id);
  225. if($banci_id_return !== false){
  226. return $val;
  227. }
  228. $this->success(1,$val);
  229. }
  230. //售验票列表
  231. private function shouyan_list($banci_id){
  232. $order_road = Db::name('order_road')->alias('road')
  233. ->field('road.id as road_id,road.chufaname,road.hexiao_no,road.ticket_number,road.hexiao_status,
  234. order.realname,order.mobile')
  235. ->join('order','road.order_id = order.id','LEFT')
  236. ->where('road.chufabanci_id',$banci_id)
  237. ->where('order.status','IN',[1,2]) //付完款和已完成的
  238. ->order('road.hexiao_status desc,road.id asc')
  239. ->select();
  240. if(empty($order_road)){
  241. return [];
  242. }
  243. return $order_road;
  244. }
  245. //发出准备 提交
  246. public function banci_ready(){
  247. $this->apiLimit();
  248. //检查其他准备中的
  249. $check = Db::name('product_chufabanci')->where('coach_id',$this->auth->id)->where('fache_ready = 1 or fache_status = 1')->find();
  250. if(!empty($check)){
  251. $this->error('您已经有其他班次在服务中');
  252. }
  253. //开始
  254. $banci_id = input('banci_id',0);
  255. $info = Db::name('product_chufabanci')->where('id',$banci_id)->find();
  256. if(empty($info)){
  257. $this->error('不存在的班次');
  258. }
  259. if($info['fache_status'] != 0){
  260. $this->error('该班次发车状态有误,请刷新列表重试');
  261. }
  262. if($info['fache_ready'] != 0){
  263. $this->error('该班次准备状态有误,请刷新列表重试');
  264. }
  265. $update = [
  266. 'fache_ready'=>1,
  267. ];
  268. $rs = Db::name('product_chufabanci')->where('id',$banci_id)->update($update);
  269. if($rs === false){
  270. $this->error('准备失败');
  271. }
  272. $this->success('准备成功');
  273. }
  274. //发车提交
  275. public function banci_fache(){
  276. $this->apiLimit();
  277. $banci_id = input('banci_id',0);
  278. $info = Db::name('product_chufabanci')->where('id',$banci_id)->find();
  279. if(empty($info)){
  280. $this->error('不存在的班次');
  281. }
  282. if($info['fache_status'] != 0){
  283. $this->error('该班次发车状态有误,请刷新列表重试');
  284. }
  285. if($info['fache_ready'] != 1){
  286. $this->error('该班次尚未准备,请刷新列表重试');
  287. }
  288. $update = [
  289. 'fache_status'=>1,
  290. 'fache_time'=>time(),
  291. ];
  292. $rs = Db::name('product_chufabanci')->where('id',$banci_id)->update($update);
  293. if($rs === false){
  294. $this->error('发车失败');
  295. }
  296. $this->success('发车成功');
  297. }
  298. //到站提交
  299. public function banci_daozhan(){
  300. $this->apiLimit();
  301. $banci_id = input('banci_id',0);
  302. $info = Db::name('product_chufabanci')->where('id',$banci_id)->find();
  303. if(empty($info)){
  304. $this->error('不存在的班次');
  305. }
  306. if($info['fache_status'] != 1){
  307. $this->error('该班次发车状态有误,请刷新列表重试');
  308. }
  309. if($info['fache_ready'] != 1){
  310. $this->error('该班次尚未准备,请刷新列表重试');
  311. }
  312. $update = [
  313. 'fache_ready'=>0,//解锁
  314. 'fache_status'=>2,
  315. 'daozhan_time'=>time(),
  316. ];
  317. $rs = Db::name('product_chufabanci')->where('id',$banci_id)->update($update);
  318. if($rs === false){
  319. $this->error('到站失败');
  320. }
  321. $this->success('到站成功');
  322. }
  323. //扫码核销信息
  324. public function get_hexiao_info(){
  325. $hexiao_no = input('hexiao_no','','trim');
  326. $order_road = Db::name('order_road')->where('hexiao_no',$hexiao_no)->find();
  327. if(empty($order_road)){
  328. $this->error('无效核销码');
  329. }
  330. $order = Db::name('order')->where('id',$order_road['order_id'])->find();
  331. if($order['status'] != 1 && $order['status'] != 2){
  332. $error = '无法核销的码';
  333. if($order['status'] == 0){
  334. $error = '订单未支付';
  335. }
  336. if($order['status'] == 10){
  337. $error = '订单已取消';
  338. }
  339. if($order['status'] > 20){
  340. $error = '订单已售后';
  341. }
  342. $this->error($error);
  343. }
  344. //辅助
  345. $roadtype_arr = [
  346. 1 => '单程票',
  347. 2 => '单程票',
  348. 3 => '往返票',
  349. ];
  350. $order_roadtype_text = isset($roadtype_arr[$order['roadtype']]) ? $roadtype_arr[$order['roadtype']] : '';
  351. $roadtype_arr = [
  352. 1 => '去程',
  353. 2 => '返程',
  354. ];
  355. $road_roadtype_text = isset($roadtype_arr[$order_road['roadtype']]) ? $roadtype_arr[$order_road['roadtype']] : '';
  356. //结果
  357. $result = [
  358. 'product_name' => $order['product_name'],
  359. 'roadname' => $order_roadtype_text.' '.$order_road['roadname'].' '.$road_roadtype_text,
  360. 'chufatime' => date('Y-m-d H:i',$order_road['chufatime']),
  361. 'ticket_str' => $order['ticket_str'],
  362. 'realname' => $order['realname'],
  363. 'mobile' => $order['mobile'],
  364. 'hexiao_no' => $hexiao_no,
  365. 'hexiao_status' => $order_road['hexiao_status'],
  366. ];
  367. $this->success(1,$result);
  368. }
  369. //核销提交
  370. public function hexiao_submit(){
  371. $this->apiLimit();
  372. $hexiao_no = input('hexiao_no','','trim');
  373. $banci_id = input('banci_id',0,'intval');
  374. Db::startTrans();
  375. $order_road = Db::name('order_road')->where('hexiao_no',$hexiao_no)->lock(true)->find();
  376. if(empty($order_road)){
  377. Db::rollback();
  378. $this->error('无效核销码');
  379. }
  380. if($order_road['hexiao_status'] != 0){
  381. Db::rollback();
  382. $this->error('此核销码已核销');
  383. }
  384. if($order_road['chufabanci_id'] != $banci_id){
  385. Db::rollback();
  386. $this->error('此码与班次不符');
  387. }
  388. $order = Db::name('order')->where('id',$order_road['order_id'])->lock(true)->find();
  389. if($order['status'] != 1){
  390. $error = '无法核销的码';
  391. if($order['status'] == 0){
  392. $error = '订单未支付';
  393. }
  394. if($order['status'] == 2){
  395. $error = '订单已核销';
  396. }
  397. if($order['status'] == 10){
  398. $error = '订单已取消';
  399. }
  400. if($order['status'] > 20){
  401. $error = '订单已售后';
  402. }
  403. Db::rollback();
  404. $this->error($error);
  405. }
  406. //
  407. $banci_info = Db::name('product_chufabanci')->where('id',$banci_id)->find();
  408. if($banci_info['coach_id'] != $this->auth->id){
  409. Db::rollback();
  410. $this->error('班次与司机不符');
  411. }
  412. if($banci_info['fache_status'] != 0){
  413. /*Db::rollback();
  414. $this->error('该班次已发车不能继续验票');*/
  415. }
  416. //
  417. $update = [
  418. 'hexiao_status' => 1,
  419. 'hexiao_coachid' => $this->auth->id,
  420. 'hexiao_time' => time(),
  421. ];
  422. $rs1 = Db::name('order_road')->where('id',$order_road['id'])->update($update);
  423. if($rs1 === false){
  424. Db::rollback();
  425. $this->error('核销失败');
  426. }
  427. //找到没核销的
  428. $road_find = Db::name('order_road')->where('order_id',$order_road['order_id'])->where('hexiao_status',0)->count();
  429. if($road_find == 0){
  430. //如果都核销了,修改订单状态
  431. $update = [
  432. 'finishtime' => time(),
  433. 'status'=>2
  434. ];
  435. $rs2 = Db::name('order')->where('id',$order_road['order_id'])->update($update);
  436. if($rs1 === false){
  437. Db::rollback();
  438. $this->error('核销失败,请稍后再试');
  439. }
  440. }
  441. Db::commit();
  442. $this->success('核销成功');
  443. }
  444. //验票记录
  445. public function yanpiao_history(){
  446. $date = input('date',date('Y-m-d'));
  447. if(empty($date)){
  448. $date = date('Y-m-d');
  449. }
  450. $starttime = strtotime($date);
  451. $endtime = $starttime + 86399;
  452. $order_road = Db::name('order_road')->alias('road')
  453. ->field('road.id as road_id,road.chufaname,road.hexiao_no,road.ticket_number,road.hexiao_status,
  454. order.realname,order.mobile')
  455. ->join('order','road.order_id = order.id','LEFT')
  456. ->where('road.hexiao_time','BETWEEN',[$starttime,$endtime])
  457. ->where('road.hexiao_status',1)
  458. ->where('road.hexiao_coachid',$this->auth->id)
  459. ->order('road.hexiao_time desc')
  460. ->select();
  461. $ticket = array_column($order_road,'ticket_number');
  462. $result = [
  463. 'count' => array_sum($ticket),
  464. 'list' => $order_road,
  465. ];
  466. $this->success(1,$result);
  467. }
  468. //班线-日历
  469. public function slot_index()
  470. {
  471. //本周一
  472. $this_monday = date('Y-m-d',strtotime('monday this week'));
  473. //获取本周一
  474. $monday = input('date',$this_monday);
  475. if(empty($monday)){
  476. $monday = $this_monday;
  477. }
  478. //确定周一转移量
  479. $type = input('type',0);//0本周,1往前翻,2往后翻
  480. $time_zhuanyi = 0;
  481. if($type == 1){
  482. $time_zhuanyi = -86400*7;
  483. }
  484. if($type == 2){
  485. $time_zhuanyi = 86400*7;
  486. }
  487. //最终的周一
  488. $monday_time = strtotime($monday) + $time_zhuanyi;
  489. $result = [];
  490. $week_arr = [
  491. 1 => '周一',
  492. 2 => '周二',
  493. 3 => '周三',
  494. 4 => '周四',
  495. 5 => '周五',
  496. 6 => '周六',
  497. 7 => '周日',
  498. ];
  499. $today = date('Y-m-d');
  500. for($i=0;$i<7;$i++){
  501. $time = $monday_time + ($i * 86400);
  502. $newone = [
  503. 'time' => $time,
  504. 'date' => date('Y-m-d',$time),
  505. 'simple_date' => date('m/d',$time),
  506. 'week' => date('N',$time),
  507. 'is_today' => 0,
  508. ];
  509. $newone['week_text'] = $week_arr[$newone['week']];
  510. if($newone['date'] == $today){
  511. $newone['is_today'] = 1;
  512. $newone['week_text'] = '今天';
  513. }
  514. $result[] = $newone;
  515. }
  516. $this->success(1,$result);
  517. }
  518. }