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

import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import com.jeesharp.business.modules.member.entity.Member;
import com.jeesharp.business.modules.member.entity.MemberVip;
import com.jeesharp.business.modules.member.enums.MemberVipStatusEnum;
import com.jeesharp.business.modules.member.mapper.MemberMapper;
import com.jeesharp.business.modules.member.mapper.MemberVipMapper;
import com.jeesharp.business.modules.member.service.MemberService;
import com.jeesharp.business.modules.member.service.MemberVipService;
import com.jeesharp.business.modules.member.vo.MemberVipQueryVO;
import com.jeesharp.framework.mybatis.core.service.CrudService;
import com.jeesharp.framework.pojo.Page;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * 会员付费开通服务实现类
 *
 * @author jeesharp
 * @version 2026-03-19
 */
@Service
@Transactional(readOnly = true)
@RequiredArgsConstructor
public class MemberVipServiceImpl extends CrudService<MemberVipMapper, MemberVip, MemberVipQueryVO> implements MemberVipService {

    private final MemberService memberService;
    private final MemberMapper memberMapper;

    @Override
    public MemberVip getActiveVip(String memberId) {
        if (StrUtil.isBlank(memberId)) {
            return null;
        }
        return baseMapper.selectActiveVipByMemberId(memberId, LocalDateTime.now());
    }

    @Override
    @Transactional(readOnly = false)
    public MemberVip openVip(MemberVip entity) {
        if (StrUtil.isBlank(entity.getId())) {
            entity.setId(IdUtil.getSnowflakeNextIdStr());
        }
        entity.setStatus(MemberVipStatusEnum.ACTIVE.getValue());
        entity.preInsert();
        baseMapper.insert(entity);
        syncMemberVipFields(entity.getMemberId());
        return entity;
    }

    @Override
    @Transactional(readOnly = false)
    public void cancelVip(String id) {
        MemberVip vip = get(id);
        if (vip == null) {
            return;
        }
        vip.setStatus(MemberVipStatusEnum.CANCELLED.getValue());
        vip.preUpdate();
        baseMapper.update(vip);
        syncMemberVipFields(vip.getMemberId());
    }

    @Override
    @Transactional(readOnly = false)
    public void syncMemberVipFields(String memberId) {
        if (StrUtil.isBlank(memberId)) {
            return;
        }
        Member member = memberService.get(memberId);
        if (member == null) {
            return;
        }
        MemberVip activeVip = getActiveVip(memberId);
        if (activeVip != null) {
            member.setVipLevelCode(activeVip.getLevelCode());
            member.setVipExpireDate(activeVip.getExpireDate());
            member.setIsVip(1);
        } else {
            member.setVipLevelCode(null);
            member.setVipExpireDate(null);
            member.setIsVip(0);
        }
        member.preUpdate();
        memberMapper.updateVipFields(member);
    }

    @Override
    public Page<MemberVip> findPage(Page<MemberVip> page, MemberVipQueryVO query) {
        return super.findPage(page, query);
    }

    @Override
    public List<MemberVip> listByMemberId(String memberId) {
        if (StrUtil.isBlank(memberId)) {
            return new ArrayList<>();
        }
        MemberVipQueryVO query = new MemberVipQueryVO();
        query.setMemberId(memberId);
        return findList(query);
    }
}
