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

import com.ruoyi.bst.commandLog.domain.CommandLog;
import com.ruoyi.bst.commandLog.domain.CommandLogQuery;
import com.ruoyi.bst.commandLog.domain.CommandLogVO;
import com.ruoyi.bst.commandLog.domain.enums.CommandLogType;
import com.ruoyi.bst.commandLog.mapper.CommandLogMapper;
import com.ruoyi.bst.commandLog.service.CommandLogService;
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.StringUtils;
import com.ruoyi.common.utils.collection.CollectionUtils;
import com.ruoyi.iot.interfaces.IotDevice;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

/**
 * 命令日志Service业务层处理
 *
 * @author ruoyi
 * @date 2025-04-03
 */
@Service
public class CommandLogServiceImpl implements CommandLogService
{
    @Autowired
    private CommandLogMapper commandLogMapper;

    @Autowired
    private RedisCache redisCache;

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

    /**
     * 查询命令日志列表
     *
     * @param commandLog 命令日志
     * @return 命令日志
     */
    @Override
    public List<CommandLogVO> selectCommandLogList(CommandLogQuery commandLog)
    {
        return commandLogMapper.selectCommandLogList(commandLog);
    }

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

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

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

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


    @Override
    public int addApiLog(String mac, String command, String result, String reason, LoginUser loginUser, Integer iotCode) {
        CommandLog po = new CommandLog();
        po.setType(CommandLogType.API.getType());
        po.setMac(mac);
        po.setCommand(command);
        po.setResult(result);
        po.setReason(reason);
        po.setIotCode(iotCode);
        return this.addLog(po, loginUser);
    }

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

    @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);
    }

    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.getNickName());
            po.setUserId(loginUser.getUserId());
        }

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

        return 1;
    }
}
