ShopService.php 4.6 KB

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