package com.tuoheng.admin.service.accident.forward.check;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.tuoheng.admin.entity.Accident;
import com.tuoheng.admin.entity.AccidentInspection;
import com.tuoheng.admin.enums.AccidentDroneForwardTypeEnum;
import com.tuoheng.admin.enums.MarkEnum;
import com.tuoheng.admin.mapper.AccidentInspectionMapper;
import com.tuoheng.admin.request.accident.AccidentForwardRequest;
import com.tuoheng.common.core.exception.ServiceException;
import com.tuoheng.common.core.utils.JsonResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 原无人机继续任务，派新无人机前往业务层判断参数
 *
 * @author wanjing
 * @team tuoheng
 * @date 2023-05-08
 */
@Slf4j
@Service("originalDroneContinueCheckParamStrategy")
public class OriginalDroneContinueCheckParamStrategy implements DroneForwardCheckParamStrategy {

    @Autowired
    private AccidentInspectionMapper accidentInspectionMapper;

    @Override
    public JsonResult check(AccidentForwardRequest request, Accident accident) {
        AccidentInspection accidentInspection = accidentInspectionMapper.selectOne(new LambdaQueryWrapper<AccidentInspection>()
                .eq(AccidentInspection::getAccidentId, accident.getId())
                .eq(AccidentInspection::getMark, MarkEnum.VALID.getCode()));
        if (ObjectUtil.isNotEmpty(accidentInspection)) {
            log.info("该应急事故，已做过前往，已创建应急任务，accidentInspectionId={}", accidentInspection.getId());
            throw new ServiceException("该应急事故，已做过前往，已创建应急任务，不可在前往");
        }
        return JsonResult.success();
    }

}
