package com.product.administration.service; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import com.google.common.collect.Lists; import com.google.common.collect.Maps; import com.google.common.collect.Sets; import com.product.admin.config.CmnConst; import com.product.admin.service.PublicService; import com.product.administration.service.ide.ITaskGoalService; import com.product.common.lang.StringUtils; import com.product.core.cache.DataPoolCacheImpl; import com.product.core.entity.DataTableEntity; import com.product.core.entity.FieldSetEntity; import com.product.core.exception.BaseException; import com.product.core.spring.context.SpringMVCContextHolder; import com.product.util.BaseUtil; import org.springframework.stereotype.Service; import java.util.*; /** * @Author cheng * @Description 任务指标 * @Date 2021/6/29 14:22 * @Version 1.0 */ @Service public class TaskGoalService extends PublicService implements ITaskGoalService { /** * 根据用户uuid & 单位组uuid查询员工所在机构 * * @param user_ids 用户uuids * @param group_uuids 单位组uuids * @return */ public List getStaffsLevelAll(String user_ids, String group_uuids) { if (StringUtils.isEmpty(user_ids) && StringUtils.isEmpty(group_uuids)) { return null; } if (!StringUtils.isEmpty(group_uuids)) { //查询单位组下的所有人员id String[] group_uuid = group_uuids.split(CmnConst.COMMA); StringBuilder sql = new StringBuilder(); sql.append(" SELECT "); sql.append(" CONCAT(user_ids) user_ids "); sql.append(" FROM "); sql.append("product_sys_company_user_group "); sql.append(" WHERE "); sql.append(BaseUtil.buildQuestionMarkFilter(CmnConst.UUID, group_uuid.length, true)); FieldSetEntity fs = getBaseDao().getFieldSetEntityBySQL(sql.toString(), group_uuid, false); if (fs != null) { if (StringUtils.isEmpty(user_ids)) { user_ids = ""; } else { user_ids += CmnConst.COMMA; } user_ids += fs.getString("user_ids"); } } //结果集合 List result = Lists.newArrayList(); if (!StringUtils.isEmpty(user_ids)) { String[] user_id = user_ids.split(CmnConst.COMMA); //放入set集合去重 Set u = Sets.newHashSet(user_id); //转为String数组 作为查询动态参数 user_id = u.toArray(new String[]{}); //获取需要展示的员工信息 DataTableEntity dt = getBaseDao().listTable(CmnConst.PRODUCT_SYS_STAFFS, BaseUtil.buildQuestionMarkFilter(CmnConst.USER_ID, user_id.length, true), user_id , new Object[]{"uuid", "user_id", "show_name", "dept_uuid", "org_level_uuid"}); //查询当前用户所在客户所有的组织机构 DataTableEntity org_level = getBaseDao().listTable(CmnConst.PRODUCT_SYS_ORG_LEVELS, "client_uuid=?", new Object[]{SpringMVCContextHolder.getCurrentUser().getClientUuid()}, new Object[]{"uuid", "org_level_code", "org_level_code_parent", "org_level_name", "org_level_type"}); // 所有机构信息 key为org_level_code Map orgLevel = getOrgLevel(org_level); List includeStaffOrg = Lists.newArrayList(); if (!BaseUtil.dataTableIsEmpty(org_level) && !BaseUtil.dataTableIsEmpty(dt) && orgLevel.size() > 0) { for (int i = 0; i < dt.getRows(); i++) { //员工所属部门uuid String org_level_uuid = dt.getString(i, CmnConst.DEPT_UUID); if (!StringUtils.isEmpty(org_level_uuid)) { List fieldSets = org_level.getFieldSetEntity(org_level_uuid); if (fieldSets != null && fieldSets.size() > 0) { FieldSetEntity fs = fieldSets.get(0); String org_level_code = fs.getString(CmnConst.ORG_LEVEL_CODE); //根据部门code获取部门信息 JSONObject currentDept = orgLevel.get(org_level_code); if (currentDept != null) { //获取部门下级 JSONArray children = currentDept.getJSONArray(CmnConst.CHILDREN); if (children == null) { children = new JSONArray(); currentDept.put(CmnConst.CHILDREN, children); } Map values = dt.getFieldSetEntity(i).getValues(); values.put("~type~", 2); values.put("name", values.get("show_name")); //在部门下级中增加当前遍历的人员 children.add(values); //是否包含该部门 if (!includeStaffOrg.contains(org_level_code)) { includeStaffOrg.add(org_level_code); } } } } } //排序 includeStaffOrg.sort(String::compareTo); //逆循环 (从最后往前) for (int i = includeStaffOrg.size() - 1; i > -1; i--) { orgLevelParent(result, orgLevel.get(includeStaffOrg.get(i)), orgLevel); } } } return result; } private JSONObject orgLevelTotalRow(JSONObject current){ JSONObject totalRow = new JSONObject(); current.put("totalRow", true); totalRow.put("name", current.getString(CmnConst.NAME) + "的合计行"); totalRow.put(CmnConst.ORG_LEVEL_UUID,current.getString(CmnConst.UUID)); totalRow.put(CmnConst.ORG_LEVEL_CODE,current.getString(CmnConst.ORG_LEVEL_CODE)); totalRow.put("~type~",3); return totalRow; } private void orgLevelParent(List result, JSONObject current, Map orgLevel) { String org_level_code_parent = current.getString(CmnConst.ORG_LEVEL_CODE_PARENT); //没有父级了 if (StringUtils.isEmpty(org_level_code_parent)) { result.add(current); result.add(orgLevelTotalRow(current)); } else { JSONObject parent_org = orgLevel.get(org_level_code_parent); JSONArray children = parent_org.getJSONArray(CmnConst.CHILDREN); if (children == null) { orgLevelParent(result, parent_org, orgLevel); } if (current != null) { JSONArray array = new JSONArray(); array.add(current); array.add(orgLevelTotalRow(current)); if (children != null) { array.addAll(children); } parent_org.put(CmnConst.CHILDREN, array); } } } /** * 将dt 转为 org_level_code 作为键的map * * @param dt * @return * @throws BaseException */ private Map getOrgLevel(DataTableEntity dt) throws BaseException { Map map = Maps.newHashMap(); if (!BaseUtil.dataTableIsEmpty(dt)) { JSONArray array = BaseUtil.dataTableEntityToJson(dt); for (int i = 0; i < array.size(); i++) { String code = dt.getString(i, CmnConst.ORG_LEVEL_CODE); array.getJSONObject(i).put("~type~", dt.getInt(i, CmnConst.ORG_LEVEL_TYPE)); array.getJSONObject(i).put("name", dt.getString(i, CmnConst.ORG_LEVEL_NAME)); map.put(code, array.getJSONObject(i)); } } return map; } /** * 查询功能字段 field_type = datetime,userid * * @param table_uuid * @return * @throws BaseException */ public Map getFieldsByFunction(String table_uuid) throws BaseException { DataTableEntity dt = DataPoolCacheImpl.getInstance().getCacheData("所有字段信息并按表分组", new String[]{table_uuid}); if (!BaseUtil.dataTableIsEmpty(dt)) { List user = Lists.newArrayList(); List datetime = Lists.newArrayList(); for (int i = 0; i < dt.getRows(); i++) { String field_type = dt.getString(i, CmnConst.FIELD_TYPE); HashMap values = (HashMap) dt.getFieldSetEntity(i).getValues(); values = (HashMap) values.clone(); if ("datetime".equals(field_type)) { datetime.add(values); } else if ("userid".equals(field_type)) { user.add(values); } else { values.clear(); } } Map m = Maps.newHashMap(); m.put("user", user); m.put("datetime", datetime); return m; } return null; } /** * 获取指标列表 * * @param fse * @return * @throws BaseException */ public DataTableEntity getList(FieldSetEntity fse) throws BaseException { return listTable(fse, true, 1, null, null); } public DataTableEntity getFucntionByRole() throws BaseException { String roles = SpringMVCContextHolder.getCurrentUser().getRoles(); if (!StringUtils.isEmpty(roles)) { StringBuilder sql = new StringBuilder(); sql.append(" SELECT uuid,function_name,function_description,table_uuid FROM product_sys_functions "); sql.append(" where uuid in (select function_uuid FROM product_sys_function_permission where "); sql.append(BaseUtil.buildQuestionMarkFilter("role_uuid", roles.split(CmnConst.COMMA).length, true)); sql.append(") "); return getBaseDao().listTable(sql.toString(), roles.split(CmnConst.COMMA)); } return null; } }