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

import ca.uhn.hl7v2.model.v24.segment.DG1;
import ca.uhn.hl7v2.model.v24.segment.PV1;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.web.work.domain.Diagnosis;
import com.ruoyi.web.work.hl7.service.HL7DiagnosisService;
import com.ruoyi.web.work.mapper.DiagnosisMapper;
import lombok.extern.log4j.Log4j2;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

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

/**
 * 就诊结果信息
 */
@Service
@Log4j2
@Transactional
public class HL7DiagnosisServiceImpl extends ServiceImpl<DiagnosisMapper, Diagnosis> implements HL7DiagnosisService {

    /**
     * 保存诊断基本信息
     * @param
     * @param
     */
    @Override
    public void saveDiagnosisInfo(List<DG1> dg1All, Long visitId, PV1 pv1, Long PatId) {
        if (dg1All.isEmpty()) {
            return;
        }
        String department = pv1.getAssignedPatientLocation().getRoom().getValue();
        String physician = pv1.getAttendingDoctor(0).getFamilyName().getSurname().getValue();
        List<Diagnosis> entities = new ArrayList<>();
        dg1All.forEach(dg1 -> {
            String diagnosticName = dg1.getDiagnosisCodeDG1().getText().getValue();
            String diagnosisTime = dg1.getDiagnosisDateTime().getTimeOfAnEvent().getValue();
            String diagnosticType = dg1.getDiagnosisType().getValue();
            String diagnosticCode = dg1.getDiagnosisCodeDG1().getIdentifier().getValue();
            String diagnosticCategoryCode = dg1.getMajorDiagnosticCategory().getIdentifier().getValue();

            Diagnosis entity = new Diagnosis();
            entity.setPatId(PatId);
            entity.setVisitId(visitId);
            entity.setDepartmentName(department);
            entity.setDoctor(physician);
            entity.setDiagnosisTime(diagnosisTime);
            entity.setDiagnosticType(diagnosticType);
            entity.setDiagnosticCode(diagnosticCode);
            entity.setDiagnosticName(diagnosticName);
            entity.setDiagnosticCategoryCode(diagnosticCategoryCode);
            entities.add(entity);
        });
        //批量插入
        if (!entities.isEmpty()) {
            baseMapper.insertBatch(entities);
            log.info("{} 诊断信息批量保存成功.", entities.size());
        }
    }

    /**
     * 修改dg1诊断结果表 对医生科室信息进行补充
     * @param visitId 就诊id
     */
    @Override
    public void updateDg1InfoByVisitId(Long visitId,String department,String physician) {
        LambdaQueryWrapper<Diagnosis> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Diagnosis::getVisitId,visitId);
        List<Diagnosis> diagnosisEntities = baseMapper.selectList(wrapper);
        if (!CollectionUtils.isEmpty(diagnosisEntities)){
            diagnosisEntities.forEach(item -> {
                item.setDoctor(physician);
                item.setDepartmentName(department);
                baseMapper.updateById(item);
                log.info("诊断结果更新成功!");
            });
        }
    }
}
