package com.yihu.wlyy.service.app.account;

import com.yihu.wlyy.entity.address.City;
import com.yihu.wlyy.entity.address.Province;
import com.yihu.wlyy.entity.address.Street;
import com.yihu.wlyy.entity.address.Town;
import com.yihu.wlyy.entity.demographic.DemographicInfo;
import com.yihu.wlyy.entity.patient.Patient;
import com.yihu.wlyy.entity.patient.SocialSecurityInfo;
import com.yihu.wlyy.entity.patient.invitation.PatientInvitationLog;
import com.yihu.wlyy.repository.address.CityDao;
import com.yihu.wlyy.repository.address.ProvinceDao;
import com.yihu.wlyy.repository.address.StreetDao;
import com.yihu.wlyy.repository.address.TownDao;
import com.yihu.wlyy.repository.demographic.TblBasicDao;
import com.yihu.wlyy.repository.patient.PatientDao;
import com.yihu.wlyy.repository.patient.PatientInvitationLogDao;
import com.yihu.wlyy.repository.patient.SocialSecurityInfoDao;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.yihu.wlyy.service.BaseService;
import com.yihu.wlyy.util.EncodesUtil;

/**
 * 患者基本信息类.
 * 
 * @author George
 */
@Component
@Transactional
public class PatientInfoService extends BaseService {

	@Autowired
	private PatientDao patientDao;
	@Autowired
	private ProvinceDao provinceDao;
	@Autowired
	private CityDao cityDao;
	@Autowired
	private TownDao townDao;
	@Autowired
	private StreetDao streetDao;
	@Autowired
	private PatientInvitationLogDao patientInviteLogDao;
	@Autowired
	private TblBasicDao tblBasicDao;
	@Autowired
	private SocialSecurityInfoDao socialSecurityInfoDao;
	/**
	 * 根据患者标识查询患者信息
	 * @param code
	 * @return
	 */
	public Patient findByCode(String code) {
		return patientDao.findByCode(code);
	}

	/**
	 * 根据患者身份证号码查询患者信息
	 * @param idcard
	 * @return
	 */
	public Patient findByIdcard(String idcard) {
		return patientDao.findByIdcard(idcard);
	}

	// 根據病情等级获取患者信息
	public Iterable<Patient> findInfoByLevel(int level) {
		return patientDao.findInfoByLevel(level);
	}

	/**
	 * 更新患者基本信息
	 * @param patient
	 */
	public Patient updatePatient(Patient patient) {
		if (StringUtils.isNotEmpty(patient.getStreet())) {
			Street street = streetDao.findByCode(patient.getStreet());
			patient.setStreetName(street.getName());
		}else{
			patient.setStreetName("");
		}
		Province province = provinceDao.findByCode(patient.getProvince());
		if (province != null) {
			patient.setProvinceName(province.getName());
		}else{
			patient.setProvinceName("");
		}
		City city = cityDao.findByCode(patient.getCity());
		if (city != null) {
			patient.setCityName(city.getName());
		}else{
			patient.setCityName("");
		}
		Town town = townDao.findByCode(patient.getTown());
		if (town != null) {
			patient.setTownName(town.getName());
		}else{
			patient.setTownName("");
		}
		return patientDao.save(patient);
	}

	public Patient updateUser(Patient patient) {
		return patientDao.save(patient);
	}

	/**
	 * 修改患者密码
	 * @param patient
	 */
	public void updatePatientPwd(Patient patient) {
		EncodesUtil.entryptPassword(patient);
		patientDao.save(patient);
	}

	public Patient invilogCode(String invilogCode) {
		PatientInvitationLog patientInviteLog=patientInviteLogDao.findOne(invilogCode);
		String patientCode=patientInviteLog.getInvitePatientCode();
		Patient patient=patientDao.findByCode(patientCode);
		return patient;
	}

	public boolean validatePatient(String name, String idCard, String ssc) {
		DemographicInfo demographicInfo =tblBasicDao.findByIdCard(idCard);
		SocialSecurityInfo socialSecurityInfo = socialSecurityInfoDao.findBySfzh18(idCard);
		if(demographicInfo !=null&& socialSecurityInfo !=null){
			String nameTemp= demographicInfo.getName();

		}
		return false;
	}

	public void checkMobile(String mobile) throws Exception{
		Patient p=patientDao.findByMobile(mobile);
		if(p!=null){
			throw  new Exception("手机号已经被注册");
		}
	}
}
