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

import java.math.BigDecimal;
import java.time.LocalDateTime;

import com.ruoyi.ss.app.service.AppService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.ss.channel.domain.ChannelVO;
import com.ruoyi.ss.channel.service.ChannelService;
import com.ruoyi.ss.store.domain.StoreVo;
import com.ruoyi.ss.store.service.StoreService;
import com.ruoyi.ss.user.domain.SmUserVO;
import com.ruoyi.ss.user.service.UserService;
import com.ruoyi.ss.vipLevel.domain.VipLevelVO;
import com.ruoyi.ss.vipLevel.domain.enums.VipLevelLimitType;
import com.ruoyi.ss.vipLevel.service.VipLevelService;
import com.ruoyi.ss.vipOrder.domain.VipOrder;
import com.ruoyi.ss.vipOrder.domain.VipOrderQuery;
import com.ruoyi.ss.vipOrder.domain.bo.AddPayVipOrderBO;
import com.ruoyi.ss.vipOrder.domain.dto.AddPayVipOrderDTO;
import com.ruoyi.ss.vipOrder.domain.enums.VipOrderStatus;
import com.ruoyi.ss.vipOrder.service.VipOrderConverter;

/**
 * @author wjh
 * 2025/1/17
 */
@Service
public class VipOrderConverterImpl implements VipOrderConverter {

    @Autowired
    private ChannelService channelService;

    @Autowired
    private VipLevelService vipLevelService;

    @Autowired
    private UserService userService;

    @Autowired
    private StoreService storeService;

    @Autowired
    private AppService appService;

    @Override
    public AddPayVipOrderBO toBO(AddPayVipOrderDTO dto) {
        if (dto == null) {
            return null;
        }

        AddPayVipOrderBO bo = new AddPayVipOrderBO();
        bo.setDto(dto);
        // 支付渠道
        bo.setChannel(channelService.selectSmChannelByChannelId(dto.getChannelId()));

        // level
        VipLevelVO level = vipLevelService.selectVipLevelById(dto.getLevelId());
        bo.setLevel(level);
        if (level != null) {
            if (level.getMchId() != null) {
                // 商户
                bo.setMch(userService.selectSmUserByUserId(level.getMchId()));
            }
            if (level.getStoreId() != null) {
                // 店铺
                bo.setStore(storeService.selectSmStoreById(level.getStoreId()));
            }
        }

        // 下单用户
        bo.setUser(userService.selectSmUserByUserId(SecurityUtils.getUserId()));
        if (bo.getUser() != null) {
            bo.setApp(appService.selectAppById(bo.getUser().getAppId()));
        }

        return bo;
    }

    @Override
    public VipOrder toOrder(AddPayVipOrderBO bo) {
        if (bo == null) {
            return null;
        }

        VipOrder order = new VipOrderQuery();
        // 用户数据
        SmUserVO user = bo.getUser();
        if (user != null) {
            order.setUserId(user.getUserId());
            order.setUserName(user.getRealOrUserName());
        }
        // 商户数据
        SmUserVO mch = bo.getMch();
        if (mch != null) {
            order.setMchId(mch.getUserId());
            order.setMchName(mch.getRealOrUserName());
        }
        // 店铺信息
        StoreVo store = bo.getStore();
        if (store != null) {
            order.setStoreName(store.getName());
        }
        // 解决方案
        AddPayVipOrderDTO dto = bo.getDto();
        if (dto != null) {
            order.setLevelSolution(dto.getSolution());
        }
        // level数据
        VipLevelVO level = bo.getLevel();
        if (level != null) {
            order.setLevelId(level.getId());
            order.setLevelName(level.getName());
            order.setLevelTime(level.getTime());
            order.setLevelPrice(level.getPrice());
            order.setLevelLimitType(level.getLimitType());
            if (!VipLevelLimitType.NONE.getType().equals(level.getLimitType())) {
                order.setLevelLimitCount(level.getLimitCount());
            }
            order.setLevelLimitTotal(level.getLimitTotal());
            order.setLevelDiscount(level.getDiscount());
            order.setLevelStoreId(level.getStoreId());
        }
        // 渠道信息
        ChannelVO channel = bo.getChannel();
        if (channel != null) {
            order.setChannelId(channel.getChannelId());
            order.setChannelType(channel.getType());
            order.setChannelName(channel.getName());
        }
        // 基础数据
        BigDecimal amount = level == null || level.getPrice() == null ? BigDecimal.ZERO : level.getPrice();
        order.setAmount(amount);
        order.setExpireTime(LocalDateTime.now().plusMinutes(15));   // 15分钟内过期
        order.setStatus(VipOrderStatus.WAIT_PAY.getStatus());

        return order;
    }
}
