DeliveryAppointmentService.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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. use phpDocumentor\Reflection\Types\Integer;
  11. class DeliveryAppointmentService
  12. {
  13. use ServiceAppAop;
  14. protected $modelClass=DeliveryAppointment::class;
  15. /**
  16. * 搜索QUERY
  17. *
  18. * @param array $params
  19. *
  20. * @return Builder
  21. */
  22. public function query(array $params)
  23. {
  24. $owners = app("UserService")->getPermittingOwnerIds(Auth::user());
  25. return app(QueryService::class)->query($params,DeliveryAppointment::query(),[
  26. 'created_at_start' => ['alias' => 'created_at' , 'startDate' => ' 00:00:00'],
  27. 'appointment_date_start' => ['alias' => 'appointment_date' , 'startDate' => ' 00:00:00'],
  28. 'created_at_end' => ['alias' => 'created_at' , 'endDate' => ' 23:59:59'],
  29. 'appointment_date_end' => ['alias' => 'appointment_date' , 'endDate' => ' 23:59:59'],
  30. "owner_id"=>['multi' => ','],
  31. "id"=>['multi' => ','],
  32. ])->orderByDesc("id")->whereIn("owner_id",$owners)->with(["cars.car","details.commodity.barcodes"]);
  33. }
  34. /**
  35. * 根据吨,立方,SKU减产系数计算产能
  36. *
  37. * @param double $tonne
  38. * @param double $cubicMeter
  39. * @param int $amount
  40. * @param int $rpcc reduced_production_capacity_coefficient
  41. *
  42. * @return double
  43. */
  44. public function calculateCapacity($tonne, $cubicMeter, $amount, $rpcc)
  45. {
  46. $need = max(($tonne*config("appointment.production_capacity.tonne")),
  47. ($cubicMeter*config("appointment.production_capacity.cubic_meter")));
  48. $coefficient = config("appointment.production_capacity.coefficient");
  49. if ($amount && ($need/$amount < $coefficient)){
  50. $need *= 1+(($coefficient-($need/$amount))*($rpcc/100));
  51. }
  52. return $need;
  53. }
  54. /**
  55. * 根据提供的详情数组插入详情
  56. *
  57. * @param DeliveryAppointment|object $parent
  58. * @param array $details
  59. * @param string $name
  60. * @param string $amount
  61. * @param string $barCode
  62. */
  63. public function insertDetails($parent, array $details, $name="name", $amount="amount", $barCode="bar_code")
  64. {
  65. $codes = [];
  66. $names = [];
  67. foreach ($details as $detail){
  68. if ($detail[$barCode])$codes[] = $detail[$barCode];
  69. else $names[] = $detail[$name];
  70. }
  71. $map = [];//存储条码与ID的映射
  72. CommodityBarcode::query()->with("commodity")->whereHas("commodity",function ($query)use($parent){
  73. /** @var Builder $query */
  74. $query->where("owner_id",$parent->owner_id);
  75. })->whereIn("code",$codes)->orWhereHas("commodity",function ($query)use($names){
  76. /** @var Builder $query */
  77. $query->whereIn("name",$names);
  78. })->get()->each(function ($commodity)use(&$map){
  79. $map[$commodity->code] = $commodity->commodity_id;
  80. $map[$commodity->commodity->name] = $commodity->commodity_id;
  81. });
  82. $insert = [];//插入的元素
  83. foreach ($details as $detail){
  84. $code = $detail[$barCode];
  85. $insert[] = [
  86. "delivery_appointment_id" => $parent->id,
  87. "commodity_id" => $code ? ($map[$code] ?? null) : ($map[$detail[$name]] ?? null),
  88. "bar_code" => $code,
  89. "name" => $detail[$name],
  90. "amount" => $detail[$amount],
  91. ];
  92. }
  93. if ($insert)DeliveryAppointmentDetail::query()->insert($insert);
  94. }
  95. /**
  96. * 获取一个KEY值用于验证二维码 21-4-2 切换为永久二维码
  97. *
  98. * @return string
  99. */
  100. public function getKey()
  101. {
  102. $y = 'w';//date("y");
  103. $m = 'a';//date("m");
  104. $d = 's';//date("d");
  105. $k1 = substr(env("APP_KEY"),7,2);
  106. $k2 = substr(env("APP_KEY"),10,2);
  107. $k3 = substr(env("APP_KEY"),15,2);
  108. return md5($k3.$y.$k2.$m.$k1.$d);
  109. }
  110. /**
  111. * 验证key值
  112. *
  113. * @param string $key
  114. * @return bool
  115. */
  116. public function checkKey(string $key):bool
  117. {
  118. if ($key!=$this->getKey())return false;
  119. return true;
  120. }
  121. /**
  122. * 获取当前时段
  123. *
  124. * @param int|null $hour
  125. *
  126. * @return int|bool
  127. */
  128. public function getPeriod($hour=null)
  129. {
  130. if (!$hour)$hour = date("H");
  131. foreach (DeliveryAppointment::PERIOD as $key=>$period){
  132. $arr = explode("-",$period);
  133. if (count($arr)!=2)continue;
  134. if ($hour>=$arr[0] && $hour<$arr[1])return $key;
  135. }
  136. return false;
  137. }
  138. /**
  139. * 验证预约单完整性修改状态
  140. *
  141. * @param integer $id
  142. */
  143. public function checkFull($id)
  144. {
  145. /** @var DeliveryAppointment|\stdClass $delivery */
  146. $delivery = DeliveryAppointment::query()->withCount(["cars"=>function($query){
  147. /** @var Builder $query */
  148. $query->whereNull("delivery_time")->where("status","!=",2);
  149. }])->find($id);
  150. if ($delivery->cars_count == 0)$delivery->update(["status"=>2]);
  151. }
  152. }