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.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
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.constant.ComponentSearchType;
import org.jeecg.modules.database.entity.*;
import org.jeecg.modules.database.mapper.ExperimentTestProcessMapper;
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-15
 * @Version: V1.0
 */
@Service
public class ExperimentTestProcessServiceImpl extends ServiceImpl<ExperimentTestProcessMapper, ExperimentTestProcess>
        implements IExperimentTestProcessService {
    @Autowired
    private ITestStandardsService        testStandardsService;
    @Autowired
    private IExperimentSampleInfoService experimentSampleInfoService;

    @Autowired
    private IComponentSearchService componentSearchService;


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

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

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

    @Override
    public void saveWithDetails(ExperimentTestProcess experimentTestProcess) {
        List<ExperimentSampleInfo> sampleInfoList = experimentTestProcess.getSampleInfoList();
        List<String> sampleInfoIds = new ArrayList<>();
        if (sampleInfoList != null && sampleInfoList.size() > 0) {
            for (ExperimentSampleInfo sampleInfo : sampleInfoList) {
                sampleInfo.setExperimentId(experimentTestProcess.getExperimentId());
                experimentSampleInfoService.saveOrUpdate(sampleInfo);
                sampleInfoIds.add(sampleInfo.getId());
            }
        }
        experimentTestProcess.setSampleInfo(StringUtils.join(sampleInfoIds, ","));
        saveOrUpdate(experimentTestProcess);

        //保存测试参数到搜索数据
        String testParameters = experimentTestProcess.getTestParameters();
        if (StringUtils.isNotBlank(testParameters)) {
            JSONArray jsonObject = JSON.parseArray(testParameters);
            if (jsonObject != null && !jsonObject.isEmpty()) {
                List<String> nameList = jsonObject.stream().map(v -> ((JSONObject) v).getString("name")).collect(Collectors.toList());
                nameList.forEach(name -> {
                    componentSearchService.saveComponentSearchByType(ComponentSearchType.TEST_PARAMS.getCode(), name);
                });
            }

        }
    }

    @Override
    public void copy(String oldId, String newExperimentId) {
        ExperimentTestProcess experiment = new ExperimentTestProcess();
        BeanUtil.copyProperties(getById(oldId), experiment);
        experiment.setExperimentId(newExperimentId);
        experiment.setId(null);
        experiment.setTestStartTime(null);
        experiment.setTestEndTime(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);
    }

}
