ShopService.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. <?php
  2. namespace App\Services;
  3. use App\OracleBasCustomer;
  4. use App\Owner;
  5. use App\Services\common\BatchUpdateService;
  6. use App\Shop;
  7. use Carbon\Carbon;
  8. use Illuminate\Support\Facades\Cache;
  9. use App\Traits\ServiceAppAop;
  10. class ShopService
  11. {
  12. use ServiceAppAop;
  13. protected $modelClass=Shop::class;
  14. /** @var CacheService $cacheService */
  15. private $cacheService;
  16. function __construct(){
  17. $this->instant($this->cacheService,'CacheService');
  18. }
  19. public function getSelection(array $column = ['id', 'name'])
  20. {
  21. return Shop::query()->select($column)->get();
  22. }
  23. function firstOrCreate(array $param, array $values = null){
  24. $shop = Shop::query();
  25. if ($values) return $shop->firstOrCreate($param,$values);
  26. return $shop->firstOrCreate($param);
  27. }
  28. public function batchUpdate(array $params)
  29. {
  30. return app(BatchUpdateService::class)->batchUpdate('shops',$params);
  31. }
  32. public function getByWmsOrders($orderHeaders)
  33. {
  34. $maps = []; //issuepartyname
  35. foreach ($orderHeaders as $orderHeader) {
  36. $value = ['owner_code'=>$orderHeader->customerid,'issuepartyname'=>$orderHeader->issuepartyname];
  37. if(!in_array($value,$maps))
  38. $maps[] = $value;
  39. }
  40. $issuepartynames = array_diff(array_unique(data_get($maps,'*.issuepartyname')),['','*',null]);
  41. $owner_codes = array_diff(array_unique(data_get($maps,'*.owner_code')),['','*',null]);
  42. $shops = Shop::query()->with('owner')
  43. ->whereHas('owner',function($query)use($owner_codes){
  44. $query->whereIn('code',$owner_codes);
  45. })->whereIn('name',$issuepartynames)->get();
  46. if($shops->count() < count($issuepartynames)){
  47. $shops_code = data_get($shops,'*.name');
  48. $names = array_diff($issuepartynames,$shops_code);
  49. $params = [];
  50. foreach ($maps as $item){
  51. if(in_array($item['issuepartyname'],$names))
  52. $params[] = $item;
  53. }
  54. $shops_list = $this->createByNameAndOwnerCode($params);
  55. $shops = $shops->concat($shops_list??[]);
  56. }
  57. return $shops;
  58. }
  59. public function createByNameAndOwnerCode(array $params){
  60. $owners = Owner::query()->whereIn('code',data_get($params,'*.owner_code'))->get();
  61. $owners_map = [];
  62. foreach ($owners as $owner) {
  63. $owners_map[$owner->code] = $owner;
  64. }
  65. $insert_params = [];
  66. $created_at = Carbon::now()->format('Y-m-d H:i:s');
  67. foreach ($params as $param) {
  68. $owner = $owners_map[$param['owner_code']] ?? '';
  69. $insert_params[] = [
  70. 'name' => $param['issuepartyname'] ?? '',
  71. 'owner_id' => $owner['id'] ?? '',
  72. 'created_at' => $created_at,
  73. ];
  74. }
  75. try {
  76. if (count($insert_params) > 0) {
  77. $this->insert($insert_params);
  78. app('LogService')->log(__METHOD__, __FUNCTION__, '批量创建 shop ' . count($insert_params) . json_encode($insert_params));
  79. }
  80. } catch (\Exception $e) {
  81. app('LogService')->log(__METHOD__, 'Error '.__FUNCTION__, '批量创建 shop Error' . json_encode($insert_params) .'||'.$e->getMessage().'||'.$e->getTraceAsString());
  82. } finally {
  83. return Shop::query()->whereIn('name',data_get($params,'*.issuepartyname'))->get();
  84. }
  85. }
  86. public function getShopByCodeMap($map)
  87. {
  88. /** @var OwnerService $ownerService */
  89. $ownerService = app('OwnerService');
  90. $owner_codes = [];
  91. foreach ($map as $item) {
  92. $owner_codes[$item['owner_code']]= $item['owner_code'];
  93. }
  94. $owners = $ownerService->getOwnerByCodes($owner_codes);
  95. $owner_map = [];
  96. $owners->each(function($owner)use(&$owner_map){
  97. $owner_map[$owner['code']] = $owner;
  98. });
  99. $collect = collect();
  100. if(count($map)==0)return $collect;
  101. foreach ($map as $item) {
  102. $collect->push(Cache::remember("getShopByCodeMap_{$item['owner_code']}_{$item['issuepartyname']}", config('cache.expirations.rarelyChange'), function()use($item,$owner_map){
  103. $owner = $owner_map[$item['owner_code']] ;
  104. $shop = Shop::query()->where('owner_id',$owner['id'])->where('name',$item['issuepartyname'])->first();
  105. if($shop)return $shop;
  106. return Shop::query()->create(['owner_id'=>$owner['id'],'name'=>$item['issuepartyname']]);
  107. }));
  108. }
  109. return $collect;
  110. }
  111. }