package digital.laboratory.platform.inspection.convert;

import digital.laboratory.platform.inspection.constant.TestRecordSampleDataConstant;
import digital.laboratory.platform.inspection.dto.SampleInspectDataDTO;
import digital.laboratory.platform.inspection.entity.TestRecordSampleData;
import digital.laboratory.platform.inspection.vo.TestRecordSampleDataVO;

import java.math.BigDecimal;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author ChenJiangBao
 * @version 1.0
 * @description: 检验数据转换器
 * @date 2025/3/25 17:08
 */
public class TestRecordSampleDataConverter {

    public static TestRecordSampleData voToEntity(TestRecordSampleDataVO vo) {
        if (vo == null) return null;
        TestRecordSampleData testRecordSampleData = new TestRecordSampleData();
        testRecordSampleData.setId(vo.getId());
        testRecordSampleData.setName(vo.getName());
        testRecordSampleData.setSampleNo(vo.getSampleNo());
        testRecordSampleData.setTestId(vo.getTestId());
        testRecordSampleData.setStdConcentration(vo.getStdConcentration());
        testRecordSampleData.setSampleConcentration(vo.getSampleConcentration());
        testRecordSampleData.setCompoundName(vo.getCompoundName());
        testRecordSampleData.setRtTimeWithinError(vo.getRtTimeWithinError());
        String rtTimeError = vo.getRtTimeError();
        if (rtTimeError != null && rtTimeError.equals(TestRecordSampleDataConstant.INVALID_VALUE_STR)) {
            testRecordSampleData.setRtTimeError(null);
        } else {
            testRecordSampleData.setRtTimeError(new BigDecimal(rtTimeError));
        }
        testRecordSampleData.setTargetRtTime(vo.getTargetRtTime());
        testRecordSampleData.setStdRtTime(vo.getStdRtTime());
        testRecordSampleData.setIsDetected(vo.getIsDetected().equals(TestRecordSampleDataConstant.IS) ? 1 : 0);
        testRecordSampleData.setSampleType(vo.getSampleType());
        testRecordSampleData.setDataJson(vo.getDataJson());
        testRecordSampleData.setDataResultJson(vo.getDataResultJson());
        testRecordSampleData.setStatus(vo.getStatus());
        testRecordSampleData.setCompoundCnName(vo.getCompoundCnName());
        testRecordSampleData.setWhetherCheckOut(vo.getWhetherCheckOut());
        return testRecordSampleData;
    }

    /**
     * dot 转 entity
     * @param dto
     * @return
     */
    public static TestRecordSampleData dtoToEntity(SampleInspectDataDTO dto) {
        if (dto == null) return null;
        TestRecordSampleData testRecordSampleData = new TestRecordSampleData();
        testRecordSampleData.setId(dto.getId());
        testRecordSampleData.setName(dto.getName());
        testRecordSampleData.setTestId(dto.getTestId());
        testRecordSampleData.setSampleConcentration(dto.getSampleConcentration());
        testRecordSampleData.setCompoundName(dto.getCompoundName());
        testRecordSampleData.setTargetRtTime(dto.getTargetRtTime());
        testRecordSampleData.setSampleType(dto.getSampleType());
        testRecordSampleData.setCompoundCnName(dto.getCompoundCnName());
        testRecordSampleData.setWhetherCheckOut(dto.getWhetherCheckOut());
        return testRecordSampleData;
    }

    /**
     * dto列表转实体列表
     * @param dtoList
     * @return
     */
    public static List<TestRecordSampleData> dtoToEntityList(List<SampleInspectDataDTO> dtoList) {
        if (dtoList == null) return Collections.emptyList();
        return dtoList.stream().map(TestRecordSampleDataConverter::dtoToEntity).collect(Collectors.toList());
    }
}
