package cn.hmsoft.frame.data.dao;

import java.util.ArrayList;
import java.util.List;
import org.springframework.stereotype.Repository;
import cn.hmsoft.frame.data.model.FrameOptrRes;
import cn.hmsoft.frame.data.model.FrameRes;
import cn.hmsoft.jdbc.core.PlatformDaoSupport;

@Repository
public class FrameOptrResDao extends PlatformDaoSupport<FrameOptrRes> {

	public List<FrameRes> listOptrRes(int optr_id) {
		String sql = "select r.* from frame_res r,frame_optr_res op where r.res_status='Active' and op.res_id=r.res_id where op.optr_id=? order by op.res_id";
		return this.listBySql(FrameRes.class, sql, optr_id);
	}

	public List<FrameRes> listOptrRes(int optr_id, String type) {
		String sql = "select r.* from frame_res r,frame_optr_res op where r.res_status='Active' and op.res_id=r.res_id where op.optr_id=? and op.type=? order by op.res_id";
		return this.listBySql(FrameRes.class, sql, optr_id, type);
	}

	public List<FrameRes> listFrameResList(final Integer optr_id, final String resType) {
		List<FrameRes> list = null;
		List<Object> listParam = new ArrayList<Object>();
		StringBuilder sbSql = new StringBuilder();
		sbSql.append("select * from frame_res where res_status=? ");
		listParam.add("Active");
	    if (resType != null) {
	    	sbSql.append(" and res_type=? ");
	    	listParam.add(resType);
	    }
	    if (optr_id != null) {
	    	sbSql.append(" and res_id in (select DISTINCT RES_ID from frame_role_res where ROLE_ID in (select role_id from frame_optr_role where optr_id=?))");
	    	listParam.add(optr_id);
	    }
	    sbSql.append(" order by res_level,res_order");
	    list = super.listBySql(FrameRes.class, sbSql.toString(), listParam.toArray());
	    return list;
	}
}
