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

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.jeecg.modules.database.entity.*;
import org.jeecg.modules.database.mapper.ExperimentAnnealProcessMapper;
import org.jeecg.modules.database.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @Description: 退火过程
 * @Author: jeecg-boot
 * @Date: 2024-10-21
 * @Version: V1.0
 */
@Service
public class ExperimentAnnealProcessServiceImpl extends ServiceImpl<ExperimentAnnealProcessMapper, ExperimentAnnealProcess>
        implements IExperimentAnnealProcessService {

    @Autowired
    private IExperimentSampleInfoService         experimentSampleInfoService;
    @Autowired
    private IExperimentDeviationEquipmentService experimentDeviationEquipmentService;
    @Autowired
    private IExperimentDeviationConditionService experimentDeviationConditionService;

    @Override
    public List<ExperimentAnnealProcess> getByExperimentId(String experimentId) {
        List<ExperimentAnnealProcess> list = list(
                new LambdaQueryWrapper<ExperimentAnnealProcess>().eq(ExperimentAnnealProcess::getExperimentId, experimentId));
        list.forEach(this::assembleDetails);
        return list;
    }

    @Override
    public ExperimentAnnealProcess getById(Serializable id) {
        ExperimentAnnealProcess experimentAnnealProcess = super.getById(id);
        if (Objects.isNull(experimentAnnealProcess)) {
            return null;
        }
        assembleDetails(experimentAnnealProcess);
        return experimentAnnealProcess;
    }

    @Override
    public void assembleDetails(ExperimentAnnealProcess experimentAnnealProcess) {
        //sampleInfo
        String sampleInfoIds = experimentAnnealProcess.getSampleInfo();
        List<ExperimentSampleInfo> sampleInfoList;
        if (StringUtils.isNotBlank(sampleInfoIds)) {
            sampleInfoList = Arrays.stream(sampleInfoIds.split(",")).map(sampleInfoId -> experimentSampleInfoService.getById(sampleInfoId))
                    .filter(Objects::nonNull).collect(Collectors.toList());
            experimentAnnealProcess.setSampleInfoList(sampleInfoList);
        }
        //DeviationEquipment
        String deviationEquipmentIds = experimentAnnealProcess.getDeviationEquipment();
        if (StringUtils.isNotBlank(deviationEquipmentIds)) {
            List<ExperimentDeviationEquipment> deviationEquipmentList = Arrays.stream(sampleInfoIds.split(",")).map(
                            sampleInfoId -> experimentDeviationEquipmentService.getById(sampleInfoId))
                    .filter(Objects::nonNull).collect(Collectors.toList());
            experimentAnnealProcess.setDeviationEquipmentList(deviationEquipmentList);
        }
        //DeviationCondition
        String deviationConditionIds = experimentAnnealProcess.getDeviationCondition();
        if (StringUtils.isNotBlank(deviationConditionIds)) {
            List<ExperimentDeviationCondition> deviationEquipmentList = Arrays.stream(sampleInfoIds.split(",")).map(
                            sampleInfoId -> experimentDeviationConditionService.getById(sampleInfoId))
                    .filter(Objects::nonNull).collect(Collectors.toList());
            experimentAnnealProcess.setDeviationConditionList(deviationEquipmentList);
        }
    }

    @Override
    public void saveWithDetails(ExperimentAnnealProcess experimentAnnealProcess) {
        // 保存样品信息
        List<ExperimentSampleInfo> sampleInfoList = experimentAnnealProcess.getSampleInfoList();
        List<String> sampleInfoIds = new ArrayList<>();
        if (CollUtil.isNotEmpty(sampleInfoList)) {
            for (ExperimentSampleInfo sampleInfo : sampleInfoList) {
                experimentSampleInfoService.saveOrUpdate(sampleInfo);
                sampleInfoIds.add(sampleInfo.getId());
            }
        }
        experimentAnnealProcess.setSampleInfo(StringUtils.join(sampleInfoIds, ","));

        // 保存偏置条件信息
        List<ExperimentDeviationCondition> deviationConditionList = experimentAnnealProcess.getDeviationConditionList();
        List<String> deviationConditionIds = new ArrayList<>();
        if (CollUtil.isNotEmpty(deviationConditionList)) {
            for (ExperimentDeviationCondition sampleInfo : deviationConditionList) {
                experimentDeviationConditionService.saveOrUpdate(sampleInfo);
                deviationConditionIds.add(sampleInfo.getId());
            }
        }
        experimentAnnealProcess.setDeviationCondition(StringUtils.join(deviationConditionIds, ","));

        //加偏设备信息
        List<ExperimentDeviationEquipment> deviationEquipmentList = experimentAnnealProcess.getDeviationEquipmentList();
        List<String> deviationEquipmentIds = new ArrayList<>();
        if (CollUtil.isNotEmpty(deviationEquipmentList)) {
            for (ExperimentDeviationEquipment sampleInfo : deviationEquipmentList) {
                experimentDeviationEquipmentService.saveOrUpdate(sampleInfo);
                deviationEquipmentIds.add(sampleInfo.getId());
            }
        }
        experimentAnnealProcess.setDeviationEquipment(StringUtils.join(deviationEquipmentIds, ","));
        saveOrUpdate(experimentAnnealProcess);
    }

}
