package org.jeecg.modules.diseaseAnalysis.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.jeecg.common.util.UUIDGenerator;
import org.jeecg.modules.diseaseAnalysis.algorithm.MultiProducerLine;
import org.jeecg.modules.diseaseAnalysis.dto.AnalysisLineBatchDTO;
import org.jeecg.modules.diseaseAnalysis.dto.AnalysisLineBatchInfoDTO;
import org.jeecg.modules.diseaseAnalysis.dto.AnalysisLineBatchSaveOrUpdateDTO;
import org.jeecg.modules.diseaseAnalysis.dto.EkRecordsMasterCheckDTO;
import org.jeecg.modules.diseaseAnalysis.entity.*;
import org.jeecg.modules.diseaseAnalysis.mapper.*;
import org.jeecg.modules.diseaseAnalysis.service.IAnalysisLineBatchService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.jeecg.modules.diseaseAnalysis.vo.*;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 线路质量分析	-分析批次表 服务实现类
 * </p>
 *
 * @author hkl
 * @since 2024-07-19
 */
@Service
public class AnalysisLineBatchServiceImpl extends ServiceImpl<AnalysisLineBatchMapper, AnalysisLineBatch> implements IAnalysisLineBatchService {
    @Resource
    private AnalysisLineBatchCheckDataMapMapper analysisLineBatchCheckDataMapMapper;
    @Resource
    private AnalysisLineBatchUnitDeviceMapper analysisLineBatchUnitDeviceMapper;
    @Resource
    private AnalysisLineBatchUnitDeviceTurnoutInfoMapper analysisLineBatchUnitDeviceTurnoutInfoMapper;
    @Resource
    private AnalysisLineBatchUnitDeviceRailWearInfoMapper analysisLineBatchUnitDeviceRailWearInfoMapper;
    /**
     * 线路质量分析-其他病害分析-检查记录-分页列表查询
     *
     * @param pageData
     * @param dto
     * @return
     */
    @Override
    public Page<EkRecordsMasterCheckVO> queryPageEkList(Page<EkRecordsMasterCheckVO> pageData, EkRecordsMasterCheckDTO dto) {
        return this.baseMapper.queryPageEkList(pageData, dto);
    }

    /**
     * 线路质量分析-其他病害分析-线路巡查检查记录-分页列表查询
     *
     * @param pageData
     * @param dto
     * @return
     */
    @Override
    public Page<EkRecordPatrolMasterVO> queryPageEkPatrolList(Page<EkRecordPatrolMasterVO> pageData, EkRecordsMasterCheckDTO dto) {
        return this.baseMapper.queryPageEkPatrolList(pageData, dto);
    }

    /**
     * 线路质量分析-其他病害分析-分页列表查询
     *
     * @param pageData
     * @param dto
     * @return
     */
    @Override
    public Page<AnalysisLineBatchVO> getPageList(Page<AnalysisLineBatchVO> pageData, AnalysisLineBatchDTO dto) {
        return this.baseMapper.getPageList(pageData, dto);
    }
    /**
     * 获取是否可编辑列表
     * @param analysisBatchIds
     * @return
     */
    @Override
    public List<Map<String, Object>> getIsEditList(List<String> analysisBatchIds) {
        return this.baseMapper.getIsEditList(analysisBatchIds);
    }

    /**
     * 获取分析批次对应得检查批次信息
     *
     * @param analysisLineBatchIds 分析批次id集合
     * @return
     */
    @Override
    public List<AnalysisLineBatchCheckDataMapVO> listRecordDetail(List<String> analysisLineBatchIds) {
        return this.baseMapper.listRecordDetail(analysisLineBatchIds);
    }

    /**
     * 线路质量分析-其他病害分析-详情查询-分页列表查询
     *
     * @param pageData
     * @param dto
     * @return
     */
    @Override
    public Page<AnalysisLineBatchUnitDeviceVO> getInfoPageList(Page<AnalysisLineBatchUnitDeviceVO> pageData, AnalysisLineBatchInfoDTO dto) {
        return this.baseMapper.getInfoPageList(pageData, dto);
    }

