package com.ann.demo.service.impl;

import com.ann.demo.entity.interfaceEntity.ArchiveMaster;
import com.ann.demo.repository.ArchiveMasterRepository;
import com.ann.demo.service.ArchiveMasterService;
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/7/11 20:53
 */
@Service
public class ArchiveMasterServiceImpl implements ArchiveMasterService {

    @Autowired
    private ArchiveMasterRepository archiveMasterRepository;

    @Override
    public void save(ArchiveMaster archiveMaster) {
        if (archiveMaster.getSex() != null) {
            String sex = archiveMaster.getSex();
            switch (sex) {
                case "1":
                    sex = "男";
                    break;
                case "2":
                    sex = "女";
                    break;
                case "9":
                    sex = "未说明";
                    break;
                case "0":
                    sex = "未知";
                    break;
            }
            archiveMaster.setSex(sex);
        }
        archiveMasterRepository.save(archiveMaster);
    }

    @Override
    public String findArchiveMasterId(String patientId, String visitId, String inpNo) {
        return archiveMasterRepository.findArchiveMasterId(patientId, visitId, inpNo);
    }

    @Override
    public void updateArchiveMasterValid(String archiveMasterId) {
        archiveMasterRepository.updateArchiveMasterValid(archiveMasterId);
    }

    @Override
    public void transferDepartment(ArchiveMaster archiveMaster) {
        // 20200426 因为之前考虑不周，没有考虑到转科会存在多次
        String transferDept = archiveMasterRepository.findTransferDept(archiveMaster.getId());
        if(transferDept != null){
            transferDept =  transferDept+","+archiveMaster.getTransferDept();
        }else{
            transferDept = archiveMaster.getTransferDept();
        }
        archiveMasterRepository.transferDepartment(archiveMaster.getId(),  transferDept);
    }

    @Override
    public void discharge(ArchiveMaster archiveMaster) {
        ArchiveMaster master = archiveMasterRepository.findByIdAndIsValid(archiveMaster.getId(), 0);
        if (master != null) {
            master.setDeptName(archiveMaster.getDeptName());
            master.setDischargeDateTime(archiveMaster.getDischargeDateTime());
            if(master.getArchiveState().equals("在院")){
                master.setArchiveState("归档中");
            }
            archiveMasterRepository.save(master);
        }
    }

    @Override
    public ArchiveMaster findByInpNoAndVisitIdAndIsValid(String inpNo, String visitId) throws Exception {
        return archiveMasterRepository.findByInpNoAndVisitIdAndIsValid(inpNo, visitId, 0);
    }

    @Override
    public ArchiveMaster findByPatientIdAndVisitIdAndIsValid(String patientId, String visitId) throws Exception {
        return archiveMasterRepository.findByPatientIdAndVisitIdAndIsValid(patientId, visitId, 0);
    }

    @Override
    public List<ArchiveMaster> findAllArchiveMaster() {
        return archiveMasterRepository.findAllArchiveMaster();
    }

    @Override
    public boolean updateCheckedDatetime(String id) {
        if (archiveMasterRepository.updateCheckedDatetime(id) > 0)
            return true;
        return false;
    }

    @Override
    public boolean updateArchiveMaster(ArchiveMaster archiveMaster) {
        String sex = archiveMaster.getSex();
        if (archiveMaster.getSex() != null && archiveMaster.getName() != null ) {
            switch (sex) {
                case "1":
                    sex = "男";
                    break;
                case "2":
                    sex = "女";
                    break;
                case "0":
                    sex = "未知";
                    break;
                case "9":
                    sex = "未说明";
                    break;
            }
            if (archiveMasterRepository.updateArchiveMaster(archiveMaster.getId(),sex,archiveMaster.getName()) > 0){
                return true;
            }
        }
        return false;
    }
}
