package shop.alien.store.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import shop.alien.entity.store.PlatformShopMembershipPlan;
import shop.alien.mapper.PlatformShopMembershipPlanMapper;
import shop.alien.store.service.PlatformShopMembershipPlanService;

import java.util.List;

@Service
@RequiredArgsConstructor
public class PlatformShopMembershipPlanServiceImpl
        extends ServiceImpl<PlatformShopMembershipPlanMapper, PlatformShopMembershipPlan>
        implements PlatformShopMembershipPlanService {

    @Override
    public List<PlatformShopMembershipPlan> listEnabledForStore() {
        LambdaQueryWrapper<PlatformShopMembershipPlan> w = new LambdaQueryWrapper<>();
        w.eq(PlatformShopMembershipPlan::getEnabled, 1);
        w.orderByDesc(PlatformShopMembershipPlan::getRecommended)
                .orderByAsc(PlatformShopMembershipPlan::getSortOrder)
                .orderByAsc(PlatformShopMembershipPlan::getId);
        return list(w);
    }

    @Override
    public PlatformShopMembershipPlan getEnabledById(Integer planId) {
        if (planId == null) {
            return null;
        }
        PlatformShopMembershipPlan plan = getById(planId);
        if (plan == null || plan.getEnabled() == null || plan.getEnabled() != 1) {
            return null;
        }
        return plan;
    }
}
