LogisticService.php 2.8 KB

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