LogisticService.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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. ->where('Customer_Type','CA')
  46. ->whereIn('CustomerID',$codes)
  47. ->get();
  48. $insert_params = [];
  49. $created_at = Carbon::now()->format('Y-m-d H:i:s');
  50. foreach ($baseCustomers as $baseCustomer) {
  51. $insert_params[] = [
  52. 'code' => $baseCustomer['customerid'],
  53. 'name' => $baseCustomer['descr_c'],
  54. 'created_at' => $created_at
  55. ];
  56. }
  57. try {
  58. if(count($insert_params) > 0){
  59. $this->insert($insert_params);
  60. LogService::log(__METHOD__, __FUNCTION__, '批量创建 Logistic ' . count($insert_params) . json_encode($insert_params) );
  61. }
  62. } catch (\Exception $e) {
  63. LogService::log(__METHOD__, __FUNCTION__, '批量创建 Logistic error' . json_encode($insert_params) . "||".$e->getMessage() . '||' . $e->getTraceAsString() );
  64. } finally {
  65. return Logistic::query()->whereIn('code',$codes)->get();
  66. }
  67. }
  68. public function insert(array $params){
  69. return Logistic::query()->insert($params);
  70. }
  71. public function find($id)
  72. {
  73. return Logistic::query()->find($id);
  74. }
  75. }