LogisticService.php 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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. /** @var CacheService $cacheService */
  10. private $cacheService;
  11. function __construct(){
  12. $this->cacheService=app('CacheService');
  13. }
  14. public function getSelection($column = ['id','name']){
  15. return $this->cacheService->getOrExecute('LogisticAll_idName',function()use($column){
  16. return Logistic::query()->select($column)->get();
  17. },config('cache.expirations.persistent'));
  18. }
  19. public function firstOrCreate(array $params, array $values = null){
  20. return $this->cacheService->getOrExecute('LogisticFirstOrCreate'.md5(json_encode($params).json_encode($values)),function()use($params,$values){
  21. $logistic = Logistic::query();
  22. if ($values)return $logistic->firstOrCreate($params, $values);
  23. return $logistic->firstOrCreate($params);
  24. },config('cache.expirations.commonFrequent'));
  25. }
  26. public function getByWmsOrders($orderHeaders){
  27. $codes = data_get($orderHeaders,'*.userdefine1');
  28. $codes = array_unique($codes);
  29. $codes = array_diff($codes,['','*',null]);
  30. if(!$codes){return [];}
  31. $logistics = Logistic::query()->whereIn('code',$codes)->get();
  32. if($logistics->count() < count($codes)){
  33. $codes = array_diff($codes,data_get($logistics,'*.code'));
  34. $logistic_list = $this->createLogisticByCarrierIds($codes);
  35. $logistics = $logistics->concat($logistic_list);
  36. }
  37. return $logistics;
  38. }
  39. public function createLogisticByCarrierIds($codes){
  40. if(!$codes){return [];}
  41. $baseCustomers = OracleBasCustomer::query()
  42. ->selectRaw('Customer_Type,CustomerID,Descr_C')
  43. ->where('Customer_Type','CA')
  44. ->whereIn('CustomerID',$codes)
  45. ->get();
  46. $insert_params = [];
  47. $created_at = Carbon::now()->format('Y-m-d H:i:s');
  48. foreach ($baseCustomers as $baseCustomer) {
  49. $insert_params[] = [
  50. 'code' => $baseCustomer['customerid'],
  51. 'name' => $baseCustomer['descr_c'],
  52. 'created_at' => $created_at
  53. ];
  54. }
  55. try {
  56. if(count($insert_params) > 0){
  57. $this->insert($insert_params);
  58. LogService::log(__METHOD__, __FUNCTION__, '批量创建 Logistic ' . count($insert_params) . json_encode($insert_params) );
  59. }
  60. } catch (\Exception $e) {
  61. LogService::log(__METHOD__, __FUNCTION__, '批量创建 Logistic error' . json_encode($insert_params) . "||".$e->getMessage() . '||' . $e->getTraceAsString() );
  62. } finally {
  63. return Logistic::query()->whereIn('code',$codes)->get();
  64. }
  65. }
  66. public function insert(array $params){
  67. return Logistic::query()->insert($params);
  68. }
  69. }