package com.agileboot.system.log.service.impl;

import com.agileboot.common.mybatis.core.page.PageR;
import com.agileboot.system.log.dto.LoginLogQuery;
import com.agileboot.system.log.dto.OperationLogQuery;
import com.agileboot.system.log.entity.SysLoginInfo;
import com.agileboot.system.log.entity.SysOperationLog;
import com.agileboot.system.log.mapper.SysLoginInfoMapper;
import com.agileboot.system.log.mapper.SysOperationLogMapper;
import com.agileboot.system.log.service.ILogService;
import com.agileboot.system.log.vo.LoginLogVO;
import com.agileboot.system.log.vo.OperationLogVO;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class LogServiceImpl implements ILogService {

    @Resource
    private SysLoginInfoMapper sysLoginInfoMapper;
    @Resource
    private SysOperationLogMapper sysOperationLogMapper;

    @Override
    public PageR<LoginLogVO> getLoginInfoList(LoginLogQuery query) {
        Page<SysLoginInfo> page = sysLoginInfoMapper.selectPage(query.toPage(), query.toQueryWrapper());
        IPage<LoginLogVO> pageVO = page.convert(LoginLogVO::new);
        return new PageR<>(pageVO);
    }

    @Override
    public void deleteLoginInfo(List<Long> ids) {
        LambdaQueryWrapper<SysLoginInfo> wrapper = Wrappers.lambdaQuery(SysLoginInfo.class)
                .in(SysLoginInfo::getInfoId, ids);
        sysLoginInfoMapper.delete(wrapper);
    }

    @Override
    public PageR<OperationLogVO> getOperationLogList(OperationLogQuery query) {
        Page<SysOperationLog> page = sysOperationLogMapper.selectPage(query.toPage(), query.toQueryWrapper());
        IPage<OperationLogVO> pageVO = page.convert(OperationLogVO::new);
        return new PageR<>(pageVO);
    }

    @Override
    public void deleteOperationLog(List<Long> operationIds) {
        sysOperationLogMapper.deleteByIds(operationIds);
    }
}
