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

import com.ruoyi.common.constant.ServiceConstants;
import com.ruoyi.common.core.domain.entity.User;
import com.ruoyi.ss.channel.domain.ChannelQuery;
import com.ruoyi.ss.channel.domain.ChannelVO;
import com.ruoyi.ss.channel.service.IChannelService;
import com.ruoyi.ss.equipment.domain.EquipmentVO;
import com.ruoyi.ss.equipment.service.IEquipmentService;
import com.ruoyi.ss.order.domain.dto.ProductDTO;
import com.ruoyi.ss.order.domain.dto.TopUpOrderDTO;
import com.ruoyi.ss.product.domain.ProductVO;
import com.ruoyi.ss.product.service.IProductService;
import com.ruoyi.ss.user.domain.UserQuery;
import com.ruoyi.ss.user.domain.UserVO;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.ServiceUtil;
import com.ruoyi.ss.device.domain.vo.DeviceVO;
import com.ruoyi.ss.device.service.DeviceService;
import com.ruoyi.ss.order.domain.OrderVO;
import com.ruoyi.ss.order.domain.dto.ReOrderDTO;
import com.ruoyi.ss.order.service.IOrderService;
import com.ruoyi.ss.room.domain.RoomVO;
import com.ruoyi.ss.feeRule.domain.FeeRuleVO;
import com.ruoyi.ss.store.domain.StoreVO;
import com.ruoyi.ss.order.domain.OrderBO;
import com.ruoyi.ss.order.domain.dto.OrderDTO;
import com.ruoyi.ss.order.service.IOrderConverter;
import com.ruoyi.ss.feeRule.service.IFeeRuleService;
import com.ruoyi.ss.room.service.IRoomService;
import com.ruoyi.ss.store.service.IStoreService;
import com.ruoyi.ss.user.service.IUserService;
import org.jetbrains.annotations.NotNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.List;

import static com.ruoyi.common.constant.ServiceConstants.*;

@Service
public class OrderConverterImpl implements IOrderConverter {


    @Resource
    private IRoomService roomService;

    @Resource
    private IFeeRuleService feeRuleService;

    @Resource
    private IUserService userService;

    @Resource
    private IStoreService storeService;

    @Autowired
    private IOrderService orderService;

    @Autowired
    private DeviceService deviceService;

    @Autowired
    private IChannelService channelService;

    @Autowired
    private IProductService productService;

    @Autowired
    private IEquipmentService equipmentService;


