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

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

import org.apache.commons.lang3.StringUtils;
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 com.yihu.wlyy.entity.consult.Consult;
import com.yihu.wlyy.entity.consult.ConsultCommunication;
import com.yihu.wlyy.entity.consult.ConsultCommunicationReply;
import com.yihu.wlyy.entity.patient.Patient;
import com.yihu.wlyy.repository.consult.ConsultCommunicationDao;
import com.yihu.wlyy.repository.dict.DiseaseDao;

/**
 * 病友圈业务处理类
 * @author George
 *
 */
@Component
@Transactional(rollbackFor = Exception.class)
public class ConsultCommunicationService extends ConsultService {

	@Autowired
	private ConsultCommunicationDao consultCommunicationDao;
	@Autowired
	private DiseaseDao diseaseDao;

	/**
	 * 根据咨询标识查询咨询信息
	 * @param consult
	 * @return
	 */
	public ConsultCommunication findByConsult(String consult) {
		return consultCommunicationDao.findByConsult(consult);
	}

	/**
	 * 添加病友圈帖子
	 * @param movi 视频咨询对象
	 * @param patient 患者标识
	 * @return
	 */
	public ConsultCommunication addConsult(ConsultCommunication cc) {
		// 查询患者信息
		Patient patient = patientDao.findByCode(cc.getPatient());
		if (StringUtils.isNotEmpty(cc.getDisease()) && StringUtils.isEmpty(cc.getDiseaseName())) {
			cc.setDiseaseName(diseaseDao.findNameByCode(cc.getDisease()));
		}
		// 设置患者姓名
		cc.setName(patient.getName());
		// 设置患者头像
		cc.setPhoto(patient.getPhoto());
		// 保存咨询记录
		Consult consult = addConsult(patient.getCode(), cc.getTitle(), cc.getContent(), cc.getImages(), 5);
		cc.setConsult(consult.getCode());
		cc.setCzrq(new Date());
		cc.setDel("1");
		// 保存到数据库
		return consultCommunicationDao.save(cc);
	}

	/**
	 * 查询病友圈帖子列表
	 * @param patient 患者标识
	 * @param type 类型：0全部，1我的提问，2我的回答
	 * @param id
	 * @param pagesize 分页大小
	 * @return
	 */
	public Page<ConsultCommunication> findConsultByType(String patient, int type, String disease, long id, int pagesize) {
		if (pagesize <= 0) {
			pagesize = 10;
		}
		// 排序
		Sort sort = new Sort(Direction.DESC, "id");
		// 分页信息
		PageRequest pageRequest = new PageRequest(0, pagesize, sort);

		if (type == 0) {
			// 查询全部
			Map<String, SearchFilter> filters = new HashMap<String, SearchFilter>();
			if (id > 0) {
				filters.put("id", new SearchFilter("id", Operator.LT, id));
			}
			// 过滤疾病分类
			if (StringUtils.isNotEmpty(disease)) {
				filters.put("disease", new SearchFilter("disease", Operator.EQ, disease));
			}
			// 未作废
			filters.put("del", new SearchFilter("del", Operator.EQ, "1"));
			Specification<ConsultCommunication> spec = DynamicSpecifications.bySearchFilter(filters.values(), ConsultCommunication.class);
			return consultCommunicationDao.findAll(spec, pageRequest);
		} else if (type == 1) {
			// 我的提问
			Map<String, SearchFilter> filters = new HashMap<String, SearchFilter>();
			filters.put("patient", new SearchFilter("patient", Operator.EQ, patient));
			if (id > 0) {
				filters.put("id", new SearchFilter("id", Operator.LT, id));
			}
			// 过滤疾病分类
			if (StringUtils.isNotEmpty(disease)) {
				filters.put("disease", new SearchFilter("disease", Operator.EQ, disease));
			}
			// 未作废
			filters.put("del", new SearchFilter("del", Operator.EQ, "1"));
			Specification<ConsultCommunication> spec = DynamicSpecifications.bySearchFilter(filters.values(), ConsultCommunication.class);
			return consultCommunicationDao.findAll(spec, pageRequest);
		} else if (type == 2) {
			// 我的回答
			if (StringUtils.isNotEmpty(disease)) {
				if (id > 0) {
					return consultCommunicationDao.findPatienReplied(patient, patient, disease, id, pageRequest);
				} else {
					return consultCommunicationDao.findPatienReplied(patient, patient, disease, pageRequest);
				}
			} else {
				if (id > 0) {
					return consultCommunicationDao.findPatienReplied(patient, patient, id, pageRequest);
				} else {
					return consultCommunicationDao.findPatienReplied(patient, patient, pageRequest);
				}
			}

		}
		return null;
	}

	/**
	 * 回复病友圈
	 * @param reply
	 * @param imageCodes
	 * @return
	 */
	public String addReply(ConsultCommunicationReply reply) {
		// 查询患者信息
		Patient patient = patientDao.findByCode(reply.getPatient());
		// 设置患者姓名
		reply.setName(patient.getName());
		// 设置患者头像
		reply.setPhoto(patient.getPhoto());
		// 保存到数据
		ConsultCommunicationReply temp = consultCommunicationReplyDao.save(reply);
		if (temp != null) {
			// 回复数+1
			consultCommunicationDao.increaseAmount(reply.getConsult());
			// 查询咨询信息
			ConsultCommunication consult = consultCommunicationDao.findByConsult(reply.getConsult());
			if (!StringUtils.equals(consult.getPatient(), reply.getPatient())) {
				// 不是自己的帖子，未读数+1
				consultCommunicationDao.increaseUnread(reply.getConsult());
				return consult.getPatient();
			}
		}
		return patient.getCode();
	}

	/**
	 * 查询病友圈的回复列表
	 * @param consult 咨询标识
	 * @param pagesize 分页大小
	 * @return
	 */
	public Page<ConsultCommunicationReply> findReplyListByConsult(String consult, long id, int pagesize) {
		if (pagesize <= 0) {
			pagesize = 10;
		}
		// 排序
		Sort sort = new Sort(Direction.DESC, "id");
		// 分页信息
		PageRequest pageRequest = new PageRequest(0, pagesize, sort);
		// 查询全部
		Map<String, SearchFilter> filters = new HashMap<String, SearchFilter>();
		filters.put("consult", new SearchFilter("consult", Operator.EQ, consult));
		if (id > 0) {
			filters.put("id", new SearchFilter("id", Operator.LT, id));
		}
		// 未作废
		filters.put("del", new SearchFilter("del", Operator.EQ, "1"));
		Specification<ConsultCommunicationReply> spec = DynamicSpecifications.bySearchFilter(filters.values(), ConsultCommunicationReply.class);
		return consultCommunicationReplyDao.findAll(spec, pageRequest);
	}

	/**
	 * 清空未读数
	 * @param consult
	 */
	public void clearUnread(String consult) {
		consultCommunicationDao.clearUnRead(consult);
	}

}
