package com.seer.teach.admin.service.impl;

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 com.seer.teach.admin.request.AdminModificationLogPageReq;
import com.seer.teach.admin.service.AdminModificationLogService;
import com.seer.teach.teacher.module.entity.ModificationLogEntity;
import com.seer.teach.teacher.module.mapper.ModificationLogMapper;
import com.seer.teach.teacher.service.ModificationLogService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.Objects;

/**
 * 修改日志管理服务实现类
 *
 * @author Lingma
 * @since 2025-11-27
 */
@Service
@RequiredArgsConstructor
public class AdminModificationLogServiceImpl implements AdminModificationLogService {

    private final ModificationLogService modificationLogService;


    @Override
    public IPage<ModificationLogEntity> pageLogs(AdminModificationLogPageReq pageReq) {

        // 创建分页对象
        Page<ModificationLogEntity> page = new Page<>(pageReq.getPageNo(), pageReq.getPageSize());

        LambdaQueryWrapper<ModificationLogEntity> queryWrapper = new LambdaQueryWrapper<>();
        
        // 添加目标类型查询条件
        queryWrapper.eq(Objects.nonNull(pageReq.getTargetType()), ModificationLogEntity::getTargetType, pageReq.getTargetType());
        queryWrapper.eq(Objects.nonNull(pageReq.getTargetId()), ModificationLogEntity::getTargetId, pageReq.getTargetId());
        // 添加操作类型查询条件
        queryWrapper.in(ModificationLogEntity::getOperationType, Arrays.asList(ModificationLogEntity.OPERATION_TYPE_ADD,
                ModificationLogEntity.OPERATION_TYPE_UPDATE, ModificationLogEntity.OPERATION_TYPE_DELETE));
        
        // 按创建时间倒序排列
        queryWrapper.orderByDesc(ModificationLogEntity::getCreateTime);
        
        return modificationLogService.page(page, queryWrapper);
    }
}