package com.hdkj.lt.core.log.service.impl;

import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hdkj.lt.base.constants.DictConstants;
import com.hdkj.lt.base.constants.ErrorLogConstant;
import com.hdkj.lt.base.enums.ErrorLevelEnum;
import com.hdkj.lt.base.enums.EventTypeEnum;
import com.hdkj.lt.core.log.dao.FhzgLogInfoMapper;
import com.hdkj.lt.core.log.model.dto.LogInfoDTO;
import com.hdkj.lt.core.log.model.param.ErrorLogParam;
import com.hdkj.lt.core.log.model.param.LogInfoVO;
import com.hdkj.lt.core.log.model.po.FhzgLogInfoEntity;
import com.hdkj.lt.core.log.service.FhzgLogInfoService;
import com.hdkj.lt.core.sys.service.ISysDictService;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.system.api.model.LoginUser;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;

/**
 * @Description:
 * @Author: LiuAini
 * @Date: 2025/7/18 10:21
 * @Version: v1.0
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class FhzgLogInfoServiceImpl extends ServiceImpl<FhzgLogInfoMapper, FhzgLogInfoEntity> implements FhzgLogInfoService {

    private final ISysDictService sysDictService;

    @Override
    public void saveLog(ErrorLogParam logParam) {
        FhzgLogInfoEntity logInfoEntity = new FhzgLogInfoEntity();
        BeanUtils.copyProperties(logParam, logInfoEntity);
        logInfoEntity.setId(IdUtil.getSnowflakeNextId());
        logInfoEntity.setErrorTime(Objects.nonNull(logParam.getErrorTime()) ? logParam.getErrorTime() : LocalDateTime.now());
        logInfoEntity.setErrorLevel(StringUtils.isNotEmpty(logParam.getErrorLevel()) ? logParam.getErrorLevel() : ErrorLogConstant.ErrorLevel.INFO);
        LoginUser loginUser = SecurityUtils.getLoginUser();
        if (Objects.nonNull(loginUser)) {
            logInfoEntity.setCreateTime(LocalDateTime.now());
            logInfoEntity.setCreator(loginUser.getUserid());
        }
        //有重复就不新增
        List<FhzgLogInfoEntity> list = this.lambdaQuery().eq(FhzgLogInfoEntity::getErrorTime, logInfoEntity.getErrorTime())
                .eq(FhzgLogInfoEntity::getEventType, logInfoEntity.getEventType())
                .eq(FhzgLogInfoEntity::getEventId, logInfoEntity.getEventId())
                .eq(FhzgLogInfoEntity::getStage, logInfoEntity.getStage())
                .eq(FhzgLogInfoEntity::getRelatedId, logInfoEntity.getRelatedId())
                .eq(FhzgLogInfoEntity::getRelatedCategory, logInfoEntity.getRelatedCategory())
                .eq(FhzgLogInfoEntity::getOperationStep, logInfoEntity.getOperationStep())
                .eq(FhzgLogInfoEntity::getErrorCategory, logInfoEntity.getErrorCategory())
                .eq(FhzgLogInfoEntity::getErrorLevel, logInfoEntity.getErrorLevel())
                .eq(FhzgLogInfoEntity::getDetails, logInfoEntity.getDetails()).list();
        if (CollectionUtils.size(list) == 0) {
            this.save(logInfoEntity);
        }
    }

    @Override
    public Page<LogInfoDTO> listPage(LogInfoVO logInfoVO) {
        Page<LogInfoDTO> page = new Page<>(Optional.of(logInfoVO.getCurrent()).orElse(1L), Optional.of(logInfoVO.getSize()).orElse(10L));
        Page<LogInfoDTO> logInfoDtoPage = this.getBaseMapper().selectLogInfoPage(page, logInfoVO);
        Map<String, Map<String, String>> dictVosByTypeNames = sysDictService.getCacheByTypeNames(DictConstants.SOURCE_SYSTEM, DictConstants.LOG_STAGE);
        Map<String, String> sourceSystemDict = dictVosByTypeNames.get(DictConstants.SOURCE_SYSTEM);
        Map<String, String> stageDict = dictVosByTypeNames.get(DictConstants.LOG_STAGE);
        logInfoDtoPage.getRecords().forEach(e -> {
            e.setEventType(EventTypeEnum.getEventTypeName(e.getEventType())).setSourceSystem(sourceSystemDict.get(e.getSourceSystem()))
                    .setStage(stageDict.get(e.getStage())).setErrorLevel(ErrorLevelEnum.getErrorLevelName(e.getErrorLevel()));
        });
        return logInfoDtoPage;
    }
}
