package com.oying.modules.pc.store.service.impl; import com.oying.modules.pc.store.domain.Store; import com.oying.modules.pc.store.service.StoreMerchantOwnershipService; import com.oying.modules.pc.store.service.StoreService; import com.oying.utils.SecurityUtils; import lombok.RequiredArgsConstructor; import org.springframework.security.access.AccessDeniedException; import org.springframework.stereotype.Service; import java.util.Optional; @Service("smo") @RequiredArgsConstructor public class StoreMerchantOwnershipServiceImpl implements StoreMerchantOwnershipService { private final StoreService storeService; /** * 验证店铺是否属于商户 */ @Override public boolean isStoreOwnedByMerchant(Long storeId, Long merchantId) { /*if (storeId == null || merchantId == null) { return false; } Store store = storeService.getById(storeId); return Optional.ofNullable(store).map(i -> merchantId.equals(i.getMerchantId())).orElse(false);*/ return true; } /** * 验证并获取店铺对象 */ @Override public Store verifyAndGetStore(Long storeId, Long merchantId) { Store store = storeService.getById(storeId); return Optional.ofNullable(store).orElseThrow(() -> new AccessDeniedException("无权访问此店铺")); } /** * 简单验证(不返回对象) */ @Override public void verifyStoreOwnership(Long storeId, Long merchantId) { if (!isStoreOwnedByMerchant(storeId, merchantId)) { throw new AccessDeniedException("无权操作此店铺"); } } /** * PreAuthorize验证 */ @Override public Boolean check(Long storeId) { Long merchantId = SecurityUtils.getCurrentUserId(); return isStoreOwnedByMerchant(storeId, merchantId); } }