package com.dji.sample.manage.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.dji.sample.common.util.AddressUtils;
import com.dji.sample.component.mqtt.config.MqttPropertyConfiguration;
import com.dji.sample.manage.dao.ISysOperLogMapper;
import com.dji.sample.manage.log.event.OperLogEvent;
import com.dji.sample.manage.model.dto.UserDTO;
import com.dji.sample.manage.model.entity.SysOperLogEntity;
import com.dji.sample.manage.model.entity.UserEntity;
import com.dji.sample.manage.service.ISysOperLogService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.event.EventListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;


@Service
@Transactional
@Slf4j
public class SysOperLogServiceImpl implements ISysOperLogService {


    @Autowired
    private ISysOperLogMapper mapper;


    /**
     * 操作日志记录
     *
     * @param operLogEvent 操作日志事件
     */
    @Async
    @EventListener
    public void recordOper(OperLogEvent operLogEvent) {
        SysOperLogEntity operLog = eventConvertToEntity(operLogEvent);
        // 远程查询操作地点
        operLog.setOperLocation(AddressUtils.getRealAddressByIP(operLog.getOperIp()));
        insertOperlog(operLog);
    }



    /**
     * 新增操作日志
     *
     * @param operLog 操作日志对象
     */
    @Override
    public void insertOperlog(SysOperLogEntity operLog) {
        operLog.setOperTime(new Date());
        mapper.insert(operLog);
    }



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

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

    /**
     * 清空操作日志
     */
    @Override
    public void cleanOperLog() {
        mapper.delete(new LambdaQueryWrapper<>());
    }

    /**
     * Convert database entity objects into user data transfer object.
     * @param operLogEvent
     * @return
     */
    private SysOperLogEntity eventConvertToEntity(OperLogEvent operLogEvent) {
        if (operLogEvent == null) {
            return null;
        }
        return SysOperLogEntity.builder()
                .operId(operLogEvent.getOperId())
                .title(operLogEvent.getTitle())
                .businessType(operLogEvent.getBusinessType())
                .method(operLogEvent.getMethod())
                .requestMethod(operLogEvent.getRequestMethod())
                .operatorType(operLogEvent.getOperatorType())
                .operName(operLogEvent.getOperName())
                .operUrl(operLogEvent.getOperUrl())
                .operIp(operLogEvent.getOperIp())
                .operLocation(operLogEvent.getOperLocation())
                .operParam(operLogEvent.getOperParam())
                .jsonResult(operLogEvent.getJsonResult())
                .status(operLogEvent.getStatus())
                .errorMsg(operLogEvent.getErrorMsg())
                .operTime(operLogEvent.getOperTime())
                .costTime(operLogEvent.getCostTime())
                .build();
    }

}