    // 订单dto转bo
    @Override
    public OrderBO toOrderBO(OrderDTO order) {
        OrderBO orderBO = new OrderBO();
        StoreVO eStoreVO;
        RoomVO eRoomVO;
        if(ROOM_TYPE2_LOBBY_DEVICE.equals(order.getType2())){//下单的是大厅的设施
            eRoomVO = roomService.selectERoomByRoomId(order.getRoomId());
            ServiceUtil.assertion(eRoomVO == null, "该大厅设施（房间级别）【"+order.getRoomId()+"】不存在");
            List<EquipmentVO> equipmentVOS = equipmentService.selectEquipmentListByRoomId(order.getRoomId());
            ServiceUtil.assertion(equipmentVOS.isEmpty(), "该大厅设施（房设施级别）【"+order.getRoomId()+"】不存在");
            EquipmentVO equipmentVO = equipmentVOS.get(0);
            ServiceUtil.assertion(equipmentVO.getDeviceId() == null, "大厅设施未绑定设备");
            DeviceVO device = deviceService.selectById(equipmentVO.getDeviceId());
            // 店铺
            eStoreVO = storeService.selectEStoreByStoreId(equipmentVO.getStoreId());
            orderBO.setRoom(eRoomVO);
            orderBO.setDevice(device);
            orderBO.setEquipment(equipmentVO);
        }else{
            // 房间
            eRoomVO = roomService.selectERoomByRoomId(order.getRoomId());
            ServiceUtil.assertion(eRoomVO == null, "房间不存在");

            // 房间门
            // 更新房间门sn
            EquipmentVO roomDoor = equipmentService.selectDoorByRoomId(eRoomVO.getRoomId(), FACILITY_TYPE_ROOM_LIGHT);
            ServiceUtil.assertion(roomDoor == null, "房间灯设施不存在");
            DeviceVO roomDoorDevice = deviceService.selectById(roomDoor.getDeviceId());
            ServiceUtil.assertion(roomDoorDevice == null, "房间灯未绑定设备");

            // 店铺
            eStoreVO = storeService.selectEStoreByStoreId(eRoomVO.getStoreId());
            orderBO.setRoom(eRoomVO);
            orderBO.setDevice(roomDoorDevice);
        }

        ServiceUtil.assertion(eStoreVO == null, "店铺不存在");
        DeviceVO gate= null;
        // 大门设施
        if(eStoreVO.getGateId() != null){
            gate = deviceService.selectById(eStoreVO.getGateId());
        }
//        ServiceUtil.assertion(eStoreVO.getGateId() == null, "大门未绑定设备");

//        ServiceUtil.assertion(gate == null, "店铺大门设备不存在");

        // 商户
        UserVO merchantVO =  userService.selectUserById(eStoreVO.getMerchantId());
        ServiceUtil.assertion(merchantVO == null, "商户不存在");

        ServiceUtil.assertion(merchantVO.getServiceFeeProportion() == null, "商户【"+merchantVO.getUserName()+"】的平台服务费未配置");

        // 支付渠道  先查询商户的支付渠道，如果未配置，则查询系统默认的支付渠道
        List<ChannelVO> channelVO = channelService.selectByIds(merchantVO.getChannelIds());
        if(channelVO.isEmpty()){
            ChannelQuery channelQuery = new ChannelQuery();
            channelQuery.setEnabled(true);
            channelQuery.setType(ServiceConstants.PAY_CHANNEL_PLATFORM);
            channelVO = channelService.selectSmChannelList(channelQuery);
        }
        ServiceUtil.assertion(channelVO.isEmpty(), "支付渠道不存在");

        // 套餐
        FeeRuleVO eFeeRuleVO = feeRuleService.selectEFeeRuleByRuleId(order.getRuleId());
        ServiceUtil.assertion(eFeeRuleVO == null, "套餐不存在");

        // 用户
        UserVO eUserVO = userService.selectUserById(SecurityUtils.getUserId());
        ServiceUtil.assertion(eUserVO == null, "用户不存在");
        ServiceUtil.assertion(eUserVO.getWxopenid() == null, "用户微信openId为空");

        orderBO.setParams(order);
        orderBO.setFeeRule(eFeeRuleVO);
        orderBO.setMerchant(merchantVO);
        orderBO.setChannel(channelVO.get(0));
        orderBO.setStore(eStoreVO);
        orderBO.setGate(gate);
        orderBO.setUser(eUserVO);
        orderBO.setType(eRoomVO.getType());
        orderBO.setReType(ServiceConstants.ORDER_TYPE_RESERVE);
        orderBO.setType2(order.getType2());
        return orderBO;
    }