    /**
     * 线路质量分析-其他病害分析-详情检查记录查询
     *
     * @param analysisBatchId 分析批次id
     * @param ekId            检查批次id
     * @param deviceId        线路质量分析-其他病害分析-单元表id
     * @return
     */
    @Override
    public List<AnalysisLineBatchUnitDeviceTurnoutInfoVO> getAnalysisLineBatchUnitEkInfoList(String analysisBatchId, String ekId, String deviceId) {
        List<AnalysisLineBatchUnitDeviceTurnoutInfoVO> list = this.baseMapper.getAnalysisLineBatchUnitEkInfoList(analysisBatchId, ekId, deviceId);
        return list;
    }

    /**
     * 线路质量分析-其他病害分析-钢轨探伤单元详情检查记录查询
     *
     * @param analysisBatchId 分析批次id
     * @param ekId            检查批次id
     * @param deviceId        线路质量分析-其他病害分析-单元表id
     * @return
     */
    @Override
    public List<AnalysisLineBatchUnitDeviceRailWearInfoVO> getAnalysisLineBatchUnitDeviceRailWearInfoList(String analysisBatchId, String ekId, String deviceId) {
        List<AnalysisLineBatchUnitDeviceRailWearInfoVO> list = this.baseMapper.getAnalysisLineBatchUnitDeviceRailWearInfoList(analysisBatchId, ekId, deviceId);
        return list;
    }

    /**
     *其他病害分析-分析批次-新增分析批次
     * @param dto
     */
    @Override
    public void saveRecord(AnalysisLineBatchSaveOrUpdateDTO dto) {
        // 主表
        AnalysisLineBatch analysisLineBatch = BeanUtil.copyProperties(dto, AnalysisLineBatch.class);
        analysisLineBatch.setId(UUIDGenerator.generate());
        analysisLineBatch.setAnalysisStatus(1); // 1-分析中


        // 明细
        List<AnalysisLineBatchCheckDataMap> analysisLineBatchCheckDataMaps = new ArrayList<>();
        List<AnalysisLineBatchSaveOrUpdateDTO.CheckDataDTO> checkDataList = dto.getCheckDataList();
        for (AnalysisLineBatchSaveOrUpdateDTO.CheckDataDTO checkDataDTO : checkDataList) {
            AnalysisLineBatchCheckDataMap analysisLineBatchCheckDataMap = new AnalysisLineBatchCheckDataMap();
            analysisLineBatchCheckDataMap.setId(UUIDGenerator.generate());
            analysisLineBatchCheckDataMap.setAnalysisBatchId(analysisLineBatch.getId());
            analysisLineBatchCheckDataMap.setAnalysisBatchCode(analysisLineBatch.getAnalysisBatchCode());
            analysisLineBatchCheckDataMap.setEkCode(checkDataDTO.getEkCode());
            analysisLineBatchCheckDataMap.setEkId(checkDataDTO.getEkId());
            analysisLineBatchCheckDataMap.setStartingMileage(checkDataDTO.getStartingMileage());
            analysisLineBatchCheckDataMap.setEndMileage(checkDataDTO.getEndMileage());
            analysisLineBatchCheckDataMap.setCheckTime(checkDataDTO.getCheckTime());
            analysisLineBatchCheckDataMap.setEkType(checkDataDTO.getEkType());
            analysisLineBatchCheckDataMap.setAnalysisStatus(1);// 分析中
            analysisLineBatchCheckDataMap.setRemark(dto.getRemark());
            analysisLineBatchCheckDataMaps.add(analysisLineBatchCheckDataMap);
        }

        this.save(analysisLineBatch);
        for (AnalysisLineBatchCheckDataMap analysisLineBatchCheckDataMap : analysisLineBatchCheckDataMaps) {
            analysisLineBatchCheckDataMapMapper.insert(analysisLineBatchCheckDataMap);
        }

        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        // 执行分析
        MultiProducerLine.produce(analysisLineBatch.getId());
    }

