/**
 * Company Name : 中贸促信息技术有限责任公司
 * Project Name:project
 * File Name:OrgService.java
 * Package Name:ccpit.base.orgManage
 * Date:2015年9月18日下午4:55:11
 * Copyright (c) 2015, dingwei@ccpit.org All Rights Reserved.
 *
*/

package org.ccpit.base.orgManage;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.transaction.Transactional;

import org.ccpit.base.controller.Page;
import org.ccpit.base.controller.PageRequest;
import org.ccpit.base.role.Role;
import org.ccpit.base.role.RoleDao;
import org.ccpit.base.user.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

/**
 * ClassName:OrgService <br/>
 * Function: TODO ADD FUNCTION. <br/>
 * Reason:	 TODO ADD REASON. <br/>
 * Date:     2015年9月18日 下午4:55:11 <br/>
 * @author   dingwei
 * @version  
 * @since    JDK 1.6
 * @see 	 
 */
@Repository
@Transactional
public class OrgService {

	@Autowired
	private OrgDao orgDao; 
	@Autowired
	private RoleDao roleDao;
	/**
	 * 
	 * addOrg:(新增机构信息). <br/>
	 * @author dingwei
	 * @param orgInfo
	 * @return
	 * @since JDK 1.6
	 */
	public boolean addOrg(OrgInfo orgInfo){
		return orgDao.save(orgInfo);
	}
	/**
	 * 
	 * updateOrg:(修改机构信息). <br/>
	 * @author dingwei
	 * @param orgInfo
	 * @return
	 * @since JDK 1.6
	 */
	public boolean updateOrg(OrgInfo orgInfo){
		return orgDao.update(orgInfo);
	}
	/**
	 * 
	 * updateOrg:(根据Id删除机构信息). <br/>
	 * @author dingwei
	 * @param orgInfo
	 * @return
	 * @since JDK 1.6
	 */
	public boolean delOrgById(long id){
		return orgDao.deleteById(id);
	}
	/**
	 * 
	 * updateOrg:(根据id查询机构详细信息). <br/>
	 * @author dingwei
	 * @param orgInfo
	 * @return
	 * @since JDK 1.6
	 */
	public OrgInfo getOrgById(long id){
		return orgDao.queryById(id);
	}
	/**
	 * 
	 * updateOrg:(获取机构列表信息). <br/>
	 * @author dingwei
	 * @param orgInfo
	 * @return
	 * @since JDK 1.6
	 */
	public List<OrgInfo> getOrgInfoList(){
		return orgDao.queryAll();
	}
	/**
	 * 
	 * getOrgInfoList:(根据条件查询机构信息). <br/>
	 * @author dingwei
	 * @param hql
	 * @return
	 * @since JDK 1.6
	 */
	public List<OrgInfo> getOrgInfoList(String hql){
		return orgDao.query(hql, null);
	}
	
	public List<OrgInfo> getChildOrgById(long id){
		return orgDao.getChildOrgById(id);
	}
	/**
	 * 
	 * updateOrg:(获取机构列表信息 分页展示). <br/>
	 * @author dingwei
	 * @param orgInfo
	 * @return
	 * @since JDK 1.6
	 */
	public Page<OrgInfo> queryPage(PageRequest pageRequest){
		Page<OrgInfo> page = orgDao.findPage(pageRequest, "from " + OrgInfo.class.getName(), new String[]{});
        return page;
    }
	/**
	 * 
	 * updateOrg:(根据查询条件获取机构列表信息 分页展示). <br/>
	 * @author dingwei
	 * @param orgInfo
	 * @return
	 * @since JDK 1.6
	 */
	public Page<OrgInfo> queryPage(PageRequest pageRequest,String hql){
		Page<OrgInfo> page = orgDao.findPage(pageRequest, hql + OrgInfo.class.getName(), new String[]{});
        return page;
    }
	
    public boolean save(OrgInfo org){
        return orgDao.save(org);
    }

    /**
     * 查询用户所在的部门
     * @param user
     * @return
     */
    public List<OrgInfo> getUserGroupsByUser(User user){
        return orgDao.getUserGroupsByUser(user);
    }

    /**
     * 查询所有拥有此角色的部门
     * @param role
     * @return
     */
    public List<OrgInfo> getUserGroupsByRole(Role role){
       return orgDao.getUserGroupsByRole(role);
    }

    /**
     * 用户所在的部门是否具有role的角色
     * @param user
     * @param role
     * @return
     */
    public boolean canUserInGroup(User user,Role role){
        return orgDao.canUserInGroup(user, role);
    }


    /**
     * 获取用户所在部门的所有角色
     * @param user
     * @return
     */
    public Set<Role> getRolesForUser(User user){
       return orgDao.getRolesForUser(user);
    }

    /**
     * Find OrgInfo by id
     * @param id
     * @return
     */
    public OrgInfo get(long id){
        return orgDao.queryById(id);
    }
    /**
     * 
     * setOrg_roles:(给部门设置角色). <br/>
     * @author dingwei
     * @param orgInfo
     * @param ids
     * @return
     * @since JDK 1.6
     */
    public boolean setOrg_roles(OrgInfo orgInfo,Long[] ids){
    	Set<Role> roleSet = new HashSet<Role>();
    	boolean flag = false;
    	for(long id : ids){
    		roleSet.add(roleDao.queryById(id));
    	}
    	orgInfo.getRoles().clear();
    	boolean flag1 = orgDao.update(orgInfo);
    	if(flag1){
    		OrgInfo org = orgDao.queryById(orgInfo.getId());
    		org.setRoles(roleSet);
    		flag = orgDao.save(org);
    	}
    	return flag;
    }
}

