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.

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