    /**
     * 其他病害分析-分析批次-更新分析批次
     * @param dto
     */
    @Override
    public void updateRecord(AnalysisLineBatchSaveOrUpdateDTO dto) {
        // 主表
        AnalysisLineBatch analysisLineBatch = BeanUtil.copyProperties(dto, AnalysisLineBatch.class);
        analysisLineBatch.setAnalysisStatus(1); // 1-分析中

        // 明细
        // 1.先删除
        LambdaQueryWrapper<AnalysisLineBatchCheckDataMap> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(AnalysisLineBatchCheckDataMap::getAnalysisBatchId, analysisLineBatch.getId());
        analysisLineBatchCheckDataMapMapper.delete(wrapper);
        // 2.后新增
        List<AnalysisLineBatchCheckDataMap> analysisLineBatchCheckDataMaps = new ArrayList<>();
        List<AnalysisLineBatchSaveOrUpdateDTO.CheckDataDTO> checkDataList = dto.getCheckDataList();
        for (AnalysisLineBatchSaveOrUpdateDTO.CheckDataDTO checkDataDTO : checkDataList) {
            AnalysisLineBatchCheckDataMap analysisLineBatchCheckDataMap = new AnalysisLineBatchCheckDataMap();
            analysisLineBatchCheckDataMap.setId(UUIDGenerator.generate());
            analysisLineBatchCheckDataMap.setAnalysisBatchId(analysisLineBatch.getId());
            analysisLineBatchCheckDataMap.setAnalysisBatchCode(analysisLineBatch.getAnalysisBatchCode());
            analysisLineBatchCheckDataMap.setEkCode(checkDataDTO.getEkCode());
            analysisLineBatchCheckDataMap.setEkId(checkDataDTO.getEkId());
            analysisLineBatchCheckDataMap.setStartingMileage(checkDataDTO.getStartingMileage());
            analysisLineBatchCheckDataMap.setEndMileage(checkDataDTO.getEndMileage());
            analysisLineBatchCheckDataMap.setCheckTime(checkDataDTO.getCheckTime());
            analysisLineBatchCheckDataMap.setEkType(checkDataDTO.getEkType());
            analysisLineBatchCheckDataMap.setAnalysisStatus(1);// 分析中
            analysisLineBatchCheckDataMap.setRemark(dto.getRemark());
            analysisLineBatchCheckDataMaps.add(analysisLineBatchCheckDataMap);
        }

        this.updateById(analysisLineBatch);
        for (AnalysisLineBatchCheckDataMap analysisLineBatchCheckDataMap : analysisLineBatchCheckDataMaps) {
            analysisLineBatchCheckDataMapMapper.insert(analysisLineBatchCheckDataMap);
        }
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        // 执行分析
        MultiProducerLine.produce(analysisLineBatch.getId());
    }

    /**
     * 其他病害分析-分析批次-删除
     * @param idList
     */
    @Override
    @Transactional
    public void deleteBatchRecord(List<String> idList,Integer type) {
        // 1.先删除病害分析主表与检查记录主表之间的关系表
        LambdaQueryWrapper<AnalysisLineBatchCheckDataMap> wrapper = Wrappers.lambdaQuery();
        wrapper.in(AnalysisLineBatchCheckDataMap::getAnalysisBatchId, idList);
        analysisLineBatchCheckDataMapMapper.delete(wrapper);
        // 2.删除单元表
        LambdaQueryWrapper<AnalysisLineBatchUnitDevice> wrapper1 = Wrappers.lambdaQuery();
        wrapper1.in(AnalysisLineBatchUnitDevice::getAnalysisBatchId, idList);
        analysisLineBatchUnitDeviceMapper.delete(wrapper1);
        //type值检查数据类型 1-道岔检查 2-钢轨常规探伤检查 3-曲线钢轨磨耗检查 4-钢轨波磨检查 5-道床检查 6-线路检查
        if(type == 1){
            //3.删除道岔单元详情表
            LambdaQueryWrapper<AnalysisLineBatchUnitDeviceTurnoutInfo> wrapper2 = Wrappers.lambdaQuery();
            wrapper2.in(AnalysisLineBatchUnitDeviceTurnoutInfo::getAnalysisBatchId, idList);
            analysisLineBatchUnitDeviceTurnoutInfoMapper.delete(wrapper2);
        }else if(type ==2){
            //3.删除道岔单元详情表
            LambdaQueryWrapper<AnalysisLineBatchUnitDeviceRailWearInfo> wrapper2 = Wrappers.lambdaQuery();
            wrapper2.in(AnalysisLineBatchUnitDeviceRailWearInfo::getAnalysisBatchId, idList);
            analysisLineBatchUnitDeviceRailWearInfoMapper.delete(wrapper2);
        }
        //4.删除分析主表
        this.baseMapper.deleteBatchIds(idList);
    }
}
