DeliveryAppointmentController.php 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\CarType;
  4. use App\CommodityBarcode;
  5. use App\Components\AsyncResponse;
  6. use App\DeliveryAppointment;
  7. use App\DeliveryAppointmentCar;
  8. use App\DeliveryAppointmentDetail;
  9. use App\Imports\AppointmentDetail;
  10. use App\Warehouse;
  11. use Carbon\Carbon;
  12. use Carbon\CarbonPeriod;
  13. use Illuminate\Database\Eloquent\Builder;
  14. use Illuminate\Support\Facades\Auth;
  15. use Illuminate\Support\Facades\DB;
  16. use Illuminate\Support\Facades\Validator;
  17. class DeliveryAppointmentController extends Controller
  18. {
  19. use AsyncResponse;
  20. public function appointment()
  21. {
  22. $owners = app("OwnerService")->getIntersectPermitting();
  23. $cars = CarType::query()->get();
  24. $warehouses = Warehouse::query()->select("id","name")->get();
  25. return view("store.deliveryAppointment.appointment",compact("owners","cars","warehouses"));
  26. }
  27. public function import()
  28. {
  29. $this->importExcel(new AppointmentDetail());
  30. }
  31. /**
  32. * 获取产能
  33. *
  34. */
  35. public function getCapacity()
  36. {
  37. /** @var \stdClass $warehouse */
  38. $warehouse = Warehouse::query()->find(request("warehouse_id"));
  39. $tonne = request("tonne");
  40. $cubicMeter = request("cubic_meter");
  41. $amount = request("detail_amount");
  42. if (!$warehouse || (!$tonne && !$cubicMeter))$this->error("非法参数");
  43. $need = app("DeliveryAppointmentService")->calculateCapacity($tonne,$cubicMeter,$amount,$warehouse->reduced_production_capacity_coefficient);//所需产能
  44. $start = Carbon::tomorrow();
  45. $end = Carbon::today()->addDays(7);
  46. $map = [];
  47. DeliveryAppointment::query()->selectRaw("appointment_date,date_period,SUM(capacity) AS capacity")
  48. ->whereBetween("appointment_date",[$start->toDateString(),$end->toDateString()])
  49. ->where("status",0)
  50. ->where("warehouse_id",$warehouse->id)
  51. ->groupBy(["appointment_date","date_period"])->get()
  52. ->each(function ($appointment)use(&$map){
  53. $map[$appointment->appointment_date."-".$appointment->date_period] = $appointment->capacity;
  54. });
  55. $list = [];
  56. $capacity = $warehouse->production_capacity;
  57. foreach (CarbonPeriod::create($start,$end) as $date){
  58. /** @var $date Carbon */
  59. $date = $date->format("Y-m-d");
  60. $periods = [];
  61. foreach (DeliveryAppointment::PERIOD as $key=>$period){
  62. $total = $capacity*DeliveryAppointment::HOUR[$key];//仓库该时段产能总量
  63. $used = $map[$date."-".$key] ?? 0; //已使用产能
  64. $available = $total-$used; //可用产能
  65. if ($available < $need)$periods[] = ["time"=>$period,"index"=>$key,"isAvailable"=>false];
  66. else $periods[] = ["time"=>$period,"index"=>$key,"isAvailable"=>true];
  67. }
  68. $list[] = ["date"=>$date,"period"=>$periods];
  69. }
  70. $this->success($list);
  71. }
  72. /**
  73. * 确定预约
  74. */
  75. public function submitAppointment()
  76. {
  77. $model = request("model");
  78. $selectDate = request("selectDate");
  79. $details = request("details");
  80. $errors = Validator::make($model,[
  81. "owner_id" => ["required","integer"],
  82. "warehouse_id" => ["required","integer"],
  83. "tonne" => ["required_without:cubic_meter","numeric"],
  84. "cubic_meter" => ["required_without:tonne","numeric"],
  85. "box_amount" => ["nullable","integer"],
  86. "cars.*.license_plate_number" => ["required","size:7"],
  87. "cars.*.car_id" => ["nullable","integer"],
  88. "cars.*.driver_phone" => ["nullable"],
  89. "cars.*.driver_name" => ["nullable"],
  90. ],[
  91. 'required'=>':attribute 不应为空',
  92. 'integer'=>':attribute 应为数值',
  93. 'required_without'=>':attribute 不应为空',
  94. 'numeric'=>':attribute 必须为数字',
  95. 'size'=>':attribute 非法',
  96. ],[
  97. 'owner_id'=>'货主',
  98. 'warehouse_id'=>'仓库',
  99. 'tonne'=>'吨',
  100. 'cubic_meter'=>'立方',
  101. 'cars.*.license_plate_number'=>'车牌号',
  102. 'cars.*.car_id'=>'车型',
  103. 'cars.*.driver_phone'=>'司机电话',
  104. 'cars.*.driver_name'=>'司机姓名',
  105. ])->errors();
  106. if (count($errors)>0)$this->success(["errors"=>$errors]);
  107. $errors = Validator::make($selectDate,[
  108. "date" => ["required","date","after:today"],
  109. "time" => ["required","integer"],
  110. ])->errors();
  111. if (count($errors)>0)$this->error("未选定预约日期");
  112. DB::transaction(function ()use($model,$selectDate,$details){
  113. $result = DeliveryAppointment::query()->selectRaw("appointment_date,date_period,SUM(capacity) AS capacity")
  114. ->where("appointment_date",$selectDate["date"])
  115. ->where("date_period",$selectDate["time"])
  116. ->where("warehouse_id",$model["warehouse_id"])
  117. ->where("status",0)
  118. ->groupBy(["appointment_date","date_period"])
  119. ->lockForUpdate()->first();
  120. /** @var \stdClass $warehouse */
  121. $warehouse = Warehouse::query()->find($model["warehouse_id"]);
  122. $need = app("DeliveryAppointmentService")->
  123. calculateCapacity($model["tonne"],$model["cubic_meter"],count($details),
  124. $warehouse->reduced_production_capacity_coefficient);
  125. if ($result){
  126. $total = $warehouse->production_capacity*DeliveryAppointment::HOUR[$selectDate["time"]];
  127. $available = $total-$result->capacity;
  128. if ($available < $need)$this->success(["isFail"=>false]);
  129. }
  130. /** @var \stdClass $appointment */
  131. $appointment = DeliveryAppointment::query()->create([
  132. "user_id" => Auth::id(),
  133. "owner_id" => $model["owner_id"],
  134. "procurement_number" => $model["procurement_number"],
  135. "asn_number" => $model["asn_number"],
  136. "warehouse_id" => $model["warehouse_id"],
  137. "tonne" => $model["tonne"],
  138. "cubic_meter" => $model["cubic_meter"],
  139. "box_amount" => $model["box_amount"],
  140. "capacity" => $need,
  141. "appointment_date" => $selectDate["date"],
  142. "date_period" => $selectDate["time"],
  143. ]);
  144. if ($details)app("DeliveryAppointmentService")->insertDetails($appointment,$details);
  145. $insert = [];
  146. foreach ($model["cars"] as $index=>$car){
  147. $rand = mt_rand(0,9);
  148. $len = strlen($appointment->id);
  149. $ten = $len < 2 ? "0" : substr($appointment->id,$len-2,1);
  150. $one = substr($appointment->id,$len-1,1);
  151. //唯一码 随机数+十位+当前下标+个位+日期
  152. $number = $rand.$ten.$index.$one.date("d");
  153. $insert[] = [
  154. "delivery_appointment_id" => $appointment->id,
  155. "license_plate_number" => $car["license_plate_number"],
  156. "car_id" => $car["car_id"],
  157. "driver_name" => $car["driver_name"],
  158. "driver_phone" => $car["driver_phone"],
  159. "appointment_number" => $number,
  160. ];
  161. }
  162. DeliveryAppointmentCar::query()->insert($insert);
  163. //md5加密在密文第五位后插入
  164. $md5 = substr_replace(md5(date("m-d")),$appointment->id,5,0);
  165. $this->success(["key"=>$md5]);
  166. });
  167. }
  168. /**
  169. * 根据key取id 鉴权数据
  170. */
  171. public function showAppointmentInfo()
  172. {
  173. $key = request("k");
  174. $len = strlen($key);
  175. $id = substr($key,5,$len-32);
  176. $md5 = substr($id,0,5).substr($id,5+$len-32);
  177. if ($md5!==md5(date("m-d")))return view("exception.404");
  178. /** @var \stdClass $appointment */
  179. $appointment = DeliveryAppointment::query()->with("cars")->find($id);
  180. if (!$appointment || $appointment->user_id != Auth::id())return view("exception.404");
  181. return $appointment->cars;
  182. }
  183. }