package shop.alien.store.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import shop.alien.entity.store.PlatformShopMembership;
import shop.alien.mapper.PlatformShopMembershipMapper;
import shop.alien.store.service.PlatformShopMembershipAdminMutationService;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

@Service
public class PlatformShopMembershipAdminMutationServiceImpl
        extends ServiceImpl<PlatformShopMembershipMapper, PlatformShopMembership>
        implements PlatformShopMembershipAdminMutationService {

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int logicDeleteByStoreIds(List<Integer> storeIds) {
        if (storeIds == null || storeIds.isEmpty()) {
            throw new IllegalArgumentException("门店ID列表不能为空");
        }
        List<Integer> ids = storeIds.stream().filter(Objects::nonNull).distinct().collect(Collectors.toList());
        if (ids.isEmpty()) {
            throw new IllegalArgumentException("门店ID列表不能为空");
        }
        LambdaQueryWrapper<PlatformShopMembership> w = new LambdaQueryWrapper<>();
        w.in(PlatformShopMembership::getStoreId, ids);
        List<PlatformShopMembership> list = list(w);
        if (list.isEmpty()) {
            return 0;
        }
        List<Integer> pkIds = list.stream().map(PlatformShopMembership::getId).collect(Collectors.toList());
        removeByIds(pkIds);
        return list.size();
    }
}
