| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970 |
- <?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 firstOrCreate(array $params, array $values = null){
- $logistic = Logistic::query();
- if ($values)return $logistic->firstOrCreate($params, $values);
- return $logistic->firstOrCreate($params);
- }
- public function getByWmsOrders($orderHeaders){
- $codes = data_get($orderHeaders,'*.userdefine1');
- $codes = array_unique($codes);
- $codes = array_diff($codes,['','*',null]);
- if(!$codes){return [];}
- $logistics = Logistic::query()->whereIn('code',$codes)->get();
- if($logistics->count() < $codes){
- $codes = array_diff($codes,data_get($logistics,'*.code'));
- $logistic_list = $this->createLogisticByCarrierIds($codes);
- $logistics = $logistics->concat($logistic_list);
- }
- return $logistics;
- }
- public function createLogisticByCarrierIds($codes){
- if(!$codes){return [];}
- $baseCustomers = OracleBasCustomer::query()
- ->where('Customer_Type','CA')
- ->whereIn('CustomerID',$codes)
- ->get();
- $insert_params = [];
- $created_at = Carbon::now()->format('Y-m-d H:i:s');
- foreach ($baseCustomers as $baseCustomer) {
- $insert_params[] = [
- 'code' => $baseCustomer['customerid'],
- 'name' => $baseCustomer['descr_c'],
- 'created_at' => $created_at
- ];
- }
- try {
- if(count($insert_params) > 0){
- $this->insert($insert_params);
- LogService::log(__METHOD__, __FUNCTION__, '批量创建 owner error' . count($insert_params) . json_encode($insert_params) );
- }
- } catch (\Exception $e) {
- LogService::log(__METHOD__, __FUNCTION__, '批量创建 owner error' . json_encode($insert_params) . "||".$e->getMessage() . '||' . $e->getTraceAsString() );
- } finally {
- return Logistic::query()->whereIn('code',$codes)->get();
- }
- }
- public function insert(array $params){
- return Logistic::query()->insert($params);
- }
- }
|