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

import ca.uhn.hl7v2.HL7Exception;
import ca.uhn.hl7v2.model.v24.message.ADT_A01;
import ca.uhn.hl7v2.model.v24.segment.NK1;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.web.work.domain.NextOfKin;
import com.ruoyi.web.work.hl7.service.HL7NextOfKinService;
import com.ruoyi.web.work.mapper.NextOfKinMapper;
import lombok.extern.log4j.Log4j2;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * 亲属关系
 */
@Service
@Log4j2
@Transactional
public class HL7NextOfKinServiceImpl extends ServiceImpl<NextOfKinMapper, NextOfKin> implements HL7NextOfKinService {

    /**
     * 保存亲属关系表数据
     * @param patId
     * @param a01
     */
    @Override
    public void saveKinShipInfo(Long patId, ADT_A01 a01) throws HL7Exception {
        List<NK1> nk1All = a01.getNK1All();
        if (CollectionUtils.isEmpty(nk1All)) {
            log.info("没有亲属关系数据需要保存。");
            return;
        }
        for (NK1 nk1 : nk1All) {
            String name = nk1.getNKName(0).getFamilyName().getSurname().getValue();
            String kinship = nk1.getRelationship().getIdentifier().getValue();
            String address = nk1.getAddress(0).getStreetAddress().getStreetOrMailingAddress().getValue();
            String phone = nk1.getPhoneNumber(0).getPhoneNumber().getValue();
            String workName = nk1.getNextOfKinAssociatedPartiesJobTitle().getValue();
            String workCode = nk1.getNextOfKinAssociatedPartiesJobCodeClass().getJcc1_JobCode().getValue();
            NextOfKin entity = new NextOfKin();
            entity.setPatId(patId);
            entity.setName(name);
            entity.setAddress(address);
            entity.setKinship(kinship);
            entity.setPhone(phone);
            entity.setWorkCode(workCode);
            entity.setWorkName(workName);
            baseMapper.insert(entity);
        }
        log.info("保存亲属关系数据完成!");
    }
}
