package com.jeesharp.business.modules.member.service.impl;

import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.jeesharp.business.modules.member.entity.MemberFavorite;
import com.jeesharp.business.modules.member.mapper.MemberFavoriteMapper;
import com.jeesharp.business.modules.member.service.MemberFavoriteService;
import com.jeesharp.framework.pojo.Page;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.Optional;

/**
 * 会员统一收藏领域服务实现。
 *
 * @author jeesharp
 * @since 2026-05-20
 */
@Service
@AllArgsConstructor
public class MemberFavoriteServiceImpl implements MemberFavoriteService {

    private final MemberFavoriteMapper memberFavoriteMapper;

    @Override
    public Optional<MemberFavorite> findActive(String memberId, String bizType, String bizId) {
        LambdaQueryWrapper<MemberFavorite> w = baseWrapper(memberId);
        w.eq(MemberFavorite::getBizType, bizType);
        w.eq(MemberFavorite::getBizId, bizId);
        w.last("LIMIT 1");
        return Optional.ofNullable(memberFavoriteMapper.selectOne(w));
    }

    @Override
    public boolean exists(String memberId, String bizType, String bizId) {
        return findActive(memberId, bizType, bizId).isPresent();
    }

    @Override
    public void saveFavorite(MemberFavorite favorite) {
        if (favorite.getId() == null) {
            favorite.setId(IdUtil.getSnowflakeNextIdStr());
        }
        favorite.preInsert();
        if (favorite.getDelFlag() == null) {
            favorite.setDelFlag("0");
        }
        memberFavoriteMapper.insert(favorite);
    }

    @Override
    public void removeFavorite(String memberId, String bizType, String bizId) {
        findActive(memberId, bizType, bizId).ifPresent(row -> memberFavoriteMapper.deleteById(row.getId()));
    }

    @Override
    public Page<MemberFavorite> findPageByMember(Page<MemberFavorite> page, String memberId, String bizType) {
        LambdaQueryWrapper<MemberFavorite> w = baseWrapper(memberId);
        if (StringUtils.hasText(bizType)) {
            w.eq(MemberFavorite::getBizType, bizType.trim());
        }
        w.orderByDesc(MemberFavorite::getCreateDate);
        return memberFavoriteMapper.selectPage(page, w);
    }

    @Override
    public List<MemberFavorite> listByMember(String memberId, String bizType, int limit) {
        LambdaQueryWrapper<MemberFavorite> w = baseWrapper(memberId);
        if (StringUtils.hasText(bizType)) {
            w.eq(MemberFavorite::getBizType, bizType.trim());
        }
        w.orderByDesc(MemberFavorite::getCreateDate);
        w.last("LIMIT " + Math.max(1, Math.min(limit, 500)));
        return memberFavoriteMapper.selectList(w);
    }

    @Override
    public long countByMemberId(String memberId) {
        LambdaQueryWrapper<MemberFavorite> w = baseWrapper(memberId);
        return memberFavoriteMapper.selectCount(w);
    }

    private LambdaQueryWrapper<MemberFavorite> baseWrapper(String memberId) {
        LambdaQueryWrapper<MemberFavorite> w = new LambdaQueryWrapper<>();
        w.eq(MemberFavorite::getMemberId, memberId);
        w.eq(MemberFavorite::getDelFlag, "0");
        return w;
    }
}
