StoreTypeService.php 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. <?php
  2. namespace App\Services;
  3. use App\OracleBasCode;
  4. use App\StoreType;
  5. use Carbon\Carbon;
  6. Class StoreTypeService
  7. {
  8. public function getByWms($asnHerders)
  9. {
  10. $types = array_unique(data_get($asnHerders, '*.asntype'));
  11. $types = array_diff($types, [null, '', '*']);
  12. $storeTypes = StoreType::query()->whereIn('code', $types)->get();
  13. if ($storeTypes->count() < count($types)) {
  14. $types = array_diff($types, data_get($storeTypes, '*.code'));
  15. $storeType_list = $this->createStoreTypeByWms($types);
  16. $storeTypes=$storeTypes->concat($storeType_list);
  17. }
  18. return $storeTypes;
  19. }
  20. public function createStoreTypeByWms($codes){
  21. if(!$codes) {return [];}
  22. $basCodes = OracleBasCode::query()
  23. ->where('codeid','ASN_TYP')
  24. ->whereIn('code', $codes)
  25. ->get();
  26. $insert_params = [];
  27. $created_at = Carbon::now()->format('Y-m-d H:i:s');
  28. foreach ($basCodes as $item) {
  29. $insert_params[] = [
  30. 'code' => $item->code,
  31. 'name' => $item->codename_c,
  32. 'created_at' => $created_at,
  33. ];
  34. }
  35. try {
  36. if (count($insert_params) > 0) {
  37. $this->insert($insert_params);
  38. LogService::log(__METHOD__, __FUNCTION__, '批量创建 storeType success' . count($insert_params) . json_encode($insert_params) );
  39. }
  40. } catch (\Exception $e) {
  41. LogService::log(__METHOD__, __FUNCTION__, '批量创建 storeType error' . json_encode($insert_params) . '||' . $e->getMessage() . '||' . $e->getTraceAsString());
  42. } finally {
  43. return StoreType::query()->whereIn('code', $codes)->get();
  44. }
  45. }
  46. public function insert($fillables){
  47. return StoreType::query()->insert($fillables);
  48. }
  49. }