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

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springside.modules.persistence.DynamicSpecifications;
import org.springside.modules.persistence.SearchFilter;
import org.springside.modules.persistence.SearchFilter.Operator;
import org.springside.modules.utils.Clock;

import com.yihu.wlyy.entity.consult.Consult;
import com.yihu.wlyy.entity.consult.ConsultMoVi;
import com.yihu.wlyy.entity.consult.ConsultMoViReferral;
import com.yihu.wlyy.entity.patient.Patient;
import com.yihu.wlyy.repository.consult.ConsultMoViDao;
import com.yihu.wlyy.repository.consult.ConsultMoViReferralDao;

/**
 * 电话视频咨询业务处理类
 * 
 * @author George
 */
@Component
@Transactional(rollbackFor = Exception.class)
public class ConsultMoviService extends ConsultService {

	private Clock clock = Clock.DEFAULT;

	@Autowired
	private ConsultMoViDao consultMoViDao;
	@Autowired
	private ConsultMoViReferralDao consultMoViReferralDao;

	/**
	 * 患者电话视频预约列表查询接口
	 * @param patient 患者标识
	 * @param page 查询页码，默认为1
	 * @param pagesize 每页显示数，默认为10
	 * @return
	 */
	public Page<ConsultMoVi> findByPatient(String patient, Date appoDate, int pagesize) {
		if (pagesize <= 0) {
			pagesize = 10;
		}
		// 排序
		Sort sort = new Sort(Direction.DESC, "appoDate");
		// 分页信息
		PageRequest pageRequest = new PageRequest(0, pagesize, sort);
		if(appoDate != null){
			return consultMoViDao.findByPatient(patient, appoDate, pageRequest);
		}else{
			return consultMoViDao.findByPatient(patient, pageRequest);
		}
	}

	/**
	 * 患者电话视频预约列表查询接口
	 * @param patient 患者标识
	 * @param type 1、电话预约，2、视频预约，3电话咨询记录，4视频咨询记录
	 * @param page 查询页码，默认为1
	 * @param pagesize 每页显示数，默认为10
	 * @return
	 */
	public Page<ConsultMoVi> findByPatient(String patient, int type, int page, int pagesize) {
		if (page <= 0) {
			page = 1;
		}
		if (pagesize <= 0) {
			pagesize = 10;
		}
		// 排序
		Sort sort = new Sort(Direction.DESC, "appoDate");
		// 分页信息
		PageRequest pageRequest = new PageRequest(page - 1, pagesize, sort);
		switch (type) {
		case 1:
		case 2:
			// 查询电话或视频预约列表
			return consultMoViDao.findByPatient(patient, type == 1 || type == 3 ? 1 : 2, pageRequest);
		case 3:
		case 4:
			// 查询电话或视频记录列表
			return consultMoViDao.findDoneByPatient(patient, type == 1 || type == 3 ? 1 : 2, pageRequest);
		}
		return null;
	}

	/**
	 * 电话视频预约列表查询接口
	 * @param type 1、电话预约，2、视频预约，3电话咨询记录，4视频咨询记录
	 * @param page 查询页码，默认为1
	 * @param pagesize 每页显示数，默认为10
	 * @return
	 */
	public Page<ConsultMoVi> findByDoctor(String uid, int type, Date appoDate, int pagesize) {
		if (pagesize <= 0) {
			pagesize = 10;
		}
		// 排序
		Sort sort = new Sort(Direction.ASC, "appoDate");
		// 分页信息
		PageRequest pageRequest = new PageRequest(0, pagesize, sort);
		switch (type) {
		case 1:
		case 2:
			// 查询电话或视频预约列表
			if(appoDate != null){
				return consultMoViDao.findByDoctor(uid, type == 1 || type == 3 ? 1 : 2, appoDate, pageRequest);
			}else{
				return consultMoViDao.findByDoctor(uid, type == 1 || type == 3 ? 1 : 2, pageRequest);
			}
		case 3:
		case 4:
			// 查询电话或视频记录列表
			if(appoDate != null){
				return consultMoViDao.findDoneByDoctor(uid, type == 1 || type == 3 ? 1 : 2, appoDate, pageRequest);
			}else{
				return consultMoViDao.findDoneByDoctor(uid, type == 1 || type == 3 ? 1 : 2, pageRequest);
			}
		}
		return null;
	}

