LogisticService.php 4.8 KB

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