package com.ekexiu.portal.service;

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

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.po.Consult;

@Path("/consult")
public class ConsultService {
	@Autowrie
	private ConsultDao consultDao;
	public ConsultDao getConsultDao() {
		return consultDao;
	}
	public void setConsultDao(ConsultDao consultDao) {
		this.consultDao = consultDao;
	}
	
	@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("/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);
			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);
			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("/qapro")
	public Consult queryPro(@JdbcConn Connection con, String consultId) throws SQLException {
		return this.consultDao.queryPro(con, consultId);
	}
	
	@Get
	@Path("/qacon")
	public Consult queryCon(@JdbcConn Connection con, String consultId) throws SQLException {
		return this.consultDao.queryCon(con, consultId);
	}
	
	@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> 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);
	}
	
}
