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

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.alibaba.fastjson.JSON;
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(deviationEquipmentIds.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(deviationConditionIds.split(",")).map(
                    sampleInfoId -> experimentDeviationConditionService.getById(sampleInfoId)).filter(Objects::nonNull).collect(
                    Collectors.toList());
            experimentAnnealProcess.setDeviationConditionList(deviationEquipmentList);
        }
    }

    @Override
    public void saveWithDetails(ExperimentAnnealProcess experimentAnnealProcess, String type) {
        // 保存样品信息
        List<ExperimentSampleInfo> sampleInfoList = experimentAnnealProcess.getSampleInfoList();
        List<String> sampleInfoIds = new ArrayList<>();
        if (CollUtil.isNotEmpty(sampleInfoList)) {
            for (ExperimentSampleInfo sampleInfo : sampleInfoList) {
                // 20250502 - 邓春: 不修改前端 传过来的样品信息列表
                //sampleInfo.setExperimentId(experimentAnnealProcess.getExperimentId());
                // experimentSampleInfoService.saveOrUpdate(sampleInfo);
                sampleInfoIds.add(sampleInfo.getId());
            }
        }
        experimentAnnealProcess.setSampleInfo(StringUtils.join(sampleInfoIds, ","));

        // 保存偏置条件信息
        // 20250502 - 邓春: 不修改前端 传过来的退火设备大字段
  /*      String deviationCondition = experimentAnnealProcess.getDeviationCondition();
        if (StringUtils.isNotBlank(deviationCondition)) {
            List<ExperimentDeviationCondition> deviationConditionList = JSON.parseArray(deviationCondition,
                    ExperimentDeviationCondition.class);
            for (ExperimentDeviationCondition sampleInfo : deviationConditionList) {
                if (StringUtils.equals(type, "add")) {
                    sampleInfo.setId(null);
                }
                sampleInfo.setExperimentId(experimentAnnealProcess.getExperimentId());
                experimentDeviationConditionService.saveOrUpdate(sampleInfo);
            }
            experimentAnnealProcess.setDeviationCondition(JSON.toJSONString(deviationConditionList));
        }*/

        //加偏设备信息
        //20250502 - 邓春:不修改前端 传过来的退火设备大字段
/*        String deviationEquipment = experimentAnnealProcess.getDeviationEquipment();
        if (StringUtils.isNotBlank(deviationEquipment)) {
            List<ExperimentDeviationEquipment> deviationEquipmentList = JSON.parseArray(deviationEquipment,
                    ExperimentDeviationEquipment.class);
            for (ExperimentDeviationEquipment sampleInfo : deviationEquipmentList) {
                if (StringUtils.equals(type, "add")) {
                    sampleInfo.setId(null);
                }
                sampleInfo.setExperimentId(experimentAnnealProcess.getExperimentId());
                experimentDeviationEquipmentService.saveOrUpdate(sampleInfo);
            }
            experimentAnnealProcess.setDeviationEquipment(JSON.toJSONString(deviationEquipmentList));
        }*/
        saveOrUpdate(experimentAnnealProcess);
    }

    @Override
    public void copy(String oldId, String newExperimentId) {
        ExperimentAnnealProcess experiment = new ExperimentAnnealProcess();
        ExperimentAnnealProcess oldObj = getById(oldId);
        BeanUtil.copyProperties(oldObj, experiment);
        experiment.setId(null);
        experiment.setExperimentId(newExperimentId);
        experiment.setAnnealStartTime(null);
        experiment.setAnnealEndTime(null);
        experiment.setCreateTime(new Date());
        List<ExperimentSampleInfo> sampleInfoList = experimentSampleInfoService.getByExperimentId(newExperimentId);
        if (CollUtil.isNotEmpty(sampleInfoList)) {
            experiment.setSampleInfo(sampleInfoList.stream().map(v -> v.getId()).collect(Collectors.joining(",")));
        } else {
            experiment.setSampleInfo(null);
        }
        save(experiment);

        // 保存偏置条件信息
        String deviationCondition = oldObj.getDeviationCondition();
        if (StringUtils.isNotBlank(deviationCondition)) {
            List<ExperimentDeviationCondition> deviationConditionList = JSON.parseArray(deviationCondition,
                    ExperimentDeviationCondition.class);
            deviationConditionList.forEach(sampleInfo -> {
                sampleInfo.setId(null);
                sampleInfo.setExperimentId(experiment.getId());
                experimentDeviationConditionService.saveOrUpdate(sampleInfo);
            });
            experiment.setDeviationCondition(JSON.toJSONString(deviationConditionList));
        }

        //加偏设备信息
        String deviationEquipment = oldObj.getDeviationEquipment();
        if (StringUtils.isNotBlank(deviationEquipment)) {
            List<ExperimentDeviationEquipment> deviationEquipmentList = JSON.parseArray(deviationEquipment,
                    ExperimentDeviationEquipment.class);
            deviationEquipmentList.forEach(sampleInfo -> {
                sampleInfo.setId(null);
                sampleInfo.setExperimentId(experiment.getId());
                experimentDeviationEquipmentService.saveOrUpdate(sampleInfo);
            });
            experiment.setDeviationEquipment(JSON.toJSONString(deviationEquipmentList));
        }

    }

}
