DeliveryAppointmentService.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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. $insert = [];//插入的元素
  68. foreach ($details as $index=>$detail){
  69. if (isset($detail["commodity_id"]) && $detail["commodity_id"]){
  70. $insert[] = [
  71. "delivery_appointment_id" => $parent->id,
  72. "commodity_id" => $detail["commodity_id"],
  73. "bar_code" => null,
  74. "name" => null,
  75. "amount" => $detail[$amount],
  76. ];
  77. unset($details[$index]);continue;
  78. }
  79. if ($detail[$barCode])$codes[] = $detail[$barCode];
  80. else $names[] = $detail[$name];
  81. }
  82. $map = [];//存储条码与ID的映射
  83. if ($codes || $names){
  84. CommodityBarcode::query()->with("commodity")->whereHas("commodity",function ($query)use($parent){
  85. /** @var Builder $query */
  86. $query->where("owner_id",$parent->owner_id);
  87. })->whereIn("code",$codes)->orWhereHas("commodity",function ($query)use($names){
  88. /** @var Builder $query */
  89. $query->whereIn("name",$names);
  90. })->get()->each(function ($commodity)use(&$map){
  91. $map[$commodity->code] = $commodity->commodity_id;
  92. $map[$commodity->commodity->name] = $commodity->commodity_id;
  93. });
  94. }
  95. foreach ($details as $detail){
  96. $code = $detail[$barCode];
  97. $insert[] = [
  98. "delivery_appointment_id" => $parent->id,
  99. "commodity_id" => $code ? ($map[$code] ?? null) : ($map[$detail[$name]] ?? null),
  100. "bar_code" => $code,
  101. "name" => $detail[$name],
  102. "amount" => $detail[$amount],
  103. ];
  104. }
  105. if ($insert)DeliveryAppointmentDetail::query()->insert($insert);
  106. }
  107. /**
  108. * 获取一个KEY值用于验证二维码 21-4-2 切换为永久二维码
  109. *
  110. * @return string
  111. */
  112. public function getKey()
  113. {
  114. $y = 'w';//date("y");
  115. $m = 'a';//date("m");
  116. $d = 's';//date("d");
  117. $k1 = substr(env("APP_KEY"),7,2);
  118. $k2 = substr(env("APP_KEY"),10,2);
  119. $k3 = substr(env("APP_KEY"),15,2);
  120. return md5($k3.$y.$k2.$m.$k1.$d);
  121. }
  122. /**
  123. * 验证key值
  124. *
  125. * @param string $key
  126. * @return bool
  127. */
  128. public function checkKey(string $key):bool
  129. {
  130. if ($key!=$this->getKey())return false;
  131. return true;
  132. }
  133. /**
  134. * 获取当前时段
  135. *
  136. * @param int|null $hour
  137. *
  138. * @return int|bool
  139. */
  140. public function getPeriod($hour=null)
  141. {
  142. if (!$hour)$hour = date("H");
  143. foreach (DeliveryAppointment::PERIOD as $key=>$period){
  144. $arr = explode("-",$period);
  145. if (count($arr)!=2)continue;
  146. if ($hour>=$arr[0] && $hour<$arr[1])return $key;
  147. }
  148. return false;
  149. }
  150. /**
  151. * 验证预约单完整性修改状态
  152. *
  153. * @param integer $id
  154. */
  155. public function checkFull($id)
  156. {
  157. /** @var DeliveryAppointment|\stdClass $delivery */
  158. $delivery = DeliveryAppointment::query()->withCount(["cars"=>function($query){
  159. /** @var Builder $query */
  160. $query->whereNull("delivery_time")->where("status","!=",2);
  161. }])->find($id);
  162. if ($delivery->cars_count == 0)$delivery->update(["status"=>2]);
  163. }
  164. }