package com.product.org.admin.service; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import com.google.common.collect.Lists; import com.product.admin.service.CodeService; import com.product.admin.service.UpdateLoginUserInfoService; import com.product.core.cache.DataPoolCacheImpl; import com.product.core.dao.BaseDao; import com.product.core.entity.DataTableEntity; import com.product.core.entity.FieldMetaEntity; import com.product.core.entity.FieldSetEntity; import com.product.core.entity.SQLEntity; import com.product.core.exception.BaseException; import com.product.core.permission.PermissionService; import com.product.core.service.support.AbstractBaseService; import com.product.core.spring.context.SpringMVCContextHolder; import com.product.core.transfer.Transactional; import com.product.module.sys.entity.SystemUser; import com.product.org.admin.config.CmnConst; import com.product.org.admin.config.SystemCode; import com.product.org.admin.service.idel.ISystemOrgLevelsService; import java.util.ArrayList; import java.util.Date; import java.util.HashMap; import java.util.List; import java.util.Map; import com.product.util.BaseUtil; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.apache.commons.lang3.StringUtils; /** * Copyright © 2020 MR-BASE * * @Title: SystemOrgLevelsService * @Project: LX-BASE-SERVER * @Date: 2019-05-26 11:07 * @Author:Mr.Xu * @Description: 组织架构Service层 */ @Service public class SystemOrgLevelsService extends AbstractBaseService implements ISystemOrgLevelsService { @Autowired public BaseDao baseDao; @Autowired private CodeService codeService; @Autowired private PermissionService permissionService; @Autowired private UpdateLoginUserInfoService updateLoginUserInfoService; /** * 验证组织机构是否存在子级 * * @param uuid * @return * @throws BaseException */ public boolean verifyOrgChildExists(String uuid) throws BaseException { if (StringUtils.isEmpty(uuid)) { throw new BaseException(SystemCode.SYSTEM_FORM_NODATA.getValue(), SystemCode.SYSTEM_FORM_NODATA.getText(), this.getClass(), "verifyOrgChildExists"); } String sql = "select * FROM product_sys_org_levels a join product_sys_org_levels b on a.org_level_code=b.org_level_code_parent where a.uuid=?"; DataTableEntity listTable = baseDao.listTable(sql, new Object[]{uuid}); if (listTable != null && listTable.getRows() > 0) { return false; } return true; } /** * 查询组织机构列表 * * @param fse * @return * @throws BaseException */ public JSONArray listOrgLevels(FieldSetEntity fse) throws BaseException { if (SpringMVCContextHolder.getCurrentUser() != null && SpringMVCContextHolder.getCurrentUser().getUuid() != null) { String sql = ""; String dept_parent = null; DataTableEntity data = new DataTableEntity(); Object[] objects; SystemUser user = SpringMVCContextHolder.getCurrentUser(); String clientUuid = fse.getString(CmnConst.CLIENT_UUID); Integer org_level_type = fse.getInteger(CmnConst.ORG_LEVEL_TYPE); if (1 == org_level_type) {// 部门 dept_parent = fse.getString(CmnConst.ORG_LEVEL_CODE); if (StringUtils.isEmpty(dept_parent)) { sql = "select a.*,(select count(*) from " + CmnConst.PRODUCT_SYS_STAFFS + " where dept_uuid=a.uuid) staff_number from " + CmnConst.PRODUCT_SYS_ORG_LEVELS + " a where " + CmnConst.ORG_LEVEL_TYPE + "=? " + " ORDER BY length(org_level_code),sequence asc,org_level_code"; objects = new Object[]{1}; data = baseDao.listInternationDataTable(baseDao.listTable(sql, objects), ""); } else { sql = "select a.*,(select count(*) from " + CmnConst.PRODUCT_SYS_STAFFS + " where dept_uuid=a.uuid) staff_number from " + CmnConst.PRODUCT_SYS_ORG_LEVELS + " a where " + CmnConst.ORG_LEVEL_CODE_PARENT + " like ? and " + CmnConst.ORG_LEVEL_TYPE + "=? " + " ORDER BY length(org_level_code),sequence asc,org_level_code"; objects = new Object[]{fse.getString(CmnConst.ORG_LEVEL_CODE) + "%", 1}; data = baseDao.listInternationDataTable(baseDao.listTable(sql, objects), ""); } } else if (user.getUserType() == 3 || user.getUserType() == 4) {// 公司 if (clientUuid == null) { clientUuid = user.getClientUuid(); } if (clientUuid == null) { return null; } // updatetime 2021年1月9日13:04:31 xupengcheng 修改隐藏管理员查看公司只能查看当前公司,加多单位过滤 String orgLevelFilter = permissionService.getDataFilter("uuid"); String filter = orgLevelFilter + " and " + CmnConst.ORG_LEVEL_TYPE + "=? and " + CmnConst.ORG_LEVEL_STATUS + "=? and (" + CmnConst.CLIENT_UUID + " = ?"; String[] clients_uuid = clientUuid.split(","); objects = new Object[2 + clients_uuid.length]; objects[0] = fse.getInteger(CmnConst.ORG_LEVEL_TYPE); objects[1] = 0; objects[2] = clients_uuid[0]; for (int i = 1; i < clients_uuid.length; i++) { filter += " or client_uuid=? "; objects[2 + i] = clients_uuid[i]; } filter += ")"; data = baseDao.listInternationDataTable( baseDao.listTable(CmnConst.PRODUCT_SYS_ORG_LEVELS, filter, objects), ""); } else if (user.getUserType() == 2) { sql = "select level.*," + " case when security_password_length then security_password_length else '' end security_password_length, " + " case when security_upper=1 then '是' else '' end security_upper, " + " case when security_lower=1 then '是' else '' end security_lower, " + " case when security_number=1 then '是' else '' end security_number, " + " case when security_other=1 then '是' else '' end security_other, " + " case when security_repeate_history_password=1 then '是' else '' end security_repeate_history_password, " + " case when security_update_frequency_day then security_update_frequency_day else '' end security_update_frequency_day " + " from " + CmnConst.PRODUCT_SYS_ORG_LEVELS + " level LEFT JOIN " + CmnConst.PRODUCT_SYS_PASSWORD_SECURITY + " secur ON level.uuid=secur.org_level_uuid " + " where level." + CmnConst.ORG_LEVEL_TYPE + "=? and level." + CmnConst.ORG_LEVEL_STATUS + "=? and ( level." + CmnConst.UUID + "=? or org_level_code_parent like CONCAT((select org_level_code FROM product_sys_org_levels where uuid=?),'%'))"; sql += " Order by level.org_level_code_parent,level.org_level_code,level.sequence"; String org_level = SpringMVCContextHolder.getCurrentUser().getOrg_level_uuid(); objects = new Object[]{fse.getInteger(CmnConst.ORG_LEVEL_TYPE), 0, org_level, org_level}; DataTableEntity data2 = baseDao.listInternationDataTable(baseDao.listTable(sql, objects), ""); if (data2.getRows() > 0) { for (int i = 0; i < data2.getRows(); i++) { data.addFieldSetEntity(data2.getFieldSetEntity(i)); } } } else if (user.getUserType() == 1) { sql = "select level.*," + " case when security_password_length then security_password_length else '' end security_password_length, " + " case when security_upper=1 then '是' else '' end security_upper, " + " case when security_lower=1 then '是' else '' end security_lower, " + " case when security_number=1 then '是' else '' end security_number, " + " case when security_other=1 then '是' else '' end security_other, " + " case when security_repeate_history_password=1 then '是' else '' end security_repeate_history_password, " + " case when security_update_frequency_day then security_update_frequency_day else '' end security_update_frequency_day " + " from " + CmnConst.PRODUCT_SYS_ORG_LEVELS + " level LEFT JOIN " + CmnConst.PRODUCT_SYS_PASSWORD_SECURITY + " secur ON level.uuid=secur.org_level_uuid " + " where " + CmnConst.ORG_LEVEL_TYPE + " =? and " + CmnConst.ORG_LEVEL_STATUS + "=?"; sql += " Order by org_level_code_parent,org_level_code,sequence"; objects = new Object[]{fse.getInteger(CmnConst.ORG_LEVEL_TYPE), 0}; DataTableEntity data2 = baseDao.listInternationDataTable(baseDao.listTable(sql, objects), ""); if (data2.getRows() > 0) { for (int i = 0; i < data2.getRows(); i++) { data.addFieldSetEntity(data2.getFieldSetEntity(i)); } } } else { // 公司 sql = "select * from " + CmnConst.PRODUCT_SYS_ORG_LEVELS + " where " + CmnConst.ORG_LEVEL_TYPE + "=? and " + CmnConst.ORG_LEVEL_STATUS + "=? and ( " + CmnConst.CREATED_BY + " =? or " + CmnConst.ORG_LEVEL_MANAGER_UUID + "=? )"; sql += " Order by org_level_code_parent,org_level_code,sequence"; if (StringUtils.isEmpty(clientUuid)) { return null; } for (String s : clientUuid.split(",")) { objects = new Object[]{fse.getInteger(CmnConst.ORG_LEVEL_TYPE), 0, SpringMVCContextHolder.getCurrentUser().getUser_id(), s}; DataTableEntity data2 = baseDao.listInternationDataTable(baseDao.listTable(sql, objects), ""); if (data2.getRows() > 0) { for (int i = 0; i < data2.getRows(); i++) { data.addFieldSetEntity(data2.getFieldSetEntity(i)); } } } } for (int i = 0; i < data.getRows(); i++) { FieldSetEntity fieldSetEntity = data.getFieldSetEntity(i); DataTableEntity org_level_name = fieldSetEntity.getSubDataTable("org_level_name"); if (!BaseUtil.dataTableIsEmpty(org_level_name)) { List names = new ArrayList<>(); for (int j = 0; j < org_level_name.getRows(); j++) { Map name = new HashMap<>(); FieldSetEntity fieldSetEntity1 = org_level_name.getFieldSetEntity(j); name.put(fieldSetEntity1.getString(CmnConst.LANGUAGE_CODE), fieldSetEntity1.getString(fieldSetEntity1.getString(CmnConst.LANGUAGE_CODE))); names.add(name); } fieldSetEntity.setValue("org_level_name", names); } } if (!BaseUtil.dataTableIsEmpty(data)) {// 封装树 return OrgLevelsTree(data, fse.getInteger(CmnConst.ORG_LEVEL_TYPE), dept_parent); } return new JSONArray(); } // 用户获取失败 throw new BaseException(SystemCode.SYSTEM_USER_ACQUIRE_FAIL.getValue(), SystemCode.SYSTEM_USER_ACQUIRE_FAIL.getText(), this.getClass(), "listCompany"); } public int sumStaff(FieldSetEntity fs, int sum) throws BaseException { if (fs.getValue(CmnConst.CHILDREN) != null) { sum += fs.getInteger(CmnConst.STAFF_NUMBER); DataTableEntity dt = (DataTableEntity) fs.getValue(CmnConst.CHILDREN); for (int i = 0; i < dt.getRows(); i++) { FieldSetEntity fss = dt.getFieldSetEntity(i); if (fss.getValue(CmnConst.CHILDREN) != null) { this.sumStaff(fss, sum); } else { sum += fss.getInteger(CmnConst.STAFF_NUMBER); } } } else { return sum; } return sum; } /** * 新增公司 * * @param fse * @return * @throws BaseException */ @Transactional public String addCompany(FieldSetEntity fse) throws BaseException { //获取公司名称 String org_level_name=fse.getString(CmnConst.ORG_LEVEL_NAME); //获取公司上级公司 String org_level_code_parent=fse.getString(CmnConst.ORG_LEVEL_CODE_PARENT); // 公司名称唯一验证 uniqueVerification(org_level_name,null, org_level_code_parent, 0); if (StringUtils.isEmpty(org_level_code_parent)) { fse.setValue(CmnConst.ORG_LEVEL_CODE_PARENT, ""); } // 拼接机构全称 createOrgLevelAllLanguage(fse, org_level_name, org_level_code_parent); SystemUser currentUser = SpringMVCContextHolder.getCurrentUser(); fse.setValue(CmnConst.ORG_LEVEL_MANAGER_UUID, currentUser.getUuid()); fse.setValue(CmnConst.ORG_LEVEL_TYPE, 0); // 创建机构编码 fse.setValue(CmnConst.ORG_LEVEL_CODE, codeService.createCode(CmnConst.PRODUCT_SYS_ORG_LEVELS, CmnConst.ORG_LEVEL_CODE, fse.getString(CmnConst.ORG_LEVEL_CODE_PARENT))); fse.setValue(CmnConst.CLIENT_UUID, currentUser.getClient_uuid()); fse.setValue(CmnConst.ORG_LEVEL_STATUS, 0); fse.setValue(CmnConst.CREATED_BY, currentUser.getUser_id()); fse.setValue(CmnConst.CREATED_UTC_DATETIME, new Date()); String uuid = baseDao.add(fse, true); if (null != uuid) { FieldSetEntity fs = new FieldSetEntity(); FieldMetaEntity f = new FieldMetaEntity(); f.setTableName(new Object[]{CmnConst.PRODUCT_SYS_DATA_STRATEGY_MASTER}); fs.setMeta(f); fs.setValue(CmnConst.ORG_LEVEL_UUID, fse.getString(CmnConst.UUID)); fs.setValue("property_uuid", "001"); fs.setValue("is_used", "1"); fs.setValue("effective_start_date", new Date()); fs.setValue(CmnConst.CREATED_BY, currentUser.getUser_id()); fs.setValue(CmnConst.CREATED_UTC_DATETIME, new Date()); baseDao.add(fs); } if (currentUser.getUserType() == 2) { FieldSetEntity currentManager = currentUser.getCurrentManager(); String manager_org_level_uuid = currentManager.getString(CmnConst.ORG_LEVEL_UUID); if (StringUtils.isEmpty(manager_org_level_uuid)) { manager_org_level_uuid = uuid; } else { manager_org_level_uuid = manager_org_level_uuid + "," + uuid; } currentManager.setValue(CmnConst.ORG_LEVEL_UUID, manager_org_level_uuid); baseDao.saveFieldSetEntity(currentManager); updateLoginUserInfoService.updateUserInfoByUserId(currentUser.getUser_id(), 1, true); } // 回写到隐藏管理员所管理的公司 // baseDao.executeUpdate("UPDATE product_sys_org_manager SET org_level_uuid =(SELECT group_concat(UUID) FROM product_sys_org_levels WHERE CLIENT_UUID =? AND ORG_LEVEL_TYPE =0) WHERE CLIENTS_UUID=? AND MANAGER_TYPE=? " // , new Object[]{fse.getString(CmnConst.CLIENT_UUID), fse.getString(CmnConst.CLIENT_UUID), 3}); return uuid; } /** * 组织机构名称唯一值验证 * * @param org_level_name * @param uuid * @throws BaseException */ public void uniqueVerification(String org_level_name, String uuid,String org_level_code_parent,Integer type) throws BaseException { List param= Lists.newArrayList(); String filter=" org_level_type = "+ type +" and org_level_name= ? "; param.add(org_level_name); if (!StringUtils.isEmpty(uuid)) { param.add(uuid); filter += " AND uuid != ? "; } if (!StringUtils.isEmpty(org_level_code_parent)) { filter += " AND org_level_code_parent = ?"; param.add(org_level_code_parent); } DataTableEntity dt=baseDao.listTable(CmnConst.PRODUCT_SYS_ORG_LEVELS, filter, param.toArray()); if (!BaseUtil.dataTableIsEmpty(dt)) { throw new BaseException(SystemCode.SYSTEM_COMPANY_NAME_DUPLICATE_OPERATION_FIAL.getValue(), SystemCode.SYSTEM_COMPANY_NAME_DUPLICATE_OPERATION_FIAL.getText()); } } /** * 更新公司 * * @param fseNew * @throws BaseException */ @Transactional public boolean updateCompany(FieldSetEntity fseNew) throws BaseException { // 公司名称唯一验证 uniqueVerification(fseNew.getString(CmnConst.ORG_LEVEL_NAME),fseNew.getUUID(),fseNew.getString(CmnConst.ORG_LEVEL_CODE_PARENT), 0); // 原数据信息 FieldSetEntity fseOrigin = baseDao.getFieldSetEntity(CmnConst.PRODUCT_SYS_ORG_LEVELS, fseNew.getString(CmnConst.UUID), false); // 获取相应组织机构上级编码 String newOrgLevelCodeParent = fseNew.getString(CmnConst.ORG_LEVEL_CODE_PARENT); String originOrgLevelCodeParent = fseOrigin.getString(CmnConst.ORG_LEVEL_CODE_PARENT); if (newOrgLevelCodeParent == null) { newOrgLevelCodeParent = ""; } if (originOrgLevelCodeParent == null) { originOrgLevelCodeParent = ""; } // 上级组织机构发生改变 if (!newOrgLevelCodeParent.equals(originOrgLevelCodeParent)) { // 生成当前新公司编码 String newOrgLevelCode = codeService.createCode(fseNew, CmnConst.PRODUCT_SYS_ORG_LEVELS, CmnConst.ORG_LEVEL_CODE, newOrgLevelCodeParent); // fseNew.setValue(CmnConst.ORG_LEVEL_CODE, newOrgLevelCode); // 获取变更前组织机构编码 String originOrgLevelCode = fseOrigin.getString(CmnConst.ORG_LEVEL_CODE); // 变更相关组织机构编码,父级组织机构编码 changeCode(originOrgLevelCode, newOrgLevelCode); // 变更相关组织机构全称 changeOrgLevelAll(fseNew, originOrgLevelCodeParent, newOrgLevelCodeParent); } else if (!fseOrigin.getString(CmnConst.ORG_LEVEL_NAME).equals(fseNew.getString(CmnConst.ORG_LEVEL_NAME))) { changeOrgLevelAll(fseNew, originOrgLevelCodeParent, newOrgLevelCodeParent); } // 更新组织机构下属员工信息 updateLoginUserInfoService.updateUserInfoByOrgLevelUuid(new String[]{fseNew.getUUID()}, 1, true); return baseDao.update(fseNew, true); } /** * 变更组织机构全称 * * @param fseOrgInfo * @param originParentCode * @param newParentCode */ public void changeOrgLevelAll(FieldSetEntity fseOrgInfo, String originParentCode, String newParentCode) { String newOrgLevelAll = null; String originOrgLevelAll = fseOrgInfo.getString(CmnConst.ORG_LEVEL_ALL); //获取原组织机构全称 String orgLevelName = fseOrgInfo.getString(CmnConst.ORG_LEVEL_NAME); //获取组织机构名称 if (!StringUtils.isEmpty(newParentCode)) { //新上级组织机构 FieldSetEntity fseNewParent = baseDao.getFieldSetEntityByFilter(CmnConst.PRODUCT_SYS_ORG_LEVELS, "org_level_code=?", new Object[]{newParentCode}, false); if (fseNewParent == null) { throw new BaseException(SystemCode.SYSTEM_ORG_LEVEL_PARENT_NO_EXIST.getValue(), SystemCode.SYSTEM_ORG_LEVEL_PARENT_NO_EXIST.getText(), this.getClass(), "changeOrgLevelAll"); } newOrgLevelAll = fseNewParent.getString(CmnConst.ORG_LEVEL_ALL) + ">" + orgLevelName; fseOrgInfo.setValue(CmnConst.ORG_LEVEL_ALL, newOrgLevelAll); } else { //无上级组织机构 newOrgLevelAll = orgLevelName; fseOrgInfo.setValue(CmnConst.ORG_LEVEL_ALL, newOrgLevelAll); } changeOrgLevelAll(originOrgLevelAll, newOrgLevelAll, fseOrgInfo.getString(CmnConst.ORG_LEVEL_CODE)); } /** * 变更相关组织机构全称 * * @param originOrgLevelAll * @param newOrgLevelAll * @param org_level_code */ public void changeOrgLevelAll(String originOrgLevelAll, String newOrgLevelAll, String org_level_code) { String sql = "UPDATE product_sys_org_levels SET org_level_all=REPLACE(org_level_all,?,?) WHERE org_level_code like ?"; baseDao.executeUpdate(sql, new Object[]{originOrgLevelAll, newOrgLevelAll, org_level_code + "%"}); } /** * 更改当前公司下所有组织机构编码,组织机构父级编码,员工编码 * * @param originCode 原公司编码 * @param newCode 新公司编码 * @return * @throws BaseException */ public boolean changeCode(String originCode, String newCode) throws BaseException { //郑盟 2020/12/18 boolean flag = true; String sqlOrgCode = "update " + CmnConst.PRODUCT_SYS_ORG_LEVELS + " set org_level_code=concat('" + newCode + "', mid(org_level_code,length('" + originCode + "')+1)) where org_level_code like '" + originCode + "%'"; flag = baseDao.executeUpdate(sqlOrgCode); if (!flag) { throw new BaseException(SystemCode.SYSTEM_ORG_UPDATE_FAIL_ORGCODE.getValue(), SystemCode.SYSTEM_ORG_UPDATE_FAIL_ORGCODE.getText(), this.getClass(), "changeCode"); } String sqlOrgCodeParent = "update " + CmnConst.PRODUCT_SYS_ORG_LEVELS + " set org_level_code_parent=concat('" + newCode + "', mid(org_level_code_parent,length('" + originCode + "')+1)) where org_level_code_parent like '" + originCode + "%'"; flag = baseDao.executeUpdate(sqlOrgCodeParent); if (!flag) { throw new BaseException(SystemCode.SYSTEM_ORG_UPDATE_FAIL_ORGPARENTCODE.getValue(), SystemCode.SYSTEM_ORG_UPDATE_FAIL_ORGPARENTCODE.getText(), this.getClass(), "changeCode"); } DataPoolCacheImpl.getInstance().reloadCodeManager(CmnConst.PRODUCT_SYS_ORG_LEVELS, CmnConst.ORG_LEVEL_CODE); String sqlCodeStaff = "update " + CmnConst.PRODUCT_SYS_STAFFS + " set tricode=concat('" + newCode + "', mid(tricode,length('" + originCode + "')+1)) where tricode like '" + originCode + "%'"; flag = baseDao.executeUpdate(sqlCodeStaff); if (!flag) { throw new BaseException(SystemCode.SYSTEM_ORG_UPDATE_FAIL_ORGSTAFFCODE.getValue(), SystemCode.SYSTEM_ORG_UPDATE_FAIL_ORGSTAFFCODE.getText(), this.getClass(), "changeCode"); } String sqlCodeStaffDirect = "update " + CmnConst.PRODUCT_SYS_STAFFS + " set direct_leader_code=concat('" + newCode + "', mid(direct_leader_code,length('" + originCode + "')+1)) where direct_leader_code like '" + originCode + "%'"; flag = baseDao.executeUpdate(sqlCodeStaffDirect); if (!flag) { throw new BaseException(SystemCode.SYSTEM_ORG_UPDATE_FAIL_ORGSTAFFCODE.getValue(), SystemCode.SYSTEM_ORG_UPDATE_FAIL_ORGSTAFFCODE.getText(), this.getClass(), "changeCode"); } DataPoolCacheImpl.getInstance().reloadCodeManager(CmnConst.PRODUCT_SYS_STAFFS, "tricode"); return flag; } /** * 查询组织机构详情 * * @param fse * @return * @throws BaseException */ public FieldSetEntity findOrgLevel(FieldSetEntity fse) throws BaseException { return baseDao.listInternationDataTable( baseDao.getFieldSetEntityByFilter(CmnConst.PRODUCT_SYS_ORG_LEVELS, CmnConst.UUID + "=? and " + CmnConst.ORG_LEVEL_TYPE + "=?", new Object[]{fse.getString(CmnConst.UUID), fse.getString(CmnConst.ORG_LEVEL_TYPE)}, true), null); } @Transactional @Override public boolean deleteCompany(FieldSetEntity fse) throws BaseException { String uuid = fse.getUUID(); if (StringUtils.isEmpty(uuid)) { throw new BaseException(SystemCode.SYSTEM_COMANY_DELETE_FAIL.getValue(), SystemCode.SYSTEM_COMANY_DELETE_FAIL.getText(), this.getClass(), "public boolean deleteCompany(FieldSetEntity fse) throws BaseException"); } // 存在下级公司不能删除 if (!verifyOrgChildExists(fse.getString(CmnConst.UUID))) { throw new BaseException(SystemCode.SYSTEM_ORG_UPDATE_EXIST_CHILDREN_FAIL.getValue(), SystemCode.SYSTEM_ORG_UPDATE_EXIST_CHILDREN_FAIL.getText(), this.getClass(), "public boolean deleteCompany(FieldSetEntity fse) throws BaseException"); } //删除公司数据策略 baseDao.delete(CmnConst.PRODUCT_SYS_DATA_STRATEGY_MASTER, "org_level_uuid=?", new String[]{uuid}); //删除公司 baseDao.delete(CmnConst.PRODUCT_SYS_ORG_LEVELS, "uuid=?", new String[]{uuid}); //删除管理员管理里该公司的uuid baseDao.executeUpdate("update product_sys_org_manager set org_level_uuid=replace(replace(org_level_uuid,concat(',',?),''),concat(?,','),'') where org_level_uuid like concat('%',?,'%')", new Object[]{uuid,uuid,uuid}); return true; } /** * 部门列表(包含公司) * * @param fse * @return * @throws BaseException */ public DataTableEntity listDepartment(FieldSetEntity fse) throws BaseException { if (SpringMVCContextHolder.getCurrentUser() != null && SpringMVCContextHolder.getCurrentUser().getUuid() != null) { FieldSetEntity fs = baseDao.getFieldSetEntity(CmnConst.PRODUCT_SYS_ORG_LEVELS, fse.getString(CmnConst.ORG_LEVEL_UUID), false); if (fs == null) { // 抛出请求参数错误 throw new BaseException(SystemCode.SYSTEM_FORM_COUNT.getValue(), SystemCode.SYSTEM_FORM_COUNT.getText(), this.getClass(), "listDepartment"); } SQLEntity sql = new SQLEntity(); sql.setTableNames(CmnConst.PRODUCT_SYS_ORG_LEVELS); sql.setFilter(CmnConst.ORG_LEVEL_TYPE + "=? and " + CmnConst.ORG_LEVEL_STATUS + "=?"); sql.setParas(new Object[]{1, 0}); sql.setOrderBy("org_level_code_parent"); DataTableEntity dt = baseDao.listTable(sql); if (dt != null && dt.getRows() > 0) { return findDepartmentChild(dt, fs, new DataTableEntity()); } return null; } // 用户获取失败 throw new BaseException(SystemCode.SYSTEM_USER_ACQUIRE_FAIL.getValue(), SystemCode.SYSTEM_USER_ACQUIRE_FAIL.getText(), this.getClass(), "listDepartment"); } public JSONArray OrgLevelsTree(DataTableEntity data, Integer org_level_type, String dept_parent) throws BaseException { JSONArray menus = new JSONArray(); JSONObject parent_menu = new JSONObject(); if (data != null && data.getRows() > 0) { // for (int i = 0; i < data.getRows(); i++) { // FieldSetEntity fs=data.getFieldSetEntity(i); // //|| (!StringUtils.isEmpty(dept_parent) && dept_parent.equals(fs.getString(CmnConst.ORG_LEVEL_CODE_PARENT)))|| (!StringUtils.isEmpty(fs.getString(CmnConst.ORG_LEVEL_CODE_PARENT)) && fs.getString(CmnConst.ORG_LEVEL_CODE_PARENT).equals("001")) // if(StringUtils.isEmpty(fs.getString(CmnConst.ORG_LEVEL_CODE_PARENT)) || (!StringUtils.isEmpty(dept_parent) && dept_parent.equals(fs.getString(CmnConst.ORG_LEVEL_CODE)))) { // JSONObject org_top=new JSONObject(); // org_top.put(CmnConst.UUID,fs.getString(CmnConst.UUID)); // org_top.put(CmnConst.CLIENT_UUID,fs.getString(CmnConst.CLIENT_UUID)); // org_top.put(CmnConst.ORG_LEVEL_NAME,fs.getObject(CmnConst.ORG_LEVEL_NAME)); // org_top.put(CmnConst.ORG_LEVEL_CODE,fs.getString(CmnConst.ORG_LEVEL_CODE)); // org_top.put(CmnConst.ORG_LEVEL_CODE_PARENT,fs.getString(CmnConst.ORG_LEVEL_CODE_PARENT)); // org_top.put(CmnConst.ORG_LEVEL_TYPE,fs.getString(CmnConst.ORG_LEVEL_TYPE)); // if(org_level_type==1)org_top.put("staff_number", fs.getInteger("staff_number")); // org_top.put("children", OrgLevelChildrenTree(data,fs.getString(CmnConst.ORG_LEVEL_CODE),org_level_type)); // top_array.add(org_top); // } // } for (int i = 0; i < data.getRows(); i++) { FieldSetEntity fs = data.getFieldSetEntity(i); JSONObject menu = BaseUtil.fieldSetEntityToJson(fs); String pcode = fs.getString("org_level_code_parent"); if (StringUtils.isEmpty(pcode) || parent_menu.getString(pcode) == null) { menus.add(menu); } else {// 上级菜单 JSONObject pm = parent_menu.getJSONObject(pcode); JSONArray subs = pm.getJSONArray(CmnConst.CHILDREN); JSONArray submenus = null; if (subs == null) { submenus = new JSONArray(); pm.put(CmnConst.CHILDREN, submenus); } else { submenus = subs; } submenus.add(menu); } parent_menu.put(menu.getString(CmnConst.ORG_LEVEL_CODE), menu); } } return menus; } public JSONArray OrgLevelChildrenTree(DataTableEntity data, String level_code_parent, Integer org_level_type) throws BaseException { JSONArray children_array = new JSONArray(); if (data != null && data.getRows() > 0 && !StringUtils.isEmpty(level_code_parent)) { for (int i = 0; i < data.getRows(); i++) { FieldSetEntity fs = data.getFieldSetEntity(i); if (level_code_parent.equals(fs.getString(CmnConst.ORG_LEVEL_CODE_PARENT))) { JSONObject org_children = new JSONObject(); org_children.put(CmnConst.UUID, fs.getString(CmnConst.UUID)); org_children.put(CmnConst.ORG_LEVEL_NAME, fs.getObject(CmnConst.ORG_LEVEL_NAME)); org_children.put(CmnConst.CLIENT_UUID, fs.getString(CmnConst.CLIENT_UUID)); org_children.put(CmnConst.ORG_LEVEL_CODE, fs.getString(CmnConst.ORG_LEVEL_CODE)); org_children.put(CmnConst.ORG_LEVEL_CODE_PARENT, fs.getString(CmnConst.ORG_LEVEL_CODE_PARENT)); org_children.put(CmnConst.ORG_LEVEL_TYPE, fs.getString(CmnConst.ORG_LEVEL_TYPE)); if (org_level_type == 1) org_children.put(CmnConst.STAFF_NUMBER, fs.getInteger(CmnConst.STAFF_NUMBER)); if (org_level_type == 1) org_children.put("staff_sum", fs.getInteger(CmnConst.STAFF_NUMBER)); org_children.put(CmnConst.CHILDREN, OrgLevelChildrenTree(data, fs.getString(CmnConst.ORG_LEVEL_CODE), org_level_type)); children_array.add(org_children); } } } return children_array; } private DataTableEntity findDepartmentChild(DataTableEntity dt, FieldSetEntity fse, DataTableEntity newDt) throws BaseException { if (dt != null && dt.getRows() > 0 && fse != null) { String org_level_code = fse.getString(CmnConst.ORG_LEVEL_CODE); if ("1".equals(fse.getString(CmnConst.ORG_LEVEL_TYPE))) { newDt.addFieldSetEntity(fse); newDt.setMeta(fse.getMeta()); } for (int i = 0; i < dt.getRows(); i++) { FieldSetEntity fs = dt.getFieldSetEntity(i); if (StringUtils.equals(org_level_code, fs.getString(CmnConst.ORG_LEVEL_CODE_PARENT))) { newDt = findDepartmentChild(dt, fs, newDt); } } return newDt; } return null; } /** * 新增部门 * * @param fse * @return * @throws BaseException */ @Transactional public String addDepartment(FieldSetEntity fse) throws BaseException { uniqueVerification(fse.getString(CmnConst.ORG_LEVEL_NAME), null,fse.getString(CmnConst.ORG_LEVEL_CODE_PARENT), 1); createOrgLevelAllLanguage(fse, fse.getString(CmnConst.ORG_LEVEL_NAME), fse.getString(CmnConst.ORG_LEVEL_CODE_PARENT)); fse.setValue(CmnConst.ORG_LEVEL_TYPE, 1); fse.setValue(CmnConst.ORG_LEVEL_CODE, codeService.createCode(CmnConst.PRODUCT_SYS_ORG_LEVELS, CmnConst.ORG_LEVEL_CODE, fse.getString(CmnConst.ORG_LEVEL_CODE_PARENT))); fse.setValue(CmnConst.CLIENT_UUID, SpringMVCContextHolder.getCurrentUser().getClient_uuid()); // 待修改 fse.setValue(CmnConst.ORG_LEVEL_STATUS, 0); DataTableEntity orgLevelDte = baseDao.listTable(CmnConst.PRODUCT_SYS_ORG_LEVELS, "? like concat(org_level_code,'%') and org_level_type=0", new Object[]{fse.getString(CmnConst.ORG_LEVEL_CODE)}, "org_level_code desc"); if (!BaseUtil.dataTableIsEmpty(orgLevelDte)) { fse.setValue(CmnConst.ORG_LEVEL_UUID, orgLevelDte.getFieldSetEntity(0).getUUID()); } BaseUtil.createCreatorAndCreationTime(SpringMVCContextHolder.getCurrentUser(), fse); return baseDao.add(fse); } /** * 创建组织机构全称国际化 * * @param fse * @return * @throws BaseException */ public void createOrgLevelAllLanguage(FieldSetEntity fse, String org_level_name, String org_level_code_parent) throws BaseException { //判断上级组织机构是否存在 if (StringUtils.isEmpty(org_level_code_parent)) { fse.setValue(CmnConst.ORG_LEVEL_ALL, org_level_name); } else { //获取上级组织机构 FieldSetEntity fseOrgParent = baseDao.getFieldSetEntityByFilter(CmnConst.PRODUCT_SYS_ORG_LEVELS, "org_level_code=?", new Object[]{org_level_code_parent}, false); if (fseOrgParent == null) { throw new BaseException(SystemCode.SYSTEM_ORG_LEVEL_PARENT_NO_EXIST.getValue(), SystemCode.SYSTEM_ORG_LEVEL_PARENT_NO_EXIST.getText(), this.getClass(), "addDepartment"); } //获取上级组织机构全称 String org_level_parent_all = fseOrgParent.getString(CmnConst.ORG_LEVEL_ALL); //拼接当前组织机构全称 fse.setValue(CmnConst.ORG_LEVEL_ALL, org_level_parent_all + ">" + org_level_name); } } /** * 国际化解析 * * @param currentField 当前字段 * @param language_code 语言代码 * @param languageValue 语言内容 * @return * @throws BaseException */ public FieldSetEntity parseInternational(String currentField, String language_code, String languageValue) throws BaseException { FieldSetEntity fseLanguage = new FieldSetEntity(); fseLanguage.setTableName(currentField); fseLanguage.setValue(language_code, languageValue); return fseLanguage; } /** * 更新部门 * * @param fse * @return * @throws BaseException */ @Override @Transactional public boolean updateDepartment(FieldSetEntity fse) throws BaseException { FieldSetEntity fs = baseDao.getFieldSetEntity(CmnConst.PRODUCT_SYS_ORG_LEVELS, fse.getString(CmnConst.UUID), false); // 没有查询到结果 if (fs == null) throw new BaseException(SystemCode.SYSTEM_COMANY_UPDATE_FAIL.getValue(), SystemCode.SYSTEM_COMANY_UPDATE_FAIL.getText(), this.getClass(), "updateDepartment"); // 判断是否修改了上级编码 if (!StringUtils.equals(fs.getString(CmnConst.ORG_LEVEL_CODE_PARENT), fse.getString(CmnConst.ORG_LEVEL_CODE_PARENT))) { throw new BaseException(SystemCode.SYSTEM_DEPARTMENT_UPDATE_FAIL.getValue(), SystemCode.SYSTEM_DEPARTMENT_UPDATE_FAIL.getText(), this.getClass(), "updateDepartment"); } // 注释国际化代码 // DataTableEntity level_name_dt = fse.getSubDataTable(CmnConst.ORG_LEVEL_NAME); // DataTableEntity level_all_dt = jointOrgLevelAll(fs, level_name_dt); // fse.addSubDataTable(level_all_dt); // updateOrgLevelAll(listInternationfs.getSubDataTable(CmnConst.ORG_LEVEL_ALL), level_all_dt, // fs.getString(CmnConst.ORG_LEVEL_CODE)); updateOrgLevelAll(fse); BaseUtil.createCreatorAndCreationTime(SpringMVCContextHolder.getCurrentUser(), fse); // xupengcheng dateTime 2020年11月28日14:12:18 更新部门下属员工信息 updateLoginUserInfoService.updateUserInfoByDeptUuid(new String[]{fse.getUUID()}, true); return baseDao.update(fse); } public void updateOrgLevelAll(FieldSetEntity fse) throws BaseException { FieldSetEntity fs = baseDao.getFieldSetEntity(CmnConst.PRODUCT_SYS_ORG_LEVELS, fse.getUUID(), false); if (fs != null) { if (!StringUtils.isEmpty(fs.getString(CmnConst.ORG_LEVEL_NAME)) && fs.getString(CmnConst.ORG_LEVEL_NAME).equals(fse.getString(fse.getString(CmnConst.ORG_LEVEL_NAME)))) { return; } String org_level_code_parent = fse.getString(CmnConst.ORG_LEVEL_CODE_PARENT); FieldSetEntity parentOrg = baseDao.getFieldSetEntityByFilter(CmnConst.PRODUCT_SYS_ORG_LEVELS, "org_level_code=?", new Object[]{org_level_code_parent}, false); String parent_level = parentOrg.getString(CmnConst.ORG_LEVEL_ALL); String org_level_code = fse.getString(CmnConst.ORG_LEVEL_CODE); String org_level_all = parent_level + '>' + fse.getString(CmnConst.ORG_LEVEL_NAME); fse.setValue(CmnConst.ORG_LEVEL_ALL, org_level_all); String befor_level_all = fs.getString(CmnConst.ORG_LEVEL_ALL); String sql = "UPDATE " + CmnConst.PRODUCT_SYS_ORG_LEVELS + " SET org_level_all=replace(org_level_all,?,?) where org_level_code_parent like concat(?,'%') "; baseDao.executeUpdate(sql, new Object[]{befor_level_all, org_level_all, org_level_code}); } } /** * 更新下级机构全称 * * @param before_org_level_all 更新前 机构全称 * @param later_org_level_all 更新后 机构全称 * @param org_level_code 机构编码 * @return * @throws BaseException */ public boolean updateOrgLevelAll(DataTableEntity before_org_level_all, DataTableEntity later_org_level_all, String org_level_code) throws BaseException { DataTableEntity level_dt = baseDao.listTable(CmnConst.PRODUCT_SYS_ORG_LEVELS, " org_level_code_parent like '" + org_level_code + "%'", new String[]{}); if (level_dt.getRows() == 0) return true; String[] level_uuid_arr = new String[level_dt.getRows()]; for (int i = 0; i < level_dt.getRows(); i++) { level_uuid_arr[i] = level_dt.getString(i, CmnConst.UUID); } String level_uuid = StringUtils.join(level_uuid_arr, ","); String level_uuid_new = level_uuid.replace(",", "','"); DataTableEntity language_dt = baseDao.listTable(CmnConst.PRODUCT_SYS_LANGUAGE_CONT_VALUES, "data_uuid in('" + level_uuid_new + "') and field_name='org_level_all'", new String[]{}); JSONObject before_level_all_json = dt_json(before_org_level_all); JSONObject later_level_all_json = dt_json(later_org_level_all); for (int i = 0; i < language_dt.getRows(); i++) { FieldSetEntity fs = language_dt.getFieldSetEntity(i); String new_field_value = fs.getString(CmnConst.FIELD_VALUE).replace( before_level_all_json.get(fs.getString(CmnConst.LANGUAGE_CODE)).toString(), later_level_all_json.get(fs.getString(CmnConst.LANGUAGE_CODE)).toString()); fs.setValue(CmnConst.FIELD_VALUE, new_field_value); } boolean update = baseDao.update(language_dt); if (!update){ return update; } if (SpringMVCContextHolder.getCurrentUser() != null) { String sql = "UPDATE " + CmnConst.PRODUCT_SYS_ORG_LEVELS + " SET org_level_all=replace(org_level_all,'" + before_level_all_json.get("zh-CN").toString() + "','" + later_level_all_json.get("zh-CN").toString() + "') where org_level_code_parent like '" + org_level_code + "%'"; return baseDao.executeUpdate(sql); } return false; } // 将国际化dt转换未JSONObject public JSONObject dt_json(DataTableEntity dt) throws BaseException { JSONObject name_json = new JSONObject(); for (int i = 0; i < dt.getRows(); i++) { FieldSetEntity level_name_fs = dt.getFieldSetEntity(i); if (null != level_name_fs.getString("en") && !"".contentEquals(level_name_fs.getString("en"))) { name_json.put("en", level_name_fs.getString("en")); } if (null != level_name_fs.getString("zh-CN") && !"".contentEquals(level_name_fs.getString("zh-CN"))) { name_json.put("zh-CN", level_name_fs.getString("zh-CN")); } if (null != level_name_fs.getString("zh-HK") && !"".contentEquals(level_name_fs.getString("zh-HK"))) { name_json.put("zh-HK", level_name_fs.getString("zh-HK")); } } return name_json; } /** * 机构全称拼接 * * @param odlfs * @param org_level_name 机构名称 * @return * @throws BaseException */ public DataTableEntity jointOrgLevelAll(FieldSetEntity odlfs, DataTableEntity org_level_name) throws BaseException { String org_level_code_parent = odlfs.getString(CmnConst.ORG_LEVEL_CODE_PARENT); DataTableEntity odldt = baseDao.listTable(CmnConst.PRODUCT_SYS_LANGUAGE_CONT_VALUES, "data_uuid=? and field_name='org_level_all'", new String[]{odlfs.getUUID()}); if (!StringUtils.isEmpty(org_level_code_parent) && null != org_level_name) { FieldSetEntity fs = baseDao.getFieldSetEntityByFilter(CmnConst.PRODUCT_SYS_ORG_LEVELS, "org_level_code=?", new Object[]{org_level_code_parent}, false); if (fs != null && fs.getString(CmnConst.ORG_LEVEL_ALL) != null) { JSONObject level_name_json = dt_json(org_level_name); DataTableEntity dt = baseDao.listTable(CmnConst.PRODUCT_SYS_LANGUAGE_CONT_VALUES, "data_uuid=? and field_name='org_level_all'", new String[]{fs.getUUID()}); DataTableEntity level_name_dt = new DataTableEntity(); for (int i = 0; i < dt.getRows(); i++) { FieldSetEntity level_all_fs = new FieldSetEntity(); FieldMetaEntity ff = new FieldMetaEntity(); ff.setTableName(new Object[]{"org_level_all"}); level_all_fs.setMeta(ff); level_all_fs.setValue(dt.getString(i, CmnConst.LANGUAGE_CODE), dt.getString(i, CmnConst.FIELD_VALUE) + ">" + level_name_json.get(dt.getString(i, CmnConst.LANGUAGE_CODE))); for (int j = 0; j < odldt.getRows(); j++) { if (dt.getString(i, CmnConst.LANGUAGE_CODE).equals(odldt.getString(j, CmnConst.LANGUAGE_CODE))) { level_all_fs.setValue(CmnConst.UUID, odldt.getString(j, CmnConst.UUID)); } } level_name_dt.addFieldSetEntity(level_all_fs); } return level_name_dt; } } throw new BaseException(SystemCode.SYSTEM_FORM_NODATA.getValue(), SystemCode.SYSTEM_FORM_NODATA.getText(), this.getClass(), "jointOrgLevelAll"); } /** * 查询制定部门详情 * * @param uuid 部门uuid * @return * @throws BaseException */ public FieldSetEntity findDept(String uuid) throws BaseException { return baseDao.getFieldSetEntityByFilter(CmnConst.PRODUCT_SYS_ORG_LEVELS, "uuid=? and org_level_type=1", new Object[]{uuid}, false); } }