package com.ann.demo.service.impl;

import com.ann.demo.entity.normalEntity.*;
import com.ann.demo.repository.*;
import com.ann.demo.service.MedicalRecordHomepageService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @Author: LeiJiaXin
 * @Date: 2019/8/6 15:46
 */
@Service
public class MedicalRecordHomepageServiceImpl implements MedicalRecordHomepageService {

    static final Logger logger = LoggerFactory.getLogger(MedicalRecordHomepageServiceImpl.class);


    @Autowired
    DiagDetailRepository diagDetailRepository;

    @Autowired
    CommonTable1Repository commonTable1Repository;

    @Autowired
    CommonTableRepository commonTableRepository;

    @Autowired
    OperDetailRepository operDetailRepository;

    @Autowired
    ArchiveMasterRepository archiveMasterRepository;

    public boolean save(MedicalRecordHomepage medicalRecordHomepage) {
        //  查询主键  姓名简写 是否手术 是否过敏药物
        String masterId = medicalRecordHomepage.getCommonTable().getMasterId();
        try {
            List<OperDetail> operDetails = medicalRecordHomepage.getOperDetails();
            List<DiagDetail> diagDetails = medicalRecordHomepage.getDiagDetails();
            // 保存手术表
            if (operDetails != null) {
                for (OperDetail operDetail : operDetails) {
                    operDetail.setMasterId(masterId);
                    operDetailRepository.save(operDetail);
                }
            }
            // 诊断表
            if (diagDetails != null) {
                for (DiagDetail diagDetail : diagDetails) {
                    diagDetail.setMasterId(masterId);
                    diagDetailRepository.save(diagDetail);
                }
            }
            // 保存公共表
            commonTableRepository.save(medicalRecordHomepage.getCommonTable());
            // 保存公共表 1
            commonTable1Repository.save(medicalRecordHomepage.getCommonTable1());
            return true;
        } catch (Exception e) {
            logger.error("保存病案首页出错咯！错误信息:" + e.getMessage());
            return false;
        }
    }
}