DepartUtil.java 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. package cn.com.lzt.common.util;
  2. import org.apache.commons.lang3.StringUtils;
  3. import org.jeecgframework.core.common.dao.jdbc.JdbcDao;
  4. import org.jeecgframework.core.constant.Globals;
  5. import org.jeecgframework.core.util.StringUtil;
  6. import org.jeecgframework.core.util.*;
  7. import org.jeecgframework.web.system.pojo.base.TSDepart;
  8. import org.jeecgframework.web.system.pojo.base.TSUser;
  9. import org.jeecgframework.web.system.pojo.base.TSUserOrg;
  10. import org.jeecgframework.web.system.service.SystemService;
  11. import java.util.List;
  12. import java.util.Map;
  13. public class DepartUtil {
  14. private static SystemService systemService;
  15. public static final String HR_ID="00000000610f6b0d01610f7078050007";//人事部门id
  16. public static final String XXB="00000000614594980161462963290d31";//信息发展部
  17. public static final String GENERALMANAGEROFFICE="00000000610f6afa01611cd2da0b0094";//总经理办公室
  18. static {
  19. systemService = ApplicationContextUtil.getContext().getBean(SystemService.class);
  20. }
  21. /**
  22. * 判断所属部门是项目还是职能部门
  23. * @param departId
  24. * @return
  25. */
  26. public static DepartType departType(String departId){
  27. SystemService service = ApplicationContextUtil.getContext().getBean(SystemService.class);
  28. departId = getQueryDepIdByDepId(departId);
  29. TSDepart depart = service.getEntity(TSDepart.class,departId);
  30. return departType(depart);
  31. }
  32. public static DepartType departType(TSDepart depart){
  33. if(StringUtils.equals(depart.getOrgType(),"2")&&depart.getOrgCode().length()==6){//职能部门
  34. return DepartType.department;
  35. }else{//项目
  36. return DepartType.project;
  37. }
  38. }
  39. public static enum DepartType{
  40. project(1)//项目
  41. ,department(2);//职能部门
  42. private int typeCode;
  43. private DepartType(int typeCode){
  44. this.typeCode=typeCode;
  45. }
  46. public int getTypeCode() {
  47. return typeCode;
  48. }
  49. public void setTypeCode(int typeCode) {
  50. this.typeCode = typeCode;
  51. }
  52. }
  53. /**
  54. * 获得用于查询的部门id
  55. * @param userid
  56. * @return
  57. */
  58. public static String getQueryDepIdByUserId(String userid){
  59. SystemService service = ApplicationContextUtil.getContext().getBean(SystemService.class);
  60. List<TSUserOrg> userorgList = service.findHql("from TSUserOrg where tsUser.id=? and ifpluralism=? ", userid, Globals.PLURALISM_NO.toString());
  61. if(userorgList.size() > 0) {
  62. TSDepart depart = userorgList.get(0).getTsDepart();
  63. return getQueryDepId(depart);
  64. }
  65. return null;
  66. }
  67. public static String getQueryDepIdByDepId(String depid){
  68. SystemService service = ApplicationContextUtil.getContext().getBean(SystemService.class);
  69. TSDepart depart = service.getEntity(TSDepart.class,depid);
  70. if(depart==null){//处理projectid 问题
  71. String findProjectDepartHql = "from TSDepart where projectid=? and length(orgCode)=6";
  72. List<TSDepart> list = service.findHql(findProjectDepartHql,depid);
  73. if(!list.isEmpty()){
  74. depart =list.get(0);
  75. }
  76. }
  77. return getQueryDepId(depart);
  78. }
  79. /**
  80. * 获得用于查询的部门id
  81. * @param dep
  82. * @return
  83. */
  84. public static String getQueryDepId(TSDepart dep){
  85. String depId ;
  86. if (dep.getTSPDepart() == null || StringUtil.isEmpty(dep.getTSPDepart().getId()) || "4028e4a55f6c84ec015f6ca5c6ab0001".equals(dep.getTSPDepart().getId())) {
  87. // 上级所属部门是公司总部或者空,取当前所属部门
  88. depId = dep.getId();
  89. } else {
  90. depId = selectById("3", dep.getTSPDepart().getId());
  91. }
  92. return depId;
  93. }
  94. private static String selectById(String code,String id){
  95. TSDepart tsDepart = systemService.getEntity(TSDepart.class,id);
  96. String temp = "";
  97. if(code.equals("3")){
  98. if(code.equals(tsDepart.getOrgType()) || "5".equals(tsDepart.getOrgType())){
  99. temp = tsDepart.getId();
  100. }else{
  101. if(tsDepart.getTSPDepart() != null){
  102. selectById(code, tsDepart.getTSPDepart().getId());
  103. }
  104. }
  105. }else{
  106. if(code.equals(tsDepart.getOrgType())){
  107. temp = tsDepart.getId();
  108. }else{
  109. if(tsDepart.getTSPDepart() != null){
  110. selectById(code, tsDepart.getTSPDepart().getId());
  111. }
  112. }
  113. }
  114. return temp;
  115. }
  116. public static synchronized String getMaxLocalCode(String parentCode){
  117. if(oConvertUtils.isEmpty(parentCode)){
  118. parentCode = "";
  119. }
  120. int localCodeLength = parentCode.length() + YouBianCodeUtil.zhanweiLength;
  121. StringBuilder sb = new StringBuilder();
  122. sb.append("SELECT org_code FROM t_s_depart");
  123. //-update-begin--author:scott--date:20160414--for:数据库兼容性修改---
  124. if(ResourceUtil.getJdbcUrl().indexOf(JdbcDao.DATABSE_TYPE_SQLSERVER)!=-1){
  125. sb.append(" where LEN(org_code) = ").append(localCodeLength);
  126. }else{
  127. sb.append(" where LENGTH(org_code) = ").append(localCodeLength);
  128. }
  129. //-update-end--author:scott--date:20160414--for:数据库兼容性修改---
  130. if(oConvertUtils.isNotEmpty(parentCode)){
  131. sb.append(" and org_code like '").append(parentCode).append("%'");
  132. }
  133. //update-begin-Alex 20160310 for:去除LIMIT,解决数据库兼容性问题
  134. sb.append(" ORDER BY org_code DESC");
  135. List<Map<String, Object>> objMapList = systemService.findForJdbc(sb.toString(), 1, 1);
  136. String returnCode = null;
  137. if(objMapList!=null && objMapList.size()>0){
  138. returnCode = (String)objMapList.get(0).get("org_code");
  139. }
  140. //update-end-Alex 20160310 for:去除LIMIT,解决数据库兼容性问题
  141. return returnCode;
  142. }
  143. /**
  144. * 根据项目id转换成组织机构
  145. * */
  146. public static TSDepart getDepartFromProject(String projectid) {
  147. String hql = "from TSDepart where projectid =? and LENGTH(org_code ) = 6";
  148. List<TSDepart> departList = systemService.findHql(hql, projectid);
  149. if(departList != null && departList.size() > 0) {
  150. return departList.get(0);
  151. }else {
  152. return null;
  153. }
  154. }
  155. /**
  156. * 判断用户是否属于总经办
  157. * */
  158. public static boolean userWhetherBelongToGeneralManagerOffice(TSUser user) {
  159. StringBuffer sql = new StringBuffer();
  160. sql.append(" SELECT ");
  161. sql.append(" dep.* ");
  162. sql.append(" FROM ");
  163. sql.append(" t_s_depart dep, ");
  164. sql.append(" t_s_user_org org ");
  165. sql.append(" WHERE ");
  166. sql.append(" dep.id = org.org_id ");
  167. sql.append(" AND org.ifpluralism = '0' ");
  168. sql.append(" AND org.STATUS = 0 ");
  169. sql.append(" AND org.user_id = '");
  170. sql.append(user.getId());
  171. sql.append("' ");
  172. List<Map<String,Object>> list = systemService.findForJdbc(sql.toString());
  173. if(GENERALMANAGEROFFICE.equals(list.get(0).get("ID"))){
  174. return true;
  175. }else{
  176. return false;
  177. }
  178. }
  179. }