package com.ruoyi.drill.service.impl;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.bean.BeanUtils;
import com.ruoyi.common.utils.uuid.IdUtils;
import com.ruoyi.drill.domain.TyDrillAccidentIncubation;
import com.ruoyi.drill.domain.TyDrillAccidentIncubationDetails;
import com.ruoyi.drill.domain.TyDrillInfoDetails;
import com.ruoyi.drill.domain.TyDrillOrientationEnvironment;
import com.ruoyi.drill.domain.TyDrillSocialEmergencyResources;
import com.ruoyi.drill.dto.TyDrillAccidentIncubationDto;
import com.ruoyi.drill.dto.TyDrillInfoDetailsDto;
import com.ruoyi.drill.mapper.TyDrillInfoDetailsMapper;
import com.ruoyi.drill.service.ITyDrillAccidentIncubationDetailsService;
import com.ruoyi.drill.service.ITyDrillAccidentIncubationService;
import com.ruoyi.drill.service.ITyDrillInfoDetailsService;
import com.ruoyi.drill.service.ITyDrillOrientationEnvironmentService;
import com.ruoyi.drill.service.ITyDrillSocialEmergencyResourcesService;
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.Objects;

/**
 * 推演详情Service业务层处理
 *
 * @author ruoyi
 * @date 2023-09-26
 */
@Service
public class TyDrillInfoDetailsServiceImpl extends ServiceImpl<TyDrillInfoDetailsMapper, TyDrillInfoDetails> implements ITyDrillInfoDetailsService {

    @Resource
    private ITyDrillOrientationEnvironmentService drillOrientationEnvironmentService;
    @Resource
    private ITyDrillSocialEmergencyResourcesService drillSocialEmergencyResourcesService;
    @Resource
    private ITyDrillAccidentIncubationService drillAccidentIncubationService;
    @Resource
    private ITyDrillAccidentIncubationDetailsService drillAccidentIncubationDetailsService;

    @Override
    public AjaxResult getDetails(String drillInfoId) {
        TyDrillInfoDetails one = this.getOne(Wrappers.<TyDrillInfoDetails>lambdaQuery().eq(TyDrillInfoDetails::getTyDrillInfoId, drillInfoId));
        if (Objects.isNull(one)) {
            return AjaxResult.warn("无权限");
        }
        TyDrillInfoDetailsDto dto = new TyDrillInfoDetailsDto();
        BeanUtils.copyProperties(one, dto);
        List<TyDrillOrientationEnvironment> environments = drillOrientationEnvironmentService.list(Wrappers.<TyDrillOrientationEnvironment>lambdaQuery()
                .eq(TyDrillOrientationEnvironment::getTyDrillInfoDetailsId, dto.getId()));
        dto.setTyDrillOrientationEnvironmentList(environments);
        List<TyDrillSocialEmergencyResources> resources = drillSocialEmergencyResourcesService.list(Wrappers.<TyDrillSocialEmergencyResources>lambdaQuery()
                .eq(TyDrillSocialEmergencyResources::getTyDrillInfoDetailsId, dto.getId()));
        dto.setTyDrillSocialEmergencyResourcesList(resources);
        List<TyDrillAccidentIncubation> incubations = drillAccidentIncubationService.list(Wrappers.<TyDrillAccidentIncubation>lambdaQuery()
                .eq(TyDrillAccidentIncubation::getTyDrillInfoDetailsId, dto.getId()));
        if (CollectionUtils.isNotEmpty(incubations)) {
            List<TyDrillAccidentIncubationDto> incubationDtos = new ArrayList<>();
            BeanUtils.copyProperties(incubations, incubationDtos);
            for (TyDrillAccidentIncubationDto incubationDto : incubationDtos) {
                String id = incubationDto.getId();
                List<TyDrillAccidentIncubationDetails> incubationDetails1 = drillAccidentIncubationDetailsService.list(Wrappers.<TyDrillAccidentIncubationDetails>lambdaQuery()
                        .eq(TyDrillAccidentIncubationDetails::getType, "1")
                        .eq(TyDrillAccidentIncubationDetails::getTyDrillAccidentIncubationId, id));
                incubationDto.setTyDrillAccidentIncubationDetailsList1(incubationDetails1);
                List<TyDrillAccidentIncubationDetails> incubationDetails2 = drillAccidentIncubationDetailsService.list(Wrappers.<TyDrillAccidentIncubationDetails>lambdaQuery()
                        .eq(TyDrillAccidentIncubationDetails::getType, "1")
                        .eq(TyDrillAccidentIncubationDetails::getTyDrillAccidentIncubationId, id));
                incubationDto.setTyDrillAccidentIncubationDetailsList2(incubationDetails2);
                List<TyDrillAccidentIncubationDetails> incubationDetails3 = drillAccidentIncubationDetailsService.list(Wrappers.<TyDrillAccidentIncubationDetails>lambdaQuery()
                        .eq(TyDrillAccidentIncubationDetails::getType, "1")
                        .eq(TyDrillAccidentIncubationDetails::getTyDrillAccidentIncubationId, id));
                incubationDto.setTyDrillAccidentIncubationDetailsList3(incubationDetails3);
                List<TyDrillAccidentIncubationDetails> incubationDetails4 = drillAccidentIncubationDetailsService.list(Wrappers.<TyDrillAccidentIncubationDetails>lambdaQuery()
                        .eq(TyDrillAccidentIncubationDetails::getType, "1")
                        .eq(TyDrillAccidentIncubationDetails::getTyDrillAccidentIncubationId, id));
                incubationDto.setTyDrillAccidentIncubationDetailsList4(incubationDetails4);
            }
            dto.setTyDrillAccidentIncubationDtoList(incubationDtos);
        }

        return AjaxResult.success(dto);
    }

