LogisticService.php 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. <?php
  2. namespace App\Services;
  3. use App\Logistic;
  4. use App\OracleBasCustomer;
  5. use Carbon\Carbon;
  6. use Illuminate\Support\Facades\Auth;
  7. Class LogisticService
  8. {
  9. public function getSelection($column = ['id','name']){
  10. return Logistic::query()->select($column)->get();
  11. }
  12. public function firstOrCreate(array $params, array $values = null){
  13. $logistic = Logistic::query();
  14. if ($values)return $logistic->firstOrCreate($params, $values);
  15. return $logistic->firstOrCreate($params);
  16. }
  17. public function getByWmsOrders($orderHeaders){
  18. $codes = data_get($orderHeaders,'*.userdefine1');
  19. $codes = array_unique($codes);
  20. $codes = array_diff($codes,['','*',null]);
  21. if(!$codes){return [];}
  22. $logistics = Logistic::query()->whereIn('code',$codes)->get();
  23. if($logistics->count() < $codes){
  24. $codes = array_diff($codes,data_get($logistics,'*.code'));
  25. $logistic_list = $this->createLogisticByCarrierIds($codes);
  26. $logistics = $logistics->concat($logistic_list);
  27. }
  28. return $logistics;
  29. }
  30. public function createLogisticByCarrierIds($codes){
  31. if(!$codes){return [];}
  32. $baseCustomers = OracleBasCustomer::query()
  33. ->where('Customer_Type','CA')
  34. ->whereIn('CustomerID',$codes)
  35. ->get();
  36. $insert_params = [];
  37. $created_at = Carbon::now()->format('Y-m-d H:i:s');
  38. foreach ($baseCustomers as $baseCustomer) {
  39. $insert_params[] = [
  40. 'code' => $baseCustomer['customerid'],
  41. 'name' => $baseCustomer['descr_c'],
  42. 'created_at' => $created_at
  43. ];
  44. }
  45. try {
  46. if(count($insert_params) > 0){
  47. $this->insert($insert_params);
  48. LogService::log(__METHOD__, __FUNCTION__, '批量创建 owner error' . count($insert_params) . json_encode($insert_params) );
  49. }
  50. } catch (\Exception $e) {
  51. LogService::log(__METHOD__, __FUNCTION__, '批量创建 owner error' . json_encode($insert_params) . "||".$e->getMessage() . '||' . $e->getTraceAsString() );
  52. } finally {
  53. return Logistic::query()->whereIn('code',$codes)->get();
  54. }
  55. }
  56. public function insert(array $params){
  57. return Logistic::query()->insert($params);
  58. }
  59. }