package org.cs.mgr.admin.service.impl;

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

import org.cs.core.service.impl.BaseService;
import org.cs.mgr.admin.dao.IRoleDao;
import org.cs.mgr.admin.model.Role;
import org.cs.mgr.admin.service.IRoleService;
import org.cs.util.Pager;
import org.cs.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service("roleService")
public class RoleService extends BaseService<Role> implements IRoleService{
	
	@Autowired
	private IRoleDao roleDao;
	public Pager getRoleList(Pager page){
		return this.findPageByHql("from Role ", page, null);
	}
	@Override
	public Pager getRoleList(Pager page,Map<String, String> map){
		Map<String, Object> params = new HashMap<>();
		StringBuffer sb = new StringBuffer("from Role where 1=1");
		if(StringUtil.isNotBlank(map.get("name"))){
			params.put("name", "%" + map.get("name") +"%");
			sb.append(" and name like :name");
		}
		return this.findPageByHql(sb.toString(), page, params);
	}
	
	@Override
	public Map<String, Object> findRoleByUserId(Map<String, String> map){
		List<Map<String, Object>> mlist = this.roleDao.findBySql("select mr.* from mgr_role mr left join mgr_user_role mur on mur.role_code=mr.code where mur.user_code='"+map.get("id")+"'");
		if(mlist !=null && !mlist.isEmpty()){
			return mlist.get(0);
		}else{
			return new HashMap<>();
		}
	}

}
