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.

117 lines
3.8 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
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. if (env('APP_ENV') === 'prod') {
  41. $closedTime = strtotime(config('market.rest_time'));
  42. if($nowTime > $closedTime){
  43. throw new ErrorCodeException(ErrorCode::MARKET_REST);
  44. }
  45. }
  46. //取得所有店铺营业时间交集
  47. foreach($stores as $store){
  48. //获取店铺开始营业时间
  49. $time1 = strtotime(($store['time1']));
  50. //店铺是否在营业时间 店铺手动点击休息 || 小于店铺开始营业时间 || 当前时间大于服务站休业时间
  51. if($store['is_rest'] == 1 || $nowTime < $time1){
  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 > $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 = array_merge($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 = array_merge($res,$this->calculateTime($time3Max, $time4Min));
  81. }
  82. }
  83. return $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. //取得订单下所有店铺时间段 交集 并计算时间段横跨单位区间长度
  96. $length = ceil(($endTime - $startTime) / (30*60)) + 1;
  97. for ($i=1;$i<=$length;$i++){
  98. $addTime = $startTime + (30*60*$i);
  99. if($addTime >= $peakPeriod && $addTime < $peakPeriodEnd){
  100. $span = ($peakPeriodEnd - $addTime)/(30*60);
  101. $i+=$span;
  102. $addTime = $startTime + (30*60*$i);
  103. }
  104. $data['title'] = date('H:i',$addTime) . ' - ' . date('H:i',$addTime + (30*60));
  105. $data['value'] = date('H:i',$addTime) . ' - ' . date('H:i',$addTime + (30*60));
  106. $res[] = $data;
  107. }
  108. return $res;
  109. }
  110. }