package cn.com.goldenwater.dcproj.service.impl.system; import cn.com.goldenwater.dcproj.dao.*; import cn.com.goldenwater.dcproj.model.*; import cn.com.goldenwater.dcproj.param.*; import cn.com.goldenwater.dcproj.service.OlBisInspOrgService; import cn.com.goldenwater.dcproj.utils.InspUtils; import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.math.NumberUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import java.text.SimpleDateFormat; import java.util.*; import java.util.function.Function; import java.util.stream.Collectors; @Service @Transactional public class OrganizationTreeImpl { //机构id @Value("${org.code}") public String orgId; //七个委的父id @Value("${org.pGuid}") public String pGuid; @Autowired private AttOrgBaseDao attOrgBaseDao; //新版的机构表 @Autowired private BisInspAllDao bisInspAllDao; //批次相关dao @Autowired private BisInspPlanDao bisInspPlanDao; @Autowired private UserRoleDao userRoleDao; @Autowired private BisInspGroupDao bisInspGroupDao; @Autowired private AttMampuInfoDao attMampuInfoDao; @Autowired private BisInspKeyRegisterSectionDao bisInspKeyRegisterSectionDao; @Autowired private BisInspKeyRegSecUnitDao bisInspKeyRegSecUnitDao; /** * 获取树形结构相关信息 * * @param guid * @param userId * @param type * @param code * @return */ public List getOrganizationTreeByGuidAndOthers(String guid, String userId, String type, String code) { //当type和code为null是,是查询默认能看的树形根节点,其他是点击查询下一级树 if (StringUtils.isEmpty(type) || StringUtils.isEmpty(code)) { //根据机构id查询跟节点 AttOrgBase attOrgBase = attOrgBaseDao.get(guid); if (null != attOrgBase) { OrganizationAndBatchAndGroup organizationAndBatchAndGroup = new OrganizationAndBatchAndGroup(); organizationAndBatchAndGroup.setCode(attOrgBase.getGuid()); organizationAndBatchAndGroup.setName(attOrgBase.getOrgName()); if (orgId.equalsIgnoreCase(guid)) { organizationAndBatchAndGroup.setType("1"); } else { organizationAndBatchAndGroup.setType("2"); } ArrayList organizationAndBatchAndGroups = new ArrayList<>(); organizationAndBatchAndGroups.add(organizationAndBatchAndGroup); return organizationAndBatchAndGroups; } else { return null; } } else { //根据点击查询下一级,先判断是否是管理员,如果是,一查到底所有,如果不是查自己能看到的 UserRoleParam userRoleParam = new UserRoleParam(); userRoleParam.setUserId(userId); List list = userRoleDao.findList(userRoleParam); //暂时认为1是管理员2是普通用户 if (null != list && 0 < list.size()) { UserRole userRole = list.get(0); String roleId = userRole.getRoleId(); //根据角色分流 if ("1098064352187461632".equalsIgnoreCase(roleId)) { //管理员,一查到底,没限制 List result = new ArrayList(); switch (NumberUtils.toInt(type)) { case 1: //根据监督司查机构 AttOrgBaseParam attOrgBaseParam = new AttOrgBaseParam(); attOrgBaseParam.setPguid(pGuid); attOrgBaseParam.setAreaType("4"); List list1 = attOrgBaseDao.findList(attOrgBaseParam); if (null != list1 && 0 < list1.size()) { for (AttOrgBase att : list1) { OrganizationAndBatchAndGroup bean = new OrganizationAndBatchAndGroup(); bean.setType("2"); bean.setName(att.getOrgName()); bean.setCode(att.getGuid()); result.add(bean); } } break; case 2: //根据机构查询批次 BisInspPlanParam bisInspPlanParam = new BisInspPlanParam(); bisInspPlanParam.setGuid(code); List list2 = bisInspPlanDao.findList(bisInspPlanParam); if (null != list2 && 0 < list2.size()) { for (BisInspPlan bis : list2) { OrganizationAndBatchAndGroup bean = new OrganizationAndBatchAndGroup(); bean.setType("3"); bean.setName(bis.getPrsnTitle()); bean.setCode(bis.getPlnaId()); result.add(bean); } } break; case 3: //根据批次查组 BisInspGroupParam bisInspGroupParam = new BisInspGroupParam(); bisInspGroupParam.setGuid(guid); bisInspGroupParam.setPlnaId(code); bisInspGroupParam.setDataStat("1"); List list3 = bisInspGroupDao.findList(bisInspGroupParam); if (null != list3 && 0 < list3.size()) { for (BisInspGroup group : list3) { OrganizationAndBatchAndGroup bean = new OrganizationAndBatchAndGroup(); bean.setType("4"); bean.setName(group.getInspGroupName()); bean.setCode(group.getInspGroupId()); result.add(bean); } } } return result; } else { //为2,普通用户,只查自己参与的组 List result = new ArrayList(); switch (NumberUtils.toInt(type)) { case 1: //根据监督司查机构,组员应该进不到这个情况 AttOrgBaseParam attOrgBaseParam = new AttOrgBaseParam(); attOrgBaseParam.setPguid(pGuid); attOrgBaseParam.setAreaType("4"); List list1 = attOrgBaseDao.findList(attOrgBaseParam); if (null != list1 && 0 < list1.size()) { for (AttOrgBase att : list1) { OrganizationAndBatchAndGroup bean = new OrganizationAndBatchAndGroup(); bean.setType("2"); bean.setName(att.getOrgName()); bean.setCode(att.getGuid()); result.add(bean); } } break; case 2: //根据人员id查批次 List list2 = bisInspPlanDao.findListByUserId(userId); if (null != list2 && 0 < list2.size()) { for (BisInspPlan bis : list2) { OrganizationAndBatchAndGroup bean = new OrganizationAndBatchAndGroup(); bean.setType("3"); bean.setName(bis.getPrsnTitle()); bean.setCode(bis.getPlnaId()); result.add(bean); } } break; case 3: //根据批次查组,注意普通用户只查跟自己相关的组 List list3 = bisInspGroupDao.findListByBatchIdAndUserId(code, userId); if (null != list3 && 0 < list3.size()) { for (BisInspGroup group : list3) { OrganizationAndBatchAndGroup bean = new OrganizationAndBatchAndGroup(); bean.setType("4"); bean.setName(group.getInspGroupName()); bean.setCode(group.getInspGroupId()); result.add(bean); } } } return result; } } } return null; } @Autowired private OlBisInspOrgService olBisInspOrgService; public List getAllNode(String userid, String orgType, String orgId, String tabType, String year, String yearTaskId, String curYear, String leadDep) { if (orgType.length() == 2) { orgType = "0" + orgType; } if (orgType.length() == 1) { orgType = "00" + orgType; } if (orgType.length() == 4) { if (Integer.parseInt(orgType) > 100) { orgType = orgType.substring(1, orgType.length()); } } String province = olBisInspOrgService.getProvince(orgId); String nowTime = new SimpleDateFormat("yyyy-MM-dd").format(new Date()); if (null != year && year.equals("全部")) { year = null; } return bisInspAllDao.getAllNode(userid, orgType, province, orgId, tabType, nowTime, year, yearTaskId, curYear, leadDep); } public List getAllNodeById(String id) { // 前台传过来的字符串格式化成 in 能接受的 return bisInspAllDao.getAllNodeById(InspUtils.setOrgIds(id)); } public List getOrganizationTreeByParams(String guid, String userId, String type, String code, String objType) { //第一次查询code,type为空 List result = new ArrayList(); //非第一次查询,根据父id,查询子id集合 //非水利部,直接根据id查询根节点 BisInspAllParam bisInspAllParam = new BisInspAllParam(); //查水利部下面所有的职能部门 ______ bisInspAllParam.setPid(code); List list = bisInspAllDao.findList(bisInspAllParam); if (null != list && 0 < list.size()) { for (BisInspAll o : list) { OrganizationAndBatchAndGroup bean = new OrganizationAndBatchAndGroup(); //根据id长度判断type String typeStr = o.getId(); if (null != typeStr) { if (3 == typeStr.length()) { //待定是谁 bean.setType("1"); } else if (6 == typeStr.length()) { //各委员会 bean.setType("2"); } else if (9 == typeStr.length()) { //批次 bean.setType("3"); } else if (12 == typeStr.length()) { //组 bean.setType("4"); } } bean.setName(o.getPnm()); bean.setCode(o.getId()); result.add(bean); } } return result; } public List getAllNodeAndObj(String persId, String objType, int level, String province) { List list = new ArrayList<>(); level = level * 3; List bisList = bisInspAllDao.getAllNodeByLevel(persId, objType, level, province); boolean notHas = true; String maxNode = ""; String maxPid = ""; for (BisInspAll bis : bisList) { String id = bis.getId(); String pid = bis.getPid(); if (id.length() == level) { notHas = false; list = getNodeList(bisList, pid, province); } if (StringUtils.isBlank(maxNode)) { maxNode = id; maxPid = pid; } else { if (maxNode.length() >= id.length()) { maxNode = id; maxPid = pid; } } } if (notHas) { list = getNodeList(bisList, maxPid, province); } if (list != null) { if (list.size() > 1) { BisInspAllNode bisInspAllNode = new BisInspAllNode(); bisInspAllNode.setId(objType); bisInspAllNode.setPid("0"); bisInspAllNode.setChildren(list); list = new ArrayList<>(); list.add(bisInspAllNode); } } return list; } public List getNodeList(List bisList, String pid, String province) { List list = new ArrayList<>(); if (StringUtils.isNotBlank(pid)) { for (BisInspAll bis : bisList) { String id = bis.getId(); String pnm = bis.getPnm(); String pId = bis.getPid(); if (pid.equals(pId)) { List children = new ArrayList<>(); if (id.length() == 12) { String prov = province.replace("00", ""); children = bisInspAllDao.BisInspAllObjById(id, prov); for (BisInspAllNode child : children) { getChildrenList(child); } } else { children = getNodeList(bisList, id, province); } BisInspAllNode node = new BisInspAllNode(); node.setId(id); node.setPnm(pnm); node.setPid(pId); node.setChildren(children); list.add(node); } } } return list; } private void getChildrenList(BisInspAllNode node) { if ("004".equals(node.getPid().substring(0, 3))) { List secs = bisInspKeyRegisterSectionDao.getByRegId(node.getId()); for (BisInspKeyRegisterSection sec : secs) { BisInspAllNode secNode = new BisInspAllNode(); secNode.setId(sec.getId()); secNode.setObjId(sec.getObjId()); secNode.setPnm(sec.getNm()); secNode.setPid(node.getId()); secNode.setFlag("sec"); secNode.setChildren(new ArrayList<>()); List units = bisInspKeyRegSecUnitDao.getBySecId(sec.getId()); for (BisInspKeyRegSecUnit unit : units) { BisInspAllNode unitNode = new BisInspAllNode(); unitNode.setId(unit.getId()); unitNode.setPnm(unit.getNm()); unitNode.setObjId(unit.getObjId()); unitNode.setPid(sec.getId()); unitNode.setFlag("unit"); secNode.getChildren().add(unitNode); } if (node.getChildren() == null) { node.setChildren(new ArrayList<>()); } node.getChildren().add(secNode); } } else if ("007".equals(node.getPid().substring(0, 3))) { //得到所有单位信息 List mampus = new ArrayList<>(); List mampusSec = new ArrayList<>(); List mampusThi = new ArrayList<>(); AttMampuInfoParam param = new AttMampuInfoParam(); param.setLev("1"); param.setRegId("'" + node.getId() + "'"); mampus = attMampuInfoDao.getAttMampus(param); param.setLev("2"); mampusSec = attMampuInfoDao.getAttMampus(param); param.setLev("3"); mampusThi = attMampuInfoDao.getAttMampus(param); List nodes = new ArrayList<>(); Map map = nodes.stream().collect(Collectors.toMap(BisInspAllNode::getId, Function.identity())); map.put(node.getId(), node); for (AttMampuInfo info : mampus) { info.setPid(info.getRegId()); getNodeList(map, info, "mamp1"); } //缓存所有三级节点 Map nodeThr = new HashMap<>(); for (AttMampuInfo info : mampusSec) { Map parents = map.get(info.getRegId()).getChildren().stream().collect(Collectors.toMap(BisInspAllNode::getId, Function.identity())); BisInspAllNode inspAllNode = getNodeList(parents, info, "mamp2"); if (inspAllNode == null) { continue; } nodeThr.put(inspAllNode.getId(), inspAllNode); } for (AttMampuInfo info : mampusThi) { getNodeList(nodeThr, info, "mamp3"); } } } private BisInspAllNode getNodeList(Map map, AttMampuInfo info, String flag) { BisInspAllNode node = map.get(info.getPid()); if (node == null) { return null; } BisInspAllNode child = new BisInspAllNode(); child.setId(info.getId()); child.setPnm(info.getNm()); child.setObjId(node.getObjId()); child.setPid(node.getId()); child.setFlag(flag); if (node.getChildren() == null) { node.setChildren(new ArrayList<>()); } node.getChildren().add(child); return child; } }