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

import cn.hutool.core.collection.CollectionUtil;
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.mapper.AccidentMapper;
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;
import java.util.stream.Collectors;

/**
 * 原无人机前往业务层判断参数
 *
 * @author wanjing
 * @team tuoheng
 * @date 2023-05-08
 */
@Slf4j
@Service("originalDroneForwardCheckParamStrategy")
public class OriginalDroneForwardCheckParamStrategy implements DroneForwardCheckParamStrategy {

    @Autowired
    private AccidentInspectionMapper accidentInspectionMapper;

    @Autowired
    private AccidentMapper accidentMapper;

    @Override
    public JsonResult check(AccidentForwardRequest request, Accident accident) {
        List<AccidentInspection> accidentInspectionList = accidentInspectionMapper.selectList(new LambdaQueryWrapper<AccidentInspection>()
                .eq(AccidentInspection::getAccidentId, accident.getId())
                .eq(AccidentInspection::getMark, MarkEnum.VALID.getCode()));
        if (CollectionUtil.isNotEmpty(accidentInspectionList)) {
            log.info("该应急事故，已做过前往，已创建应急任务，accidentId={}", accident.getId());
            throw new ServiceException("该应急事故，已做过前往，已创建应急任务，不可在前往");
        }
        List<Accident> accidentList = accidentMapper.selectList(new LambdaQueryWrapper<Accident>()
                        .eq(Accident::getInspectionId, accident.getInspectionId())
                        .eq(Accident::getMark, MarkEnum.VALID.getCode()));
        if (CollectionUtil.isEmpty(accidentList)) {
            return JsonResult.success();
        }
        List<String> accidentIdList = accidentList.stream().map(o -> o.getId()).collect(Collectors.toList());
        accidentInspectionList = accidentInspectionMapper.selectList(Wrappers.<AccidentInspection>lambdaQuery()
                .eq(AccidentInspection::getMark, MarkEnum.VALID.getCode())
                .in(AccidentInspection::getAccidentId, accidentIdList)
                .eq(AccidentInspection::getDroneForwardType, AccidentDroneForwardTypeEnum.ORIGINAL_DRONE_FORWARD.getCode()));
        if (CollectionUtil.isNotEmpty(accidentInspectionList)) {
            log.info("执行原巡检任务的无人机已派往事故地点，请派新无人机前往，accidentId={}", accident.getId());
            throw new ServiceException("执行原巡检任务的无人机已派往事故地点，请派新无人机前往");
        }
        return JsonResult.success();
    }

}
