LogisticService.php 3.3 KB

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