package com.ruoyi.system.operLog.service.impl;

import java.math.BigDecimal;
import java.util.Collections;
import java.util.List;

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

import com.ruoyi.bst.device.domain.DeviceVO;
import com.ruoyi.common.constant.CacheConstants;
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.collection.CollectionUtils;
import com.ruoyi.system.operLog.domain.OperLog;
import com.ruoyi.system.operLog.domain.OperLogQuery;
import com.ruoyi.system.operLog.domain.OperLogVO;
import com.ruoyi.system.operLog.domain.enums.OperLogIotStatus;
import com.ruoyi.system.operLog.domain.enums.OperLogStatus;
import com.ruoyi.system.operLog.mapper.OperLogMapper;
import com.ruoyi.system.operLog.service.OperLogService;

/**
 * 操作日志 服务层处理
 *
 * @author ruoyi
 */
@Service
public class OperLogServiceImpl implements OperLogService
{
    @Autowired
    private OperLogMapper operLogMapper;

    @Autowired
    private RedisCache redisCache;

    /**
     * 新增操作日志
     *
     * @param operLog 操作日志对象
     */
    @Override
    public void insertOperlog(OperLog operLog)
    {
        operLogMapper.insertOperlog(operLog);
    }

    /**
     * 查询系统操作日志集合
     *
     * @param query 操作日志对象
     * @return 操作日志集合
     */
    @Override
    public List<OperLogVO> selectOperLogList(OperLogQuery query)
    {
        return operLogMapper.selectOperLogList(query);
    }

    /**
     * 批量删除系统操作日志
     *
     * @param operIds 需要删除的操作日志ID
     * @return 结果
     */
    @Override
    public int deleteOperLogByIds(Long[] operIds)
    {
        return operLogMapper.deleteOperLogByIds(operIds);
    }

    /**
     * 查询操作日志详细
     *
     * @param operId 操作ID
     * @return 操作日志对象
     */
    @Override
    public OperLogVO selectOperLogById(Long operId)
    {
        return operLogMapper.selectOperLogById(operId);
    }

    /**
     * 清空操作日志
     */
    @Override
    public void cleanOperLog()
    {
        operLogMapper.cleanOperLog();
    }

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

    @Override
    public void operSysLog(String title, boolean success, LogBizType bizType, Long bizId, DeviceVO device) {
        Long deviceId = null;
        BigDecimal longitude = null;
        BigDecimal latitude = null;
        if (device != null) {
            deviceId = device.getId();
            if (device.getLongitude() != null) {
                longitude = device.getLongitude();
            }
            if (device.getLatitude() != null) {
                latitude = device.getLatitude();
            }
        }
        operSysLog(title, success, bizType, bizId, deviceId, longitude, latitude);
    }

    @Override
    public void operSysLog(String title, boolean success, LogBizType bizType, Long bizId, Long deviceId, BigDecimal longitude, BigDecimal latitude) {
        OperLog operLog = new OperLog();
        operLog.setTitle(title);
        operLog.setBusinessType(BusinessType.OTHER.name());
        operLog.setBizType(bizType.getType());
        operLog.setBizIds(Collections.singletonList(bizId.toString()));
        operLog.setOperName("系统");
        operLog.setOperTime(DateUtils.getNowDate());
        operLog.setStatus(success ? OperLogStatus.SUCCESS.getCode() : OperLogStatus.FAIL.getCode());
        operLog.setIotStatus(success ? OperLogIotStatus.SUCCESS.getCode() : OperLogIotStatus.FAIL.getCode());
        operLog.setDeviceId(deviceId);
        if (longitude != null) {
            operLog.setDeviceLon(longitude.toString());
        }
        if (latitude != null) {
            operLog.setDeviceLat(latitude.toString());
        }
        tempInsert(operLog);
    }

    @Override
    public void tempInsert(OperLog operLog) {
        if (operLog == null) {
            return;
        }

        // 保存到Redis缓存
        redisCache.rightPush(CacheConstants.OPER_LOG_QUEUE, operLog);
    }
}
