⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 departmentmanagerimpl.java

📁 基于java的组号查询模块
💻 JAVA
字号:
/**
 * 
 */
package com.lily.dap.service.organize.impl;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

import com.lily.dap.model.QueryCondition;
import com.lily.dap.model.QueryExpression;
import com.lily.dap.model.QueryOrder;
import com.lily.dap.model.organize.Department;
import com.lily.dap.model.organize.Person;
import com.lily.dap.model.organize.Post;
import com.lily.dap.model.organize.PostPerson;
import com.lily.dap.service.core.BaseManager;
import com.lily.dap.service.core.exception.DataHaveIncludeException;
import com.lily.dap.service.core.exception.DataNotExistException;
import com.lily.dap.service.core.exception.DataNotIncludeException;
import com.lily.dap.service.core.exception.NotSupportOperationException;
import com.lily.dap.service.organize.DepartmentManager;
import com.lily.dap.service.right.RightHoldManager;

/**
 * @author zouxuemo
 *
 */
public class DepartmentManagerImpl extends BaseManager implements
		DepartmentManager, ApplicationContextAware {
	private RightHoldManager rightHoldManager;
	
	private ApplicationContext ctx;
	
	/**
	 * @param rightHoldManager 要设置的 rightHoldManager
	 */
	public void setRightHoldManager(RightHoldManager rightHoldManager) {
		this.rightHoldManager = rightHoldManager;
	}

	public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
		ctx = applicationContext;
	}

	/* (非 Javadoc)
	 * @see com.lily.dap.service.organize.DepartmentManager#getAllChildDepartments(long)
	 */
	public List getAllChildDepartments(long parent_id) {
		Department dep = (Department)get(Department.class, parent_id);
		
		return gets(Department.class, new QueryCondition().putCondition("level", QueryExpression.OP_RLIKE, dep.getLevel()).addOrder("level"));
	}

	/* (非 Javadoc)
	 * @see com.lily.dap.service.organize.DepartmentManager#getChildDepartments(long)
	 */
	public List getChildDepartments(long depart_id) {
		return gets(Department.class, new QueryCondition().putCondition("parent_id", depart_id).addOrder("code"));
	}

	/* (非 Javadoc)
	 * @see com.lily.dap.service.organize.DepartmentManager#getDepartPersonNum(long)
	 */
	public long getDepartPersonNum(long depart_id) {
		Department dep = (Department)get(Department.class, depart_id);
		
		return count(PostPerson.class, new QueryCondition().putCondition("post_id", QueryExpression.OP_INQUERY, "select id from Post where dep_id in (select id from Department where level like '" + dep.getLevel() + "%')"));
	}

	/* (非 Javadoc)
	 * @see com.lily.dap.service.organize.DepartmentManager#getDepartment(java.lang.String)
	 */
	public Department getDepartment(String name) {
		return (Department)get(Department.class, "name", name);
	}

	/* (非 Javadoc)
	 * @see com.lily.dap.service.organize.DepartmentManager#getDepartments(int)
	 */
	public List getDepartments(int type) {
		return gets(Department.class, new QueryCondition().putCondition("type", type).addOrder("level"));
	}

	/* (非 Javadoc)
	 * @see com.lily.dap.service.organize.DepartmentManager#getPersons(long, long, java.lang.String)
	 */
	public List getPersons(long depart_id, long post_id, String person_name) {
		QueryCondition queryCondition = new QueryCondition();
		
		if (post_id > 0) {
			queryCondition.putCondition("id", QueryExpression.OP_INQUERY, "select person_id from PostPerson where post_id = " + post_id);
		} else if (depart_id > 0) {
			Department dep = (Department)get(Department.class, depart_id);
			
			queryCondition.putCondition("id", QueryExpression.OP_INQUERY, "select person_id from PostPerson where post_id in (select id from Post where dep_id in (select id from Department where level like '" + dep.getLevel() + "%'))");
		}
		
		queryCondition.putCondition("name", QueryExpression.OP_LIKE, person_name);
		queryCondition.addOrder("name");
		
		return gets(Person.class, queryCondition);
	}

	/* (非 Javadoc)
	 * @see com.lily.dap.service.organize.DepartmentManager#getPostPersonNum(long)
	 */
	public long getPostPersonNum(long post_id) {
		return count(PostPerson.class, new QueryCondition().putCondition("post_id", post_id));
	}

	/* (非 Javadoc)
	 * @see com.lily.dap.service.organize.DepartmentManager#getPosts(long)
	 */
	public List getPosts(long depart_id) {
		return gets(Post.class, new QueryCondition().putCondition("dep_id", depart_id).addOrder("code"));
	}

	/* (非 Javadoc)
	 * @see com.lily.dap.service.organize.DepartmentManager#getPostsByPerson(long)
	 */
	public List getPostsByPerson(long person_id) {
		return gets(Post.class, new QueryCondition().putCondition("id", QueryExpression.OP_INQUERY, "select post_id from PostPerson where person_id = " + person_id).addOrder("code"));
	}

	/* (非 Javadoc)
	 * @see com.lily.dap.service.organize.DepartmentManager#getRootDepartment()
	 */
	public Department getRootDepartment() throws DataNotExistException {
		try {
			return (Department)get(Department.class, "parent_id", new Long(0));
		} catch (DataNotExistException e) {
			throw new DataNotExistException("组织机构的根部们不存在!");
		}
	}

	/* (非 Javadoc)
	 * @see com.lily.dap.service.organize.DepartmentManager#initOrganize(java.lang.String, java.lang.String, java.lang.String)
	 */
	public Department initOrganize(String code, String name, String des) {
		Department rootDepart = new Department();
		
		rightHoldManager.providerHoldsRole(rootDepart);
		
		rootDepart.setLevel("001");
		rootDepart.setCode(code);
		rootDepart.setName(name);
		rootDepart.setType(Department.DEPARTMENT_TYPE_ROOT);
		rootDepart.setDes(des);
		
		return rootDepart;
	}

	/* (非 Javadoc)
	 * @see com.lily.dap.service.organize.DepartmentManager#addPostPerson(long, long)
	 */
	public void addPostPerson(long post_id, long person_id)
			throws DataNotExistException, DataHaveIncludeException {
		Post post = (Post)get(Post.class, post_id);
		Person person = (Person)get(Person.class, person_id);

		if (gets(PostPerson.class, new QueryCondition().putCondition("post_id", post_id).putCondition("person_id", person_id)).size() > 0)
			throw new DataHaveIncludeException("岗位[" + post.getName() + "]已经包含了人员[" + person.getName() + "],无需添加!");
		
		doAddPostPerson(post, person);
	}
	
	protected void doAddPostPerson(Post post, Person person) {
		rightHoldManager.addHaveHold(person, post);
		
		PostPerson postPerson = new PostPerson();
		postPerson.setPost_id(post.getId());
		postPerson.setPerson_id(person.getId());
		
		doSave(postPerson);
		
		ctx.publishEvent(new OrganizeNotifyEvent(OrganizeNotifyEvent.EVENT_CLASS_POST, OrganizeNotifyEvent.EVENT_ADD_POST_HAVE_PERSON, post, person));
	}

	/* (非 Javadoc)
	 * @see com.lily.dap.service.organize.DepartmentManager#removePostPerson(long, long)
	 */
	public void removePostPerson(long post_id, long person_id)
			throws DataNotExistException, DataNotIncludeException {
		Post post = (Post)get(Post.class, post_id);
		Person person = (Person)get(Person.class, person_id);

		List result = gets(PostPerson.class, new QueryCondition().putCondition("post_id", post_id).putCondition("person_id", person_id));
		if (result.size() == 0)
			throw new DataHaveIncludeException("岗位[" + post.getName() + "]未包含人员[" + person.getName() + "],无法去除!");
		
		doRemovePostPerson((PostPerson)result.get(0), post, person);
	}
	
	protected void doRemovePostPerson(PostPerson postPerson, Post post, Person person) {
		rightHoldManager.removeHaveHold(person, post);
		
		doRemove(PostPerson.class, new Long(postPerson.getId()));
		
		ctx.publishEvent(new OrganizeNotifyEvent(OrganizeNotifyEvent.EVENT_CLASS_POST, OrganizeNotifyEvent.EVENT_REMOVE_POST_HAVE_PERSON, post, person));
	}

	/* (非 Javadoc)
	 * @see com.lily.dap.service.core.BaseManager#onBeforeAdd(java.lang.Object)
	 */
	protected Object onBeforeAdd(Object entity) {
		if (!(entity instanceof Department) && !(entity instanceof Post))
			throw new NotSupportOperationException("接口只支持对Department、Post的add(...)方法!");
		
		entity = super.onBeforeAdd(entity);
		
		if (entity instanceof Department) {
			Department d = (Department)entity;
			Department parent = (Department)get(Department.class, d.getParent_id());
			
			rightHoldManager.providerHoldsRole(d);
			rightHoldManager.addHaveHold(d, parent);
			
			String level = generateDepartmentLevel(d.getParent_id());
			d.setLevel(level);
			
			return d;
		} else {
			Post p = (Post)entity;
			Department department = (Department)get(Department.class, p.getDep_id());
			
			rightHoldManager.providerHoldsRole(p);
			rightHoldManager.addHaveHold(p, department);
			
			return p;
		}
	}

	/* (非 Javadoc)
	 * @see com.lily.dap.service.core.BaseManager#onAfterAdd(java.lang.Object)
	 */
	protected void onAfterAdd(Object entity) {
		super.onAfterAdd(entity);

		if (entity instanceof Department) {
			Department d = (Department)entity;
			
			ctx.publishEvent(new OrganizeNotifyEvent(OrganizeNotifyEvent.EVENT_CLASS_DEPARTMENT, OrganizeNotifyEvent.EVENT_ADD_DEPARTMENT, d));
		} else {
			Post p = (Post)entity;
			
			ctx.publishEvent(new OrganizeNotifyEvent(OrganizeNotifyEvent.EVENT_CLASS_POST, OrganizeNotifyEvent.EVENT_ADD_POST, p));
		}
	}

	/* (非 Javadoc)
	 * @see com.lily.dap.service.core.BaseManager#onBeforeModify(java.lang.Object)
	 */
	protected Object onBeforeModify(Object entity) {
		if (!(entity instanceof Department) && !(entity instanceof Post))
			throw new NotSupportOperationException("接口只支持对Department、Post的modify(...)方法!");
		
		entity = super.onBeforeModify(entity);
		
		if (entity instanceof Department) {
			Department d = (Department)entity;
			
			Department department = (Department)get(Department.class, d.getId());
			department.setCode(d.getCode());
			department.setName(d.getName());
			department.setType(d.getType());
			department.setDes(d.getDes());
			
			return department;
		} else {
			Post p = (Post)entity;
			
			Post post = (Post)get(Post.class, p.getId());
			post.setCode(p.getCode());
			post.setName(p.getName());
			post.setDes(p.getDes());
			
			return post;
		}
	}

	/* (非 Javadoc)
	 * @see com.lily.dap.service.core.BaseManager#onAfterModify(java.lang.Object)
	 */
	protected void onAfterModify(Object entity) {
		super.onAfterModify(entity);

		if (entity instanceof Department) {
			Department d = (Department)entity;
			
			ctx.publishEvent(new OrganizeNotifyEvent(OrganizeNotifyEvent.EVENT_CLASS_DEPARTMENT, OrganizeNotifyEvent.EVENT_MODIFY_DEPARTMENT, d));
		} else {
			Post p = (Post)entity;
			
			ctx.publishEvent(new OrganizeNotifyEvent(OrganizeNotifyEvent.EVENT_CLASS_POST, OrganizeNotifyEvent.EVENT_MODIFY_POST, p));
		}
	}

	/* (非 Javadoc)
	 * @see com.lily.dap.service.core.BaseManager#onBeforeRemove(java.lang.Object)
	 */
	protected void onBeforeRemove(Object entity) {
		if (!(entity instanceof Department) && !(entity instanceof Post))
			throw new NotSupportOperationException("接口只支持对Department、Post的remove(...)方法!");
		
		super.onBeforeRemove(entity);

		if (entity instanceof Department) {
			Department d = (Department)entity;
			
			// 调用remove去循环递归删除子部门
			List childDepList = gets(Department.class, new QueryCondition().putCondition("parent_id", d.getId()));
			for(int i = 0; i < childDepList.size(); i++){
				Department childDep = (Department)childDepList.get(i);
				
				remove(Department.class, new Long(childDep.getId()));
			}
			
			// 调用remove去循环删除部门下面的岗位,并移除岗位继承的部门的权限
			List postList = gets(Post.class, new QueryCondition().putCondition("dep_id", d.getId()));
			for(int i = 0; i < postList.size(); i++){
				Post post = (Post)postList.get(i);
				
				remove(Post.class, new Long(post.getId()));
			}
			
			rightHoldManager.removeHoldsRoles(d);
		} else {
			Post p = (Post)entity;
			
			List postPersonList = gets(PostPerson.class, new QueryCondition().putCondition("post_id", p.getId()));
			for (int i = 0; i < postPersonList.size(); i++) {
				PostPerson postPerson = (PostPerson)postPersonList.get(i);
				
				Person person = (Person)get(Person.class, new Long(postPerson.getPerson_id()));
				
				doRemovePostPerson(postPerson, p, person);
			}
			
			rightHoldManager.removeHoldsRoles(p);
		}
	}

	/* (非 Javadoc)
	 * @see com.lily.dap.service.core.BaseManager#onAfterRemove(java.lang.Object)
	 */
	protected void onAfterRemove(Object entity) {
		super.onAfterRemove(entity);

		if (entity instanceof Department) {
			Department d = (Department)entity;
			
			ctx.publishEvent(new OrganizeNotifyEvent(OrganizeNotifyEvent.EVENT_CLASS_DEPARTMENT, OrganizeNotifyEvent.EVENT_REMOVE_DEPARTMENT, d));
		} else {
			Post p = (Post)entity;
			
			ctx.publishEvent(new OrganizeNotifyEvent(OrganizeNotifyEvent.EVENT_CLASS_POST, OrganizeNotifyEvent.EVENT_REMOVE_POST, p));
		}
	}
	
	/**
	 * 产生新建子部门的内部编码level
	 * 
	 * @param parent_depart_id
	 * @return
	 */
	private String generateDepartmentLevel(long parent_depart_id){
		Department parent = (Department)get(Department.class, parent_depart_id);
		String parentLevel = parent.getLevel();
		
		//如果没有子部门,则直接在父部门后边添加001
		List siblingDeparts = gets(Department.class, new QueryCondition()
														.putCondition("parent_id", parent_depart_id)
														.addOrder("level", QueryOrder.DIR_DESC)
														.setPageSize(1)
														.setPageNo(1));
		String lowerLevel = null;
		if(siblingDeparts.isEmpty()){
			lowerLevel = "001";
		} else {
			Department sibling = (Department)siblingDeparts.get(0);
			String s = sibling.getLevel();
			int max = Integer.parseInt(s.substring(s.length() - 3));
			max++;
			
			lowerLevel = StringUtils.leftPad(String.valueOf(max), 3, "0");
		}
		
		return parentLevel + lowerLevel;
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -