package com.ann.demo.service.impl;

import com.alibaba.fastjson.JSON;
import com.ann.demo.entity.interfaceEntity.ArchiveMaster;
import com.ann.demo.entity.normalEntity.*;
import com.ann.demo.repository.MedicalRecordHomepageCacheRepository;
import com.ann.demo.service.ArchiveMasterService;
import com.ann.demo.service.MedicalRecordHomepageCacheService;
import com.ann.demo.service.MedicalRecordHomepageService;
import com.ann.demo.utils.PinYinUtils;
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;
import java.util.Objects;

/**l
 * @Author: LeiJiaXin
 * @Date: 2019/9/26 9:27
 */
@Service
public class MedicalRecordHomepageCacheServiceImpl implements MedicalRecordHomepageCacheService {

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

    @Autowired
    MedicalRecordHomepageCacheRepository medicalRecordHomepageCacheRepository;

    @Autowired
    MedicalRecordHomepageService medicalRecordHomepageService;

    @Autowired
    ArchiveMasterService archiveMasterService;


    @Override
    public boolean save(String masterId) throws Exception{
        MedicalRecordHomepageCache medicalRecordHomepageCache = medicalRecordHomepageCacheRepository.findByMasterId(masterId);
        if (medicalRecordHomepageCache != null) {
            MedicalRecordHomepage m = JSON.parseObject(medicalRecordHomepageCache.getContent(), MedicalRecordHomepage.class);
            if (medicalRecordHomepageService.save(m)) {
                return true;
            }
        }
        return false;
    }

    @Override
    public boolean save(MedicalRecordHomepageCache medicalRecordHomepageCache) throws Exception{
        try {
            MedicalRecordHomepageCache mrh = medicalRecordHomepageCacheRepository.findByMasterId(medicalRecordHomepageCache.getMasterId());
            if (mrh != null) {
                medicalRecordHomepageCache.setRemark("已更新");
            }
            medicalRecordHomepageCacheRepository.save(medicalRecordHomepageCache);
            return true;
        } catch (Exception e) {
            return false;
        }
    }

    @Override
    @Transactional
    public void delete(String masterId) throws Exception{
        medicalRecordHomepageCacheRepository.updateIsValid(masterId);
    }

    @Override
    public MedicalRecordHomepageCache handleMedicalRecordHomepageCache(String masterId,MedicalRecordHomepage medicalRecordHomepage) throws Exception{
        //  查询主键  姓名简写 是否手术 是否过敏药物
        MedicalRecordHomepageCache medicalRecordHomepageCache = null;
        CommonTable commonTable = medicalRecordHomepage.getCommonTable();
        if (commonTable != null) {
            List<OperDetail> operDetails = medicalRecordHomepage.getOperDetails();
            List<DiagDetail> diagDetails = medicalRecordHomepage.getDiagDetails();
            CommonTable1 commonTable1 = medicalRecordHomepage.getCommonTable1();

            if (commonTable1 != null) {
                // 处理公共表
                commonTable.setMasterId(masterId);
                if (operDetails != null) {
                    if (operDetails.size() > 0) {
                        commonTable.setIsOper("有");
                    } else {
                        commonTable.setIsOper("没有");
                    }
                } else {
                    commonTable.setIsOper("没有");
                }
                if (commonTable.getAAC003() != null) {
                    commonTable.setNameCym(PinYinUtils.getPingYin(commonTable.getAAC003()));
                }
                String akc023 = commonTable.getAKC023();
                if (akc023 != null) {
                    if (akc023.indexOf("岁") != -1) {
                        commonTable.setAge(Integer.parseInt(akc023.substring(0, akc023.indexOf("岁"))));
                    } else if (akc023.indexOf("月") != -1 && akc023.indexOf("岁") == -1) {
                        commonTable.setAge_month(Integer.parseInt(akc023.substring(0, akc023.indexOf("月"))));
                    } else if (akc023.indexOf("天") != -1 && akc023.indexOf("岁") == -1 && akc023.indexOf("月") == -1) {
                        commonTable.setAge_day(Integer.parseInt(akc023.substring(0, akc023.indexOf("天"))));
                    }
                }

                // 处理公共表1
                if (commonTable1.getYZY046() != null) {
                    commonTable1.setIs_medicine("是");
                } else {
                    commonTable1.setIs_medicine("否");
                }
                commonTable1.setMasterId(masterId);


                // 保存手术表
                if (operDetails != null) {
                    for (OperDetail operDetail : operDetails) {
                        operDetail.setMasterId(masterId);
                    }
                }
                // 诊断表
                if (diagDetails != null) {
                    for (DiagDetail diagDetail : diagDetails) {
                        diagDetail.setMasterId(masterId);
                        // 20200528 取出院诊断的主要诊断
                        if(Objects.equals(diagDetail.getYZY203(),"3") && Objects.equals(diagDetail.getYZY201(),1) ){
                            commonTable.setMainDiagCode(diagDetail.getAKC196());
                            commonTable.setMainDiagName(diagDetail.getAKC185());
                        }
                    }
                }

                medicalRecordHomepage.setCommonTable(commonTable);
                medicalRecordHomepage.setCommonTable1(commonTable1);
                medicalRecordHomepage.setDiagDetails(diagDetails);
                medicalRecordHomepage.setOperDetails(operDetails);

                medicalRecordHomepageCache = new MedicalRecordHomepageCache(masterId, JSON.toJSONString(medicalRecordHomepage));
                return medicalRecordHomepageCache;
            }
        }
        return null;
    }
}
