/*******************************************************************************
 * Copyright (c) 2005, 2014 springside.github.io
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 *******************************************************************************/
package com.yihu.wlyy.service.common.account;

import org.apache.commons.lang3.StringUtils;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
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.utils.Clock;

import com.yihu.wlyy.entity.doctor.profile.Doctor;
import com.yihu.wlyy.entity.security.Token;
import com.yihu.wlyy.repository.doctor.DoctorDao;
import com.yihu.wlyy.util.EncodesUtil;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 医生基本信息类.
 *
 * @author George
 */
// Spring Service Bean的标识.
@Component
@Transactional(rollbackFor = Exception.class)
public class DoctorService extends TokenService {
	@Autowired
	private DoctorDao doctorDao;
	private Clock clock = Clock.DEFAULT;

	public Doctor findDoctorByCode(String code) {
		return doctorDao.findByCode(code);
	}

	public Doctor findDoctorByMobile(String mobile) {
		return doctorDao.findByMobile(mobile);
	}

	public JSONObject registerDoctor(Doctor doctor, String imei) throws Exception {
		EncodesUtil.entryptPassword(doctor);
		doctor.setCode(getCode());
		doctor.setCzrq(clock.getCurrentDate());

		Doctor temp = doctorDao.save(doctor);
		if (temp != null) {
			//生成登录tokey
			Token token = newToken(doctor.getCode(), imei, 2);
			if (token != null) {
				//生成登录信息成功
				JSONObject json = new JSONObject();
				json.put("uid", doctor.getCode());
				json.put("token", token.getToken());
				return json;
			}
		}
		return null;
	}

	/**
	 * 修改医生密码
	 */
	public void updateDoctorPwd(Doctor doctor) {
		//EncodesUtil.entryptPassword(doctor);
		doctor.setIsPasswordPrompt("1");
		doctorDao.save(doctor);
	}


	public List<Doctor> famousDoctorList(String name) {
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");

		if (StringUtils.isNoneEmpty(name)) {
			name="%"+name+"%";
			return doctorDao.doctorFamousDoctorList(name);
		}else{
			return doctorDao.doctorFamousDoctorList();
		}
	}

	public List<Doctor> getDoctorFamousDoctorList(String name) {

		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");

		if (StringUtils.isNoneEmpty(name)) {
			name="%"+name+"%";
			return doctorDao.doctorFamousDoctorList(name);
		}else{
			return doctorDao.doctorFamousDoctorList();
		}
	}

	public Doctor findbyIdCard(String idcard) {
		return doctorDao.findbyIdCard(idcard);
	}
}
