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

import com.ruoyi.bst.app.domain.AppVO;
import com.ruoyi.bst.app.service.AppService;
import com.ruoyi.bst.channel.domain.ChannelVO;
import com.ruoyi.bst.channel.service.ChannelService;
import com.ruoyi.bst.device.service.DeviceIotService;
import com.ruoyi.bst.device.service.DeviceService;

import com.ruoyi.bst.model.service.ModelService;
import com.ruoyi.bst.order.domain.Order;
import com.ruoyi.bst.order.domain.bo.OrderCreateBO;
import com.ruoyi.bst.order.domain.dto.*;
import com.ruoyi.bst.order.domain.enums.OrderStatus;
import com.ruoyi.bst.order.domain.vo.OrderPrePriceVO;
import com.ruoyi.bst.order.service.OrderConverter;
import com.ruoyi.bst.order.service.OrderService;
import com.ruoyi.bst.order.utils.OrderUtil;
import com.ruoyi.bst.store.domain.StoreVO;
import com.ruoyi.bst.store.service.StoreService;
import com.ruoyi.bst.suit.domain.SuitVO;
import com.ruoyi.bst.suit.service.SuitService;
import com.ruoyi.bst.userApp.domain.UserAppVO;
import com.ruoyi.bst.userApp.service.UserAppService;
import com.ruoyi.common.core.domain.vo.UserVO;

import com.ruoyi.system.user.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

@Service
public class OrderConverterImpl implements OrderConverter{

    @Autowired
    private SuitService suitService;

    @Autowired
    private DeviceService deviceService;

    @Autowired
    private UserService userService;


    @Autowired
    private ChannelService channelService;

    @Autowired
    private AppService appService;

    @Autowired
    private UserAppService userAppService;

    @Autowired
    private DeviceIotService deviceIotService;

    @Autowired
    private OrderService orderService;

    @Autowired
    private ModelService modelService;
    @Autowired
    private StoreService storeService;


    @Override
    public OrderCreateBO toCreateBO(OrderCreateDTO dto) {
        if (dto == null) {
            return null;
        }
        OrderCreateBO bo = new OrderCreateBO();
        bo.setDto(dto);

        // 套餐
        SuitVO suit = suitService.selectSuitById(dto.getSuitId());
        bo.setSuit(suit);

        // 用户
        UserVO user = userService.selectUserById(dto.getUserId());
        bo.setUser(user);

        // 查询渠道
        ChannelVO channel = channelService.selectChannelByChannelId(dto.getChannelId());
        bo.setChannel(channel);

        // 查询应用
        AppVO app = appService.selectAppById(dto.getAppId());
        bo.setApp(app);

        // 价格信息
        OrderPrePriceVO prePrice = new OrderPrePriceVO();
        prePrice.setNumber(suit.getLightingNums());
        prePrice.setPayAmount(suit.getRechargeAmount());
        bo.setPrePrice(prePrice);


        // 查询用户应用信息
        if (user != null && app != null) {
            UserAppVO userApp = userAppService.selectByUserApp(user.getUserId(), app.getId());
            bo.setUserApp(userApp);
        }

        // 生成订单
        Order order = this.generateOrder(bo);
        bo.setOrder(order);

        return bo;
    }


    // 生成订单
    private Order generateOrder(OrderCreateBO bo) {
        if (bo == null) {
            return null;
        }
        Order order = new Order();
        // 基础信息
        order.setPayExpireTime(LocalDateTime.now().plusMinutes(3));

        // 用户信息
        UserVO user = bo.getUser();
        if (user != null) {
            order.setUserId(user.getUserId());
        }

        // 套餐店铺信息
        SuitVO suit = bo.getSuit();
        if (suit != null) {
            StoreVO store = storeService.selectStoreById(suit.getStoreId());
            if (store != null) {
                order.setStoreName(store.getStoreName());
            }
            order.setStoreId(suit.getStoreId());
            order.setSuitId(suit.getSuitId());
            order.setSuitAmount(suit.getRechargeAmount());
            order.setSuitNum(suit.getLightingNums());
        }

        // 价格信息
        OrderPrePriceVO prePrice = bo.getPrePrice();
        if (prePrice != null) {
           order.setPayAmount(prePrice.getPayAmount());
        }

        // 订单状态
        order.setStatus(OrderStatus.WAIT_PAY.getCode());

        return order;
    }

}
