package com.ssyf.module.logger.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ssyf.module.logger.domain.entity.LogInfo;
import com.ssyf.module.logger.domain.query.LogInfoQuery;
import com.ssyf.module.logger.service.LogInfoService;
import com.ssyf.module.logger.mapper.LogInfoMapper;
import com.ssyf.plugs.mysql.helper.MybatisPlusHelper;
import org.springframework.stereotype.Service;

import java.util.List;

/**
* @author ELSA
* @description 针对表【log_info(日志信息表)】的数据库操作Service实现
* @createDate 2025-03-22 16:09:09
*/
@Service
public class LogInfoServiceImpl extends ServiceImpl<LogInfoMapper, LogInfo>
    implements LogInfoService{

    @Override
    public Object getOneById(Long id) {
        return this.getOne(new LambdaQueryWrapper<LogInfo>().eq(LogInfo::getId, id));
    }

    @Override
    public void record(com.ssyf.plugs.logger.LogInfo logInfo) {
        LogInfo newLogInfo = BeanUtil.toBean(logInfo, LogInfo.class);
        this.save(newLogInfo);
    }

    @Override
    public PageInfo<LogInfo> getPage(LogInfoQuery query) {
        PageHelper.startPage(query.getPageNum(), query.getPageSize());
        List<LogInfo> list = this.getList(query);
        return new PageInfo<>(list);
    }

    @Override
    public List<LogInfo> getList(LogInfoQuery query) {
        return this.list(createBaseWrapper(query));
    }
    
    private LambdaQueryWrapper<LogInfo> createBaseWrapper(LogInfoQuery query) {
        LambdaQueryWrapper<LogInfo> wrapper = new LambdaQueryWrapper<>();
        if(query == null){return  wrapper;}
        //标题
        wrapper.like(StrUtil.isNotBlank(query.getTitle()), LogInfo::getTitle, query.getTitle());
        wrapper.like(StrUtil.isNotBlank(query.getUri()), LogInfo::getUri, query.getUri());
        wrapper.like(StrUtil.isNotBlank(query.getMethod()), LogInfo::getMethod, query.getMethod());
        wrapper.like(StrUtil.isNotBlank(query.getParamJson()), LogInfo::getParamJson, query.getParamJson());
//ip
        wrapper.like(StrUtil.isNotBlank(query.getIp()), LogInfo::getIp, query.getIp());
        //开始时间
        if(query.getSt() != null && query.getEt() != null){
            wrapper.between(LogInfo::getLogStartTime, query.getSt(), query.getEt());
        }else if(query.getSt() != null){
            wrapper.ge(LogInfo::getLogStartTime, query.getSt());
        }else if (query.getEt() != null){
            wrapper.le(LogInfo::getLogStartTime, query.getEt());
        }

        // 根据实际业务需求补充更多查询条件
        // 示例排序逻辑
        if (CollectionUtil.isNotEmpty(query.getSorts())) {
            MybatisPlusHelper.orderAndIgnoreError(LogInfo.class, wrapper, query.getSorts());
        } else {
            wrapper.orderByDesc(LogInfo::getCreateTime);
        }
        return wrapper;
    }
}




