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.

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