package com.ekexiu.portal.service;

import java.math.BigDecimal;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

import org.jfw.apt.annotation.Autowrie;
import org.jfw.apt.annotation.DefaultValue;
import org.jfw.apt.web.annotation.Path;
import org.jfw.apt.web.annotation.operate.Get;
import org.jfw.apt.web.annotation.operate.Post;
import org.jfw.apt.web.annotation.param.JdbcConn;
import org.jfw.util.PageQueryResult;
import org.jfw.util.StringUtil;

import com.ekexiu.portal.dao.ConsultDao;
import com.ekexiu.portal.dao.ProfessorDao;
import com.ekexiu.portal.dao.TidingsDao;
import com.ekexiu.portal.po.Consult;
import com.ekexiu.portal.pojo.EditProfessor;

@Path("/consult")
public class ConsultService {
	@Autowrie
	private ConsultDao consultDao;
	@Autowrie
	private ProfessorDao professorDao;
	@Autowrie
	private ImageService imageService;
	@Autowrie
	private TidingsDao tidingsDao;
	
	public ConsultDao getConsultDao() {
		return consultDao;
	}
	public void setConsultDao(ConsultDao consultDao) {
		this.consultDao = consultDao;
	}
	public ProfessorDao getProfessorDao() {
		return professorDao;
	}
	public void setProfessorDao(ProfessorDao professorDao) {
		this.professorDao = professorDao;
	}
	public ImageService getImageService() {
		return imageService;
	}
	public void setImageService(ImageService imageService) {
		this.imageService = imageService;
	}
	public TidingsDao getTidingsDao() {
		return tidingsDao;
	}
	public void setTidingsDao(TidingsDao tidingsDao) {
		this.tidingsDao = tidingsDao;
	}
	
	@Post
	@Path
	public String insert(@JdbcConn(true) Connection con, Consult consult) throws SQLException {
		String consultId = StringUtil.buildUUID();
		consult.setConsultId(consultId);
		this.consultDao.insert(con, consult);
		return consultId;
	}
	
	@Post
	@Path("/readStatus")
	public void updateReadStatus(@JdbcConn(true) Connection con, String consultId) throws SQLException {
		this.consultDao.updateReadStatus(con, consultId);
	}
	
	@Post
	@Path("/finishTime")
	public boolean updateFinishTime(@JdbcConn(true) Connection con, String consultId, int consultStatus) throws SQLException {
		Consult consult = this.consultDao.query(con, consultId);
		if(null == consult.getFinishTime()){
			this.consultDao.updateFinishTime(con, consultId,consultStatus);
			int finishConsult = this.consultDao.queryReceiveConsult(con, consult.getProfessorId());
			this.professorDao.updateConsultCount(con, consult.getProfessorId(), finishConsult);
			return true;
		}
		return false;
	}
	
	@Post
	@Path("/assess")
	public boolean updateAssess(@JdbcConn(true) Connection con, String consultId, int assessStatus, int assessStar, String assessContant) throws SQLException {
		Consult consult = this.consultDao.query(con, consultId);
		if(0 == consult.getAssessStatus()){
			this.consultDao.updateAssess(con, consultId, assessStatus, assessStar, assessContant);
			BigDecimal avgStar = this.consultDao.queryStarLevel(con, consult.getProfessorId());
			this.professorDao.updateStarLevel(con, consult.getProfessorId(), avgStar);
			return true;
		}
		return false;
	}
	
	@Post
	@Path("/thanks")
	public boolean updateThanks(@JdbcConn(true) Connection con, String consultId, int thanksStatus, BigDecimal thanksMoney) throws SQLException {
		Consult consult = this.consultDao.query(con, consultId);
		if(0 == consult.getThanksStatus()){
			this.consultDao.updateThanks(con, consultId, thanksStatus, thanksMoney);
			return true;
		}
		return false;
	}
	
	@Get
	@Path("/pqAssessHis")
	public PageQueryResult<Consult> queryAssessHis(@JdbcConn Connection con, String professorId,@DefaultValue("5") int pageSize,@DefaultValue("1") int pageNo) throws SQLException {
		PageQueryResult<Consult> queryResult = this.consultDao.queryAssessHis(con, professorId, pageSize, pageNo);
		List<Consult> consults = queryResult.getData();
		if(consults != null){
			for (Consult consult : consults) {
				EditProfessor professor = this.professorDao.queryBaseInfo(con, consult.getConsultantId());
				professor.setHasHeadImage(this.imageService.hasProfessorImage(consult.getConsultantId()));
				consult.setProfessor(professor);
			}
			queryResult.setData(consults);
		}
		return queryResult;
	}
	
	@Get
	@Path("/qaReadStatus")
	public int queryReadStatus(@JdbcConn Connection con, String professorId) throws SQLException {
		int notReadConsult = this.consultDao.queryReadStatus(con, professorId);
		int notReadReply = this.consultDao.queryNotReadReply(con, professorId);
		return notReadConsult+notReadReply;
	}
	
