package com.fastbee.iot.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

import com.fastbee.common.core.domain.entity.SysDept;
import com.fastbee.common.enums.DeviceDistributeTypeEnum;
import com.fastbee.common.utils.DateUtils;
import com.fastbee.iot.convert.DeviceRecordConvert;
import com.fastbee.iot.domain.Device;
import com.fastbee.iot.domain.Product;
import com.fastbee.iot.mapper.DeviceMapper;
import com.fastbee.iot.mapper.ProductMapper;
import com.fastbee.iot.model.DeviceRecordVO;
import com.fastbee.system.mapper.SysDeptMapper;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.fastbee.iot.mapper.DeviceRecordMapper;
import com.fastbee.iot.domain.DeviceRecord;
import com.fastbee.iot.service.IDeviceRecordService;

import javax.annotation.Resource;

/**
 * 设备记录Service业务层处理
 *
 * @author zhangzhiyi
 * @date 2024-07-16
 */
@Service
public class DeviceRecordServiceImpl implements IDeviceRecordService
{
    @Resource
    private DeviceRecordMapper deviceRecordMapper;
    @Resource
    private SysDeptMapper sysDeptMapper;
    @Resource
    private ProductMapper productMapper;
    @Resource
    private DeviceMapper deviceMapper;

    /**
     * 查询设备记录
     *
     * @param id 设备记录主键
     * @return 设备记录
     */
    @Override
    public DeviceRecord selectDeviceRecordById(Long id)
    {
        return deviceRecordMapper.selectDeviceRecordById(id);
    }

    /**
     * 查询设备记录列表
     *
     * @param deviceRecord 设备记录
     * @return 设备记录
     */
    @Override
    public List<DeviceRecordVO> selectDeviceRecordList(DeviceRecord deviceRecord)
    {
        List<DeviceRecord> deviceRecordList = deviceRecordMapper.selectDeviceRecordList(deviceRecord);
        if (CollectionUtils.isEmpty(deviceRecordList)) {
            return new ArrayList<>();
        }
        // 查租户、产品、设备信息
        List<Long> deptIdList = new ArrayList<>();
        deptIdList.addAll(deviceRecordList.stream().map(DeviceRecord::getOperateDeptId).distinct().collect(Collectors.toList()));
        deptIdList.addAll(deviceRecordList.stream().map(DeviceRecord::getTargetDeptId).distinct().collect(Collectors.toList()));
        Map<Long, SysDept> deptMap = new HashMap<>(2);
        if (CollectionUtils.isNotEmpty(deptIdList)) {
            List<SysDept> sysDeptList = sysDeptMapper.selectDeptByIds(deptIdList);
            deptMap = sysDeptList.stream().collect(Collectors.toMap(SysDept::getDeptId, Function.identity(), (o, n) -> n));
        }
        Map<Long, Product> productMap = new HashMap<>(2);
        List<Long> productIdList = deviceRecordList.stream().map(DeviceRecord::getProductId).distinct().collect(Collectors.toList());
        if (CollectionUtils.isNotEmpty(productIdList)) {
            List<Product> productList = productMapper.selectProductListByProductIds(productIdList);
            productMap = productList.stream().collect(Collectors.toMap(Product::getProductId, Function.identity(), (o, n) -> n));
        }
        Map<Long, Device> deviceMap = new HashMap<>(2);
        List<Long> deviceIdList = deviceRecordList.stream().map(DeviceRecord::getDeviceId).distinct().collect(Collectors.toList());
        if (CollectionUtils.isNotEmpty(deviceIdList)) {
            List<Device> deviceList = deviceMapper.selectDeviceListByDeviceIds(deviceIdList);
            deviceMap = deviceList.stream().collect(Collectors.toMap(Device::getDeviceId, Function.identity(), (o, n) -> n));
        }
        List<DeviceRecordVO> deviceRecordVOList = new ArrayList<>();
        for (DeviceRecord record : deviceRecordList) {
            DeviceRecordVO deviceRecordVO = DeviceRecordConvert.INSTANCE.convertDeviceRecordVO(record);
            SysDept operateDept = deptMap.get(deviceRecordVO.getOperateDeptId());
            if (null != operateDept) {
                deviceRecordVO.setOperateDeptName(operateDept.getDeptName());
            }
            SysDept targetDept = deptMap.get(deviceRecordVO.getTargetDeptId());
            if (null != targetDept) {
                deviceRecordVO.setTargetDeptName(targetDept.getDeptName());
            }
            Product product = productMap.get(deviceRecordVO.getProductId());
            if (null != product) {
                deviceRecordVO.setProductName(product.getProductName());
            }
            Device device = deviceMap.get(deviceRecordVO.getDeviceId());
            if (null != device) {
                deviceRecordVO.setDeviceName(device.getDeviceName());
            }
            if (null != deviceRecordVO.getDistributeType()) {
                deviceRecordVO.setDistributeTypeDesc(DeviceDistributeTypeEnum.getDesc(deviceRecordVO.getDistributeType()));
            }
            deviceRecordVOList.add(deviceRecordVO);
        }
        return deviceRecordVOList;
    }

    /**
     * 新增设备记录
     *
     * @param deviceRecord 设备记录
     * @return 结果
     */
    @Override
    public int insertDeviceRecord(DeviceRecord deviceRecord)
    {
        deviceRecord.setCreateTime(DateUtils.getNowDate());
        return deviceRecordMapper.insertDeviceRecord(deviceRecord);
    }

    /**
     * 修改设备记录
     *
     * @param deviceRecord 设备记录
     * @return 结果
     */
    @Override
    public int updateDeviceRecord(DeviceRecord deviceRecord)
    {
        deviceRecord.setUpdateTime(DateUtils.getNowDate());
        return deviceRecordMapper.updateDeviceRecord(deviceRecord);
    }

    /**
     * 批量删除设备记录
     *
     * @param ids 需要删除的设备记录主键
     * @return 结果
     */
    @Override
    public int deleteDeviceRecordByIds(Long[] ids)
    {
        return deviceRecordMapper.deleteDeviceRecordByIds(ids);
    }

    /**
     * 删除设备记录信息
     *
     * @param id 设备记录主键
     * @return 结果
     */
    @Override
    public int deleteDeviceRecordById(Long id)
    {
        return deviceRecordMapper.deleteDeviceRecordById(id);
    }
}
