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

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

import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.ss.commandLog.domain.enums.CommandLogType;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.ss.commandLog.mapper.CommandLogMapper;
import com.ruoyi.ss.commandLog.domain.CommandLog;
import com.ruoyi.ss.commandLog.domain.CommandLogVO;
import com.ruoyi.ss.commandLog.domain.CommandLogQuery;
import com.ruoyi.ss.commandLog.service.ICommandLogService;

/**
 * 命令日志Service业务层处理
 *
 * @author ruoyi
 * @date 2024-12-13
 */
@Service
@Slf4j
public class CommandLogServiceImpl implements ICommandLogService
{
    @Autowired
    private CommandLogMapper commandLogMapper;

    /**
     * 查询命令日志
     *
     * @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) {
        CommandLog po = new CommandLog();
        po.setType(CommandLogType.API.getType());
        po.setMac(mac);
        po.setCommand(command);
        po.setResult(result);
        po.setReason(reason);
        return this.addLog(po, loginUser);
    }

    @Override
    public int addBluetoothLog(CommandLog po, LoginUser loginUser) {
        po.setType(CommandLogType.BLUETOOTH.getType());
        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());
        }

        return this.insertCommandLog(po);
    }
}
