package jnpf.attendance.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jnpf.attendance.mapper.AttendanceBookOperationLogMapper;
import jnpf.attendance.service.AttendanceBookOperationLogService;
import jnpf.base.UserInfo;
import jnpf.entity.attendance.AttendanceBookOperationLogEntity;
import jnpf.model.attendance.dto.OperationLogQueryDto;
import jnpf.model.attendance.vo.OperationLogPageVo;
import jnpf.util.RandomUtil;
import jnpf.util.UserProvider;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 考勤本操作日志Service实现类
 *
 * @author Generated
 * @create 2026-04-15
 */
@Slf4j
@Service
public class AttendanceBookOperationLogServiceImpl extends ServiceImpl<AttendanceBookOperationLogMapper, AttendanceBookOperationLogEntity> implements AttendanceBookOperationLogService {

    @Override
    public void saveLog(AttendanceBookOperationLogEntity entity) {
        // 参数校验
        if (entity == null) {
            log.warn("操作日志实体为空，跳过记录");
            return;
        }

        // 自动填充主键
        if (entity.getId() == null || entity.getId().isEmpty()) {
            entity.setId(RandomUtil.uuId());
        }

        // 自动填充操作时间
        if (entity.getOperationTime() == null) {
            entity.setOperationTime(new Date());
        }

        // 获取当前用户信息
        UserInfo user = UserProvider.getUser();
        if (user != null) {
            // 如果没有传入操作人信息，则使用当前登录用户
            if (entity.getOperatorId() == null || entity.getOperatorId().isEmpty()) {
                entity.setOperatorId(user.getUserId());
            }
            if (entity.getOperatorName() == null || entity.getOperatorName().isEmpty()) {
                entity.setOperatorName(user.getUserName());
            }
        }

        this.save(entity);
        log.info("记录考勤本操作日志成功，日志ID: {}, 考勤本ID: {}, 员工ID: {}", 
                entity.getId(), entity.getBookId(), entity.getEmployeeId());
    }

    @Override
    public Page<OperationLogPageVo> queryPage(OperationLogQueryDto queryDto) {
        // 构建查询条件
        LambdaQueryWrapper<AttendanceBookOperationLogEntity> queryWrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNotBlank(queryDto.getBookId())) {
            queryWrapper.eq(AttendanceBookOperationLogEntity::getBookId, queryDto.getBookId());
        }
        if (StringUtils.isNotBlank(queryDto.getMonth())) {
            queryWrapper.eq(AttendanceBookOperationLogEntity::getMonth, queryDto.getMonth());
        }
        if (StringUtils.isNotBlank(queryDto.getEmployeeId())) {
            queryWrapper.eq(AttendanceBookOperationLogEntity::getEmployeeId, queryDto.getEmployeeId());
        }
        queryWrapper.orderByDesc(AttendanceBookOperationLogEntity::getOperationTime);

        // 执行分页查询
        Page<AttendanceBookOperationLogEntity> pageResult = this.page(
                new Page<>(queryDto.getPage(), queryDto.getSize()), queryWrapper);

        // 转换为 VO
        List<OperationLogPageVo> voList = pageResult.getRecords().stream()
                .map(this::convertToVo)
                .collect(Collectors.toList());

        // 构建返回结果
        Page<OperationLogPageVo> result = new Page<>(pageResult.getCurrent(), pageResult.getSize(), pageResult.getTotal());
        result.setRecords(voList);

        return result;
    }

    /**
     * 实体转换为VO
     *
     * @param entity 实体
     * @return VO
     */
    private OperationLogPageVo convertToVo(AttendanceBookOperationLogEntity entity) {
        OperationLogPageVo vo = new OperationLogPageVo();
        vo.setId(entity.getId());
        vo.setBookId(entity.getBookId());
        vo.setMonth(entity.getMonth());
        vo.setRecordId(entity.getRecordId());
        vo.setEmployeeId(entity.getEmployeeId());
        vo.setOperationContent(entity.getOperationContent());
        vo.setOperatorId(entity.getOperatorId());
        vo.setOperatorName(entity.getOperatorName());
        vo.setOperationTime(entity.getOperationTime());
        return vo;
    }
}