package com.ruoyi.bst.vipUser.service.impl;

import java.time.LocalDateTime;

import org.springframework.stereotype.Service;

import com.ruoyi.bst.vipOrder.domain.VipOrder;
import com.ruoyi.bst.vipUser.domain.VipUser;
import com.ruoyi.bst.vipUser.service.VipUserConverter;
import com.ruoyi.bst.vipUser.utils.VipUserUtil;

@Service
public class VipUserConverterImpl implements VipUserConverter {

    @Override
    public VipUser toPo(VipOrder order) {
        if (order == null) {
            return null;
        }

        VipUser po = new VipUser();
        // 设置基本信息
        po.setUserId(order.getUserId());
        po.setVipId(order.getVipId());
        po.setAreaId(order.getVipAreaId());
        po.setType(order.getVipType());
        po.setDiscount(order.getVipDiscountValue());
        po.setName(order.getVipName());

        // 设置有效期
        LocalDateTime now = LocalDateTime.now();
        po.setStartTime(now);
        po.setEndTime(now.plusDays(order.getVipValidDays()));

        // 设置使用限制
        po.setEnableLimit(order.getVipEnableLimit());
        po.setSurplusTotal(order.getVipLimitTotal());
        po.setLimitCount(order.getVipLimitCount());
        po.setLimitRound(order.getVipLimitRound());
        po.setLimitUnit(order.getVipLimitUnit());
        po.setMinAmount(order.getVipMinAmount());

        // 设置计数相关
        po.setRoundCount(0);
        if(order.getVipEnableLimit()) {
            po.setNextResetTime(VipUserUtil.getNextResetTime(po));
        }
        return po;
    }
}