    // 订单dto转bo
    @Override
    public OrderBO toOrderBO(ReOrderDTO order) {
        // 原订单
        OrderVO originalOrder = orderService.selectRlOrderByOrderNo(order.getOriginalOrderNo());
        ServiceUtil.assertion(originalOrder == null, "原订单不存在");

        // 房间
        RoomVO eRoomVO = roomService.selectERoomByRoomId(originalOrder.getRoomId());
        ServiceUtil.assertion(eRoomVO == null, "房间不存在");

        OrderBO orderBO = new OrderBO();

        // 房间门
        // 更新房间门sn
        DeviceVO roomDoorDevice = null;
        if(eRoomVO.getType2().equals(ROOM_TYPE2_PRIVATE_ROOM)){
            EquipmentVO roomDoor = equipmentService.selectDoorByRoomId(eRoomVO.getRoomId(), FACILITY_TYPE_ROOM_LIGHT);
            ServiceUtil.assertion(roomDoor == null, "房间门设施不存在");
            roomDoorDevice = deviceService.selectById(roomDoor.getDeviceId());
            ServiceUtil.assertion(roomDoorDevice == null, "房间灯设备不存在");
        }else{
            List<EquipmentVO> equipmentVOS = equipmentService.selectEquipmentListByRoomId(eRoomVO.getRoomId());
            ServiceUtil.assertion(equipmentVOS.isEmpty(), "该大厅设施（房设施级别）【"+eRoomVO.getRoomId()+"】不存在");
            EquipmentVO equipmentVO = equipmentVOS.get(0);
            ServiceUtil.assertion(equipmentVO.getDeviceId() == null, "大厅设施未绑定设备");
            roomDoorDevice = deviceService.selectById(equipmentVO.getDeviceId());
            orderBO.setEquipment(equipmentVO);
        }

        // 店铺
        StoreVO eStoreVO = storeService.selectEStoreByStoreId(eRoomVO.getStoreId());
        ServiceUtil.assertion(eStoreVO == null, "店铺不存在");

        // 大门设施
        DeviceVO gate= null;
        if(eStoreVO.getGateId() != null){
            gate = deviceService.selectById(eStoreVO.getGateId());
        }
//        ServiceUtil.assertion(eStoreVO.getGateId() == null, "大门未绑定设备");
//        DeviceVO gate = deviceService.selectById(eStoreVO.getGateId());
//        ServiceUtil.assertion(gate == null, "店铺大门设备不存在");

        // 商户
        UserVO merchantVO =  userService.selectUserById(eStoreVO.getMerchantId());
        ServiceUtil.assertion(merchantVO == null, "商户不存在");

        ServiceUtil.assertion(merchantVO.getServiceFeeProportion() == null, "商户【"+merchantVO.getUserName()+"】的平台服务费未配置");

        // 支付渠道  先查询商户的支付渠道，如果未配置，则查询系统默认的支付渠道
        List<ChannelVO> channelVO = channelService.selectByIds(merchantVO.getChannelIds());
        if(channelVO.isEmpty()){
            ChannelQuery channelQuery = new ChannelQuery();
            channelQuery.setEnabled(true);
            channelVO = channelService.selectSmChannelList(channelQuery);
        }
        ServiceUtil.assertion(channelVO.isEmpty(), "支付渠道不存在");

        // 套餐
        FeeRuleVO eFeeRuleVO = feeRuleService.selectEFeeRuleByRuleId(order.getRuleId());
        ServiceUtil.assertion(eFeeRuleVO == null, "套餐不存在");

        // 用户
        UserVO eUserVO = userService.selectUserById(SecurityUtils.getUserId());
        ServiceUtil.assertion(eUserVO == null, "用户不存在");
        ServiceUtil.assertion(eUserVO.getWxopenid() == null, "用户微信openId为空");

        orderBO.setReOrderDTO(order);
        orderBO.setRoom(eRoomVO);
        orderBO.setDevice(roomDoorDevice);
        orderBO.setFeeRule(eFeeRuleVO);
        orderBO.setMerchant(merchantVO);
        orderBO.setStore(eStoreVO);
        orderBO.setGate(gate);
        orderBO.setUser(eUserVO);
        orderBO.setChannel(channelVO.get(0));
        orderBO.setOriginalOrder(originalOrder);
        orderBO.setType(eRoomVO.getType());
        orderBO.setReType(ServiceConstants.ORDER_TYPE_REORDER);
        orderBO.setType2(originalOrder.getRoomType2());
        return orderBO;
    }

//    @Override
//    public OrderBO toOrderBO(TopUpOrderDTO topUpOrder) {
//
//        // 店铺
//        StoreVO eStoreVO = storeService.selectEStoreByStoreId(topUpOrder.getStoreId());
//        ServiceUtil.assertion(eStoreVO == null, "店铺不存在");
//
//        // 商户
//        UserVO merchantVO =  userService.selectUserById(eStoreVO.getManagerId());
//        ServiceUtil.assertion(merchantVO == null, "商户不存在");
//
//        ServiceUtil.assertion(merchantVO.getServiceFeeProportion() == null, "商户【"+merchantVO.getUserName()+"】的平台服务费未配置");
//        ServiceUtil.assertion(merchantVO.getPayChannel() == null, "商户【"+merchantVO.getUserName()+"】的支付渠道未配置");
//
//        // 支付渠道
//        ChannelVO channelVO = channelService.selectChannelByChannelId(merchantVO.getPayChannel());
//        ServiceUtil.assertion(channelVO == null, "支付渠道不存在");
//
//        // 用户
//        UserVO eUserVO = userService.selectUserById(SecurityUtils.getUserId());
//        ServiceUtil.assertion(eUserVO == null, "用户不存在");
//        ServiceUtil.assertion(eUserVO.getWxopenid() == null, "用户微信openId为空");
//
//        OrderBO orderBO = new OrderBO();
//        orderBO.setMerchant(merchantVO);
//        orderBO.setChannel(channelVO);
//        orderBO.setStore(eStoreVO);
//        orderBO.setTopUpOrder(topUpOrder);
//        orderBO.setUser(eUserVO);
//        orderBO.setType(ServiceConstants.ORDER_TYPE_RECHARGE);
//        return orderBO;
//    }

