DeliveryAppointmentService.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. <?php
  2. namespace App\Services;
  3. use App\CommodityBarcode;
  4. use App\DeliveryAppointmentDetail;
  5. use App\Traits\ServiceAppAop;
  6. use App\DeliveryAppointment;
  7. use Illuminate\Database\Eloquent\Builder;
  8. class DeliveryAppointmentService
  9. {
  10. use ServiceAppAop;
  11. protected $modelClass=DeliveryAppointment::class;
  12. /**
  13. * 根据吨,立方,SKU减产系数计算产能
  14. *
  15. * @param double $tonne
  16. * @param double $cubicMeter
  17. * @param int $amount
  18. * @param int $rpcc reduced_production_capacity_coefficient
  19. *
  20. * @return double
  21. */
  22. public function calculateCapacity($tonne, $cubicMeter, $amount, $rpcc)
  23. {
  24. $need = max(($tonne*config("appointment.production_capacity.tonne")),
  25. ($cubicMeter*config("appointment.production_capacity.cubic_meter")));
  26. $coefficient = config("appointment.production_capacity.coefficient");
  27. if ($amount && ($need/$amount < $coefficient)){
  28. $need *= 1-(($coefficient-($need/$amount))*($rpcc/100));
  29. }
  30. return $need;
  31. }
  32. /**
  33. * 根据提供的详情数组插入详情
  34. *
  35. * @param DeliveryAppointment|object $parent
  36. * @param array $details
  37. * @param string $name
  38. * @param string $amount
  39. * @param string $barCode
  40. */
  41. public function insertDetails($parent, array $details, $name="name", $amount="amount", $barCode="bar_code")
  42. {
  43. $codes = [];
  44. $names = [];
  45. foreach ($details as $detail){
  46. if ($detail[$barCode])$codes[] = $detail[$barCode];
  47. else $names[] = $detail[$name];
  48. }
  49. $map = [];//存储条码与ID的映射
  50. CommodityBarcode::query()->with("commodity")->whereHas("commodity",function ($query)use($parent){
  51. /** @var Builder $query */
  52. $query->where("owner_id",$parent->owner_id);
  53. })->whereIn("code",$codes)->orWhereHas("commodity",function ($query)use($names){
  54. /** @var Builder $query */
  55. $query->whereIn("name",$names);
  56. })->get()->each(function ($commodity)use(&$map){
  57. $map[$commodity->code] = $commodity->commodity_id;
  58. $map[$commodity->commodity->name] = $commodity->commodity_id;
  59. });
  60. $insert = [];//插入的元素
  61. foreach ($details as $detail){
  62. $code = $detail[$barCode];
  63. $insert[] = [
  64. "delivery_appointment_id" => $parent->id,
  65. "commodity_id" => $code ? ($map[$code] ?? null) : ($map[$detail[$name]] ?? null),
  66. "bar_code" => $code,
  67. "name" => $detail[$name],
  68. "amount" => $detail[$amount],
  69. ];
  70. }
  71. if ($insert)DeliveryAppointmentDetail::query()->insert($insert);
  72. }
  73. }