LogisticService.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <?php
  2. namespace App\Services;
  3. use App\Logistic;
  4. use App\OracleBasCustomer;
  5. use App\Shop;
  6. use Carbon\Carbon;
  7. use Illuminate\Database\Eloquent\Builder;
  8. use Illuminate\Support\Facades\Auth;
  9. use Illuminate\Support\Facades\Cache;
  10. use Illuminate\Support\Str;
  11. Class LogisticService
  12. {
  13. /** @var CacheService $cacheService */
  14. private $cacheService;
  15. function __construct(){
  16. $this->cacheService=app('CacheService');
  17. }
  18. public function getSelection($column = ['id','name'], $type = '快递'){
  19. return $this->cacheService->getOrExecute('LogisticAll_idName'.Str::studly($type),function()use($column,$type){
  20. $query = Logistic::query()->select($column);
  21. if ($type)$query->where(function ($query)use($type){
  22. /** @var Builder $query */
  23. $query->where("type",$type)->orWhere("type","全部");
  24. });
  25. return $query->get();
  26. },config('cache.expirations.persistent'));
  27. }
  28. public function firstOrCreate(array $params, array $values = null){
  29. return $this->cacheService->getOrExecute('LogisticFirstOrCreate'.md5(json_encode($params).json_encode($values)),function()use($params,$values){
  30. $logistic = Logistic::query();
  31. if ($values)return $logistic->firstOrCreate($params, $values);
  32. return $logistic->firstOrCreate($params);
  33. },config('cache.expirations.commonFrequent'));
  34. }
  35. public function getByWmsOrders($orderHeaders){
  36. $codes = data_get($orderHeaders,'*.userdefine1');
  37. $codes = array_unique($codes);
  38. $codes = array_diff($codes,['','*',null]);
  39. if(!$codes){return [];}
  40. $logistics = Logistic::query()->whereIn('code',$codes)->get();
  41. if($logistics->count() < count($codes)){
  42. $codes = array_diff($codes,data_get($logistics,'*.code'));
  43. $logistic_list = $this->createLogisticByCarrierIds($codes);
  44. $logistics = $logistics->concat($logistic_list);
  45. }
  46. return $logistics;
  47. }
  48. public function createLogisticByCarrierIds($codes){
  49. if(!$codes){return [];}
  50. $baseCustomers = OracleBasCustomer::query()
  51. ->selectRaw('Customer_Type,CustomerID,Descr_C')
  52. ->where('Customer_Type','CA')
  53. ->whereIn('CustomerID',$codes)
  54. ->get();
  55. $insert_params = [];
  56. $created_at = Carbon::now()->format('Y-m-d H:i:s');
  57. foreach ($baseCustomers as $baseCustomer) {
  58. $insert_params[] = [
  59. 'code' => $baseCustomer['customerid'],
  60. 'name' => $baseCustomer['descr_c'],
  61. 'created_at' => $created_at
  62. ];
  63. }
  64. try {
  65. if(count($insert_params) > 0){
  66. $this->insert($insert_params);
  67. LogService::log(__METHOD__, __FUNCTION__, '批量创建 Logistic ' . count($insert_params) . json_encode($insert_params) );
  68. }
  69. } catch (\Exception $e) {
  70. LogService::log(__METHOD__, __FUNCTION__, '批量创建 Logistic error' . json_encode($insert_params) . "||".$e->getMessage() . '||' . $e->getTraceAsString() );
  71. } finally {
  72. return Logistic::query()->whereIn('code',$codes)->get();
  73. }
  74. }
  75. public function insert(array $params){
  76. return Logistic::query()->insert($params);
  77. }
  78. public function find($id)
  79. {
  80. return Logistic::query()->find($id);
  81. }
  82. public function getLogisticByCodes($codes)
  83. {
  84. $collect = collect();
  85. if(count($codes) == 0) return $collect;
  86. foreach ($codes as $code) {
  87. $collect->push(Cache::remember("getLogisticByCodes_{$code}", null, function()use($code){
  88. $logistic = Logistic::query()->where('code',$code)->first();
  89. if($logistic)return $logistic;
  90. $baseCustomers = OracleBasCustomer::query()
  91. ->selectRaw('Customer_Type,CustomerID,Descr_C')
  92. ->where('Customer_Type','CA')
  93. ->where('CustomerID',$code)->first();
  94. if(!$baseCustomers)return null;
  95. return Logistic::query()->create(['name' => $baseCustomers['descr_c'], 'code' => $baseCustomers['customerid']]);
  96. }));
  97. }
  98. return $collect;
  99. }
  100. }