package com.fastbee.iot.service.impl;

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

import com.fastbee.common.utils.DateUtils;
import com.fastbee.framework.mybatis.LambdaQueryWrapperX;
import com.fastbee.iot.domain.Device;
import com.fastbee.iot.mapper.DeviceMapper;
import com.fastbee.iot.model.HistoryModel;
import com.fastbee.iot.model.ThingsModelLogCountVO;
import com.fastbee.iot.model.param.DataCenterParam;
import com.fastbee.iot.service.IDeviceService;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.fastbee.iot.mapper.FunctionLogMapper;
import com.fastbee.iot.domain.FunctionLog;
import com.fastbee.iot.service.IFunctionLogService;

import javax.annotation.Resource;

/**
 * 设备服务下发日志Service业务层处理
 *
 * @author kerwincui
 * @date 2022-10-22
 */
@Service
public class FunctionLogServiceImpl implements IFunctionLogService
{
    @Resource
    private FunctionLogMapper functionLogMapper;

    @Resource
    private DeviceMapper deviceMapper;

    /**
     * 查询设备服务下发日志
     *
     * @param id 设备服务下发日志主键
     * @return 设备服务下发日志
     */
    @Override
    public FunctionLog selectFunctionLogById(Long id)
    {
        return functionLogMapper.selectFunctionLogById(id);
    }

    /**
     * 查询设备服务下发日志列表
     *
     * @param functionLog 设备服务下发日志
     * @return 设备服务下发日志
     */
    @Override
    public List<FunctionLog> selectFunctionLogList(FunctionLog functionLog)
    {
        List<FunctionLog> logList = functionLogMapper.selectFunctionLogList(functionLog);
        List<String> serialNumberList = logList.stream().map(FunctionLog::getSerialNumber).collect(Collectors.toList());
        LambdaQueryWrapperX<Device> queryWrapperX = new LambdaQueryWrapperX<>();
        queryWrapperX.inIfPresent(Device::getSerialNumber, serialNumberList);
        List<Device> deviceList = deviceMapper.selectList(queryWrapperX);
        if (CollectionUtils.isEmpty(deviceList)) {
            return logList;
        }
        Map<String, String> deviceMapName = deviceList.stream().collect(Collectors.toMap(Device::getSerialNumber, Device::getDeviceName, (o1, o2) -> o2));
        for (FunctionLog log : logList) {
            log.setDeviceName(deviceMapName.get(log.getSerialNumber()));
        }
        return logList;
    }

    /**
     * 新增设备服务下发日志
     *
     * @param functionLog 设备服务下发日志
     * @return 结果
     */
    @Override
    public int insertFunctionLog(FunctionLog functionLog)
    {
        functionLog.setCreateTime(DateUtils.getNowDate());
        return functionLogMapper.insertFunctionLog(functionLog);
    }

    /**
     * 批量插入数据
     * @param list
     */
    @Override
    public void insertBatch(List<FunctionLog> list){
         functionLogMapper.insertBatch(list);
    }

    /**
     * 修改设备服务下发日志
     *
     * @param functionLog 设备服务下发日志
     * @return 结果
     */
    @Override
    public int updateFunctionLog(FunctionLog functionLog)
    {
        return functionLogMapper.updateFunctionLog(functionLog);
    }

    /**
     * 批量删除设备服务下发日志
     *
     * @param ids 需要删除的设备服务下发日志主键
     * @return 结果
     */
    @Override
    public int deleteFunctionLogByIds(Long[] ids)
    {
        return functionLogMapper.deleteFunctionLogByIds(ids);
    }

    /**
     * 删除设备服务下发日志信息
     *
     * @param id 设备服务下发日志主键
     * @return 结果
     */
    @Override
    public int deleteFunctionLogById(Long id)
    {
        return functionLogMapper.deleteFunctionLogById(id);
    }

    /**
     * 根据设备编号删除设备服务下发日志信息
     *
     * @param serialNumber 设备编号
     * @return 结果
     */
    @Override
    public int deleteFunctionLogByDeviceNumber(String serialNumber)
    {
        return functionLogMapper.deleteFunctionLogBySerialNumber(serialNumber);
    }

    /**
     * 批量更新日志状态值
     * @param log 参数
     */
    @Override
    public void updateFuncLogBatch(FunctionLog log){
        functionLogMapper.updateFuncLogBatch(log);
    }

    /**
     * 根据消息id更新指令下发状态
     * @param log
     */
    @Override
    public void updateByMessageId(FunctionLog log){
        functionLogMapper.updateByMessageId(log);
    }

    @Override
    public List<HistoryModel> listHistory(FunctionLog functionLog) {
        return functionLogMapper.listHistory(functionLog);
    }

    @Override
    public List<ThingsModelLogCountVO> countThingsModelInvoke(DataCenterParam dataCenterParam) {
        return functionLogMapper.countThingsModelInvoke(dataCenterParam);
    }

    /**
     * 根据消息id获取指令日志
     * @param messageId
     * @return
     */
     @Override
     public FunctionLog selectLogByMessageId(String messageId){
         return  functionLogMapper.selectLogByMessageId(messageId);
     }
}
