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.

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