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.Delete;
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 com.ekexiu.portal.dao.ProfessorDao;
import com.ekexiu.portal.dao.ResearchAreaDao;
import com.ekexiu.portal.dao.ResourceDao;
import com.ekexiu.portal.dao.WatchDao;
import com.ekexiu.portal.po.Resource;
import com.ekexiu.portal.po.Watch;
import com.ekexiu.portal.pojo.EditProfessor;

@Path("/watch")
public class WatchService {
	@Autowrie
	private WatchDao watchDao;
	@Autowrie
	private ProfessorDao professorDao;
	@Autowrie
	private ResourceDao resourceDao;
	@Autowrie
	private ImageService imageService;
	@Autowrie
	private ResearchAreaDao researchAreaDao;
	
	public WatchDao getWatchDao() {
		return watchDao;
	}
	public void setWatchDao(WatchDao watchDao) {
		this.watchDao = watchDao;
	}
	public ProfessorDao getProfessorDao() {
		return professorDao;
	}
	public void setProfessorDao(ProfessorDao professorDao) {
		this.professorDao = professorDao;
	}
	public ResourceDao getResourceDao() {
		return resourceDao;
	}
	public void setResourceDao(ResourceDao resourceDao) {
		this.resourceDao = resourceDao;
	}
	public ImageService getImageService() {
		return imageService;
	}
	public void setImageService(ImageService imageService) {
		this.imageService = imageService;
	}
	public ResearchAreaDao getResearchAreaDao() {
		return researchAreaDao;
	}
	public void setResearchAreaDao(ResearchAreaDao researchAreaDao) {
		this.researchAreaDao = researchAreaDao;
	}
	
	@Post
	@Path
	public String insert(@JdbcConn(true) Connection con, Watch watch) throws SQLException{
		this.watchDao.insert(con, watch);
		return watch.getWatchObject();
	}
	
	@Delete
	@Path("/delete")
	public void delete(@JdbcConn(true) Connection con, String professorId, String watchObject) throws SQLException{
		this.watchDao.delete(con, professorId, watchObject);
	}
	
	@Get
	@Path("/qaPro")
	public List<Watch> queryPro(@JdbcConn Connection con, String professorId, Integer watchType) throws SQLException{
		List<Watch> watchs = this.watchDao.queryPro(con, professorId, watchType);
		if(1 == watchType){
			for (Watch watch : watchs) {
				EditProfessor professor = this.professorDao.queryBaseInfo(con, watch.getWatchObject());
				professor.setHasHeadImage(this.imageService.hasProfessorImage(watch.getWatchObject()));
				professor.setResearchAreas(this.researchAreaDao.query(con, watch.getWatchObject()));
				professor.setResources(this.resourceDao.queryList(con, watch.getWatchObject()));
				watch.setProfessor(professor);
			}
		}else if(2 == watchType){
			for (Watch watch : watchs) {
				Resource resource = this.resourceDao.queryBaseInfo(con, watch.getWatchObject());
				resource.setProfessor(this.professorDao.queryBaseInfo(con, resource.getProfessorId()));
				watch.setResource(resource);
			}
		}
		return watchs;
	}
	
	@Get
	@Path("/qaWatch")
	public List<Watch> queryWatch(@JdbcConn Connection con, String watchObject) throws SQLException{
		List<Watch> watchs = this.watchDao.queryWatch(con, watchObject);
		for (Watch watch : watchs) {
			EditProfessor professor = this.professorDao.queryBaseInfo(con, watch.getProfessorId());
			professor.setHasHeadImage(this.imageService.hasProfessorImage(watch.getProfessorId()));
			professor.setResearchAreas(this.researchAreaDao.query(con, watch.getProfessorId()));
			professor.setResources(this.resourceDao.queryList(con, watch.getProfessorId()));
			watch.setProfessor(professor);
		}
		return watchs;
	}
	
}
