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.

107 lines
3.2 KiB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
  1. <?php
  2. namespace App\Service\v3\Implementations;
  3. use App\Model\v3\ShoppingCart;
  4. use App\Model\v3\Store;
  5. use App\Service\v3\Interfaces\AppointmentTimeServiceInterface;
  6. use phpDocumentor\Reflection\Types\Object_;
  7. class AppointmentTimeService implements AppointmentTimeServiceInterface
  8. {
  9. public function do()
  10. {
  11. return [
  12. '尽快送达',
  13. '08:30 - 09:00',
  14. '09:00 - 09:30',
  15. '09:30 - 10:00',
  16. '10:00 - 10:30'
  17. ];
  18. }
  19. public function check()
  20. {
  21. // TODO: Implement check() method.
  22. }
  23. public function undo()
  24. {
  25. // TODO: Implement undo() method.
  26. }
  27. public function get($shopcartIds)
  28. {
  29. $shopcartIds = explode(',',$shopcartIds);
  30. $sotreIds = ShoppingCart::query()->whereIn('id',$shopcartIds)->pluck('store_id');
  31. $stores = Store::query()->whereIn('id',$sotreIds)->get()->toArray();
  32. $time1Arr = [];
  33. $time2Arr = [];
  34. $time3Arr = [];
  35. $time4Arr = [];
  36. $nowTime = time();
  37. foreach($stores as $store){
  38. //店铺手动 点击歇业
  39. if($store['is_rest'] == 1){
  40. return $store['name'].'店铺已打烊';
  41. }
  42. //当前时间小于 开始营业时间
  43. $time1 = strtotime(($store['time1']));
  44. if($nowTime < $time1){
  45. return $store['name'].'店铺已打烊';
  46. }
  47. $time2 = strtotime(($store['time2']));
  48. if(!empty($store['time3']) && !empty($store['time4'])){
  49. $time3 = strtotime(($store['time3']));
  50. $time4 = strtotime(($store['time4']));
  51. array_push($time3Arr,$time3);
  52. array_push($time4Arr,$time4);
  53. $endTime = $time4;
  54. }else{
  55. $endTime = $time2;
  56. }
  57. if($nowTime > $endTime){
  58. return $store['name'].'店铺已打烊';
  59. }
  60. array_push($time1Arr,$time1);
  61. array_push($time2Arr,$time2);
  62. }
  63. $res = [];
  64. $time1Max = max($time1Arr);
  65. $time2Min = min($time2Arr);
  66. if($time2Min > $nowTime) {
  67. array_push($res, $this->calculateTime($time1Max, $time2Min));
  68. }
  69. if(!empty($time3Arr) && !empty($time4Arr)){
  70. $time3Max = max($time3Arr);
  71. $time4Min = min($time4Arr);
  72. if($time4Min > $nowTime) {
  73. array_push($res, $this->calculateTime($time3Max, $time4Min));
  74. }
  75. }
  76. return $res;
  77. }
  78. function calculateTime($startTime,$endTime)
  79. {
  80. $nowTime = time();
  81. //如果开始时间小于当前时间则取当前时间
  82. if($nowTime > $startTime){
  83. $startTime = ceil($nowTime / (30*60)) * (30*60);
  84. }
  85. //高峰段 区间
  86. $peakPeriod = strtotime('16:00');
  87. $peakPeriodEnd = strtotime('18:00');
  88. $span = ($peakPeriodEnd - $peakPeriod)/(30*60);
  89. //取得订单下所有店铺时间段 交集 并计算时间段横跨单位区间长度
  90. $length = ceil(($endTime - $startTime) / (30*60));
  91. for ($i=1;$i<=$length;$i++){
  92. $addTime = $startTime + (30*60*$i);
  93. if($addTime >= $peakPeriod && $addTime < $peakPeriodEnd){
  94. $i+=$span;
  95. $addTime = $startTime + (30*60*$i);
  96. }
  97. $res[] = date('H:i',$addTime) . ' - ' . date('H:i',$addTime + (30*60));
  98. }
  99. return $res;
  100. }
  101. }