package com.palmnest.service.impl;

import com.palmnest.application.Constants;
import com.palmnest.application.bean.Pagenation;
import com.palmnest.dao.RoleDao;
import com.palmnest.model.Role;
import com.palmnest.model.RoleFunction;
import com.palmnest.service.RoleManager;

import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Order;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;

/**
 * Implementation of RoleManager interface.
 *
 * @author <a href="mailto:chasel.java@gmail.com">Chasel Li</a>
 */
@Service("roleManager")
public class RoleManagerImpl extends GenericManagerImpl<Role, Long> implements RoleManager {

	RoleDao roleDao;

	@Autowired
	public RoleManagerImpl(RoleDao roleDao) {
		super(roleDao);
		this.roleDao = roleDao;
	}

	/**
	 * {@inheritDoc}
	 */
	public List<Role> getRoles(Role role) {
		return dao.getAll();
	}

	/**
	 * {@inheritDoc}
	 */
	public Role getRole(String rolename) {
		return roleDao.getRoleByName(rolename);
	}

	/**
	 * {@inheritDoc}
	 */
	public Role saveRole(Role role) {
		return dao.save(role);
	}

	/**
	 * {@inheritDoc}
	 */
	public void removeRole(String rolename) {
		roleDao.removeRole(rolename);
	}

	public Pagenation<Role> queryRole(int pageNo, List<Criterion> filterList) {

		Map<String, String> aliasMap = Collections.<String, String>emptyMap();

		List<Order> orderList = new ArrayList<Order>();

		orderList.add(Order.desc("lastUpdatedTime"));

		return roleDao.queryPage(pageNo, Constants.LIMIT_PAGESIZE, aliasMap, filterList, orderList, Role.class);
	}

	@Override
	public Pagenation<Role> queryRole(int pageNo, int pageSize, Map<String, String> criteria) {

		return roleDao.queryRole(pageNo, pageSize, criteria);
	}

	@Override
	public List<RoleFunction> queryRoleFunctionSet(Role role) {

		return roleDao.queryRoleFunctionSet(role);
	}

	@Override
	public void saveObj(Role role) {
		roleDao.saveObj(role);
	}

	@Override
	public void updateObj(Role role) {
		roleDao.updateObj(role);
	}

	@Override
	public void deleteRoleFunctions(long roleid) {
		roleDao.deleteRoleFunctions(roleid);
	}

	@Override
	public void saveRoleFunction(RoleFunction model) {
		roleDao.saveRoleFunction(model);
	}

	@Override
	public List<Role> getRoleList() {
		return roleDao.getRoleList("admin");
	}

	@Override
	public List<Role> getRoleListByOrg(String orgStr) {
		return roleDao.getRoleListByOrg(orgStr);
	}

	@Override
	public List<Role> getRoleListBySysCode(String id, String code) {
		return roleDao.getRoleListBySysCode(id, code);
	}

	@Override
	public List<Role> getByOrg(Integer orgId, Integer roleType) {
		return roleDao.getByOrg(orgId, roleType);
	}

	@Override
	public void deleteRoleFunction(Long roleId, Integer functionId) {
		roleDao.deleteRoleFunction(roleId, functionId);
	}

	@Override
	public List<Map<String, Object>> getRoleListByFuncCode(String syscode, String userId) {
		return roleDao.getRoleListByFuncCode(syscode, userId);
	}

	@Override
	public Role getVirAccountRole() {
		return roleDao.getVirAccountRole();
	}

	/**
	 * @param OrgId
	 * @return
	 */
	@Override
	public List<Map<String, Object>> getRoleByOrgAndSysCode(Integer OrgId,Integer pageType) {
		// TODO 标记 写死 这个4000是什么鬼
		String sysCode = "4000";//物品放行审核
		if(null != pageType && pageType ==2){
			sysCode = "4121";//待客服专员审核
		}
		
		return roleDao.getRoleByOrgAndSysCode(sysCode, OrgId);
	}
}