package com.ld.igds.sys.service; import com.bstek.bdf2.core.business.IUser; import com.bstek.bdf2.core.model.DefaultDept; import com.bstek.bdf2.core.model.UserDept; import com.bstek.bdf2.core.orm.hibernate.HibernateDao; import com.bstek.dorado.data.entity.EntityState; import com.bstek.dorado.data.entity.EntityUtils; import com.ld.igds.constant.Constant; import com.ld.igds.constant.RedisConst; import com.ld.igds.util.ContextUtil; import com.ld.igds.util.RedisUtil; import org.apache.commons.lang3.StringUtils; import org.hibernate.Query; import org.hibernate.Session; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Repository; import java.util.*; @Repository public class SysDeptServiceImpl extends HibernateDao implements SysDeptService { @Autowired private RedisUtil redisUtil; @Autowired private JdbcDeptServiceImpl jdbcDeptService; @Override public List loadDeptByParentId(String parentId, String companyId) { String hql = " from " + DefaultDept.class.getName() + " where companyId=:companyId "; Map args = new HashMap<>(); args.put("companyId", companyId); List list; if (parentId == null) { hql += " AND parentId IS NULL ORDER BY id "; list = this.query(hql, args); } else { hql += " AND parentId=:parentId ORDER BY id"; args.put("parentId", parentId); list = this.query(hql, args); } return list; } private List getAll(String companyId) { String hql = " from " + DefaultDept.class.getName() + " where companyId=:companyId "; Map args = new HashMap<>(); args.put("companyId", companyId); return this.query(hql, args); } @Override public void saveDept(Collection list, String companyId) { EntityState state = null; DefaultDept dept = null; String id; for (DefaultDept defaultDept : list) { if (StringUtils.isEmpty(defaultDept.getVal())) { defaultDept.setVal(Constant.YN_Y); } state = EntityUtils.getState(defaultDept); if (state.equals(EntityState.NEW)) { dept = this .findLastDeptId(defaultDept.getParentId(), companyId); if (dept != null) { id = ContextUtil.getNextId(companyId, defaultDept.getParentId(), dept.getId(), 1000); } else { id = ContextUtil.getNextId(companyId, defaultDept.getParentId(), null, 1000); } defaultDept.setId(id); defaultDept.setCompanyId(companyId); jdbcDeptService.saveDefaultDept(defaultDept); // this.saveData(defaultDept); } if (state.equals(EntityState.MODIFIED) || state.equals(EntityState.MOVED)) { this.updateData(defaultDept); } if (defaultDept.getChildren() != null) { DefaultDept[] defaultDepts = new DefaultDept[defaultDept .getChildren().size()]; saveDept( Arrays.asList(defaultDept.getChildren().toArray( defaultDepts)), defaultDept.getCompanyId()); } if (state.equals(EntityState.DELETED)) { if (countChildren(defaultDept.getId()) == 0) { this.delData(defaultDept); } else { throw new RuntimeException("请先删除子部门"); } } } } private void delData(DefaultDept defaultDept) { Session session = this.getSessionFactory().openSession(); try { session.delete(defaultDept); } finally { session.flush(); session.close(); } } private void updateData(DefaultDept defaultDept) { Session session = this.getSessionFactory().openSession(); try { session.update(defaultDept); } finally { session.flush(); session.close(); } } public void saveData(DefaultDept data) { Session session = this.getSessionFactory().openSession(); try { session.update(data); } finally { session.flush(); session.close(); } } public int countChildren(String parentId) { String hql = " from " + DefaultDept.class.getName() + " where parentId=:parentId"; Map args = new HashMap<>(); args.put("parentId", parentId); List list = this.query(hql, args); if (null == list) return 0; return list.size(); } @Override public List flushDeptCache(String companyId) { List list = this.getAll(companyId); String key = RedisConst.buildKey(companyId, RedisConst.KEY_DEPT_LIST); redisUtil.set(key, list); return list; } @Override public DefaultDept getCacheDept(String companyId, String deptId) { List list = getCacheDept(companyId); if (null == list || list.isEmpty()) return null; for (DefaultDept dept : list) { if (dept.getId().equals(deptId)) return dept; } return null; } @SuppressWarnings("unchecked") @Override public List getCacheDept(String companyId) { String key = RedisConst.buildKey(companyId, RedisConst.KEY_DEPT_LIST); return (List) redisUtil.get(key); } @Override public DefaultDept getDeptById(String id) { String hql = " from " + DefaultDept.class.getName() + " where id =:id "; Map args = new HashMap<>(); args.put("id", id); List list = this.query(hql, args); if (list == null || list.isEmpty()) { return null; } return list.get(0); } private DefaultDept findLastDeptId(String parentId, String companyId) { if (StringUtils.isEmpty(companyId)) { companyId = ContextUtil.getDefaultCompanyId(); } List list = this.loadDeptByParentId(parentId, companyId); if (list != null && list.size() > 0) { return list.get(list.size() - 1); } else { return null; } } @Override public List loadUserDepts(String username) { String hql = " from " + DefaultDept.class.getName() + " where id in(" + " select deptId from " + UserDept.class.getName() + " where username=:username)"; Map args = new HashMap<>(); args.put("username", username); return this.query(hql, args); } @Override public DefaultDept loadUserDept(String username) { List list = this.loadUserDepts(username); if (null == list || list.isEmpty()) return null; return list.get(0); } @Override public void updateUserDept(String username, String deptId) { Session session = null; try { UserDept userDept = new UserDept(); userDept.setDeptId(deptId); userDept.setId(ContextUtil.getUUID()); userDept.setUsername(username); session = this.getSessionFactory().openSession(); String hql = " delete from " + UserDept.class.getName() + " where username=:username"; Query query = session.createQuery(hql); query.setString("username", username); query.executeUpdate(); session.save(userDept); } finally { session.flush(); session.close(); } ContextUtil.updateSubDept(username, deptId); } public List loadUserDept() { String hql = " from " + UserDept.class.getName(); return this.query(hql); } @Override public void initUserDeptMap() { List list = loadUserDept(); if (null == list || list.isEmpty()) return; for (UserDept userDept : list) { ContextUtil.updateSubDept(userDept.getUsername(), userDept.getDeptId()); } } @Override public DefaultDept getSubDept(IUser user, String deptId) { if (null == user) { user = ContextUtil.getLoginUser(); } if (null == deptId) { deptId = ContextUtil.subDeptId(user); } DefaultDept defaultDept = getCacheDept(user.getCompanyId(), deptId); if (Constant.DEPT_TYPE_20.equals(defaultDept.getType())) return defaultDept; List list = getCacheDept(user.getCompanyId()); // 所在位置为公司,获取当前公司下第一个库点 if (Constant.DEPT_TYPE_10.equals(defaultDept.getType())) { for (DefaultDept depot : list) { if (depot.getParentId().equals(defaultDept.getId())) return depot; } } if (Constant.DEPT_TYPE_30.equals(defaultDept.getType())) { for (DefaultDept depot : list) { if (defaultDept.getParentId().equals(depot.getId())) return depot; } } return null; } }