LogisticService.php 4.7 KB

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