package com.ruoyi.ss.vip.service.impl;

import com.ruoyi.ss.vip.domain.Vip;
import com.ruoyi.ss.vip.service.VipConverter;
import com.ruoyi.ss.vip.utils.VipUtil;
import com.ruoyi.ss.vipLevel.domain.VipLevelVO;
import com.ruoyi.ss.vipLevel.service.VipLevelService;
import com.ruoyi.ss.vipOrder.domain.VipOrderVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

/**
 * @author wjh
 * 2025/1/18
 */
@Service
public class VipConverterImpl implements VipConverter {

    @Autowired
    private VipLevelService vipLevelService;

    @Override
    public Vip toPo(VipOrderVO order) {
        if (order == null) {
            return null;
        }

        Vip vip = new Vip();
        vip.setUserId(order.getUserId());
        vip.setLevelId(order.getLevelId());
        vip.setStartTime(LocalDateTime.now());
        vip.setEndTime(LocalDateTime.now().plusDays(order.getLevelTime()));
        vip.setDiscount(order.getLevelDiscount());
        vip.setStoreId(order.getLevelStoreId());
        vip.setTotalCount(0);
        vip.setRoundCount(0);
        vip.setLimitType(order.getLevelLimitType());
        vip.setLimitCount(order.getLevelLimitCount());
        vip.setSurplusCount(order.getLevelLimitTotal());
        vip.setNextResetTime(VipUtil.getNextResetTime(LocalDateTime.now(), order.getLevelLimitType()));

        return vip;
    }


    @Override
    public Vip toPoByAdd(Vip data) {
        if (data == null) {
            return null;
        }

        VipLevelVO level = vipLevelService.selectVipLevelById(data.getLevelId());
        if(level == null) {
            return null;
        }

        Vip vip = new Vip();
        // 自定义数据
        vip.setUserId(data.getUserId());
        vip.setStartTime(data.getStartTime());
        vip.setEndTime(data.getEndTime());
        vip.setLimitType(data.getLimitType());
        vip.setNextResetTime(VipUtil.getNextResetTime(LocalDateTime.now(), data.getLimitType()));
        vip.setTotalCount(0);
        vip.setRoundCount(0);
        vip.setLimitCount(data.getLimitCount());
        vip.setSurplusCount(data.getSurplusCount());

        // 等级数据
        vip.setLevelId(level.getId());
        vip.setDiscount(level.getDiscount());
        vip.setStoreId(level.getStoreId());
        return vip;
    }
}
