package com.ekexiu.portal.service;

import java.io.IOException;
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.param.JdbcConn;
import org.jfw.apt.web.annotation.param.PathVar;
import org.jfw.util.PageQueryResult;
import org.jfw.util.StringUtil;

import com.ekexiu.portal.dao.ImageDao;
import com.ekexiu.portal.dao.OperationDao;
import com.ekexiu.portal.dao.ResourceDao;
import com.ekexiu.portal.po.Operation;
import com.ekexiu.portal.po.Resource;

@Path("/resource")
public class ResourceService {
	@Autowrie
	private ImageDao imageDao;
	@Autowrie
	private ResourceDao resourceDao;
	@Autowrie
	private ImagesService imagesService;
	@Autowrie
	private OperationDao operationDao;

	public OperationDao getOperationDao() {
		return operationDao;
	}

	public void setOperationDao(OperationDao operationDao) {
		this.operationDao = operationDao;
	}

	public ImageDao getImageDao() {
		return imageDao;
	}

	public void setImageDao(ImageDao imageDao) {
		this.imageDao = imageDao;
	}

	public ResourceDao getResourceDao() {
		return resourceDao;
	}

	public void setResourceDao(ResourceDao resourceDao) {
		this.resourceDao = resourceDao;
	}

	public ImagesService getImagesService() {
		return imagesService;
	}

	public void setImagesService(ImagesService imagesService) {
		this.imagesService = imagesService;
	}

	@Post
	@Path
	public String insert(@JdbcConn(true) Connection con, Resource resource, @Nullable String fn) {
		String resourceId = StringUtil.buildUUID();
		try {
			if(null != fn){
				this.imagesService.insert(con, resourceId, fn);
			}
			resource.setResourceId(resourceId);
			this.resourceDao.insert(con, resource);
		} catch (SQLException | IOException e) {
			try {
				con.rollback();
			} catch (SQLException e1) {
				e1.printStackTrace();
			}
			e.printStackTrace();
		}
		return resourceId;
	}

	@Post
	@Path("/updateResource")
	public void update(@JdbcConn(true) Connection con, Resource resource, @Nullable String fn) throws SQLException, IOException {
		if(null != fn){
			this.imagesService.insert(con, resource.getResourceId(), fn);
		}
		this.resourceDao.update(con, resource);
	}

	@Get
	@Path("/{resourceId}")
	public Resource query(@JdbcConn Connection con, @PathVar String resourceId) throws SQLException {
		return this.resourceDao.query(con, resourceId);
	}
	
	@Get
	@Path("/pqall")
	public PageQueryResult<Resource> queryPageAll(@JdbcConn Connection con, @DefaultValue("10") int pageSize, @DefaultValue("1") int pageNo) throws SQLException {
		return this.resourceDao.queryPageAll(con, pageSize, pageNo);
	}

//	@Get
//	@Path("/qa")
//	public List<Resource> queryAll(@JdbcConn Connection con) throws SQLException {
//		return this.resourceDao.query(con);
//	}
	
	@Get
	@Path("/qapro")
	public List<Resource> queryPro(@JdbcConn Connection con, String professorId) throws SQLException {
		return this.resourceDao.queryPro(con, professorId);
	}
	
	@Post
	@Path("/subject")
	public void updateSubject(@JdbcConn(true) Connection con, String resourceId, String subject) throws SQLException {
		this.resourceDao.updateSubject(con, resourceId, subject);
	}

	@Post
	@Path("/industry")
	public void updateIndustry(@JdbcConn(true) Connection con, String resourceId, String industry) throws SQLException {
		this.resourceDao.updateIndustry(con, resourceId, industry);
	}

	@Post
	@Path("/descp")
	public void updateDescp(@JdbcConn(true) Connection con, String resourceId, String descp) throws SQLException {
		this.resourceDao.updateDescp(con, resourceId, descp);
	}
	
	@Post
	@Path("/hopePayMethod")
	public void updateHopePayMethod(@JdbcConn(true) Connection con, String resourceId, String hopePayMethod) throws SQLException {
		this.resourceDao.updateHopePayMethod(con, resourceId, hopePayMethod);
	}
	
	@Delete
	@Path("/{resourceId}")
	public boolean delete(@JdbcConn(true) Connection con, @PathVar String resourceId) throws SQLException {
		List<Operation> operations = this.operationDao.queryResourceId(con, resourceId);
		if(operations.isEmpty()){
			this.imageDao.deleteRes(con, resourceId);
			this.resourceDao.delete(con, resourceId);
			return true;
		}else{
			return false;
		}
	}
	
	@Get
	@Path("/pqRes")
	public PageQueryResult<Resource> queryPageRes(@JdbcConn(false) Connection con, @Nullable String key, @Nullable String subject, @Nullable String industry, @Nullable String address, @DefaultValue("10") int pageSize, @DefaultValue("1") int pageNo) throws SQLException {
		if(key!=null) key="%"+key+"%";
		if(subject!=null) subject ="%"+subject+"%";
		if(industry!=null)industry="%"+industry+"%";
		return this.resourceDao.queryPageRes(con, key, subject, industry, address, pageSize, pageNo);
	}
	
	@Get
	@Path("/pq")
	public PageQueryResult<Resource> queryPage(@JdbcConn(false) Connection con, @Nullable String key, @Nullable String subject, @Nullable String industry, @Nullable String address, @DefaultValue("10") int pageSize, @DefaultValue("1") int pageNo) throws SQLException {
		if(key!=null) key="%"+key+"%";
		if(subject!=null) subject ="%"+subject+"%";
		if(industry!=null)industry="%"+industry+"%";
		return this.resourceDao.queryPage(con, key, subject, industry, address, pageSize, pageNo);
	}
	
}