	/**
	 * 根据code查询电话和视频详情
	 * @param consult 咨询标识
	 * @return
	 */
	public ConsultMoVi findByCode(String code) {
		return consultMoViDao.findByConsult(code);
	}

	/**
	 * 保存视频/电话预约信息
	 * @param model 视频/电话 预约 信息
	 * @return
	 * @throws Exception 
	 */
	public ConsultMoVi addMoVi(ConsultMoVi model, ConsultMoViReferral cmr) throws Exception {
		Consult consult = super.addConsult(model.getPatient(), null, model.getContent(), model.getImages(), 2);
		if (consult == null) {
			throw new Exception("movi save consult failed!");
		}
		model.setConsult(consult.getCode());
		model.setDel("1");
		model.setStatus(0);
		model.setCzrq(clock.getCurrentDate());
		ConsultMoVi temp = consultMoViDao.save(model);
		if (temp != null) {
//			 保存预约日志
//			DoctorSchedulingTimes dst = new DoctorSchedulingTimes();
//			dst.setAppoDate(model.getAppoDate());
//			dst.setCzrq(new Date());
//			dst.setDoctor(model.getDoctor());
//			dst.setScheduling(model.getScheduling());
//			dst.setTimes(times);
//			doctorSchedulingTimesDao.save(dst);
			// 保存转诊信息
			cmr.setConsult(model.getConsult());
			consultMoViReferralDao.save(cmr);
		}
		return temp;
	}

	/**
	 * 根据患者标志查找预约信息
	 * @param patientCode 患者标志
	 * @param page 页码
	 * @param size 页数
	 * @return 操作结果
	 */
	public Page<ConsultMoVi> findByPatient(Patient patient, int page, int size) {
		if (page <= 0) {
			page = 1;
		}
		if (size <= 0) {
			size = 10;
		}
		// 排序
		Sort sort = new Sort(Direction.DESC, "czrq");
		// 分页信息
		PageRequest pageRequest = new PageRequest(page - 1, size, sort);

		// 设置查询条件
		Map<String, SearchFilter> filters = new HashMap<String, SearchFilter>();
		filters.put("patient", new SearchFilter("patient", Operator.EQ, patient));
		filters.put("del", new SearchFilter("del", Operator.EQ, "1"));

		Specification<ConsultMoVi> spec = DynamicSpecifications.bySearchFilter(filters.values(), ConsultMoVi.class);

		return consultMoViDao.findAll(spec, pageRequest);
	}

	/**
	 * 结束视频咨询
	 * @param code
	 * @return
	 */
	public int finish(String code) {
		return consultMoViDao.updateStatus(2, code);
	}

	/**
	 * 视频转诊
	 * @param cmr
	 */
	public void transfer(ConsultMoVi cm, ConsultMoViReferral cmr, ConsultMoViReferral oldCmr) {
		// 更新预约信息
		consultMoViDao.save(cm);
		// 添加转诊记录
		consultMoViReferralDao.save(cmr);
		// 更新老的转诊信息
		consultMoViReferralDao.save(oldCmr);
	}

	/**
	 * 查询转诊信息
	 * @param doctor
	 * @param consult
	 * @return
	 */
	public ConsultMoViReferral findReferralByConsult(String doctor, String consult) {
		return consultMoViReferralDao.findDoctorReferral(doctor, consult);
	}
	
	/**
	 * 查询视频咨询最后处理人
	 * @param consult
	 * @return
	 */
	public String findToDoctor(String consult){
		return consultMoViReferralDao.findToDoctor(consult);
	}

}
