package com.palmnest.dao.notice.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;
import org.springframework.util.CollectionUtils;

import com.palmnest.dao.hibernate.GenericDaoHibernate;
import com.palmnest.dao.notice.ApprovalRoleDao;
import com.palmnest.model.notice.ApprovalModel;
import com.palmnest.model.notice.ApprovalRoleModel;

@Repository("approvalRoleDao")
public class ApprovalRoleDaoImpl extends GenericDaoHibernate<ApprovalRoleModel, Integer> implements ApprovalRoleDao{
	public ApprovalRoleDaoImpl() {
		super(ApprovalRoleModel.class);

	}
	/**
	 * 获取多级审核-审核类型下的角色
	 * @param orgId
	 * @param type
	 * @param sourceId
	 * @return
	 */
	@Override
	public List<ApprovalRoleModel> getRoleListBySourceId(Integer orgId,
			Integer type, Integer sourceId) {
		StringBuilder sql=new StringBuilder("from ApprovalRoleModel where enabled=1 and orgId=? and type=? and sourceId=? ");
		return getSession().createQuery(sql.toString()).setInteger(0, orgId).setInteger(1, type).setInteger(2, sourceId).list();
	}
	/**
	 * 根据几审获取对应的角色id
	 * @param orgId
	 * @param type
	 * @param sourceId
	 * @param orderno 当前审核角色的序号+1  就是下一级审核人的角色id
	 * @return
	 */
	@Override
	public Map<String, Object> getRoleIdByOrderno(Integer orgId, Integer type,
			Integer sourceId, Integer orderno) {
		//778,1,13,1
		Map<String, Object> map=new HashMap<String, Object>();
		map.put("roleId", "");
		map.put("roleName", "");
		map.put("orderno", orderno);
		String sql="select ar.role_id roleId,r.`NAME` roleName from tb_approval_role ar,tb_app_role r where ar.role_id = r.id and ar.org_id="+orgId+" and ar.enabled=1 and ar.type="+type+" and ar.source_id="+sourceId+" and ar.orderno="+orderno;
		List<Map<String, Object>> list = getJdbcTemplate().queryForList(sql);
		if (!CollectionUtils.isEmpty(list)) {
			Map<String, Object> resMap=list.get(0);
			String roleId="";
			String roleName="";
			if(resMap.containsKey("roleId") && null != resMap.get("roleId") && StringUtils.isNotBlank(resMap.get("roleId").toString())){
				roleId=resMap.get("roleId").toString();
			}
			if(resMap.containsKey("roleName") && null != resMap.get("roleName") && StringUtils.isNotBlank(resMap.get("roleName").toString())){
				roleName=resMap.get("roleName").toString();
			}
			map.put("roleId", roleId);
			map.put("roleName", roleName);
			map.put("orderno", orderno);
		}
		return map;
	}
}
