package com.bosshand.virgo.service; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import com.bosshand.virgo.core.dao.*; import com.bosshand.virgo.core.model.*; import com.bosshand.virgo.util.StringUtil; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.*; @Service public class DepartmentService { @Autowired MgrDepartmentDao mgrDepartmentDao; @Autowired MgrOrganizationTypeDao mgrOrganizationTypeDao; @Autowired MgrOrganizationDao mgrOrganizationDao; @Autowired MgrUserRoleDao mgrUserRoleDao; @Autowired MgrUserDao mgrUserDao; @Autowired AttendanceDao attendanceDao; public void saveDepartment(MgrDepartment department) { mgrDepartmentDao.insert(department); } public void deleteDepartment(int id) { mgrDepartmentDao.delete(id); } public void updateDepartment(MgrDepartment department) { mgrDepartmentDao.update(department); } public List getListByProjectId(long projectId) { List list = mgrDepartmentDao.getProjectId(projectId); if (list.size() > 0) { Set set = new HashSet<>(); list.forEach(ls -> set.add(ls.getOrganizationId())); MgrDepartment department = new MgrDepartment(); department.setProjectId(projectId); List organizationList = mgrOrganizationDao.organizationByIds(new ArrayList(set)); for (MgrOrganization organization : organizationList) { department.setOrganizationId(organization.getId()); organization.setDepartmentList(getList(department)); } return organizationList; } return new ArrayList(); } // get hierarchy public List getList(MgrDepartment department) { List mList = mgrUserRoleDao.getOrganizationId(department.getOrganizationId()); List list = mgrDepartmentDao.getList(department); list.forEach(ls -> ls.setUsers(getUserListByDepartment(mList, ls.getId()))); List ids = new ArrayList<>(); list.forEach(ls -> { if (ls.getParentId() == -1) { ids.add(ls.getId()); } }); List hierarchy = new ArrayList<>(); if (ids.size() > 0) { ids.forEach(ll -> hierarchy.add(getNodeTreeById(ll, list))); } return hierarchy; } private MgrDepartment getNodeTreeById(int id, List list) { MgrDepartment node = null; List departmentList = list; for (MgrDepartment data : departmentList) { if (data.getId() == id) { node = data; break; } } if (node != null) { node.setChildren(getChildNode(node.getId(), departmentList)); } return node; } private List getChildNode(int parentId, List list) { List nodeList = new ArrayList(); nodeList.clear(); for (MgrDepartment data : list) { if (data.getParentId() == parentId) { nodeList.add(data); } } if (nodeList.size() > 0) { for (MgrDepartment data : nodeList) { List childList = getChildNode(data.getId(), list); data.setChildren(childList); } } return nodeList; } private List getUserListByDepartment(List list, int departmentId) { List userIds = new ArrayList<>(); List mlist = new ArrayList<>(); list.forEach(ls -> { String departments = ls.getDepartments(); if (StringUtil.notBlank(departments)) { String[] split = departments.split(","); for (int i = 0; i < split.length; i++) { if(StringUtil.notBlank(split[i])) { if (Integer.parseInt(split[i]) == departmentId) { userIds.add(ls.getUserId()); mlist.add(ls); } } } } }); if (userIds.size() == 0) { return new ArrayList(); } List userList = mgrUserDao.getIds(userIds); for (MgrUser user : userList) { for (MgrUserRole ur : mlist) { if (user.getId() == ur.getUserId()) { user.setMenus(ur.getMenus()); user.setResources(ur.getResources()); user.setRoles(ur.getRoles()); user.setOrganizationTypeRoles(ur.getOrganizationTypeRoles()); } } } return userList; } // generation hierarchy public void saveDepartmentHierarchy(long organizationTypeId, long organizationId, long projectId) { MgrOrganizationType organizationType = mgrOrganizationTypeDao.get(organizationTypeId); String hierarchy = organizationType.getHierarchy(); recursion(organizationId, projectId, hierarchy, -1); } private void recursion(long organizationId, long projectId, String st, int parentId) { JSONArray list = JSON.parseArray(st); if (list.size() == 0) { return; } for (int i = 0; i < list.size(); i++) { MgrDepartment d = new MgrDepartment(); d.setOrganizationId(organizationId); d.setProjectId(projectId); d.setParentId(parentId); d.setTemplateId(Long.parseLong(list.getJSONObject(i).get("id").toString())); d.setName((String) list.getJSONObject(i).get("name")); mgrDepartmentDao.insert(d); recursion(organizationId, projectId, JSON.toJSONString(list.getJSONObject(i).get("children")), d.getId()); } } public Map statisticalListByProjectId(long projectId, String startDate, String endDate) { Map map = new HashMap<>(); List list = mgrDepartmentDao.getProjectId(projectId); if (list.size() > 0) { Set set = new HashSet<>(); list.forEach(ls -> set.add(ls.getOrganizationId())); MgrDepartment department = new MgrDepartment(); department.setProjectId(projectId); List organizationList = mgrOrganizationDao.organizationByIds(new ArrayList(set)); int con = 0; int si = 0; Attendance attendance = new Attendance(); attendance.setStartDate(startDate + " 00:00:00"); attendance.setEndDate(endDate + " 23:59:59"); for (MgrOrganization organization : organizationList) { department.setOrganizationId(organization.getId()); List mList = mgrUserRoleDao.getOrganizationId(department.getOrganizationId()); List alist = mgrDepartmentDao.getList(department); int cont = 0; for (MgrDepartment mgrDepartment : alist) { int size = getUserListByDepartment(mList, mgrDepartment.getId()).size(); cont += size; } JSONObject json = new JSONObject(); json.put("all", cont); attendance.setProjectId(projectId); attendance.setOrganizationId(organization.getId()); List users = attendanceDao.getByUser(attendance); json.put("sign", users.size()); map.put(organization.getName(), json); con += cont; si += users.size(); } map.put("项目总人数", con); map.put("allSign", si); } return map; } public String personByProjectId(long projectId) { List list = mgrDepartmentDao.getProjectId(projectId); JSONArray ja = new JSONArray(); if (list.size() > 0) { Set set = new HashSet<>(); list.forEach(ls -> set.add(ls.getOrganizationId())); MgrDepartment department = new MgrDepartment(); department.setProjectId(projectId); List organizationList = mgrOrganizationDao.organizationByIds(new ArrayList(set)); for (MgrOrganization organization : organizationList) { JSONObject js = new JSONObject(); department.setOrganizationId(organization.getId()); List mList = mgrUserRoleDao.getOrganizationId(department.getOrganizationId()); List alist = mgrDepartmentDao.getList(department); Set set1 = new HashSet<>(); for (MgrDepartment mgrDepartment : alist) { List userListByDepartment = getUserListByDepartment(mList, mgrDepartment.getId()); userListByDepartment.forEach(ls -> set1.add(ls)); } js.put("organizationName", organization.getName()); js.put("organizationId", organization.getId()); JSONArray jaa = new JSONArray(); for (MgrUser u : set1) { JSONObject jss = new JSONObject(); jss.put("userId", u.getId()); jss.put("userName", u.getName()); jaa.add(jss); } js.put("userIds", jaa); ja.add(js); } } return JSONObject.toJSONString(ja); } }