| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133 |
- <?php
- namespace App\Services;
- use App\Owner;
- use App\Services\common\BatchUpdateService;
- use App\Shop;
- use Carbon\Carbon;
- class ShopService
- {
- function firstOrCreate(array $param, array $values = null){
- $shop = Shop::query();
- if ($values) return $shop->firstOrCreate($param,$values);
- return $shop->firstOrCreate($param);
- }
- public function insert(array $params)
- {
- return Shop::query()->insert($params);
- }
- public function batchUpdate(array $params)
- {
- return app(BatchUpdateService::class)->batchUpdate('shops',$params);
- }
- public function 根据追踪的WMS订单返回商铺($WMSOrderHeaders, $owners = null)
- {
- if ($owners == null) {
- /** @var $ownerService OwnerService */
- $ownerService = app('ownerService');
- $owners = $ownerService->获取订单跟踪的货主();
- }
- if(count($WMSOrderHeaders) == 0){
- return [];
- }
- $issuePartyNames = [];
- foreach ($WMSOrderHeaders as $WMSOrderHeader) {
- $issuePartyNames[$WMSOrderHeader['issuepartyname']] = $WMSOrderHeader;
- }
- $issuePartyNames = array_unique($issuePartyNames);
- $issuePartyNames = array_diff($issuePartyNames,['','*',null]);
- $shops = Shop::query()->whereIn('name',array_keys($issuePartyNames))->get();
- $insert_params = [];
- if (count($shops) < count($issuePartyNames)) {
- $shopNames = data_get($shops, '*.name');
- $shopDiff = array_diff($issuePartyNames, $shopNames);
- foreach ($shopDiff as $key=>$item) {
- if($key === '' ){
- continue;
- }
- $owner = $owners->where('code', $item['customerid'])->first();
- if($owner === null || $item['issuepartyname'] ?? false)
- continue;
- array_push($insert_params,['owner_id' => $owner['id'], 'name' => $item['issuepartyname']]);
- }
- }
- try {
- if(count($insert_params) > 0){
- Shop::query()->insert($insert_params);
- LogService::log(__METHOD__, __FUNCTION__, '批量创建 shop' .json_encode($insert_params));
- }
- } catch (\Exception $e) {
- LogService::log(__METHOD__, __FUNCTION__, '创建创建 shop error' .json_encode($insert_params) . $e->getMessage(),$e->getTraceAsString());
- }
- return Shop::query()->whereIn('name',array_keys($issuePartyNames))->get();
- }
- public function getByWmsOrders($orderHeaders){
- //issuepartyname
- $maps = [];
- foreach ($orderHeaders as $orderHeader) {
- $value = ['owner_code'=>$orderHeader->customerid,'issuepartyname'=>$orderHeader->issuepartyname];
- if(!in_array($value,$maps)){
- $maps[] = $value;
- }
- }
- $issuepartynames = array_diff(array_unique(data_get($maps,'*.issuepartyname')),['','*',null]) ;
- $shops = Shop::query()
- ->whereIn('name',$issuepartynames)
- ->get();
- if($shops->count() < count($issuepartynames)){
- $shops_code = data_get($shops,'*.name');
- $issuepartynames = array_diff($issuepartynames,$shops_code);
- $params = [];
- foreach ($maps as $item){
- if(in_array($item['issuepartyname'],$issuepartynames)){
- $params[] = $item;
- }
- }
- $shops_list = $this->createByNameAndOwnerCode($params);
- $shops->concat($shops_list);
- }
- return $shops;
- }
- public function createByNameAndOwnerCode(array $params){
- $owners = Owner::query()
- ->whereIn('code',data_get($params,'*.owner_code'))
- ->get();
- $owners_map = [];
- foreach ($owners as $owner) {
- $owners_map[$owner->code] = $owner;
- }
- $insert_params = [];
- $created_at = Carbon::now()->format('Y-m-d H:i:s');
- foreach ($params as $param) {
- $owner = $owners_map[$param['owner_code']] ?? '';
- $insert_params[] = [
- 'name' => $param['issuepartyname'] ?? '',
- 'owner_id' => $owner['id'] ?? '',
- 'created_at' => $created_at,
- ];
- }
- try {
- if (count($insert_params) > 0) {
- $this->insert($insert_params);
- LogService::log(__METHOD__, __FUNCTION__, '批量创建 shop ' . count($insert_params) . json_encode($insert_params));
- }
- } catch (\Exception $e) {
- LogService::log(__METHOD__, __FUNCTION__, '批量创建 shop ' . json_encode($insert_params) .'||'.$e->getMessage().'||'.$e->getTraceAsString());
- } finally {
- return Shop::query()
- ->whereIn('name',data_get($params,'*.issuepartyname'))
- ->get();
- }
- }
- }
|