package com.xinelu.manage.service.patientinfo.impl;

import com.xinelu.common.utils.SecurityUtils;
import com.xinelu.common.utils.bean.BeanUtils;
import com.xinelu.manage.domain.patientinfo.PatientInfo;
import com.xinelu.manage.domain.patientvisitrecord.PatientVisitRecord;
import com.xinelu.manage.domain.residentinfo.ResidentInfo;
import com.xinelu.manage.dto.patientinfo.PatientInfoDto;
import com.xinelu.manage.dto.patientvisitrecord.PatientVisitRecordDto;
import com.xinelu.manage.mapper.patientinfo.PatientInfoMapper;
import com.xinelu.manage.mapper.residentinfo.ResidentInfoMapper;
import com.xinelu.manage.service.patientinfo.IPatientInfoService;
import com.xinelu.manage.service.patientvisitrecord.IPatientVisitRecordService;
import java.time.LocalDateTime;
import java.util.List;
import javax.annotation.Resource;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 患者信息Service业务层处理
 *
 * @author haown
 * @date 2024-02-26
 */
@Service
public class PatientInfoServiceImpl implements IPatientInfoService {

    @Resource
    private PatientInfoMapper patientInfoMapper;
	@Resource
	private ResidentInfoMapper residentInfoMapper;
	@Resource
	private IPatientVisitRecordService patientVisitRecordService;

    /**
     * 查询患者信息
     *
     * @param id 患者信息主键
     * @return 患者信息
     */
    @Override
    public PatientInfo selectPatientInfoById(Long id) {
        return patientInfoMapper.selectPatientInfoById(id);
    }

    /**
     * 查询患者信息列表
     *
     * @param patientInfo 患者信息
     * @return 患者信息
     */
    @Override
    public List<PatientInfo> selectPatientInfoList(PatientInfoDto patientInfo) {
        return patientInfoMapper.selectPatientInfoList(patientInfo);
    }

    /**
     * 新增患者信息
     *
     * @param patientInfo 患者信息
     * @return 结果
     */
    @Override
    @Transactional
    public PatientInfo insertPatientInfo(PatientInfo patientInfo) {
	    patientInfo.setCreateTime(LocalDateTime.now());
	    patientInfo.setCreateBy(SecurityUtils.getLoginUser().getUser().getNickName());
	    patientInfo.setDelFlag(0);
		// 根据身份证号查询是否有该居民
	    ResidentInfo residentQuery = new ResidentInfo();
		residentQuery.setCardNo(patientInfo.getCardNo());
	    List<ResidentInfo> residentList = residentInfoMapper.selectResidentInfoList(residentQuery);

		ResidentInfo residentInfo = new ResidentInfo();
	    BeanUtils.copyBeanProp(residentInfo, patientInfo);
		if (CollectionUtils.isEmpty(residentList)) {
			// 新增居民
			residentInfoMapper.insertResidentInfo(residentInfo);
			patientInfo.setResidentId(residentInfo.getId());
		} else {
			// 修改居民信息
			residentInfo.setId(residentList.get(0).getId());
			residentInfoMapper.updateResidentInfo(residentInfo);
			patientInfo.setResidentId(residentList.get(0).getId());
		}
        patientInfoMapper.insertPatientInfo(patientInfo);
		return patientInfo;
    }

    /**
     * 修改患者信息
     *
     * @param patientInfo 患者信息
     * @return 结果
     */
    @Override
    public PatientInfo updatePatientInfo(PatientInfo patientInfo) {
	    ResidentInfo residentInfo = new ResidentInfo();
		if (patientInfo.getResidentId() != null) {
			residentInfo = residentInfoMapper.selectResidentInfoById(patientInfo.getResidentId());
			BeanUtils.copyBeanProp(residentInfo, patientInfo);
			residentInfo.setId(patientInfo.getResidentId());
			residentInfoMapper.updateResidentInfo(residentInfo);
		} else {
			// 根据身份证号查询是否有该居民
			ResidentInfo residentQuery = new ResidentInfo();
			residentQuery.setCardNo(patientInfo.getCardNo());
			List<ResidentInfo> residentList = residentInfoMapper.selectResidentInfoList(residentQuery);
			if (CollectionUtils.isNotEmpty(residentList)) {
				Long residentId = residentList.get(0).getId();
				residentInfo = residentList.get(0);
				BeanUtils.copyBeanProp(residentInfo, patientInfo);
				residentInfo.setId(residentId);
				residentInfoMapper.updateResidentInfo(residentInfo);
			} else {
				BeanUtils.copyBeanProp(residentInfo, patientInfo);
				residentInfo.setId(null);
				residentInfoMapper.insertResidentInfo(residentInfo);
			}
			patientInfo.setResidentId(residentInfo.getId());
		}
	    patientInfo.setUpdateBy(SecurityUtils.getLoginUser().getUser().getNickName());
	    patientInfo.setUpdateTime(LocalDateTime.now());
	    patientInfo.setDelFlag(0);
		patientInfoMapper.updatePatientInfo(patientInfo);
		return patientInfo;
    }

    /**
     * 批量删除患者信息
     *
     * @param ids 需要删除的患者信息主键
     * @return 结果
     */
    @Override
    @Transactional
    public int deletePatientInfoByIds(Long[] ids) {
		int flag = 0;
	    for (Long id : ids) {
			PatientInfo patientInfo = patientInfoMapper.selectPatientInfoById(id);
		    patientVisitRecordService.deletePatientVisitRecordById(patientInfo.getPatientVisitRecordId());
		    // 根据患者最新一条就诊记录修改患者状态
		    PatientVisitRecordDto patientVisitRecordDto = new PatientVisitRecordDto();
		    patientVisitRecordDto.setPatientId(id);
		    PatientVisitRecord patientVisitRecord = patientVisitRecordService.getLastRecord(patientInfo.getHospitalAgencyId(), id);
		    if (ObjectUtils.isEmpty(patientVisitRecord)) {
			    setVisitInfoNull(patientInfo);
		    } else {
			    BeanUtils.copyBeanProp(patientInfo, patientVisitRecord);
			    patientInfo.setId(id);
			    patientInfo.setPatientVisitRecordId(patientVisitRecord.getId());
			    // 设置patientType
			    patientVisitRecordService.setPatientType(patientInfo, patientVisitRecord);
		    }
		    flag += patientInfoMapper.updatePatientInfo(patientInfo);
		}
        return flag;
    }

	/**
	 * 设置患者就诊信息为空
	 * @param patientInfo 患者信息
	 */
	private void setVisitInfoNull(PatientInfo patientInfo){
		patientInfo.setVisitDate(null);
		patientInfo.setPatientType(null);
		patientInfo.setPatientVisitRecordId(null);
		patientInfo.setVisitMethod(null);
		patientInfo.setAdmissionTime(null);
		patientInfo.setDischargeTime(null);
		patientInfo.setAttendingPhysicianId(null);
		patientInfo.setAttendingPhysicianName(null);
		patientInfo.setResponsibleNurse(null);
		patientInfo.setMainDiagnosis(null);
		patientInfo.setVisitSerialNumber(null);
		patientInfo.setInHospitalNumber(null);
	}
    /**
     * 删除患者信息
     *
     * @param id 患者信息主键
     * @return 结果
     */
    @Override
    public int deletePatientInfoById(Long id) {
        return patientInfoMapper.deletePatientInfoById(id);
    }
}
