package com.ekexiu.portal.service;

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

import org.jfw.apt.annotation.Autowrie;
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.apt.web.annotation.param.PathVar;
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.Tidings;
import com.ekexiu.portal.pojo.EditProfessor;

@Path("/tidings")
public class TidingsService {
	@Autowrie
	private TidingsDao tidingsDao;
	@Autowrie
	private ConsultDao consultDao;
	@Autowrie
	private ProfessorDao professorDao;
	@Autowrie
	private ImageService imageService;
	
	public TidingsDao getTidingsDao() {
		return tidingsDao;
	}
	public void setTidingsDao(TidingsDao tidingsDao) {
		this.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;
	}
	
	@Post
	@Path
	public String insert(@JdbcConn(true) Connection con, Tidings tidings) throws SQLException {
		String tidingsId = StringUtil.buildUUID();
		tidings.setTidingsId(tidingsId);
		this.tidingsDao.insert(con, tidings);
		this.tidingsDao.updateReadStatus(con, tidings.getSenderId(), tidings.getConsultId());
		String consultId = tidings.getConsultId();
		this.consultDao.updateRevoveryTime(con, consultId);
		this.consultDao.updateReadStatus(con, consultId);
		return tidingsId;
	}
	
	@Get
	@Path("/qaNotReadTidings")
	public int queryNotReadTidings(@JdbcConn Connection con, String consultId, String senderId) throws SQLException {
		return this.tidingsDao.queryNotReadTidings(con, consultId, senderId);
	}
	
	@Get
	@Path("/qaNewReply")
	public Tidings queryNewReply(@JdbcConn Connection con, String consultantId) throws SQLException {
		Tidings tidings = this.tidingsDao.queryNewReply(con, consultantId);
		if(tidings != null){
			EditProfessor professor = this.professorDao.queryBaseInfo(con, tidings.getSenderId());
			professor.setHasHeadImage(this.imageService.hasProfessorImage(tidings.getSenderId()));
			tidings.setProfessor(professor);
		}
		return tidings;
	}
	
	@Get
	@Path("/qaLastRevovery")
	public Tidings queryLastRevovery(@JdbcConn Connection con, String consultId, String senderId) throws SQLException {
		return this.tidingsDao.queryLastRevovery(con, consultId, senderId);
	}
	
	@Get
	@Path("/{tidingsId}")
	public Tidings query(@JdbcConn Connection con, @PathVar String tidingsId) throws SQLException {
		return this.tidingsDao.query(con, tidingsId);
	}
	
	@Get
	@Path("/qacon")
	public List<Tidings> queryCon(@JdbcConn Connection con, String consultId) throws SQLException {
		this.consultDao.updateReadStatus(con, consultId);
		return this.tidingsDao.queryCon(con, consultId);
	}
}
