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.

110 lines
3.3 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
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. //取得所有店铺营业时间交集
  38. foreach($stores as $store){
  39. //店铺手动 点击歇业
  40. if($store['is_rest'] == 1){
  41. return $store['name'].'店铺已打烊';
  42. }
  43. //当前时间小于 开始营业时间
  44. $time1 = strtotime(($store['time1']));
  45. if($nowTime < ($time1+60*30)){
  46. return $store['name'].'店铺已打烊';
  47. }
  48. $time2 = strtotime(($store['time2']));
  49. if(!empty($store['time3']) && !empty($store['time4'])){
  50. $time3 = strtotime(($store['time3']));
  51. $time4 = strtotime(($store['time4']));
  52. array_push($time3Arr,$time3);
  53. array_push($time4Arr,$time4);
  54. $endTime = $time4;
  55. }else{
  56. $endTime = $time2;
  57. }
  58. if(($nowTime+60*30) > $endTime){
  59. return $store['name'].'店铺已打烊';
  60. }
  61. array_push($time1Arr,$time1);
  62. array_push($time2Arr,$time2);
  63. }
  64. $res = [];
  65. $time1Max = max($time1Arr);
  66. $time2Min = min($time2Arr);
  67. if($time2Min > $nowTime) {
  68. $res[] = $this->calculateTime($time1Max, $time2Min);
  69. }
  70. if(!empty($time3Arr) && !empty($time4Arr)){
  71. $time3Max = max($time3Arr);
  72. $time4Min = min($time4Arr);
  73. if($time4Min > $nowTime) {
  74. $res[] = $this->calculateTime($time3Max, $time4Min);
  75. }
  76. }
  77. return array_shift($res);
  78. }
  79. function calculateTime($startTime,$endTime)
  80. {
  81. $nowTime = time();
  82. //如果开始时间小于当前时间则取当前时间
  83. if($nowTime > $startTime){
  84. $startTime = ceil($nowTime / (30*60)) * (30*60);
  85. }
  86. //高峰段 区间
  87. $peakPeriod = strtotime('16:00');
  88. $peakPeriodEnd = strtotime('18:00');
  89. $span = ($peakPeriodEnd - $peakPeriod)/(30*60);
  90. //取得订单下所有店铺时间段 交集 并计算时间段横跨单位区间长度
  91. $length = ceil(($endTime - $startTime) / (30*60));
  92. for ($i=1;$i<=$length;$i++){
  93. $addTime = $startTime + (30*60*$i);
  94. if($addTime >= $peakPeriod && $addTime < $peakPeriodEnd){
  95. $i+=$span;
  96. $addTime = $startTime + (30*60*$i);
  97. }
  98. $date['title'] = date('H:i',$addTime) . ' - ' . date('H:i',$addTime + (30*60));
  99. $date['value'] = date('H:i',$addTime) . ' - ' . date('H:i',$addTime + (30*60));
  100. $res[] = $date;
  101. }
  102. return $res;
  103. }
  104. }