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.annotation.Nullable;
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 org.jfw.util.PageQueryResult;
import org.jfw.util.StringUtil;

import com.ekexiu.portal.dao.OperationDao;
import com.ekexiu.portal.po.Operation;

@Path("/operation")
public class OperationService {
	@Autowrie
	private OperationDao operationDao;
	
	public OperationDao getOperationDao() {
		return operationDao;
	}
	public void setOperationDao(OperationDao operationDao) {
		this.operationDao = operationDao;
	}
	
	@Post
	@Path
	public String insert(@JdbcConn(true) Connection con, Operation operation) throws SQLException{
		String operationId = StringUtil.buildUUID();
		operation.setOperationId(operationId);
		this.operationDao.insert(con, operation);
		return operationId;
	}
	
	@Put
	@Path
	public void update(@JdbcConn(true) Connection con, @RequestBody Operation operation) throws SQLException{
		this.operationDao.update(con, operation);
	}
	
	@Get
	@Path("/qaOnGoingOper")
	public int queryOnGoingOper(@JdbcConn Connection con, @Nullable String professorId) throws SQLException {
		return this.operationDao.queryOnGoingOper(con, professorId);
	}
	
	@Get
	@Path("/{operationId}")
	public Operation query(@JdbcConn Connection con, @PathVar String operationId) throws SQLException{
		return this.operationDao.query(con, operationId);
	}
	
	@Get
	@Path("/qa")
	public List<Operation> queryAll(@JdbcConn Connection con) throws SQLException{
		return this.operationDao.query(con);
	}
	
//	@Get
//	@Path("/qadem")
//	public List<Operation> queryDem(@JdbcConn Connection con, String demandId) throws SQLException{
//		return this.operationDao.queryDem(con, demandId);
//	}
	
	@Get
	@Path("/pqpro")
	public PageQueryResult<Operation> quetyPro(@JdbcConn(false) Connection con, @Nullable String id, 
			@DefaultValue("3") int pageSize, @DefaultValue("1") int pageNo) throws SQLException {
		return this.operationDao.queryPro(con, id, pageSize, pageNo);
	}
	
	@Get
	@Path("/pqdem")
	public PageQueryResult<Operation> queryDemPage(@JdbcConn Connection con, String demandId, 
			@DefaultValue("10") int pageSize, @DefaultValue("1") int pageNo) throws SQLException{
		return this.operationDao.queryDemPage(con, demandId, pageSize, pageNo);
	}
	
	@Get
	@Path("/pqres")
	public PageQueryResult<Operation> queryResPage(@JdbcConn Connection con, String resourceId, 
			@DefaultValue("3") int pageSize, @DefaultValue("1") int pageNo) throws SQLException{
		return this.operationDao.queryResPage(con, resourceId, pageSize, pageNo);
	}
	
//	@Get
//	@Path("/qares")
//	public List<Operation> queryRes(@JdbcConn Connection con, String resourceId) throws SQLException{
//		return this.operationDao.queryRes(con, resourceId);
//	}
	
	@Post
	@Path("/detailDemand")
	public void updateDetailDemand(@JdbcConn(true) Connection con, String operationId, String detailDemand) throws SQLException{
		this.operationDao.updateDetailDemand(con, operationId, detailDemand);
	}
	
	@Post
	@Path("/operationStatus")
	public void updateOperationStatus(@JdbcConn(true) Connection con, String operationId, int operationStatus) throws SQLException{
		this.operationDao.updateOperationStatus(con, operationId, operationStatus);
	}
	
	@Post
	@Path("/deliverTime")
	public void updateDeliverTime(@JdbcConn(true) Connection con, String operationId, String deliverTime) throws SQLException{
		this.operationDao.updateDeliverTime(con, operationId, deliverTime);
	}
	
	@Post
	@Path("/dealTime")
	public void updateDealTime(@JdbcConn(true) Connection con, String operationId, String dealTime) throws SQLException{
		Operation operation = this.operationDao.query(con, operationId);
		if (null == operation.getDealTime()){
			this.operationDao.updateDealTime(con, operationId, dealTime);
		}
	}
	
	@Post
	@Path("/payMethod")
	public void updatePayMethod(@JdbcConn(true) Connection con, String operationId, String payMethod) throws SQLException{
		this.operationDao.updatePayMethod(con, operationId, payMethod);
	}
	
//	@Delete
//	@Path("/{operationId}")
//	public void delete(@JdbcConn(true) Connection con, String operationId) throws SQLException{
//		this.operationDao.delete(con, operationId);
//	}
	
	@Delete
	@Path("/deleteDem")
	public boolean deleteDem(@JdbcConn(true) Connection con, String operationId, String demandId) throws SQLException {
		if(1 == this.operationDao.queryOper(con, operationId)){
			this.operationDao.deleteDem(con, operationId, demandId);
			return true;
		}else{
			return false;
		}
	}
	
	@Get
	@Path("/pq")
	public PageQueryResult<Operation> queryPage(@JdbcConn(false) Connection con, @Nullable String professorName, 
			@Nullable String resourceName, @Nullable String firstTime, @Nullable String lastTime, @Nullable BigDecimal lowPrice, 
			@Nullable BigDecimal highPrice, @DefaultValue("10") int pageSize, @DefaultValue("1") int pageNo) throws SQLException {
		if(professorName!=null) professorName = "%"+professorName+"%";
		if(resourceName!=null) resourceName = "%"+resourceName+"%";
		if(firstTime!=null) firstTime = firstTime+"%";
		if(lastTime!=null) lastTime = lastTime+"%";
		return this.operationDao.queryPage(con, professorName, resourceName, firstTime, lastTime, lowPrice, highPrice, pageSize, pageNo);
	}
	
}
