package com.tuoheng.admin.service.third.dsp;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.tuoheng.admin.entity.*;
import com.tuoheng.admin.enums.AccidentStatusEnum;
import com.tuoheng.admin.enums.MarkEnum;
import com.tuoheng.admin.enums.SourcesEnum;
import com.tuoheng.admin.mapper.ChannelMapper;
import com.tuoheng.admin.mapper.DeptMapper;
import com.tuoheng.admin.mapper.InspectionMapper;
import com.tuoheng.admin.service.accident.IAccidentService;
import com.tuoheng.admin.websocket.WebSocketService;
import com.tuoheng.common.core.utils.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Slf4j
@Service
public class SaveAccidentDataService {

    @Autowired
    private InspectionMapper inspectionMapper;

    @Autowired
    private ChannelMapper channelMapper;

    @Autowired
    private IAccidentService accidentService;

    @Autowired
    private WebSocketService webSocketService;

    @Autowired
    private DeptMapper deptMapper;

    /**
     * 应急记录数据入库
     *
     * @param inspectionFileList
     * @return
     */
    @Transactional
    public Boolean saveAccidentData(Map<String, QuestionType> questionTypeMap, List<InspectionFile> inspectionFileList) {
        log.info("调用保存应急数据方法,应急数据入库开始....");
        List<Accident> accidentList = new ArrayList<>();
        for (InspectionFile inspectionFile : inspectionFileList) {
            QuestionType questionType = questionTypeMap.get(inspectionFile.getQuestionCode());
            if (0 == questionType.getAccidentQuestion()) {
                // 非事故问题，不用添加到事故表
                continue;
            }
            Accident accident = new Accident();
            //问题类型为应急类型时添加数据
            accident.setTenantId(inspectionFile.getTenantId());
            accident.setInspectionId(inspectionFile.getInspectionId());
            accident.setInspectionFileId(inspectionFile.getId());
            //根据巡检任务查询对应的公路id
            Inspection inspection = inspectionMapper.selectById(inspectionFile.getInspectionId());
            if (ObjectUtils.isNotEmpty(inspection)) {
                accident.setChannelId(inspection.getChannelId());
                accident.setChannelName(inspection.getChannelName());
                accident.setLegId(inspection.getLegId());
                accident.setLegName(inspection.getLegName());

                //根据巡检任务类型判断当前应急事故类型flag设值
                if (2 == inspection.getType()) {
                    accident.setSource(SourcesEnum.ACCIDENT.getCode());
                } else {
                    // 该巡检任务存在应急任务，将后面发现的预警直接设置为不展示
                    Inspection emergencyInspection = inspectionMapper.selectOne(new LambdaQueryWrapper<Inspection>()
                            .eq(Inspection::getEmergencyDataInspectionId, inspectionFile.getInspectionId())
                            .eq(Inspection::getMark, MarkEnum.VALID.getCode()));
                    if (ObjectUtils.isNotEmpty(emergencyInspection)) {
                        accident.setSource(SourcesEnum.ACCIDENT.getCode());
                    } else {
                        accident.setSource(SourcesEnum.INSPECTION_ACCIDENT.getCode());
                    }
                }
                String deptId = inspection.getDeptId();
                accident.setDeptId(deptId);
                //根据部门id查询部门名称
                Dept dept = deptMapper.selectById(deptId);
                if(ObjectUtil.isNotNull(dept)){
                    if(StringUtils.isNotEmpty(dept.getName())){
                        accident.setDeptName(dept.getName());
                    }
                }
            }

            //航道代号
            Channel channel = channelMapper.selectById(inspection.getChannelId());
            if (ObjectUtils.isNotEmpty(channel)) {
                accident.setChannelCode(channel.getCode());
            }
            accident.setQuestionId(inspectionFile.getQuestionId());
            accident.setQuestionCode(inspectionFile.getQuestionCode());
            accident.setQuestionName(inspectionFile.getQuestionName());
            accident.setLongitude(inspectionFile.getLongitude());
            accident.setLatitude(inspectionFile.getLatitude());
            accident.setStatus(AccidentStatusEnum.UNTREATED.getCode());
            accident.setCreateUser(inspectionFile.getCreateUser());
            accident.setCreateTime(inspectionFile.getCreateTime());
            accidentList.add(accident);
        }
        //产生的应急事故数据入库
        Boolean result = false;
        for (Accident accident : accidentList) {
            result = accidentService.save(accident);
            //数据发生变化推送通知
            webSocketService.accidentNotice(accident.getTenantId());
        }
        return result;
        //return accidentService.saveBatch(accidentList);
    }

}
