package cn.novalon.gym.manage.db.converter;

import cn.novalon.gym.manage.sys.core.domain.OperationLog;
import cn.novalon.gym.manage.db.entity.OperationLogEntity;

import org.springframework.stereotype.Component;

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

/**
 * 操作日志实体转换器
 * 
 * @author 张翔
 * @date 2026-03-13
 */
@Component
public class OperationLogConverter {

    public OperationLog toDomain(OperationLogEntity entity) {
        if (entity == null) {
            return null;
        }
        OperationLog domain = new OperationLog();
        domain.setId(entity.getId());
        domain.setUsername(entity.getUsername());
        domain.setOperation(entity.getOperation());
        domain.setMethod(entity.getMethod());
        domain.setParams(entity.getParams());
        domain.setResult(entity.getResult());
        domain.setIp(entity.getIp());
        domain.setDuration(entity.getDuration());
        domain.setStatus(entity.getStatus());
        domain.setErrorMsg(entity.getErrorMsg());
        domain.setCreatedAt(entity.getCreatedAt());
        domain.setUpdatedAt(entity.getUpdatedAt());
        domain.setDeletedAt(entity.getDeletedAt());
        return domain;
    }

    public OperationLogEntity toEntity(OperationLog domain) {
        if (domain == null) {
            return null;
        }
        OperationLogEntity entity = new OperationLogEntity();
        entity.setId(domain.getId());
        entity.setUsername(domain.getUsername());
        entity.setOperation(domain.getOperation());
        entity.setMethod(domain.getMethod());
        entity.setParams(domain.getParams());
        entity.setResult(domain.getResult());
        entity.setIp(domain.getIp());
        entity.setDuration(domain.getDuration());
        entity.setStatus(domain.getStatus());
        entity.setErrorMsg(domain.getErrorMsg());
        entity.setCreatedAt(domain.getCreatedAt());
        entity.setUpdatedAt(domain.getUpdatedAt());
        entity.setDeletedAt(domain.getDeletedAt());
        return entity;
    }

    public List<OperationLog> toDomainList(List<OperationLogEntity> entities) {
        if (entities == null) {
            return null;
        }
        return entities.stream()
                .map(this::toDomain)
                .collect(Collectors.toList());
    }

    public List<OperationLogEntity> toEntityList(List<OperationLog> domains) {
        if (domains == null) {
            return null;
        }
        return domains.stream()
                .map(this::toEntity)
                .collect(Collectors.toList());
    }
}
