ShopService.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. <?php
  2. namespace App\Services;
  3. use App\Owner;
  4. use App\Services\common\BatchUpdateService;
  5. use App\Shop;
  6. use Carbon\Carbon;
  7. class ShopService
  8. {
  9. function firstOrCreate(array $param, array $values = null){
  10. $shop = Shop::query();
  11. if ($values) return $shop->firstOrCreate($param,$values);
  12. return $shop->firstOrCreate($param);
  13. }
  14. public function insert(array $params)
  15. {
  16. return Shop::query()->insert($params);
  17. }
  18. public function batchUpdate(array $params)
  19. {
  20. return app(BatchUpdateService::class)->batchUpdate('shops',$params);
  21. }
  22. public function 根据追踪的WMS订单返回商铺($WMSOrderHeaders, $owners = null)
  23. {
  24. if ($owners == null) {
  25. /** @var $ownerService OwnerService */
  26. $ownerService = app('ownerService');
  27. $owners = $ownerService->获取订单跟踪的货主();
  28. }
  29. if(count($WMSOrderHeaders) == 0){
  30. return [];
  31. }
  32. $issuePartyNames = [];
  33. foreach ($WMSOrderHeaders as $WMSOrderHeader) {
  34. $issuePartyNames[$WMSOrderHeader['issuepartyname']] = $WMSOrderHeader;
  35. }
  36. $issuePartyNames = array_unique($issuePartyNames);
  37. $issuePartyNames = array_diff($issuePartyNames,['','*',null]);
  38. $shops = Shop::query()->whereIn('name',array_keys($issuePartyNames))->get();
  39. $insert_params = [];
  40. if (count($shops) < count($issuePartyNames)) {
  41. $shopNames = data_get($shops, '*.name');
  42. $shopDiff = array_diff($issuePartyNames, $shopNames);
  43. foreach ($shopDiff as $key=>$item) {
  44. if($key === '' ){
  45. continue;
  46. }
  47. $owner = $owners->where('code', $item['customerid'])->first();
  48. if($owner === null || $item['issuepartyname'] ?? false)
  49. continue;
  50. array_push($insert_params,['owner_id' => $owner['id'], 'name' => $item['issuepartyname']]);
  51. }
  52. }
  53. try {
  54. if(count($insert_params) > 0){
  55. Shop::query()->insert($insert_params);
  56. LogService::log(__METHOD__, __FUNCTION__, '批量创建 shop' .json_encode($insert_params));
  57. }
  58. } catch (\Exception $e) {
  59. LogService::log(__METHOD__, __FUNCTION__, '创建创建 shop error' .json_encode($insert_params) . $e->getMessage(),$e->getTraceAsString());
  60. }
  61. return Shop::query()->whereIn('name',array_keys($issuePartyNames))->get();
  62. }
  63. public function getByWmsOrders($orderHeaders){
  64. //issuepartyname
  65. $maps = [];
  66. foreach ($orderHeaders as $orderHeader) {
  67. $value = ['owner_code'=>$orderHeader->customerid,'issuepartyname'=>$orderHeader->issuepartyname];
  68. if(!in_array($value,$maps)){
  69. $maps[] = $value;
  70. }
  71. }
  72. $issuepartynames = array_diff(array_unique(data_get($maps,'*.issuepartyname')),['','*',null]) ;
  73. $shops = Shop::query()
  74. ->whereIn('name',$issuepartynames)
  75. ->get();
  76. if($shops->count() < count($issuepartynames)){
  77. $shops_code = data_get($shops,'*.name');
  78. $issuepartynames = array_diff($issuepartynames,$shops_code);
  79. $params = [];
  80. foreach ($maps as $item){
  81. if(in_array($item['issuepartyname'],$issuepartynames)){
  82. $params[] = $item;
  83. }
  84. }
  85. $shops_list = $this->createByNameAndOwnerCode($params);
  86. $shops->concat($shops_list);
  87. }
  88. return $shops;
  89. }
  90. public function createByNameAndOwnerCode(array $params){
  91. $owners = Owner::query()
  92. ->whereIn('code',data_get($params,'*.owner_code'))
  93. ->get();
  94. $owners_map = [];
  95. foreach ($owners as $owner) {
  96. $owners_map[$owner->code] = $owner;
  97. }
  98. $insert_params = [];
  99. $created_at = Carbon::now()->format('Y-m-d H:i:s');
  100. foreach ($params as $param) {
  101. $owner = $owners_map[$param['owner_code']] ?? '';
  102. $insert_params[] = [
  103. 'name' => $param['issuepartyname'] ?? '',
  104. 'owner_id' => $owner['id'] ?? '',
  105. 'created_at' => $created_at,
  106. ];
  107. }
  108. try {
  109. if (count($insert_params) > 0) {
  110. $this->insert($insert_params);
  111. LogService::log(__METHOD__, __FUNCTION__, '批量创建 shop ' . count($insert_params) . json_encode($insert_params));
  112. }
  113. } catch (\Exception $e) {
  114. LogService::log(__METHOD__, __FUNCTION__, '批量创建 shop ' . json_encode($insert_params) .'||'.$e->getMessage().'||'.$e->getTraceAsString());
  115. } finally {
  116. return Shop::query()
  117. ->whereIn('name',data_get($params,'*.issuepartyname'))
  118. ->get();
  119. }
  120. }
  121. }