package com.tuoheng.admin.service.third;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.tuoheng.admin.entity.domain.ThMission;
import com.tuoheng.admin.entity.domain.WarningRecord;
import com.tuoheng.admin.enums.MarkEnum;
import com.tuoheng.admin.enums.MarkTypeEnum;
import com.tuoheng.admin.mapper.ThMissionMapper;
import com.tuoheng.admin.mapper.WarningRecordMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Slf4j
@Service
public class UpdateEmergencyMissionStatusService {

    @Autowired
    private WarningRecordMapper warningRecordMapper;

    @Autowired
    private ThMissionMapper thMissionMapper;

    /**
     * 修改应急任务数据
     *
     * @param oldMission
     */
    public void updateStatus(ThMission oldMission, Integer status) {
        log.info("修改应急任务数据, type:{}, emergencyDataMissionId:{}, status={}", oldMission.getType(), oldMission.getEmergencyDataMissionId(), status);
        ThMission emergencyMission = thMissionMapper.selectOne(new LambdaQueryWrapper<ThMission>()
                .eq(ThMission::getEmergencyDataMissionId, oldMission.getId())
                .eq(ThMission::getMark, MarkEnum.VALID.getCode()));
        if (ObjectUtil.isNull(emergencyMission)) {
            log.info("修改应急任务数据，该巡检任务没有对应的应急任务");
            return;
        }
        // 在查询一次，保证数据是最新的
        ThMission mission = thMissionMapper.selectOne(new LambdaQueryWrapper<ThMission>()
                .eq(ThMission::getId, oldMission.getId())
                .eq(ThMission::getMark, MarkEnum.VALID.getCode()));
        if (ObjectUtil.isNotNull(status)) {
            emergencyMission.setStatus(status);
        }
        emergencyMission.setPlayUrl(mission.getPlayUrl());
        emergencyMission.setAiplayUrl(mission.getAiplayUrl());
        emergencyMission.setPullUrl(mission.getAipullUrl());
        emergencyMission.setPushUrl(mission.getAipushUrl());
        emergencyMission.setAipullUrl(mission.getAipullUrl());
        emergencyMission.setAipushUrl(mission.getAipushUrl());
        emergencyMission.setAnalyseStatus(mission.getAnalyseStatus());
        emergencyMission.setVideoUrl(mission.getVideoUrl());
        emergencyMission.setAiVideoUrl(mission.getAiVideoUrl());
        thMissionMapper.updateById(emergencyMission);
        log.info("修改应急任务数据完成");
    }

}
