ShopService.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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. public function getSelection(array $column = ['id', 'name'])
  10. {
  11. return Shop::query()->select($column)->get();
  12. }
  13. function firstOrCreate(array $param, array $values = null){
  14. $shop = Shop::query();
  15. if ($values) return $shop->firstOrCreate($param,$values);
  16. return $shop->firstOrCreate($param);
  17. }
  18. public function insert(array $params)
  19. {
  20. return Shop::query()->insert($params);
  21. }
  22. public function batchUpdate(array $params)
  23. {
  24. return app(BatchUpdateService::class)->batchUpdate('shops',$params);
  25. }
  26. public function getByWmsOrders($orderHeaders)
  27. {
  28. $maps = []; //issuepartyname
  29. foreach ($orderHeaders as $orderHeader) {
  30. $value = ['owner_code'=>$orderHeader->customerid,'issuepartyname'=>$orderHeader->issuepartyname];
  31. if(!in_array($value,$maps))
  32. $maps[] = $value;
  33. }
  34. $issuepartynames = array_diff(array_unique(data_get($maps,'*.issuepartyname')),['','*',null]);
  35. $owner_codes = array_diff(array_unique(data_get($maps,'*.owner_code')),['','*',null]);
  36. $shops = Shop::query()->with('owner')
  37. ->whereHas('owner',function($query)use($owner_codes){
  38. $query->whereIn('code',$owner_codes);
  39. })->whereIn('name',$issuepartynames)->get();
  40. if($shops->count() < count($issuepartynames)){
  41. $shops_code = data_get($shops,'*.name');
  42. $names = array_diff($issuepartynames,$shops_code);
  43. $params = [];
  44. foreach ($maps as $item){
  45. if(in_array($item['issuepartyname'],$names))
  46. $params[] = $item;
  47. }
  48. $shops_list = $this->createByNameAndOwnerCode($params);
  49. $shops = $shops->concat($shops_list??[]);
  50. }
  51. return $shops;
  52. }
  53. public function createByNameAndOwnerCode(array $params){
  54. $owners = Owner::query()->whereIn('code',data_get($params,'*.owner_code'))->get();
  55. $owners_map = [];
  56. foreach ($owners as $owner) {
  57. $owners_map[$owner->code] = $owner;
  58. }
  59. $insert_params = [];
  60. $created_at = Carbon::now()->format('Y-m-d H:i:s');
  61. foreach ($params as $param) {
  62. $owner = $owners_map[$param['owner_code']] ?? '';
  63. $insert_params[] = [
  64. 'name' => $param['issuepartyname'] ?? '',
  65. 'owner_id' => $owner['id'] ?? '',
  66. 'created_at' => $created_at,
  67. ];
  68. }
  69. try {
  70. if (count($insert_params) > 0) {
  71. $this->insert($insert_params);
  72. app('LogService')->log(__METHOD__, __FUNCTION__, '批量创建 shop ' . count($insert_params) . json_encode($insert_params));
  73. }
  74. } catch (\Exception $e) {
  75. app('LogService')->log(__METHOD__, 'Error '.__FUNCTION__, '批量创建 shop Error' . json_encode($insert_params) .'||'.$e->getMessage().'||'.$e->getTraceAsString());
  76. } finally {
  77. return Shop::query()->whereIn('name',data_get($params,'*.issuepartyname'))->get();
  78. }
  79. }
  80. }