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.

156 lines
5.7 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
  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['distribution'][] = ['title'=>'尽快送达','value'=>'尽快送达'];
  72. $res['self_take'][] = ['title'=>'尽快自提','value'=>'尽快自提'];
  73. }
  74. $data['title'] = date('H:i',$i + 30 * 60) . ' - ' . date('H:i',$i + 30 * 60 * 2);
  75. $data['value'] = date('H:i',$i + 30 * 60) . ' - ' . date('H:i',$i + 30 * 60 * 2);
  76. $res['distribution'][] = $data;
  77. $res['self_take'][]= $data;
  78. }
  79. }
  80. // foreach($stores as $store){
  81. // //获取店铺开始营业时间
  82. // $time1 = strtotime(($store['time1']));
  83. // //店铺是否在营业时间 店铺手动点击休息 || 小于店铺开始营业时间 || 当前时间大于服务站休业时间
  84. // if($store['is_rest'] == 1 || $nowTime < $time1){
  85. // throw new ErrorCodeException(ErrorCode::STORE_REST);
  86. // }
  87. //
  88. // $time2 = strtotime(($store['time2']));
  89. // if(!empty($store['time3']) && !empty($store['time4'])){
  90. // $time3 = strtotime(($store['time3']));
  91. // $time4 = strtotime(($store['time4']));
  92. // array_push($time3Arr,$time3);
  93. // array_push($time4Arr,$time4);
  94. // $endTime = $time4;
  95. // }else{
  96. // $endTime = $time2;
  97. // }
  98. // if($nowTime > $endTime){
  99. // throw new ErrorCodeException(ErrorCode::STORE_REST);
  100. // }
  101. // array_push($time1Arr,$time1);
  102. // array_push($time2Arr,$time2);
  103. // }
  104. // $res = [];
  105. // $time1Max = max($time1Arr);
  106. // $time2Min = min($time2Arr);
  107. // if($time2Min > $nowTime) {
  108. // $res = array_merge($res,$this->calculateTime($time1Max, $time2Min));
  109. // }
  110. // if(!empty($time3Arr) && !empty($time4Arr)){
  111. // $time3Max = max($time3Arr);
  112. // $time4Min = min($time4Arr);
  113. // if($time4Min > $nowTime) {
  114. // $res = array_merge($res,$this->calculateTime($time3Max, $time4Min));
  115. // }
  116. // }
  117. return $res;
  118. }
  119. function calculateTime($startTime,$endTime)
  120. {
  121. $nowTime = time();
  122. //如果开始时间小于当前时间则取当前时间
  123. if($nowTime > $startTime){
  124. $startTime = ceil($nowTime / (30*60)) * (30*60);
  125. }
  126. if (env('APP_ENV') === 'prod') {
  127. $closedTime = strtotime(config('market.rest_time'));
  128. if($closedTime < $endTime){
  129. $endTime = $closedTime;
  130. }
  131. }
  132. //高峰段 区间
  133. // $peakPeriod = strtotime('16:00');
  134. // $peakPeriodEnd = strtotime('18:00');
  135. //取得订单下所有店铺时间段 交集 并计算时间段横跨单位区间长度
  136. $length = ceil(($endTime - $startTime) / (30*60)) + 1;
  137. for ($i=1;$i<=$length;$i++){
  138. $addTime = $startTime + (30*60*$i);
  139. // if($addTime >= $peakPeriod && $addTime < $peakPeriodEnd){
  140. // $span = ($peakPeriodEnd - $addTime)/(30*60);
  141. // $i+=$span;
  142. // $addTime = $startTime + (30*60*$i);
  143. // }
  144. $data['title'] = date('H:i',$addTime) . ' - ' . date('H:i',$addTime + (30*60));
  145. $data['value'] = date('H:i',$addTime) . ' - ' . date('H:i',$addTime + (30*60));
  146. $res[] = $data;
  147. }
  148. return $res;
  149. }
  150. }