	@Get
	@Path("/qaReceiveConsult")
	public int queryReceiveConsult(@JdbcConn Connection con, String professorId) throws SQLException {
		return this.consultDao.queryReceiveConsult(con, professorId);
	}
	
	@Get
	@Path("/qaNewConsult")
	public Consult queryNewConsult(@JdbcConn Connection con, String professorId) throws SQLException {
		Consult consult = this.consultDao.queryNewConsult(con, professorId);
		if(consult != null){
			EditProfessor professor = this.professorDao.queryBaseInfo(con, consult.getConsultantId());
			professor.setHasHeadImage(this.imageService.hasProfessorImage(consult.getConsultantId()));
			consult.setProfessor(professor);
		}
		return consult;
	}
	
	@Get
	@Path("/qapro")
	public Consult queryPro(@JdbcConn Connection con, String consultId) throws SQLException {
		Consult consult = this.consultDao.queryPro(con, consultId);
		this.tidingsDao.updateReadStatus(con, consult.getProfessorId(), consultId);
		return consult;
	}
	
	@Get
	@Path("/qacon")
	public Consult queryCon(@JdbcConn Connection con, String consultId) throws SQLException {
		Consult consult = this.consultDao.queryCon(con, consultId);
		this.tidingsDao.updateReadStatus(con, consult.getConsultantId(), consultId);
		return consult;
	}
	
	@Get
	@Path("/pqPro")
	public PageQueryResult<Consult> queryPageProBase(@JdbcConn Connection con, String professorId, 
			@DefaultValue("0") int status, @DefaultValue("0") int timeType, @DefaultValue("0") int sortType, 
			@DefaultValue("5") int pageSize, @DefaultValue("1") int pageNo) throws SQLException {
		PageQueryResult<Consult> queryResult = this.consultDao.queryPageProBase(con, professorId, status, timeType, sortType, pageSize, pageNo);
		List<Consult> consults = queryResult.getData();
		if(consults != null){
			for (Consult consult : consults) {
				int hasHeadImage = this.imageService.hasProfessorImage(consult.getConsultantId());
				consult.getProfessor().setHasHeadImage(hasHeadImage);
			}
			queryResult.setData(consults);
		}
		return queryResult;
	}	
	
	@Get
	@Path("/pqpro")
	public PageQueryResult<Consult> queryPagePro(@JdbcConn Connection con, String professorId, 
			@DefaultValue("0") int status, @DefaultValue("0") int timeType, @DefaultValue("0") int sortType, 
			@DefaultValue("5") int pageSize, @DefaultValue("1") int pageNo) throws SQLException {
		return this.consultDao.queryPagePro(con, professorId, status, timeType, sortType, pageSize, pageNo);
	}	
	
	@Get
	@Path("/pqCon")
	public PageQueryResult<Consult> queryPageConBase(@JdbcConn Connection con, String consultantId, 
			@DefaultValue("0") int status, @DefaultValue("0") int timeType, @DefaultValue("0") int sortType, 
			@DefaultValue("5") int pageSize, @DefaultValue("1") int pageNo) throws SQLException {
		PageQueryResult<Consult> queryResult = this.consultDao.queryPageConBase(con, consultantId, status, timeType, sortType, pageSize, pageNo);
		List<Consult> consults = queryResult.getData();
		if(consults != null){
			for (Consult consult : consults) {
				int hasHeadImage = this.imageService.hasProfessorImage(consult.getProfessorId());
				consult.getProfessor().setHasHeadImage(hasHeadImage);
			}
			queryResult.setData(consults);
		}
		return queryResult;
	}
	
	@Get
	@Path("/pqcon")
	public PageQueryResult<Consult> queryPageCon(@JdbcConn Connection con, String consultantId, 
			@DefaultValue("0") int status, @DefaultValue("0") int timeType, @DefaultValue("0") int sortType, 
			@DefaultValue("5") int pageSize, @DefaultValue("1") int pageNo) throws SQLException {
		return this.consultDao.queryPageCon(con, consultantId, status, timeType, sortType, pageSize, pageNo);
	}
	
	@Get
	@Path("/pq")
	public PageQueryResult<Consult> queryPageBase(@JdbcConn Connection con, String professorId, 
			@DefaultValue("0") int consultOrNeed, @DefaultValue("0") int status, @DefaultValue("1") int timeType, 
			@DefaultValue("10") int pageSize, @DefaultValue("1") int pageNo) throws SQLException {
		PageQueryResult<Consult> queryResult = this.consultDao.queryPageBase(con, professorId, consultOrNeed, status, timeType, pageSize, pageNo);
		List<Consult> consults = queryResult.getData();
		if(consults != null){
			for (Consult consult : consults) {
				int hasHeadImage = this.imageService.hasProfessorImage(consult.getProfessorId());
				consult.getProfessor().setHasHeadImage(hasHeadImage);
			}
			queryResult.setData(consults);
		}
		return queryResult;
	}
	
}
