| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100 |
- <?php
- namespace App\Services;
- use App\CommodityBarcode;
- use App\DeliveryAppointmentDetail;
- use App\Services\common\QueryService;
- use App\Traits\ServiceAppAop;
- use App\DeliveryAppointment;
- use Illuminate\Database\Eloquent\Builder;
- use Illuminate\Support\Facades\Auth;
- class DeliveryAppointmentService
- {
- use ServiceAppAop;
- protected $modelClass=DeliveryAppointment::class;
- /**
- * 搜索QUERY
- *
- * @param array $params
- *
- * @return Builder
- */
- public function query(array $params)
- {
- $owners = app("UserService")->getPermittingOwnerIds(Auth::user());
- return app(QueryService::class)->query($params,DeliveryAppointment::query(),[
- 'created_at_start' => ['alias' => 'created_at' , 'startDate' => ' 00:00:00'],
- 'appointment_date_start' => ['alias' => 'appointment_date' , 'startDate' => ' 00:00:00'],
- 'created_at_end' => ['alias' => 'created_at' , 'endDate' => ' 23:59:59'],
- 'appointment_date_end' => ['alias' => 'appointment_date' , 'endDate' => ' 23:59:59'],
- "owner_id"=>['multi' => ','],
- "id"=>['multi' => ','],
- ])->orderByDesc("id")->whereIn("owner_id",$owners)->with(["cars.car","details.commodity.barcodes"]);
- }
- /**
- * 根据吨,立方,SKU减产系数计算产能
- *
- * @param double $tonne
- * @param double $cubicMeter
- * @param int $amount
- * @param int $rpcc reduced_production_capacity_coefficient
- *
- * @return double
- */
- public function calculateCapacity($tonne, $cubicMeter, $amount, $rpcc)
- {
- $need = max(($tonne*config("appointment.production_capacity.tonne")),
- ($cubicMeter*config("appointment.production_capacity.cubic_meter")));
- $coefficient = config("appointment.production_capacity.coefficient");
- if ($amount && ($need/$amount < $coefficient)){
- $need *= 1-(($coefficient-($need/$amount))*($rpcc/100));
- }
- return $need;
- }
- /**
- * 根据提供的详情数组插入详情
- *
- * @param DeliveryAppointment|object $parent
- * @param array $details
- * @param string $name
- * @param string $amount
- * @param string $barCode
- */
- public function insertDetails($parent, array $details, $name="name", $amount="amount", $barCode="bar_code")
- {
- $codes = [];
- $names = [];
- foreach ($details as $detail){
- if ($detail[$barCode])$codes[] = $detail[$barCode];
- else $names[] = $detail[$name];
- }
- $map = [];//存储条码与ID的映射
- CommodityBarcode::query()->with("commodity")->whereHas("commodity",function ($query)use($parent){
- /** @var Builder $query */
- $query->where("owner_id",$parent->owner_id);
- })->whereIn("code",$codes)->orWhereHas("commodity",function ($query)use($names){
- /** @var Builder $query */
- $query->whereIn("name",$names);
- })->get()->each(function ($commodity)use(&$map){
- $map[$commodity->code] = $commodity->commodity_id;
- $map[$commodity->commodity->name] = $commodity->commodity_id;
- });
- $insert = [];//插入的元素
- foreach ($details as $detail){
- $code = $detail[$barCode];
- $insert[] = [
- "delivery_appointment_id" => $parent->id,
- "commodity_id" => $code ? ($map[$code] ?? null) : ($map[$detail[$name]] ?? null),
- "bar_code" => $code,
- "name" => $detail[$name],
- "amount" => $detail[$amount],
- ];
- }
- if ($insert)DeliveryAppointmentDetail::query()->insert($insert);
- }
- }
|