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.

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