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

import java.util.Date;

import javax.transaction.Transactional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.yihu.wlyy.entity.statistics.DoctorStatisticsComment;
import com.yihu.wlyy.entity.statistics.DoctorStatisticsConsult;
import com.yihu.wlyy.repository.doctor.DoctorStatisticsCommentDao;
import com.yihu.wlyy.repository.doctor.DoctorStatisticsConsultDao;
import com.yihu.wlyy.service.BaseService;
import com.yihu.wlyy.util.DateUtil;

/**
 * 医生统计业务处理类
 * @author George
 *
 */
@Component
@Transactional(rollbackOn = Exception.class)
public class DoctorStatisticsService extends BaseService {

	@Autowired
	private DoctorStatisticsConsultDao doctorStatisticsConsultDao;
	@Autowired
	private DoctorStatisticsCommentDao doctorStatisticsCommentDao;

	/**
	 * 添加评价星级统计数据
	 * @param doctor 医生标识
	 * @param type 类型：1三师咨询（指定咨询），2视频咨询
	 * @param star 星级
	 * @return
	 */
	synchronized public DoctorStatisticsComment addComment(String doctor, int type, int star) {
		if (star <= 0) {
			return null;
		}
		// 当前年月
		int yyyymm = Integer.parseInt(DateUtil.dateToStr(new Date(), DateUtil.YYYYMM));
		// 查询当前患者是否存在统计数据
		DoctorStatisticsComment dsc = doctorStatisticsCommentDao.findByDoctorDate(doctor, yyyymm);
		if (dsc == null) {
			dsc = new DoctorStatisticsComment();
			dsc.setDate(yyyymm);
			dsc.setDoctor(doctor);
			try {
				dsc = doctorStatisticsCommentDao.save(dsc);
			} catch (Exception e) {
				dsc = doctorStatisticsCommentDao.findByDoctorDate(doctor, yyyymm);
			}
		}
		if (dsc == null) {
			return null;
		}
		if (star == 1) {
			doctorStatisticsCommentDao.increaseOneStar(doctor, yyyymm);
		} else if (star == 2) {
			doctorStatisticsCommentDao.increaseTwoStar(doctor, yyyymm);
		} else if (star == 3) {
			doctorStatisticsCommentDao.increaseThreeStar(doctor, yyyymm);
		} else if (star == 4) {
			doctorStatisticsCommentDao.increaseFourStar(doctor, yyyymm);
		} else if (star == 5) {
			doctorStatisticsCommentDao.increaseFiveStar(doctor, yyyymm);
		}
		return dsc;
	}

	/**
	 * 添加医生咨询统计信息
	 * @param doctor 医生标识
	 * @param type 咨询类型：1三师咨询（指定咨询），2视频咨询
	 * @return
	 */
	synchronized public DoctorStatisticsConsult addConsult(String doctor, int type) {
		if (type <= 0) {
			return null;
		}
		// 当前年月
		int yyyymm = Integer.parseInt(DateUtil.dateToStr(new Date(), DateUtil.YYYYMM));
		// 查询当前患者是否存在统计数据
		DoctorStatisticsConsult dsc = doctorStatisticsConsultDao.findByDoctorDate(doctor, yyyymm);
		if (dsc == null) {
			dsc = new DoctorStatisticsConsult();
			dsc.setDate(yyyymm);
			dsc.setDoctor(doctor);
			try {
				dsc = doctorStatisticsConsultDao.save(dsc);
			} catch (Exception e) {
				dsc = doctorStatisticsConsultDao.findByDoctorDate(doctor, yyyymm);
			}
		}
		if (dsc == null) {
			return null;
		}
		if (type == 1) {
			// 添加三师咨询（指定咨询）数量
			doctorStatisticsConsultDao.increaseTeam(doctor, yyyymm);
		} else if (type == 2) {
			// 添加视频咨询数量
			doctorStatisticsConsultDao.increaseMovi(doctor, yyyymm);
		}
		return dsc;
	}

}
