LogisticService.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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 getWASLogisticsByWMSOrderHeaders($WMSOrderHeaders){
  13. $carrierIds = [];
  14. foreach ($WMSOrderHeaders as $WMSOrderHeader) {
  15. $carrierIds [] = $WMSOrderHeader->carrierid;
  16. }
  17. $carrierIds = array_unique($carrierIds);
  18. $carrierIds = array_diff($carrierIds,['*','',null]);
  19. $logistics = Logistic::query()->whereIn('code',$carrierIds)->get();
  20. $insert_params = [];
  21. if(count($carrierIds) > count($logistics)){
  22. $logistics_fillter= data_get($logistics,'*.code');
  23. $logisticDiff = array_diff($carrierIds, $logistics_fillter);
  24. $basCustomers = OracleBasCustomer::query()->where('Customer_Type','CA')->whereIn('CustomerID',$logisticDiff)->get();
  25. foreach ($basCustomers as $basCustomer){
  26. $fillable = ['name' => $basCustomer['descr_c'], 'code' => $basCustomer['customerid']];
  27. array_push($insert_params,$fillable);
  28. }
  29. }
  30. try {
  31. if(count($insert_params) > 0){
  32. Logistic::query()->insert($insert_params);
  33. LogService::log(__METHOD__,__FUNCTION__,'批量创建 logistic'.count($insert_params).json_encode($insert_params));
  34. }
  35. } catch (\Exception $e) {
  36. LogService::log(__METHOD__,__FUNCTION__,'创建失败 logistic error' .json_encode($insert_params). $e->getMessage().$e->getTraceAsString());
  37. } finally {
  38. return $logistics = Logistic::query()->whereIn('code',$carrierIds)->get();
  39. }
  40. }
  41. public function firstOrCreate(array $params, array $values = null){
  42. $logistic = Logistic::query();
  43. if ($values)return $logistic->firstOrCreate($params, $values);
  44. return $logistic->firstOrCreate($params);
  45. }
  46. public function getByWmsOrders($orderHeaders){
  47. $codes = data_get($orderHeaders,'*.userdefine1');
  48. $codes = array_unique($codes);
  49. $codes = array_diff($codes,['','*',null]);
  50. if(!$codes){return [];}
  51. $logistics = Logistic::query()->whereIn('code',$codes)->get();
  52. if($logistics->count() < $codes){
  53. $codes = array_diff($codes,data_get($logistics,'*.code'));
  54. $logistic_list = $this->createLogisticByCarrierIds($codes);
  55. $logistics = $logistics->concat($logistic_list);
  56. }
  57. return $logistics;
  58. }
  59. public function createLogisticByCarrierIds($codes){
  60. if(!$codes){return [];}
  61. $baseCustomers = OracleBasCustomer::query()
  62. ->where('Customer_Type','CA')
  63. ->whereIn('CustomerID',$codes)
  64. ->get();
  65. $insert_params = [];
  66. $created_at = Carbon::now()->format('Y-m-d H:i:s');
  67. foreach ($baseCustomers as $baseCustomer) {
  68. $insert_params[] = [
  69. 'code' => $baseCustomer['customerid'],
  70. 'name' => $baseCustomer['descr_c'],
  71. 'created_at' => $created_at
  72. ];
  73. }
  74. try {
  75. if(count($insert_params) > 0){
  76. $this->insert($insert_params);
  77. LogService::log(__METHOD__, __FUNCTION__, '批量创建 owner error' . count($insert_params) . json_encode($insert_params) );
  78. }
  79. } catch (\Exception $e) {
  80. LogService::log(__METHOD__, __FUNCTION__, '批量创建 owner error' . json_encode($insert_params) . "||".$e->getMessage() . '||' . $e->getTraceAsString() );
  81. } finally {
  82. return Logistic::query()->whereIn('code',$codes)->get();
  83. }
  84. }
  85. public function insert(array $params){
  86. return Logistic::query()->insert($params);
  87. }
  88. }