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.operate.Put;
import org.jfw.apt.web.annotation.param.JdbcConn;
import org.jfw.apt.web.annotation.param.PathVar;
import org.jfw.apt.web.annotation.param.RequestBody;

import com.ekexiu.portal.dao.ResearchAreaDao;
import com.ekexiu.portal.dao.ResearchAreaLogDao;
import com.ekexiu.portal.po.ResearchArea;
import com.ekexiu.portal.po.ResearchAreaLog;

@Path("/researchArea")
public class ResearchAreaService {

	@Autowrie
	private ResearchAreaDao researchAreaDao;
	@Autowrie
	private ResearchAreaLogDao researchAreaLogDao;

	public ResearchAreaDao getResearchAreaDao() {
		return researchAreaDao;
	}

	public void setResearchAreaDao(ResearchAreaDao researchAreaDao) {
		this.researchAreaDao = researchAreaDao;
	}

	public ResearchAreaLogDao getResearchAreaLogDao() {
		return researchAreaLogDao;
	}

	public void setResearchAreaLogDao(ResearchAreaLogDao researchAreaLogDao) {
		this.researchAreaLogDao = researchAreaLogDao;
	}

	@Get
	@Path("/{professorId}")
	public List<ResearchArea> query(@JdbcConn Connection con,@PathVar String professorId) throws SQLException {
		return this.researchAreaDao.query(con, professorId);
	}

	@Post
	@Path("/agree")
	public void agree(@JdbcConn(true) Connection con, String targetId, String targetCaption, String opId) throws SQLException {

		if (this.researchAreaDao.inc(con, targetId, targetCaption) > 0) {
			ResearchAreaLog log = new ResearchAreaLog();
			log.setCaption(targetCaption);
			log.setOpreteProfessorId(opId);
			log.setProfessorId(targetId);
			this.researchAreaLogDao.insert(con, log);
		}
	}

	@Post
	@Path("/unAgree")
	public void unAgree(@JdbcConn(true) Connection con, String targetId, String targetCaption, String opId) throws SQLException {

		if (this.researchAreaDao.dec(con, targetId, targetCaption) > 0) {
			this.researchAreaLogDao.delete(con, targetId, opId, targetCaption);
		}
	}

	
	@Delete
	@Path("/{professorId}")
	public void delete(@JdbcConn(true) Connection con,@PathVar String professorId) throws SQLException{
		this.researchAreaDao.delete(con, professorId);
		this.researchAreaLogDao.delete(con, professorId);
	}
	@Put
	@Path
	public void saveResearchArea(@JdbcConn(true) Connection con, @RequestBody List<ResearchArea> areas) throws SQLException {
		String professorId = areas.get(0).getProfessorId();
		List<ResearchArea> olds = this.researchAreaDao.query(con, professorId);
		int sortNum = 1;
		for (ResearchArea area : areas) {
			area.setSortNum(sortNum++);
			boolean found = false;
			for (ResearchArea old : olds) {
				if (area.getCaption().equals(old.getCaption())) {
					found = true;
					this.researchAreaDao.updateDescp(con, professorId, area.getCaption(), area.getDescp(), area.getSortNum());
					break;
				}
			}
			if (!found) {
				area.setProfessorId(professorId);
				this.researchAreaDao.insert(con, area);
			}
		}
		for (ResearchArea old : olds) {
			boolean found = false;
			for (ResearchArea area : areas) {
				if (area.getCaption().equals(old.getCaption())) {
					found = true;
					break;
				}
			}
			if (!found) {
				this.researchAreaDao.delete(con, professorId, old.getCaption());
				this.researchAreaLogDao.delete(con, professorId, old.getCaption());
			}
		}
	}
}
