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

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.ExperimentRadiationProcess;
import org.jeecg.modules.database.entity.ExperimentSampleInfo;
import org.jeecg.modules.database.mapper.ExperimentRadiationProcessMapper;
import org.jeecg.modules.database.service.IExperimentRadiationProcessService;
import org.jeecg.modules.database.service.IExperimentSampleInfoService;
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-15
 * @Version: V1.0
 */
@Service
public class ExperimentRadiationProcessServiceImpl extends ServiceImpl<ExperimentRadiationProcessMapper, ExperimentRadiationProcess>
        implements IExperimentRadiationProcessService {
    @Autowired
    private IExperimentSampleInfoService experimentSampleInfoService;

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

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

    @Override
    public void assembleDetails(ExperimentRadiationProcess experimentRadiationProcess) {
        String sampleInfoIds = experimentRadiationProcess.getSampleInfo();
        List<ExperimentSampleInfo> sampleInfoList;
        if (StringUtils.isNotBlank(sampleInfoIds)) {
            sampleInfoList = Arrays.stream(sampleInfoIds.split(",")).map(sampleInfoId -> experimentSampleInfoService.getById(sampleInfoId))
                    .filter(Objects::nonNull).collect(Collectors.toList());
            experimentRadiationProcess.setSampleInfoList(sampleInfoList);
        }
    }

    @Override
    public void saveWithDetails(ExperimentRadiationProcess experimentRadiationProcess) {

        List<ExperimentSampleInfo> sampleInfoList = experimentRadiationProcess.getSampleInfoList();
        List<String> sampleInfoIds = new ArrayList<>();
        if (sampleInfoList != null && sampleInfoList.size() > 0) {
            for (ExperimentSampleInfo sampleInfo : sampleInfoList) {
                experimentSampleInfoService.saveOrUpdate(sampleInfo);
                sampleInfoIds.add(sampleInfo.getId());
            }
        }

        experimentRadiationProcess.setSampleInfo(StringUtils.join(sampleInfoIds, ","));
        saveOrUpdate(experimentRadiationProcess);
    }
}
