package com.ruoyi.web.work.hl7.service.impl;

import ca.uhn.hl7v2.model.v24.datatype.FT;
import ca.uhn.hl7v2.model.v24.group.ORU_R01_OBSERVATION;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.web.work.domain.MicrobialReport;
import com.ruoyi.web.work.hl7.service.HL7MicrobialReportService;
import com.ruoyi.web.work.mapper.MicrobialReportMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * 微生物补充信息NTE
 */
@Service
@Transactional
public class HL7MicrobialReportServiceImpl extends ServiceImpl<MicrobialReportMapper, MicrobialReport> implements HL7MicrobialReportService {

    @Override
    public void saveNotesForOBX(ORU_R01_OBSERVATION r01Observation, Long obrId) {
        FT[] comments = r01Observation.getNTE().getComment();
        List<String> commentValues = new ArrayList<>(); // 创建一个列表来保存注释值
        for (FT comment : comments) {
            String commentValue  = comment.getValue();
            if (commentValue != null && !commentValue.isEmpty()) {
                commentValues.add(commentValue);
            }
        }
        String methods = (commentValues.size() > 0) ? commentValues.get(0) : null;
        String mechanism = (commentValues.size() > 1) ? commentValues.get(1) : null;
        String sensitivityResults = (commentValues.size() > 2) ? commentValues.get(2) : null;
        String antibioticCode = (commentValues.size() > 3) ? commentValues.get(3) : null;
        String antibioticName = (commentValues.size() > 4) ? commentValues.get(4) : null;
        String testResult = (commentValues.size() > 5) ? commentValues.get(5) : null;
        String colonyCount = (commentValues.size() > 6) ? commentValues.get(6) : null;
        String remarks = (commentValues.size() > 7) ? commentValues.get(7) : null;

        MicrobialReport entity = new MicrobialReport();
        entity.setMethods(methods);
        entity.setAntibioticName(antibioticName);
        entity.setMechanism(mechanism);
        entity.setRemarks(remarks);
        entity.setTestResult(testResult);
        entity.setColonyCount(colonyCount);
        entity.setSensitivityResults(sensitivityResults);
        entity.setAntibioticCode(antibioticCode);
        entity.setObrId(obrId);
        baseMapper.insert(entity);
    }
}