    @Override
    @Transactional
    public AjaxResult saveAndUpdate(TyDrillInfoDetailsDto dto) {
        String tyDrillInfoId = dto.getTyDrillInfoId();
        if (StringUtils.isEmpty(tyDrillInfoId)) {
            return AjaxResult.warn("必填参数不能为空");
        }
        TyDrillInfoDetails infoDetails = this.getById(dto.getId());
        if (Objects.isNull(infoDetails)) {
            dto.setId(IdUtils.fastSimpleUUID());
        }

        TyDrillInfoDetails one = new TyDrillInfoDetails();
        BeanUtils.copyProperties(dto, one);
        this.saveOrUpdate(one);

        List<TyDrillAccidentIncubationDto> incubationDtoList = dto.getTyDrillAccidentIncubationDtoList();
        if (CollectionUtils.isNotEmpty(incubationDtoList)) {
            List<TyDrillAccidentIncubation> incubationList = new ArrayList<>();
            for (TyDrillAccidentIncubationDto tyDrillAccidentIncubationDto : incubationDtoList) {
                TyDrillAccidentIncubation incubation = new TyDrillAccidentIncubation();
                BeanUtils.copyProperties(tyDrillAccidentIncubationDto, incubation);
                incubation.setId(IdUtils.fastSimpleUUID());
                incubation.setTyDrillInfoDetailsId(one.getId());
                incubationList.add(incubation);

                saveIncubationDetails(incubation.getId(), "1", tyDrillAccidentIncubationDto.getTyDrillAccidentIncubationDetailsList1());
                saveIncubationDetails(incubation.getId(), "2", tyDrillAccidentIncubationDto.getTyDrillAccidentIncubationDetailsList1());
                saveIncubationDetails(incubation.getId(), "3", tyDrillAccidentIncubationDto.getTyDrillAccidentIncubationDetailsList1());
                saveIncubationDetails(incubation.getId(), "4", tyDrillAccidentIncubationDto.getTyDrillAccidentIncubationDetailsList1());
            }
            drillAccidentIncubationService.remove(Wrappers.<TyDrillAccidentIncubation>lambdaQuery()
                    .eq(TyDrillAccidentIncubation::getTyDrillInfoDetailsId, one.getId()));
            drillAccidentIncubationService.saveBatch(incubationList);
        }

        List<TyDrillSocialEmergencyResources> resourcesList = dto.getTyDrillSocialEmergencyResourcesList();
        if (CollectionUtils.isNotEmpty(resourcesList)) {
            for (TyDrillSocialEmergencyResources resources : resourcesList) {
                resources.setTyDrillInfoDetailsId(one.getId());
            }
            drillSocialEmergencyResourcesService.remove(Wrappers.<TyDrillSocialEmergencyResources>lambdaQuery().eq(TyDrillSocialEmergencyResources::getTyDrillInfoDetailsId, one.getId()));
            drillSocialEmergencyResourcesService.saveBatch(resourcesList);
        }
        List<TyDrillOrientationEnvironment> environmentList = dto.getTyDrillOrientationEnvironmentList();
        if (CollectionUtils.isNotEmpty(environmentList)) {
            for (TyDrillOrientationEnvironment environment : environmentList) {
                environment.setTyDrillInfoDetailsId(one.getId());
            }
            drillOrientationEnvironmentService.remove(Wrappers.<TyDrillOrientationEnvironment>lambdaQuery().eq(TyDrillOrientationEnvironment::getTyDrillInfoDetailsId, one.getId()));
            drillOrientationEnvironmentService.saveBatch(environmentList);
        }

        return AjaxResult.success();
    }

    private void saveIncubationDetails(String id, String type, List<TyDrillAccidentIncubationDetails> incubationDetailsList) {
        if (CollectionUtils.isNotEmpty(incubationDetailsList)) {
            for (TyDrillAccidentIncubationDetails tyDrillAccidentIncubationDetails : incubationDetailsList) {
                tyDrillAccidentIncubationDetails.setId(IdUtils.fastSimpleUUID());
                tyDrillAccidentIncubationDetails.setTyDrillAccidentIncubationId(id);
                tyDrillAccidentIncubationDetails.setType(type);
            }
            drillAccidentIncubationDetailsService.saveBatch(incubationDetailsList);
        }
    }
}
