package com.palmnest.service.impl;

import com.palmnest.application.Constants;
import com.palmnest.application.bean.Pagenation;
import com.palmnest.dao.AppFunctionDao;
import com.palmnest.dao.RoleDao;
import com.palmnest.dao.estateserve.ServeConfigDao;
import com.palmnest.model.AppFunction;
import com.palmnest.model.Role;
import com.palmnest.model.RoleFunction;
import com.palmnest.model.User;
import com.palmnest.service.AppFunctionManager;
import org.apache.commons.lang.StringUtils;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Order;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.*;

/**
 * 系统功能业务接口实现
 *
 * @author yb
 * @since 2014-05-28
 */
@Component("appFunctionManager")
public class AppFunctionManagerImpl implements AppFunctionManager {

	private AppFunctionDao appFunctionDao;

	@Autowired
	public void setAppFunctionDao(AppFunctionDao appFunctionDao) {
		this.appFunctionDao = appFunctionDao;
	}

	@Autowired
	private ServeConfigDao serveConfigDao;
	@Autowired
	private RoleDao        roleDao;

	public List<AppFunction> getAll() {
		return appFunctionDao.getAll();
	}

	@Override
	public List<AppFunction> getAll(Integer port) {
		return appFunctionDao.getAll(port);
	}

	@Override
	public List<AppFunction> getAll(Integer port, Integer rightType) {
		return appFunctionDao.getAll(port, rightType);
	}

	@Override
	public List<AppFunction> getFunctionListByRole(Long roleId, String isApp) {
		return appFunctionDao.getFunctionListByRole(roleId, isApp);
	}

	@Override
	public List<AppFunction> getFunctionListByRole(Set<Long> roleIdSet) {
		List<AppFunction> functionList = new ArrayList<AppFunction>();

		if (roleIdSet != null && roleIdSet.size() > 0) {
			String roleIds = "";
			for (Long id : roleIdSet) {
				roleIds += id + ",";
			}
			if (StringUtils.isNotBlank(roleIds)) {
				functionList = appFunctionDao.getFunctionListByRole(roleIds.substring(0, roleIds.length() - 1));
			}
		}

		return functionList;
	}

	public Pagenation<AppFunction> queryAppFunction(int pageNo,
													List<Criterion> filterList) {
		Map<String, String> aliasMap  = Collections.<String, String>emptyMap();
		List<Order>         orderList = new ArrayList<Order>();
		orderList.add(Order.desc("orderNumber"));
		return appFunctionDao.queryPage(pageNo, Constants.LIMIT_PAGESIZE, aliasMap, filterList, orderList, AppFunction.class);
	}

	public AppFunction getById(int id) {
		return appFunctionDao.get(id);
	}

	public void saveObj(AppFunction appFunction) {
		appFunctionDao.saveObj(appFunction);
	}

	@Override
	public User getUserById(long userid) {

		return appFunctionDao.getUserById(userid);
	}

	@Override
	public void remove(AppFunction obj) {
		//删除功能
		appFunctionDao.remove(obj);
		//删除功能的同时删除功能相关记录
		//1.删除 功能-服务 关联
		serveConfigDao.deleteByFuncId(obj.getId());
		//2.删除 角色-功能 对应关系
		roleDao.deleteRoleFunctionByFuncId(obj.getId());
	}

	@Override
	public void updateObj(AppFunction appFunction) {
		appFunctionDao.updateObj(appFunction);
	}

	@Override
	public AppFunction getFuctionBySysCode(String id, String sysCode) {
		return appFunctionDao.getFuctionBySysCode(id, sysCode);
	}

	@Override
	public AppFunction getFuctionByPerms(String id, String perms) {
		return appFunctionDao.getFuctionByPerms(id, perms);
	}

	@Override
	public List<AppFunction> getLevel3Function(Integer parentFuncId, Long roleId) {
		return appFunctionDao.getLevel3Function(parentFuncId, roleId);
	}

	@Override
	public void sortFunc(String funcId) {
		// 1.先查询出需要绑定关系的角色列表
		List<Role> roleList = appFunctionDao.getRoleList(funcId);
		// 2.查询出需要绑定的三级功能列表
		List<AppFunction> funcList = appFunctionDao.getLevel3Function(funcId);

		for (Role role : roleList) {
			for (AppFunction function : funcList) {
				RoleFunction roleFunction = new RoleFunction();
				roleFunction.setRole(role);
				roleFunction.setFunction(function);
				//先删除之前的记录
				roleDao.deleteRoleFunction(role.getId(), function.getId());
				roleDao.saveRoleFunction(roleFunction);
			}
		}
	}


}
