WaybillService.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. <?php
  2. namespace App\Services;
  3. use App\Services\common\QueryService;
  4. use App\Waybill;
  5. use Illuminate\Http\Request;
  6. use Illuminate\Support\Facades\DB;
  7. use Ramsey\Uuid\Uuid;
  8. Class WaybillService
  9. {
  10. private function conditionQuery(Request $request){
  11. $waybills = Waybill::with(['owner','wmsCommodities','waybillAuditLogs' => function ($query) {
  12. return $query->with('user');
  13. }])->selectRaw('waybills.* ,waybill_on_tops.id top_id ,waybill_on_tops.remark,waybill_on_tops.updated_at top_update')
  14. ->leftJoin('waybill_on_tops','waybill_on_tops.waybill_id','=','waybills.id')
  15. ->whereNull('waybill_on_tops.deleted_at')
  16. ->orderBy('waybill_on_tops.updated_at','desc')
  17. ->orderBy('waybills.id','desc');
  18. $columnQueryRules=[
  19. 'waybill_number' => ['like' => ''],
  20. 'carrier_bill' => ['like' => ''],
  21. 'owner_id' => ['multi' => ','],
  22. 'wms_bill_number' => ['like' => ''],
  23. 'origination' => ['like' => ''],
  24. 'destination' => ['like' => ''],
  25. 'created_at_start' => ['alias' => 'created_at' , 'startDate' => ' 00:00:00'],
  26. 'created_at_end' => ['alias' => 'created_at' , 'endDate' => ' 23:59:59'],
  27. 'uriType' => ['alias' => 'type']
  28. ];
  29. $waybills = app(QueryService::class)->query($request,$waybills,$columnQueryRules,"waybills");
  30. return $waybills;
  31. }
  32. public function paginate(Request $request){
  33. $waybills = $this->conditionQuery($request);
  34. return $waybills->paginate($request->paginate ?? 50);
  35. }
  36. public function get(Request $request){
  37. $waybills = $this->conditionQuery($request);
  38. return $waybills->get();
  39. }
  40. public function some(Request $request){
  41. return $waybills = Waybill::with(['owner'])->selectRaw('waybills.* ,waybill_on_tops.id top_id ,waybill_on_tops.remark,waybill_on_tops.updated_at top_update')
  42. ->leftJoin('waybill_on_tops','waybill_on_tops.waybill_id','=','waybills.id')
  43. ->whereNull('waybill_on_tops.deleted_at')
  44. ->orderBy('waybill_on_tops.updated_at','desc')
  45. ->orderBy('waybills.id','desc')
  46. ->whereIn('id',explode(',',$request->data))
  47. ->get();
  48. }
  49. public function store(Request $request){
  50. return DB::transaction(function ()use($request){
  51. $waybill=new Waybill([
  52. 'type'=>$request->type,
  53. 'status'=>'未审核',
  54. 'waybill_number'=>Uuid::uuid1(),
  55. 'owner_id'=>$request->owner_id,
  56. 'wms_bill_number'=>$request->wms_bill_number,
  57. 'origination'=>$request->origination,
  58. 'destination'=>$request->destination,
  59. 'recipient'=>$request->recipient,
  60. 'recipient_mobile'=>$request->recipient_mobile,
  61. 'charge'=>$request->charge,
  62. 'ordering_remark'=>$request->ordering_remark
  63. ]);
  64. if ($request->collect_fee)$waybill->collect_fee=$request->collect_fee;
  65. $waybill->save();
  66. $number_id=$waybill->id;
  67. if ($request->type=='直发车') $waybill_number='BSZF';
  68. else $waybill_number='BSZX';
  69. $waybill_number .= date ("ymd").str_pad($number_id>99999?$number_id%99999:$number_id,4,"0",STR_PAD_LEFT);
  70. $waybill->update(['waybill_number' => $waybill_number ]);
  71. return $waybill;
  72. });
  73. }
  74. public function find($id){
  75. return Waybill::query()->find($id);
  76. }
  77. public function update(Request $request,$id)
  78. {
  79. $waybill = $this->find($id);
  80. //替换换行符
  81. if ($request->dispatch_remark) {
  82. $request->offsetSet('dispatch_remark', str_replace(PHP_EOL, ' ', $request->dispatch_remark));
  83. }
  84. if (!$request->destination) $request->offsetSet('destination', $waybill->destination);
  85. if ($request->destination_city_id && $waybill->destination_city_id != $request->destination_city_id) {
  86. $city = app(CityService::class)->find($request->destination_city_id);
  87. if ($city && $city->province_name && (mb_strpos($request->destination, $city->name) === false || mb_strpos($request->destination, $city->province_name) === false)) {
  88. if (mb_strpos($request->destination, $city->name) === false && mb_strpos($request->destination, $city->province_name) === false) {
  89. $request->offsetSet('destination', $city->province_name . $city->name . $request->destination);
  90. goto sign;
  91. }
  92. if (mb_strpos($request->destination, $city->province_name) === false) {
  93. $request->offsetSet('destination', $city->province_name . $request->destination);
  94. }
  95. if (mb_strpos($request->destination, $city->name) === false) {
  96. $province_name = $city->province_name;
  97. $start_index = mb_strpos($request->destination, $city->province_name . '省');
  98. if ($start_index === false) $start_index = mb_strpos($request->destination, $city->province_name);
  99. else $province_name = $province_name . '省';
  100. $strBefore = mb_substr($request->destination, $start_index, mb_strlen($province_name));
  101. $strAfter = mb_substr($request->destination, $start_index + mb_strlen($province_name));
  102. $request->offsetSet('destination', $strBefore . $city->name . $strAfter);
  103. }
  104. }
  105. }
  106. sign:
  107. $waybill->fill($request->input());
  108. $waybill->update();
  109. return $waybill;
  110. }
  111. }