package com.aegis.resource.team.service.impl;

import com.aegis.common.utils.SecurityUtils;
import com.aegis.resource.team.domain.TeamLog;
import com.aegis.resource.team.mapper.ResourceTeamLogMapper;
import com.aegis.resource.team.service.IResourceTeamLogService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import java.util.Collection;
import java.util.Date;
import java.util.List;
import org.springframework.stereotype.Service;

/**
 * 队伍操作日志Service业务层处理
 * 
 * @author aegis
 */
@Service
public class ResourceTeamLogServiceImpl extends ServiceImpl<ResourceTeamLogMapper, TeamLog>
    implements IResourceTeamLogService {

    @Override
    public List<TeamLog> listByCondition(TeamLog condition) {
        LambdaQueryWrapper<TeamLog> wrapper = buildConditionWrapper(condition);
        wrapper.orderByDesc(TeamLog::getCreatedAt);
        return list(wrapper);
    }

    @Override
    public List<TeamLog> listByTeamId(Long teamId) {
        return list(new LambdaQueryWrapper<TeamLog>()
            .eq(TeamLog::getTeamId, teamId)
            .orderByDesc(TeamLog::getCreatedAt));
    }

    @Override
    public boolean save(TeamLog entity) {
        fillOperatorInfo(entity);
        entity.setCreatedAt(new Date());
        return super.save(entity);
    }

    @Override
    public boolean saveBatch(Collection<TeamLog> entityList, int batchSize) {
        if (entityList == null || entityList.isEmpty()) {
            return true;
        }
        for (TeamLog log : entityList) {
            fillOperatorInfo(log);
            if (log.getCreatedAt() == null) {
                log.setCreatedAt(new Date());
            }
        }
        return super.saveBatch(entityList, batchSize);
    }

    private LambdaQueryWrapper<TeamLog> buildConditionWrapper(TeamLog condition) {
        LambdaQueryWrapper<TeamLog> wrapper = new LambdaQueryWrapper<>();
        if (condition == null) {
            return wrapper;
        }
        wrapper.eq(condition.getTeamId() != null, TeamLog::getTeamId, condition.getTeamId());
        wrapper.eq(condition.getAction() != null, TeamLog::getAction, condition.getAction());
        wrapper.eq(condition.getMemberId() != null, TeamLog::getMemberId, condition.getMemberId());
        wrapper.eq(condition.getRequisitionId() != null, TeamLog::getRequisitionId, condition.getRequisitionId());
        wrapper.eq(condition.getDispatchId() != null, TeamLog::getDispatchId, condition.getDispatchId());
        wrapper.eq(condition.getEventId() != null, TeamLog::getEventId, condition.getEventId());
        wrapper.eq(condition.getOperatorId() != null, TeamLog::getOperatorId, condition.getOperatorId());
        return wrapper;
    }

    private void fillOperatorInfo(TeamLog log) {
        if (log == null) {
            return;
        }
        try {
            if (log.getOperatorId() == null) {
                log.setOperatorId(SecurityUtils.getUserId());
            }
            if (log.getOperatorName() == null) {
                log.setOperatorName(SecurityUtils.getUsername());
            }
        } catch (Exception ignored) {
            // ignore when no security context
        }
    }
}