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.annotation.DefaultValue;
import org.jfw.apt.annotation.Nullable;
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.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.basepo.LogicDict;
import com.ekexiu.portal.dao.DataDictDao;
import com.ekexiu.portal.po.DataDict;
import com.ekexiu.portal.pojo.DataDictCode;

@Path("/dataDict")
public class DataDictService {
	
	@Autowrie
	private DataDictDao dataDictDao;
	
	private int defautLimitQuerySize = 5;
	
	
	public DataDictDao getDataDictDao() {
		return dataDictDao;
	}

	public void setDataDictDao(DataDictDao dataDictDao) {
		this.dataDictDao = dataDictDao;
	}

	public int getDefautLimitQuerySize() {
		return defautLimitQuerySize;
	}

	public void setDefautLimitQuerySize(int defautLimitQuerySize) {
		if(defautLimitQuerySize>0)
		   this.defautLimitQuerySize = defautLimitQuerySize;
	}

	@Post
	@Path
	public void insert(@JdbcConn(true) Connection con,String dictCode,String code,@Nullable String parentCode,String caption,String descp ) throws SQLException
	{
		DataDict  dd = new DataDict();
		dd.setDescp(descp);
		dd.setCaption(caption);
		dd.setCode(code);
		dd.setDictCode(dictCode);
		dd.setParentCode(parentCode);
		dataDictDao.insert(con, dd);
	}
	
	@Put
	@Path
	public void update(@JdbcConn(true) Connection con,@RequestBody DataDict dict) throws SQLException{
		this.dataDictDao.update(con, dict);
	}
	
	@Get
	@Path("/{dictCode}")
	List<DataDict> query(@JdbcConn(false) Connection con,@PathVar String dictCode) throws SQLException{
		 return this.dataDictDao.query(con, dictCode);
	}
	
	@Get
	@Path("/qa/{dictCode}")
	public List<DataDictCode> queryDictCode(@JdbcConn(false) Connection con, @PathVar String dictCode) throws SQLException{
		return this.dataDictDao.queryDictCode(con, dictCode);
	}
	
	@Get
	@Path("/{dictCode}/{code}")
	public DataDict query(@JdbcConn(false) Connection con,@PathVar String dictCode,@PathVar String code)throws SQLException{
		return this.dataDictDao.query(con, dictCode, code);	
	}
	
	@Get
	@Path("/lq")
	List<LogicDict> limitQuery(@JdbcConn(false) Connection con,String dictCode,@Nullable String caption,@DefaultValue("5") int rows) throws SQLException{
		if(rows<=0) rows = this.defautLimitQuerySize;
		if(caption!=null) caption="%"+caption+"%";
		return this.dataDictDao.limitQuery(con, dictCode, caption, rows);
	}

}
