package org.nl.system.service.logging.impl;

import cn.hutool.core.lang.Dict;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.nl.common.domain.query.PageQuery;
import org.nl.common.utils.ValidationUtil;
import org.nl.system.service.logging.ISysLogService;
import org.nl.system.service.logging.dao.SysLog;
import org.nl.system.service.logging.dao.mapper.SysLogMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * <p>
 * 系统日志 服务实现类
 * </p>
 *
 * @author generator
 * @since 2023-05-08
 */
@Slf4j
@Service
public class SysLogServiceImpl extends ServiceImpl<SysLogMapper, SysLog> implements ISysLogService {

    @Autowired
    private SysLogMapper logMapper;

    @Override
    public IPage<SysLog> queryAll(Map whereJson, PageQuery pageable) {
        String blurry = ObjectUtil.isNotEmpty(whereJson.get("blurry"))?whereJson.get("blurry").toString():null;
        String log_type = ObjectUtil.isNotEmpty(whereJson.get("log_type"))?whereJson.get("log_type").toString():null;
        String username = ObjectUtil.isNotEmpty(whereJson.get("username"))?whereJson.get("username").toString():null;
        String begin_time = ObjectUtil.isNotEmpty(whereJson.get("begin_time"))?whereJson.get("begin_time").toString():null;
        String end_time = ObjectUtil.isNotEmpty(whereJson.get("end_time"))?whereJson.get("end_time").toString():null;
        LambdaQueryWrapper<SysLog> lam = new LambdaQueryWrapper<>();
        lam.eq(ObjectUtil.isNotEmpty(log_type), SysLog::getLog_type, log_type)
                .eq(ObjectUtil.isNotEmpty(username), SysLog::getUsername, username)
                .like(ObjectUtil.isNotEmpty(blurry), SysLog::getDescription, blurry)
                .le(ObjectUtil.isNotEmpty(end_time), SysLog::getCreate_time, end_time)
                .ge(ObjectUtil.isNotEmpty(begin_time), SysLog::getCreate_time, begin_time)
                .orderByDesc(SysLog::getCreate_time);
        IPage<SysLog> page = new Page<>(pageable.getPage()+1, pageable.getSize());
        logMapper.selectPage(page, lam);
        return page;
    }

    @Override
    public Object findByErrDetail(String id) {
        SysLog sysLog = logMapper.selectById(id);
        if (ObjectUtil.isEmpty(sysLog)) {
            sysLog = new SysLog();
        }
        ValidationUtil.isNull(sysLog.getLog_id(), "SysLog", "log_id", id);
        byte[] details = sysLog.getException_detail();
        return Dict.create().set("exception", new String(ObjectUtil.isNotNull(details) ? details : "".getBytes()));
    }

    @Override
    public void delAllByError() {
        logMapper.delete(new LambdaQueryWrapper<SysLog>().eq(SysLog::getLog_type, "ERROR"));
    }

    @Override
    public void delAllByInfo() {
        logMapper.delete(new LambdaQueryWrapper<SysLog>().eq(SysLog::getLog_type, "INFO"));
    }
}
