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

import java.util.Date;

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 com.yihu.wlyy.entity.consult.Consult;
import com.yihu.wlyy.repository.consult.ConsultCommunicationDao;
import com.yihu.wlyy.repository.consult.ConsultCommunicationReplyDao;
import com.yihu.wlyy.repository.consult.ConsultDao;
import com.yihu.wlyy.repository.consult.ConsultMoViDao;
import com.yihu.wlyy.repository.consult.ConsultPublicDao;
import com.yihu.wlyy.repository.consult.ConsultPublicReplyDao;
import com.yihu.wlyy.repository.consult.ConsultTeamDao;
import com.yihu.wlyy.repository.doctor.DoctorDao;
import com.yihu.wlyy.repository.patient.PatientDao;
import com.yihu.wlyy.service.BaseService;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

@Component
@Transactional
public class ConsultService extends BaseService {

	@Autowired
	public PatientDao patientDao;
	@Autowired
	public DoctorDao doctorDao;
	@Autowired
	public ConsultDao consultDao;
	@Autowired
	public ConsultMoViDao consultMoViDao;
	@Autowired
	public ConsultTeamDao consultTeamDao;
	@Autowired
	public ConsultPublicDao consultPublicDao;
	@Autowired
	public ConsultPublicReplyDao consultPublicReplyDao;
	@Autowired
	public ConsultCommunicationDao consultCommunicationDao;
	@Autowired
	public ConsultCommunicationReplyDao consultCommunicationReplyDao;

	/**
	 * 添加一条咨询记录
	 * @param patient 患者信息
	 * @param title 咨询标题
	 * @param type 咨询类型：1三师咨询，2视频咨询，3图文咨询，4公共咨询，5病友圈
	 * @return
	 */
	public Consult addConsult(String patient, String title, String symptoms, String images, int type) {
		Consult consult = new Consult();
		consult.setCode(getCode());
		consult.setCzrq(new Date());
		consult.setDel("1");
		consult.setPatient(patient);
		consult.setTitle(title);
		consult.setSymptoms(symptoms);
		consult.setImages(images);
		consult.setType(type);
		return consultDao.save(consult);
	}

	/**
	 * 查询患者所有的咨询记录
	 * @param patient 患者标识
	 * @param id
	 * @param pagesize 分页大小
	 * @return
	 */
	public Page<Object> findConsultRecordByPatient(String patient, long id, int pagesize,String title) {
		if (id < 0) {
			id = 0;
		}
		if (pagesize <= 0) {
			pagesize = 10;
		}
		// 排序
		Sort sort = new Sort(Direction.DESC, "id");
		// 分页信息
		PageRequest pageRequest = new PageRequest(0, pagesize, sort);
		if(!StringUtils.isEmpty(title)){
			title="%"+title+"%";
			if (id > 0) {
				return consultDao.findByPatient(patient,title, id, pageRequest);
			} else {
				return consultDao.findByPatient(patient,title, pageRequest);
			}
		}else{
			if (id > 0) {
				return consultDao.findByPatient(patient, id, pageRequest);
			} else {
				return consultDao.findByPatient(patient, pageRequest);
			}
		}
	}

	public Consult findOneConsult(String consult) {
		return consultDao.findByCode(consult);
	}
}
