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

import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.jeesharp.business.modules.member.entity.MemberVisitor;
import com.jeesharp.business.modules.member.mapper.MemberVisitorMapper;
import com.jeesharp.business.modules.member.service.MemberVisitorService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 会员常用游客领域服务实现，基于 MyBatis-Plus 条件查询与更新。
 *
 * @author jeesharp
 * @since 2026-05-12
 */
@Service
@RequiredArgsConstructor
@Transactional(readOnly = true)
public class MemberVisitorServiceImpl implements MemberVisitorService {

    private final MemberVisitorMapper memberVisitorMapper;

    @Override
    public List<MemberVisitor> listByMemberId(String memberId) {
        LambdaQueryWrapper<MemberVisitor> w = new LambdaQueryWrapper<>();
        w.eq(MemberVisitor::getMemberId, memberId);
        w.eq(MemberVisitor::getDelFlag, "0");
        w.orderByDesc(MemberVisitor::getSortNo);
        w.orderByDesc(MemberVisitor::getCreateDate);
        return memberVisitorMapper.selectList(w);
    }

    @Override
    public long countByMemberId(String memberId) {
        LambdaQueryWrapper<MemberVisitor> w = new LambdaQueryWrapper<>();
        w.eq(MemberVisitor::getMemberId, memberId);
        w.eq(MemberVisitor::getDelFlag, "0");
        return memberVisitorMapper.selectCount(w);
    }

    @Override
    public MemberVisitor getOwned(String id, String memberId) {
        if (StrUtil.isBlank(id) || StrUtil.isBlank(memberId)) {
            return null;
        }
        MemberVisitor v = memberVisitorMapper.selectById(id);
        if (v == null || !"0".equals(v.getDelFlag()) || !memberId.equals(v.getMemberId())) {
            return null;
        }
        return v;
    }

    @Override
    public boolean existsIdCardForMember(String memberId, String idCardUpper, String excludeId) {
        LambdaQueryWrapper<MemberVisitor> w = new LambdaQueryWrapper<>();
        w.eq(MemberVisitor::getMemberId, memberId);
        w.eq(MemberVisitor::getDelFlag, "0");
        w.eq(MemberVisitor::getIdCard, idCardUpper);
        if (StrUtil.isNotBlank(excludeId)) {
            w.ne(MemberVisitor::getId, excludeId);
        }
        return memberVisitorMapper.selectCount(w) > 0;
    }

    @Override
    @Transactional(readOnly = false)
    public void insert(MemberVisitor entity) {
        if (StrUtil.isBlank(entity.getId())) {
            entity.setId(IdUtil.getSnowflakeNextIdStr());
        }
        if (entity.getSortNo() == null) {
            entity.setSortNo(0);
        }
        if (entity.getDelFlag() == null) {
            entity.setDelFlag("0");
        }
        entity.preInsert();
        memberVisitorMapper.insert(entity);
    }

    @Override
    @Transactional(readOnly = false)
    public void updateEntity(MemberVisitor entity) {
        entity.preUpdate();
        memberVisitorMapper.update(entity);
    }

    @Override
    @Transactional(readOnly = false)
    public void removeOwned(String id, String memberId) {
        MemberVisitor v = getOwned(id, memberId);
        if (v == null) {
            return;
        }
        v.setDelFlag("1");
        v.preUpdate();
        memberVisitorMapper.update(v);
    }
}
