| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104 |
- <?php
- namespace App\Services;
- use App\Logistic;
- use App\OracleBasCustomer;
- use Carbon\Carbon;
- use Illuminate\Support\Facades\Auth;
- Class LogisticService
- {
- public function getSelection($column = ['id','name']){
- return Logistic::query()->select($column)->get();
- }
- public function getWASLogisticsByWMSOrderHeaders($WMSOrderHeaders){
- $carrierIds = [];
- foreach ($WMSOrderHeaders as $WMSOrderHeader) {
- $carrierIds [] = $WMSOrderHeader->carrierid;
- }
- $carrierIds = array_unique($carrierIds);
- $carrierIds = array_diff($carrierIds,['*','',null]);
- $logistics = Logistic::query()->whereIn('code',$carrierIds)->get();
- $fillables = [];
- if(count($carrierIds) > count($logistics)){
- $logistics_fillter= data_get($logistics,'*.code');
- $logisticDiff = array_diff($carrierIds, $logistics_fillter);
- $basCustomers = OracleBasCustomer::query()->where('Customer_Type','CA')->whereIn('CustomerID',$logisticDiff)->get();
- foreach ($basCustomers as $basCustomer){
- $fillable = ['name' => $basCustomer['descr_c'], 'code' => $basCustomer['customerid']];
- array_push($fillables,$fillable);
- }
- }
- try {
- if(count($fillables) > 0){
- Logistic::query()->insert($fillables);
- LogService::log(__METHOD__,__FUNCTION__,'批量创建'.json_encode($fillables));
- }
- } catch (\Exception $e) {
- LogService::log(__METHOD__,__FUNCTION__,'创建失败「' .json_encode($fillables). $e->getMessage().$e->getTraceAsString());
- } finally {
- return $logistics = Logistic::query()->whereIn('code',$carrierIds)->get();
- }
- }
- public function firstOrCreate(array $params, array $values = null){
- $logistic = Logistic::query();
- if ($values)return $logistic->firstOrCreate($params, $values);
- return $logistic->firstOrCreate($params);
- }
- public function getLogisticByOrderHeaders($orderHeaders){
- $carrierIds = data_get($orderHeaders,'*.carrierid');
- $carrierIds = array_unique($carrierIds);
- $carrierIds = array_diff($carrierIds,['','*',null]);
- if(!$carrierIds){return [];}
- $logistics = Logistic::query()->whereIn('code',$carrierIds)->get();
- if($logistics->count() < $carrierIds){
- $carrierIds = array_diff($carrierIds,data_get($logistics,'*.code'));
- $logistic_list = $this->createLogisticByCarrierIds($carrierIds);
- $logistics = $logistics->concat($logistic_list);
- }
- return $logistics;
- }
- public function createLogisticByCarrierIds($carrierIds){
- if(!$carrierIds){return [];}
- $baseCustomers = OracleBasCustomer::query()
- ->where('Customer_Type','CA')
- ->whereIn('CustomerID',$carrierIds)
- ->get();
- $fillables = [];
- $created_at = Carbon::now()->format('Y-m-d H:i:s');
- foreach ($baseCustomers as $baseCustomer) {
- $fillables[] = [
- 'code' => $baseCustomer['customerid'],
- 'name' => $baseCustomer['descr_c'],
- 'created_at' => $created_at
- ];
- }
- try {
- if($fillables){
- $this->insert($fillables);
- LogService::log(__METHOD__, __FUNCTION__, '批量创建 owner error' . count($fillables) . json_encode($fillables) );
- }
- } catch (\Exception $e) {
- LogService::log(__METHOD__, __FUNCTION__, '批量创建 owner error' . json_encode($fillables) . "||".$e->getMessage() . '||' . $e->getTraceAsString() );
- } finally {
- return Logistic::query()->whereIn('code',$carrierIds)->get();
- }
- }
- public function insert(array $params){
- return Logistic::query()->insert($params);
- }
- }
|