LogisticService.php 3.1 KB

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