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

import java.util.List;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.google.common.base.Objects;
import com.ruoyi.bst.device.service.DeviceIotService;
import com.ruoyi.bst.order.domain.OrderVO;
import com.ruoyi.bst.order.service.OrderAssembler;
import com.ruoyi.bst.orderDevice.domain.OrderDeviceQuery;
import com.ruoyi.bst.orderDevice.domain.OrderDeviceVO;
import com.ruoyi.bst.orderDevice.service.OrderDeviceService;
import com.ruoyi.common.utils.collection.CollectionUtils;

@Service
public class OrderAssemblerImpl implements OrderAssembler {
    
    @Autowired
    private OrderDeviceService orderDeviceService;

    @Autowired
    private DeviceIotService deviceIotService;

    @Override
    public void assembleOrderDeviceList(List<OrderVO> list) {
        if (CollectionUtils.isEmptyElement(list)) {
            return;
        }

        List<Long> orderIds = CollectionUtils.map(list, OrderVO::getId);
        OrderDeviceQuery query = new OrderDeviceQuery();
        query.setOrderIds(orderIds);
        List<OrderDeviceVO> orderDeviceList = orderDeviceService.selectOrderDeviceList(query);

        for (OrderVO order : list) {
            List<OrderDeviceVO> result = orderDeviceList.stream()
                .filter(item -> Objects.equal(item.getOrderId(), order.getId()))
                .collect(Collectors.toList());
            order.setOrderDeviceList(result);
        }
    }

    @Override
    public void assembleIot(List<OrderVO> list) {
        // TODO 暂时啥也不做，只是留个口子
        // if (CollectionUtils.isEmptyElement(list)) {
        //     return;
        // }

        // for (OrderVO vo : list) {
        //     Device device = new Device();
        //     device.setMac(vo.getDeviceMac());
        //     Map<String, Object> info = deviceIotService.getIotInfo(device.getMac());
        //     if (info == null) {
        //         continue;
        //     }
        //     DeviceUtil.setIotInfo(device, info);
        // }

    }

}
