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

import cn.hutool.core.util.ObjectUtil;
import com.ruoyi.common.constant.CacheConstants;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.collection.CollectionUtils;
import com.ruoyi.ss.commandLog.domain.CommandLog;
import com.ruoyi.ss.commandLog.domain.enums.CommandLogType;
import com.ruoyi.ss.commandLog.mapper.CommandLogMapper;
import com.ruoyi.ss.commandLog.service.ICommandLogService;
import com.ruoyi.ss.device.service.DeviceService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

/**
 * 命令日志Service业务层处理
 *
 * @author 邱贞招
 * @date 2024-07-06
 */
@Service
@Slf4j
public class CommandLogServiceImpl implements ICommandLogService
{
    @Autowired
    private CommandLogMapper commandLogMapper;

    @Autowired
    private ScheduledExecutorService scheduledExecutorService;

    @Autowired
    private DeviceService deviceService;

    @Autowired
    private RedisCache redisCache;

    /**
     * 查询命令日志
     *
     * @param id 命令日志主键
     * @return 命令日志
     */
    @Override
    public CommandLog selectEtCommandLogById(Long id)
    {
        return commandLogMapper.selectEtCommandLogById(id);
    }

    /**
     * 查询命令日志列表
     *
     * @param eCommandLog 命令日志
     * @return 命令日志
     */
    @Override
    public List<CommandLog> selectEtCommandLogList(CommandLog eCommandLog)
    {
        return commandLogMapper.selectEtCommandLogList(eCommandLog);
    }

    /**
     * 新增命令日志
     *
     * @param eCommandLog 命令日志
     * @return 结果
     */
    @Override
    public int insertEtCommandLog(CommandLog eCommandLog)
    {
        return commandLogMapper.insertEtCommandLog(eCommandLog);
    }

    /**
     * 修改命令日志
     *
     * @param eCommandLog 命令日志
     * @return 结果
     */
    @Override
    public int updateEtCommandLog(CommandLog eCommandLog)
    {
        return commandLogMapper.updateEtCommandLog(eCommandLog);
    }

    /**
     * 批量删除命令日志
     *
     * @param ids 需要删除的命令日志主键
     * @return 结果
     */
    @Override
    public int deleteEtCommandLogByIds(Long[] ids)
    {
        return commandLogMapper.deleteEtCommandLogByIds(ids);
    }

    /**
     * 删除命令日志信息
     *
     * @param id 命令日志主键
     * @return 结果
     */
    @Override
    public int deleteEtCommandLogById(Long id)
    {
        return commandLogMapper.deleteEtCommandLogById(id);
    }

    /**
     * 异步保存日志
     */
    @Override
    public int asynchronousSaveLog(String command, String mac, String result, String reason, LoginUser user,Integer iotCode) {
        //异步保存发送命令日志
        CommandLog po = new CommandLog();
        po.setType(CommandLogType.API.getType());
        po.setMac(mac);
        po.setSn(deviceService.selectSnByMac(mac));
        po.setCommand(command);
        po.setResult(result);
        po.setReason(reason);
        po.setCode(iotCode);
        return this.addLog(po, user);
    }

    @Override
    public int addBluetoothLog(CommandLog po, LoginUser loginUser) {
        po.setType(CommandLogType.BLUETOOTH.getType());
        po.setSn(deviceService.selectSnByMac(po.getMac()));
        return this.addLog(po, loginUser);
    }

    private int addLog(CommandLog po, LoginUser loginUser) {
        if (po == null || StringUtils.isBlank(po.getMac())) {
            return 0;
        }
        po.setOperaTime(LocalDateTime.now());

        if (loginUser != null) {
            po.setUserId(loginUser.getUserId());
            po.setUserName(loginUser.getUsername());
            po.setUserId(loginUser.getUserId());
//            po.setUserType(loginUser.getLoginType().getType());
        }

        // 添加到缓存中
        redisCache.rightPush(CacheConstants.INSERT_COMMAND_LOG, po);

        return 1;
    }

    @Override
    public int batchInsert(List<CommandLog> list) {
        if (CollectionUtils.isEmptyElement(list)) {
            return 0;
        }
        return commandLogMapper.batchInsert(list);
    }

    @Override
    public int clearBeforeDateLater(LocalDate date) {
        if (date == null) {
            return 0;
        }
        return commandLogMapper.clearBeforeDateLater(date);
    }
}
