package com.aegis.emergency.dispatch.service.impl;

import com.aegis.common.exception.ServiceException;
import com.aegis.common.utils.SecurityUtils;
import com.aegis.emergency.dispatch.domain.DispatchTaskDisposal;
import com.aegis.emergency.dispatch.mapper.DispatchTaskDisposalMapper;
import com.aegis.emergency.dispatch.service.IDispatchTaskDisposalService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 调度任务处置记录 Service 实现
 *
 * @author aegis
 */
@Service
public class DispatchTaskDisposalServiceImpl extends ServiceImpl<DispatchTaskDisposalMapper, DispatchTaskDisposal>
        implements IDispatchTaskDisposalService {

    @Override
    public List<DispatchTaskDisposal> listByTaskId(Long taskId) {
        if (taskId == null) {
            return Collections.emptyList();
        }
        return this.list(new LambdaQueryWrapper<DispatchTaskDisposal>()
            .eq(DispatchTaskDisposal::getTaskId, taskId)
            .eq(DispatchTaskDisposal::getDelFlag, "0")
            .orderByDesc(DispatchTaskDisposal::getCreateTime));
    }

    @Override
    public List<DispatchTaskDisposal> listByTaskAndParticipant(Long taskId, Long participantId) {
        if (taskId == null || participantId == null) {
            return Collections.emptyList();
        }
        return this.list(new LambdaQueryWrapper<DispatchTaskDisposal>()
            .eq(DispatchTaskDisposal::getTaskId, taskId)
            .eq(DispatchTaskDisposal::getParticipantId, participantId)
            .eq(DispatchTaskDisposal::getDelFlag, "0")
            .orderByDesc(DispatchTaskDisposal::getCreateTime));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean save(DispatchTaskDisposal entity) {
        validateDisposal(entity, true);
        fillAuditFields(entity, true);
        entity.setDelFlag("0");
        return super.save(entity);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateById(DispatchTaskDisposal entity) {
        validateDisposal(entity, false);
        fillAuditFields(entity, false);
        return super.updateById(entity);
    }

    private void validateDisposal(DispatchTaskDisposal entity, boolean isNew) {
        if (entity == null) {
            throw new ServiceException("处置记录不能为空");
        }
        if (entity.getTaskId() == null) {
            throw new ServiceException("任务ID不能为空");
        }
        if (entity.getParticipantId() == null) {
            throw new ServiceException("参与方ID不能为空");
        }
        if (!isNew && entity.getId() == null) {
            throw new ServiceException("处置记录ID不能为空");
        }
    }

    private void fillAuditFields(DispatchTaskDisposal entity, boolean isNew) {
        String operator = SecurityUtils.getUsername();
        Date now = new Date();
        if (isNew) {
            entity.setCreateBy(operator);
            entity.setCreateTime(now);
        }
        entity.setUpdateBy(operator);
        entity.setUpdateTime(now);
    }
}