    @Override
    public OrderBO toOrderBO(ProductDTO product) {

        // 房间
        RoomVO eRoomVO = roomService.selectERoomByRoomId(product.getRoomId());
        ServiceUtil.assertion(eRoomVO == null, "房间不存在");

        // 店铺
        StoreVO eStoreVO = storeService.selectEStoreByStoreId(eRoomVO.getStoreId());
        ServiceUtil.assertion(eStoreVO == null, "店铺不存在");

        // 商户
        UserVO merchantVO =  userService.selectUserById(eStoreVO.getManagerId());
        ServiceUtil.assertion(merchantVO == null, "商户不存在");

        ServiceUtil.assertion(merchantVO.getServiceFeeProportion() == null, "商户【"+merchantVO.getUserName()+"】的平台服务费未配置");

        // 支付渠道  先查询商户的支付渠道，如果未配置，则查询系统默认的支付渠道
        List<ChannelVO> channelVO = channelService.selectByIds(merchantVO.getChannelIds());
        if(channelVO.isEmpty()){
            ChannelQuery channelQuery = new ChannelQuery();
            channelQuery.setEnabled(true);
            channelVO = channelService.selectSmChannelList(channelQuery);
        }
        ServiceUtil.assertion(channelVO.isEmpty(), "支付渠道不存在");

        // 用户
        UserVO eUserVO = userService.selectUserById(SecurityUtils.getUserId());
        ServiceUtil.assertion(eUserVO == null, "用户不存在");
        ServiceUtil.assertion(eUserVO.getWxopenid() == null, "用户微信openId为空");

        // 商品
        ProductVO eProductVO =productService.selectProductByProductId(product.getProductId());
        ServiceUtil.assertion(eProductVO == null, "商品不存在");
        ServiceUtil.assertion(eProductVO.getDeleted(), "商品已删除");
        ServiceUtil.assertion(eProductVO.getStock()<1, "库存不足");

        OrderBO orderBO = new OrderBO();
        orderBO.setProductDTO(product);
        orderBO.setProduct(eProductVO);
        orderBO.setRoom(eRoomVO);
        orderBO.setMerchant(merchantVO);
        orderBO.setChannel(channelVO.get(0));
        orderBO.setStore(eStoreVO);
        orderBO.setUser(eUserVO);
        orderBO.setType(ServiceConstants.ORDER_TYPE_CONTAINER);
        return orderBO;
    }

}
