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

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

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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.bst.device.service.DeviceService;
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.enums.BusinessType;
import com.ruoyi.common.enums.LogBizType;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.collection.CollectionUtils;
import com.ruoyi.system.operLog.domain.OperLog;
import com.ruoyi.system.operLog.domain.enums.OperLogIotStatus;
import com.ruoyi.system.operLog.domain.enums.OperLogStatus;
import com.ruoyi.system.operLog.service.OperLogService;

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

    @Autowired
    private RedisCache redisCache;

    @Autowired
    private DeviceService deviceService;

    @Autowired
    private OperLogService operLogService;

    /**
     * 查询命令日志
     *
     * @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 addBluetoothLog(CommandLog po, LoginUser loginUser) {
        po.setType(CommandLogType.BLUETOOTH.getType());
        int rows = this.addLog(po, loginUser);
        if (rows > 0) {
            // TODO 记录定位日志

            

            // 记录操作日志
            OperLog operLog = new OperLog();
            operLog.setTitle(po.getReason() + "（蓝牙）");
            operLog.setBusinessType(BusinessType.OTHER.name());
            operLog.setOperUserId(po.getUserId());
            operLog.setOperName(po.getUserName());
            operLog.setOperTime(DateUtils.getNowDate());
            operLog.setStatus(OperLogStatus.SUCCESS.getCode());
            operLog.setIotStatus(OperLogIotStatus.SUCCESS.getCode());

            if (po.getOrderId() != null) {
                operLog.setBizType(LogBizType.ORDER.getType());
                operLog.setBizIds(Collections.singletonList(po.getOrderId().toString()));
            } else {
                operLog.setBizType(LogBizType.DEVICE.getType());
            }
            
            if (po.getLongitude() != null) {
                operLog.setParamLon(po.getLongitude().toString());
            }
            if (po.getLatitude() != null) {
                operLog.setParamLat(po.getLatitude().toString());
            }

            operLogService.tempInsert(operLog);
       }
        return rows;
    }

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

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

        // 查询订单ID
        if (po.getMac() != null) {
            Long orderId = deviceService.selectOrderIdByMac(po.getMac());
            po.setOrderId(orderId);
        }

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

        return 1;
    }
}
