DeliveryAppointmentService.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. <?php
  2. namespace App\Services;
  3. use App\CommodityBarcode;
  4. use App\DeliveryAppointmentDetail;
  5. use App\Services\common\QueryService;
  6. use App\Traits\ServiceAppAop;
  7. use App\DeliveryAppointment;
  8. use Illuminate\Database\Eloquent\Builder;
  9. use Illuminate\Support\Facades\Auth;
  10. class DeliveryAppointmentService
  11. {
  12. use ServiceAppAop;
  13. protected $modelClass=DeliveryAppointment::class;
  14. /**
  15. * 搜索QUERY
  16. *
  17. * @param array $params
  18. *
  19. * @return Builder
  20. */
  21. public function query(array $params)
  22. {
  23. $owners = app("UserService")->getPermittingOwnerIds(Auth::user());
  24. return app(QueryService::class)->query($params,DeliveryAppointment::query(),[
  25. 'created_at_start' => ['alias' => 'created_at' , 'startDate' => ' 00:00:00'],
  26. 'appointment_date_start' => ['alias' => 'appointment_date' , 'startDate' => ' 00:00:00'],
  27. 'created_at_end' => ['alias' => 'created_at' , 'endDate' => ' 23:59:59'],
  28. 'appointment_date_end' => ['alias' => 'appointment_date' , 'endDate' => ' 23:59:59'],
  29. "owner_id"=>['multi' => ','],
  30. "id"=>['multi' => ','],
  31. ])->orderByDesc("id")->whereIn("owner_id",$owners)->with(["cars.car","details.commodity.barcodes"]);
  32. }
  33. /**
  34. * 根据吨,立方,SKU减产系数计算产能
  35. *
  36. * @param double $tonne
  37. * @param double $cubicMeter
  38. * @param int $amount
  39. * @param int $rpcc reduced_production_capacity_coefficient
  40. *
  41. * @return double
  42. */
  43. public function calculateCapacity($tonne, $cubicMeter, $amount, $rpcc)
  44. {
  45. $need = max(($tonne*config("appointment.production_capacity.tonne")),
  46. ($cubicMeter*config("appointment.production_capacity.cubic_meter")));
  47. $coefficient = config("appointment.production_capacity.coefficient");
  48. if ($amount && ($need/$amount < $coefficient)){
  49. $need *= 1-(($coefficient-($need/$amount))*($rpcc/100));
  50. }
  51. return $need;
  52. }
  53. /**
  54. * 根据提供的详情数组插入详情
  55. *
  56. * @param DeliveryAppointment|object $parent
  57. * @param array $details
  58. * @param string $name
  59. * @param string $amount
  60. * @param string $barCode
  61. */
  62. public function insertDetails($parent, array $details, $name="name", $amount="amount", $barCode="bar_code")
  63. {
  64. $codes = [];
  65. $names = [];
  66. foreach ($details as $detail){
  67. if ($detail[$barCode])$codes[] = $detail[$barCode];
  68. else $names[] = $detail[$name];
  69. }
  70. $map = [];//存储条码与ID的映射
  71. CommodityBarcode::query()->with("commodity")->whereHas("commodity",function ($query)use($parent){
  72. /** @var Builder $query */
  73. $query->where("owner_id",$parent->owner_id);
  74. })->whereIn("code",$codes)->orWhereHas("commodity",function ($query)use($names){
  75. /** @var Builder $query */
  76. $query->whereIn("name",$names);
  77. })->get()->each(function ($commodity)use(&$map){
  78. $map[$commodity->code] = $commodity->commodity_id;
  79. $map[$commodity->commodity->name] = $commodity->commodity_id;
  80. });
  81. $insert = [];//插入的元素
  82. foreach ($details as $detail){
  83. $code = $detail[$barCode];
  84. $insert[] = [
  85. "delivery_appointment_id" => $parent->id,
  86. "commodity_id" => $code ? ($map[$code] ?? null) : ($map[$detail[$name]] ?? null),
  87. "bar_code" => $code,
  88. "name" => $detail[$name],
  89. "amount" => $detail[$amount],
  90. ];
  91. }
  92. if ($insert)DeliveryAppointmentDetail::query()->insert($insert);
  93. }
  94. }