package com.product.admin.service; import com.alibaba.druid.util.StringUtils; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import com.product.admin.config.CmnConst; import com.product.admin.config.SystemCode; import com.product.admin.service.idel.ISelectPersonnelService; 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.exception.BaseException; import com.product.core.service.support.AbstractBaseService; import com.product.core.service.support.QueryFilterService; import com.product.core.spring.context.SpringMVCContextHolder; import com.product.core.transfer.Transactional; import com.product.module.sys.entity.SystemUser; import com.product.util.BaseUtil; import java.util.Date; import java.util.HashSet; import org.springframework.beans.factory.annotation.Autowired; import com.product.core.permission.PermissionService; import org.springframework.stereotype.Service; /** * Copyright LX-BASE * * @Title: SelectPersonnelService * @Project: LX-BASE-SERVER * @Date: 2020年8月26日 17:33:37 * @Author: luoxin * @Description: 组织架构 */ // @Service public class SelectPersonnelService extends AbstractBaseService implements ISelectPersonnelService { @Autowired public BaseDao baseDao; @Autowired PermissionService permissionService; @Autowired QueryFilterService queryFilterService; @Override public BaseDao getBaseDao() { return baseDao; } @Override public void setBaseDao(BaseDao baseDao) { this.baseDao = baseDao; } @Override public JSONObject getOrganizationStructureTree(String theCompanyUuid) throws BaseException { FieldSetEntity company = baseDao.getFieldSetEntity(CmnConst.PRODUCT_SYS_ORG_LEVELS, theCompanyUuid, false); DataTableEntity data = baseDao.listTable( "SELECT * FROM product_sys_org_levels a\n" + "WHERE a.org_level_code LIKE CONCAT((SELECT org_level_code FROM product_sys_org_levels \n" + "WHERE uuid = ?), '%') AND org_level_type=1 AND org_level_status=0 ORDER BY a.org_level_code", new String[] { theCompanyUuid }); JSONObject companyObject = new JSONObject(); companyObject.put(CmnConst.UUID, company.getString(CmnConst.UUID)); companyObject.put(CmnConst.ORG_LEVEL_NAME, company.getString(CmnConst.ORG_LEVEL_NAME)); companyObject.put(CmnConst.ORG_LEVEL_ALL, company.getString(CmnConst.ORG_LEVEL_ALL)); if (data.getRows() > 0) { companyObject.put("children", OrgLevelChildrenTree(data, company.getString("org_level_code"))); } return companyObject; } @Override public JSONArray getOrganizationThoseWho(String tablenaem, String uuid) throws BaseException { // 获取公司或部门信息 FieldSetEntity setEntity = baseDao.getFieldSetEntity(CmnConst.PRODUCT_SYS_ORG_LEVELS, uuid, false); // 1为部门 获取部门下的人或岗位 DataTableEntity dataTableEntity; String dataFilter=permissionService.getDataFilter(CmnConst.ORG_LEVEL_UUID) ; if (setEntity.getString("org_level_type").equals("1")) { // 如果为岗位表 if (CmnConst.PRODUCT_SYS_JOB_POSTS.equals(tablenaem)) { StringBuffer sql = new StringBuffer(); sql.append(" SELECT * FROM ( ") .append(" SELECT b.*,a.org_level_all FROM product_sys_org_levels a ") .append(" LEFT JOIN product_sys_job_posts b ON a.uuid = b.dept_uuid ") .append(" WHERE b.is_used = '1' AND b.dept_uuid = ? ) c"); if(!BaseUtil.strIsNull(dataFilter)) { sql.append(" where " + dataFilter); } dataTableEntity = baseDao.listTable(sql.toString(), new String[] { uuid }); } else { StringBuffer filter = new StringBuffer(); filter.append(" dept_uuid = ? "); if(!BaseUtil.strIsNull(dataFilter)) { filter.append(" and ").append(dataFilter); } dataTableEntity = baseDao.listTable(tablenaem, filter.toString(), new String[] { uuid }); if (!BaseUtil.dataTableIsEmpty(dataTableEntity)) { return acquireInternationalization(CmnConst.PRODUCT_SYS_JOB_POSTS, CmnConst.JOB_POST_NAME, dataTableEntity); } } // 0为公司 获取公司下的人 } else { // 如果为岗位表 if (CmnConst.PRODUCT_SYS_JOB_POSTS.equals(tablenaem)) { StringBuilder sql = new StringBuilder(); sql.append(" SELECT * FROM ( ") .append(" SELECT b.*,a.org_level_all FROM ") .append(" product_sys_org_levels a LEFT JOIN ") .append(" product_sys_job_posts b ON a.uuid = b.dept_uuid WHERE b.is_used = '1' AND b.org_level_uuid = ?) c "); if(!BaseUtil.strIsNull(dataFilter)) { sql.append(" where " + dataFilter); } dataTableEntity = baseDao.listTable(sql.toString(), new String[] { uuid }); } else { StringBuffer filter = new StringBuffer(); filter.append(" org_level_uuid = ? "); if(!BaseUtil.strIsNull(dataFilter)) { filter.append(" and ").append(dataFilter); } dataTableEntity = baseDao.listTable(tablenaem, filter.toString(), new String[] { uuid }); if (!BaseUtil.dataTableIsEmpty(dataTableEntity)) { return acquireInternationalization(CmnConst.PRODUCT_SYS_JOB_POSTS, CmnConst.JOB_POST_NAME, dataTableEntity); } } } return BaseUtil.dataTableEntityToJson(baseDao.listInternationDataTable(dataTableEntity, null)); } /** * 通过员工data获取对应的国际化岗位 存入员工field * * @param tableName 岗位表名 * @param parameter 岗位字段 * @param staffField 员工field数据 * @return 员工field数据 */ public JSONObject acquireInternationalization(String tableName, String parameter, FieldSetEntity staffField) throws BaseException { FieldSetEntity set = staffField; JSONObject jsonObject = new JSONObject(); // 员工uuid jsonObject.put(CmnConst.UUID, set.getString(CmnConst.UUID)); // 员工展示名称 jsonObject.put("show_name", set.getString("show_name")); // 岗位表UUID jsonObject.put(CmnConst.JOB_POST_UUID, set.getString(CmnConst.JOB_POST_UUID)); // 公司uuid jsonObject.put("org_level_uuid", set.getString("org_level_uuid")); // 部门uuid jsonObject.put("dept_uuid", set.getString("dept_uuid")); // 国际化data // DataTableEntity data = baseDao.listInternationDataTable(tableName, new String[] { parameter }, // new String[] { set.getString(CmnConst.JOB_POST_UUID) }, null); // JSONArray subArray = new JSONArray(); // JSONObject obj; // FieldSetEntity fe; // for (int j = 0, le = data.getRows(); j < le; j++) { // obj = new JSONObject(); // fe = data.getFieldSetEntity(j); // obj.put(String.valueOf(fe.getValues().get(CmnConst.LANGUAGE_CODE)), // String.valueOf(fe.getValues().get(CmnConst.FIELD_VALUE))); // obj.put(CmnConst.UUID, String.valueOf(fe.getValues().get(CmnConst.UUID))); // subArray.add(obj); // } // jsonObject.put(parameter, subArray); return jsonObject; } /** * 通过员工data获取对应的国际化岗位 存入员工data * * @param tableName 岗位表名 * @param parameter 岗位字段 * @param staffData 员工data数据 * @return 员工data数据 */ public JSONArray acquireInternationalization(String tableName, String parameter, DataTableEntity staffData) throws BaseException { JSONArray array = new JSONArray(); for (int i = 0, length = staffData.getRows(); i < length; i++) { array.add(acquireInternationalization(tableName, parameter, staffData.getFieldSetEntity(i))); } return array; } @Override @Transactional public String savePersonageTheGroup(FieldSetEntity fse) throws BaseException { SystemUser currentUser=SpringMVCContextHolder.getCurrentUser(); String userId = String.valueOf(currentUser.getUser_id()); String uuid; Integer groupType = fse.getInteger(CmnConst.GROUP_TYPE); String groupName = fse.getString(CmnConst.GROUP_NAME); FieldSetEntity fieldSetEntity = baseDao.getFieldSetEntityByFilter(CmnConst.PRODUCT_SYS_GROUP, "group_name = ? and created_by = ? ", new String[] { groupName,userId }, false); // fieldSetEntity为空 新增 if (fieldSetEntity == null) { fieldSetEntity = new FieldSetEntity(); fieldSetEntity.setTableName(fse.getTableName()); // 个人组 获取当前人ID uuid fieldSetEntity.setValue(CmnConst.USER_ID, SpringMVCContextHolder.getCurrentUser().getUser_id()); fieldSetEntity.setValue(CmnConst.GROUP_TYPE, groupType); fieldSetEntity.setValue(CmnConst.GROUP_NAME, groupName); fieldSetEntity = BaseUtil.createCreatorAndCreationTime(SpringMVCContextHolder.getCurrentUser(), fieldSetEntity); FieldMetaEntity f = new FieldMetaEntity(); f.setTableName(new Object[]{CmnConst.GROUP_NAME}); //luoxin 2020-12-17 11:38:31 新增时虚拟国际化 DataTableEntity data = baseDao.listTable("SELECT language_code FROM product_common_language WHERE is_used = 1",new Object[]{}); DataTableEntity dt = new DataTableEntity(); dt.setMeta(f); for (int i = 0,length = data.getRows(); i < length; i++) { FieldSetEntity entity = new FieldSetEntity(); entity.setMeta(f); String languageCode = data.getFieldSetEntity(i).getString("language_code"); if(!BaseUtil.strIsNull(languageCode)){ entity.setValue(languageCode, groupName); dt.addFieldSetEntity(entity); } } fieldSetEntity.addSubDataTable(dt); uuid = baseDao.add(fieldSetEntity); // fieldSetEntity不为空 替换 } else { // 个人组 获取当前人ID uuid uuid = fieldSetEntity.getString(CmnConst.UUID); fieldSetEntity.setValue(CmnConst.USER_ID, SpringMVCContextHolder.getCurrentUser().getUser_id()); fieldSetEntity.setValue(CmnConst.GROUP_TYPE, groupType); fieldSetEntity.setValue(CmnConst.GROUP_NAME, groupName); fieldSetEntity = baseDao.listInternationDataTable(fieldSetEntity,null); // 添加创建人 fieldSetEntity = BaseUtil.createCreatorAndCreationTime(SpringMVCContextHolder.getCurrentUser(), fieldSetEntity); // 清除子表 baseDao.delete(CmnConst.PRODUCT_SYS_GROUP_SUB, CmnConst.GROUP_UUID+" = ? ", new Object[] { uuid }); baseDao.update(fieldSetEntity); } // 添加子表 DataTableEntity subData; // 员工map HashSet hashSet = analyticalFramework(fse.getSubDataTable(CmnConst.PRODUCT_SYS_GROUP_SUB)); if (hashSet.size() > 0) { subData = encapsulateTheUser(hashSet, uuid); baseDao.add(subData); } return uuid; } @Override @Transactional public String saveCompanyTheGroup(FieldSetEntity fse) throws BaseException { FieldSetEntity fieldSetEntity; String uuid = fse.getString(CmnConst.UUID); Integer groupType = fse.getInteger(CmnConst.GROUP_TYPE); // 获取子表 DataTableEntity sub = fse.getSubDataTable(CmnConst.PRODUCT_SYS_GROUP_SUB); // uuid为空 新增 if (uuid == null || uuid.isEmpty()) { fieldSetEntity = new FieldSetEntity(); FieldMetaEntity f = new FieldMetaEntity(); f.setTableName(new Object[] { fse.getTableName() }); fieldSetEntity.setMeta(f); // 公司组 获取当前人ID uuid fieldSetEntity.setValue(CmnConst.COMPANY_UUID, fse.getString(CmnConst.COMPANY_UUID)); fieldSetEntity.setValue(CmnConst.GROUP_TYPE, groupType); DataTableEntity group_name = fse.getSubDataTable(CmnConst.GROUP_NAME); FieldMetaEntity ff = new FieldMetaEntity(); ff.setTableName(new Object[] { CmnConst.GROUP_NAME }); group_name.setMeta(ff); fieldSetEntity.addSubDataTable(group_name); if (!StringUtils.isEmpty(fse.getString("sort"))) { fieldSetEntity.setValue("sort", Integer.parseInt(fse.getString("sort"))); } fieldSetEntity = BaseUtil.createCreatorAndCreationTime(SpringMVCContextHolder.getCurrentUser(), fieldSetEntity); uuid = baseDao.add(fieldSetEntity); // uuid不为空 替换 } else { fieldSetEntity = baseDao.getFieldSetEntity(CmnConst.PRODUCT_SYS_GROUP, uuid, false); // 公司组 获取选择 uuid fieldSetEntity.setValue(CmnConst.COMPANY_UUID, fse.getString(CmnConst.COMPANY_UUID)); fieldSetEntity.setValue(CmnConst.GROUP_TYPE, groupType); fieldSetEntity.setValue(CmnConst.COMPANY_UUID, fse.getString(CmnConst.COMPANY_UUID)); fieldSetEntity.setValue(CmnConst.GROUP_TYPE, groupType); DataTableEntity group_name = fse.getSubDataTable(CmnConst.GROUP_NAME); FieldMetaEntity ff = new FieldMetaEntity(); ff.setTableName(new Object[] { CmnConst.GROUP_NAME }); group_name.setMeta(ff); fieldSetEntity.addSubDataTable(group_name); if (!StringUtils.isEmpty(fse.getString("sort"))) { fieldSetEntity.setValue("sort", Integer.parseInt(fse.getString("sort"))); } // 添加创建人 fieldSetEntity = BaseUtil.createCreatorAndCreationTime(SpringMVCContextHolder.getCurrentUser(), fieldSetEntity); // 清除子表 baseDao.delete(CmnConst.PRODUCT_SYS_GROUP_SUB, "group_uuid = ? ", new Object[] { uuid }); // 架构子表 baseDao.delete(CmnConst.PRODUCT_SYS_GROUP_SELECT, "group_uuid = ? ", new Object[] { uuid }); baseDao.update(fieldSetEntity); } // 判断子表是否为空 if (BaseUtil.dataTableIsEmpty(sub)) { return uuid; } else { // 添加子表 DataTableEntity subData; // 员工map HashSet hashSet = analyticalFramework(sub); if (hashSet.size() > 0) { // 创建用户子表 subData = encapsulateTheUser(hashSet, uuid); // 保存用户子表 baseDao.add(subData); } // 保存已经选择的架构子表 DataTableEntity select = packagingSelect(sub, uuid); baseDao.add(select); return uuid; } } /** * 封装架构子表 * * @param sub 子表data * @param superUuid 组uuid * @return * @throws BaseException */ public DataTableEntity packagingSelect(DataTableEntity sub, String superUuid) throws BaseException { DataTableEntity subData = new DataTableEntity(); FieldSetEntity setEntity; FieldMetaEntity f = new FieldMetaEntity(); f.setTableName(new Object[] { CmnConst.PRODUCT_SYS_GROUP_SELECT }); subData.setMeta(f); // 保存组织架构子表 for (int i = 0, length = sub.getRows(); i < length; i++) { setEntity = new FieldSetEntity(); setEntity.setMeta(f); setEntity.setValue(CmnConst.ORGANIZATION_TYPES, sub.getFieldSetEntity(i).getString(CmnConst.ORGANIZATION_TYPES)); setEntity.setValue(CmnConst.ORGANIZATION_UUIDS, sub.getFieldSetEntity(i).getString(CmnConst.ORGANIZATION_UUIDS)); setEntity.setValue(CmnConst.GROUP_UUID, superUuid); setEntity = BaseUtil.createCreatorAndCreationTime(SpringMVCContextHolder.getCurrentUser(), setEntity); subData.addFieldSetEntity(setEntity); } return subData; } @Override @Transactional public boolean deleteTheGroup(FieldSetEntity fse) throws BaseException { String uuids = fse.getString("uuids"); // 查询是否被引用 String parameter = "%" + uuids + "%"; DataTableEntity data = baseDao.listTable( "SELECT * FROM product_sys_organizational_structure_storage WHERE organization_uuids like ? ", new String[] { parameter }); if (BaseUtil.dataTableIsEmpty(data)) { String[] uuid = uuids.split(","); //luoxin 2020-12-17 11:37:56修改删除方法 for (int i = 0, length = uuid.length; i < length; ++i) { baseDao.delete(CmnConst.PRODUCT_SYS_GROUP, new String[] { uuid[i] }); } return true; } else { return false; } } /** * 封装组子表 * * @param hashSet 用户id集 * @param uuid 组uuid * @return * @throws BaseException */ public DataTableEntity encapsulateTheUser(HashSet hashSet, String uuid) throws BaseException { DataTableEntity subData = new DataTableEntity(); FieldSetEntity setEntity; FieldMetaEntity f = new FieldMetaEntity(); f.setTableName(new Object[] { CmnConst.PRODUCT_SYS_GROUP_SUB }); subData.setMeta(f); for (Integer key : hashSet) { setEntity = new FieldSetEntity(); setEntity.setMeta(f); setEntity.setValue(CmnConst.GROUP_UUID, uuid); setEntity.setValue(CmnConst.STAFF_ID, key); setEntity = BaseUtil.createCreatorAndCreationTime(SpringMVCContextHolder.getCurrentUser(), setEntity); subData.addFieldSetEntity(setEntity); } return subData; } @Override public DataTableEntity getTheGroup(FieldSetEntity fse) throws BaseException { String uuid = fse.getString(CmnConst.UUID); StringBuilder sql = new StringBuilder(); sql.append("SELECT a.* FROM product_sys_staffs a ") .append("LEFT JOIN product_sys_group_sub b ON a.staff_id = b.staff_id WHERE group_uuid = ?"); DataTableEntity dataTableEntity = baseDao.listTable(sql.toString(), new Object[] { uuid }); return dataTableEntity; } @Override public DataTableEntity listAllGroup(FieldSetEntity fse) throws BaseException { // 当前人uuid int userId = SpringMVCContextHolder.getCurrentUser().getUser_id(); // 公司uuid 4个人组 5单位组 String companyUuid = fse.getString(CmnConst.COMPANY_UUID); companyUuid = "%" + companyUuid + "%"; StringBuilder sql = new StringBuilder(); sql.append("SELECT * FROM product_sys_group WHERE group_type = 4 AND user_id = ? UNION ALL ") .append("SELECT * FROM product_sys_group WHERE group_type = 5 AND company_uuid like ? order by sort desc"); return baseDao.listTable(sql.toString(), new Object[] { userId, companyUuid }); } @Override public JSONObject listCompanyGroup(FieldSetEntity fse) throws BaseException { // 公司uuid String companyUuid = fse.getString(CmnConst.COMPANY_UUID); if(BaseUtil.strIsNull(companyUuid)){ return null; } String queryFilter; Integer cpage = fse.getInteger("cpage");// 当前页数 Integer pagesize = fse.getInteger("pagesize");// 每页条数 if(BaseUtil.dataTableIsEmpty(fse.getSubDataTable("systemSeniorQueryString"))){ queryFilter = " company_uuid = ? "; }else { queryFilter = " company_uuid = ? AND " + queryFilterService.getQueryFilter(fse); } DataTableEntity dataTableEntity = baseDao.listTable(fse.getTableName(),queryFilter,new String[]{companyUuid},null,null, pagesize, cpage); JSONObject object = new JSONObject(); if (BaseUtil.dataTableIsEmpty(dataTableEntity)) { object.put("data",dataTableEntity); return object; } JSONArray jsonArray = new JSONArray(); for (int i = 0, length = dataTableEntity.getRows(); i < length; ++i) { FieldSetEntity set = dataTableEntity.getFieldSetEntity(i); String group_uuid = set.getString(CmnConst.UUID); JSONObject obj = new JSONObject(); // 组名 obj.put(CmnConst.UUID, group_uuid); //group_name国际化 FieldSetEntity fee; JSONArray arr = new JSONArray(); DataTableEntity dd = baseDao.listInternationDataTable(CmnConst.PRODUCT_SYS_GROUP, new String[] { CmnConst.GROUP_NAME }, new String[] { group_uuid }, null); JSONObject object0; for (int j = 0, le = dd.getRows(); j < le; j++) { object0 = new JSONObject(); fee = dd.getFieldSetEntity(j); // 国际化标识 String languageCode = String.valueOf(fee.getValues().get(CmnConst.LANGUAGE_CODE)); // 国际化值 String fieldValue = String.valueOf(fee.getValues().get(CmnConst.FIELD_VALUE)); // 国际uuid String uuid = String.valueOf(fee.getValues().get(CmnConst.UUID)); object0.put(languageCode, fieldValue); object0.put(CmnConst.UUID, uuid); arr.add(object0); } obj.put("groupName", arr); JSONArray array = new JSONArray(); StringBuilder sql = new StringBuilder(); sql.append("SELECT a.organization_types,b.uuid,b.show_name name FROM product_sys_group_select a ") .append("LEFT JOIN product_sys_staffs b ON a.organization_uuids = b.uuid ") .append("WHERE a.organization_types = 1 AND a.group_uuid = ? UNION ALL ") .append("SELECT a.organization_types,b.uuid,b.org_level_name name FROM product_sys_group_select a ") .append("LEFT JOIN product_sys_org_levels b ON a.organization_uuids = b.uuid ") .append("WHERE a.organization_types = 2 AND a.group_uuid = ? UNION ALL ") .append("SELECT a.organization_types,b.uuid,b.job_post_name FROM product_sys_group_select a ") .append("LEFT JOIN product_sys_job_posts b ON a.organization_uuids = b.uuid ") .append("WHERE a.organization_types = 3 AND a.group_uuid = ? "); DataTableEntity table1 = baseDao.listTable(sql.toString(),new String[] { group_uuid, group_uuid, group_uuid }); if (!BaseUtil.dataTableIsEmpty(table1)) { for (int k = 0, ta = table1.getRows(); k < ta; k++) { FieldSetEntity setEntity = table1.getFieldSetEntity(k); JSONObject ob = new JSONObject(); ob.put(CmnConst.ORGANIZATION_TYPES, setEntity.getString(CmnConst.ORGANIZATION_TYPES)); ob.put(CmnConst.ORGANIZATION_UUIDS, setEntity.getString(CmnConst.UUID)); if ("1".equals(setEntity.getString(CmnConst.ORGANIZATION_TYPES))) { ob.put(CmnConst.ORGANIZATION_NAMES, setEntity.getString("name")); } //部门 if ("2".equals(setEntity.getString(CmnConst.ORGANIZATION_TYPES))) { FieldSetEntity fe; JSONArray ar = new JSONArray(); DataTableEntity d = baseDao.listInternationDataTable(CmnConst.PRODUCT_SYS_ORG_LEVELS, new String[] { CmnConst.ORG_LEVEL_NAME }, new String[] { setEntity.getString(CmnConst.UUID) }, null); JSONObject object1; for (int j = 0, le = d.getRows(); j < le; j++) { object1 = new JSONObject(); fe = d.getFieldSetEntity(j); // 国际化标识 String languageCode = String.valueOf(fe.getValues().get(CmnConst.LANGUAGE_CODE)); // 国际化值 String fieldValue = String.valueOf(fe.getValues().get(CmnConst.FIELD_VALUE)); // 国际uuid String uuid = String.valueOf(fe.getValues().get(CmnConst.UUID)); object1.put(languageCode, fieldValue); object1.put(CmnConst.UUID, uuid); ar.add(object1); } ob.put(CmnConst.ORGANIZATION_NAMES, ar); } //岗位 if ("3".equals(setEntity.getString(CmnConst.ORGANIZATION_TYPES))) { FieldSetEntity fe; JSONArray ar = new JSONArray(); DataTableEntity d = baseDao.listInternationDataTable(CmnConst.PRODUCT_SYS_JOB_POSTS, new String[] { CmnConst.JOB_POST_NAME }, new String[] { setEntity.getString(CmnConst.UUID) }, null); JSONObject object1; for (int j = 0, le = d.getRows(); j < le; j++) { object1 = new JSONObject(); fe = d.getFieldSetEntity(j); // 国际化标识 String languageCode = String.valueOf(fe.getValues().get(CmnConst.LANGUAGE_CODE)); // 国际化值 String fieldValue = String.valueOf(fe.getValues().get(CmnConst.FIELD_VALUE)); // 国际uuid String uuid = String.valueOf(fe.getValues().get(CmnConst.UUID)); object1.put(languageCode, fieldValue); object1.put(CmnConst.UUID, uuid); ar.add(object1); } ob.put(CmnConst.ORGANIZATION_NAMES, ar); } array.add(ob); } } obj.put("organization_info", array); jsonArray.add(obj); } object.put("data", jsonArray); object.put("totalCount", dataTableEntity.getSqle().getTotalCount());// 总条数 object.put("totalpage", dataTableEntity.getSqle().getTotalpage());// 总页数 object.put("psize", dataTableEntity.getSqle().getPsize());// 每页条数 object.put("pindex", dataTableEntity.getSqle().getPindex());// 当前页 object.put("datatable",dataTableEntity); return object; } @Override public FieldSetEntity getCompanyGroupInfo(FieldSetEntity fse) throws BaseException { String uuid = fse.getString(CmnConst.UUID); FieldSetEntity fieldSetEntity = baseDao.getFieldSetEntity(CmnConst.PRODUCT_SYS_GROUP, uuid, false); StringBuilder staffsSql = new StringBuilder(); staffsSql.append("SELECT c.uuid FROM product_sys_group a ") .append("LEFT JOIN product_sys_group_select b ON a.uuid = b.group_uuid ") .append("LEFT JOIN product_sys_staffs c ON c.uuid = b.organization_uuids ") .append("WHERE a.group_type = 5 AND b.organization_types = '1' AND a.uuid = ?"); DataTableEntity staffs = baseDao.listTable(staffsSql.toString(), new String[] { uuid }); if (!BaseUtil.dataTableIsEmpty(staffs)) { StringBuilder staffsUuids = new StringBuilder(); for (int i = 0, length = staffs.getRows(); i < length; ++i) { staffsUuids.append(staffs.getFieldSetEntity(i).getString(CmnConst.UUID)).append(","); } String suuid = staffsUuids.substring(0, staffsUuids.length() - 1); fieldSetEntity.setValue("staffsUuids", suuid); } StringBuilder levelsSql = new StringBuilder(); levelsSql.append("SELECT c.uuid FROM product_sys_group a ") .append("LEFT JOIN product_sys_group_select b ON a.uuid = b.group_uuid ") .append("LEFT JOIN product_sys_org_levels c ON c.uuid = b.organization_uuids ") .append("WHERE a.group_type = 5 AND b.organization_types = '2' AND a.uuid = ? "); DataTableEntity levels = baseDao.listTable(levelsSql.toString(), new String[] { uuid }); if (!BaseUtil.dataTableIsEmpty(levels)) { StringBuilder levelsUuids = new StringBuilder(); for (int i = 0, length = levels.getRows(); i < length; ++i) { levelsUuids.append(levels.getFieldSetEntity(i).getString(CmnConst.UUID)).append(","); } String luuid = levelsUuids.substring(0, levelsUuids.length() - 1); fieldSetEntity.setValue("levelsUuids", luuid); } StringBuilder postsSql = new StringBuilder(); postsSql.append("SELECT c.uuid FROM product_sys_group a ") .append("LEFT JOIN product_sys_group_select b ON a.uuid = b.group_uuid ") .append("LEFT JOIN product_sys_job_posts c ON c.uuid = b.organization_uuids ") .append("WHERE a.group_type = 5 AND b.organization_types = '3' AND a.uuid = ? "); DataTableEntity posts = baseDao.listTable(postsSql.toString(), new String[] { uuid }); if (!BaseUtil.dataTableIsEmpty(posts)) { StringBuilder postsUuids = new StringBuilder(); for (int i = 0, length = posts.getRows(); i < length; ++i) { postsUuids.append(posts.getFieldSetEntity(i).getString(CmnConst.UUID)).append(","); } String puuid = postsUuids.substring(0, postsUuids.length() - 1); fieldSetEntity.setValue("postsUuids", puuid); } FieldSetEntity fs = baseDao.listInternationDataTable(fieldSetEntity,null); return fs; } /** * * @param organizationalData 要处理的DATA * @param fieldSetEntity 存值fs * @param name 名称集 * @param type 类型集 * @param uuid uuid集 * @return 返回fs * @throws BaseException */ public FieldSetEntity encapsulatedOrganizationStructure(DataTableEntity organizationalData, FieldSetEntity fieldSetEntity, String name, String type, String uuid) throws BaseException { if (organizationalData != null && organizationalData.getRows() > 0) { StringBuilder names = new StringBuilder(); StringBuilder types = new StringBuilder(); StringBuilder uuids = new StringBuilder(); names.append(organizationalData.getFieldSetEntity(0).getString(name)); types.append(organizationalData.getFieldSetEntity(0).getString(type)); uuids.append(organizationalData.getFieldSetEntity(0).getString(uuid)); for (int i = 1, length = organizationalData.getRows(); i < length; ++i) { FieldSetEntity setEntity = organizationalData.getFieldSetEntity(i); names.append(",").append(setEntity.getString(name)); types.append(",").append(setEntity.getString(type)); uuids.append(",").append(setEntity.getString(uuid)); } fieldSetEntity.setValue(name, names.toString()); fieldSetEntity.setValue(type, types.toString()); fieldSetEntity.setValue(uuid, uuids.toString()); } else { fieldSetEntity.setValue(name, ""); fieldSetEntity.setValue(type, ""); fieldSetEntity.setValue(uuid, ""); } return fieldSetEntity; } /** * 保存组织架构信息 并保存人的组织架构下的人id * * @param fse 组信息 * @return * @throws BaseException */ @Override @Transactional public String saveOrganization(FieldSetEntity fse) throws BaseException { String uuid = fse.getString(CmnConst.UUID); DataTableEntity organizationalData = fse.getSubDataTable("organizational_sub"); // 创建组织架构储存表 FieldSetEntity fieldSetEntity = new FieldSetEntity(); FieldMetaEntity f = new FieldMetaEntity(); f.setTableName(new Object[]{fse.getTableName()}); fieldSetEntity.setMeta(f); if(!BaseUtil.dataTableIsEmpty(organizationalData)) { fieldSetEntity = encapsulatedOrganizationStructure(organizationalData, fieldSetEntity, CmnConst.ORGANIZATION_NAMES, CmnConst.ORGANIZATION_TYPES, CmnConst.ORGANIZATION_UUIDS); HashSet set = analyticalFramework(organizationalData); if (!set.isEmpty() && set.size() > 0) { StringBuilder userIds = new StringBuilder(); for (Integer key : set) { // 拼接员工id集 userIds.append(key).append(","); } String Ids = userIds.substring(0, userIds.length() - 1); // 储存员工id集 fieldSetEntity.setValue("staff_ids", Ids); } else { fieldSetEntity.setValue("staff_ids", ""); } // 新增 if (uuid == null || uuid.isEmpty()) { fieldSetEntity = BaseUtil.createCreatorAndCreationTime(SpringMVCContextHolder.getCurrentUser(), fieldSetEntity); uuid = baseDao.add(fieldSetEntity); // 修改 } else { fieldSetEntity.setValue(CmnConst.UUID, uuid); fieldSetEntity = BaseUtil.updatedRegeneratorAndUpdateTime(SpringMVCContextHolder.getCurrentUser(), fieldSetEntity); baseDao.update(fieldSetEntity); } }else { baseDao.executeUpdate("update product_sys_organizational_structure_storage set `organization_names` = null, `staff_ids` = null, `organization_uuids` = null, `organization_types` = null where uuid = ? ",new String[]{uuid}); } return uuid; } @Override public JSONArray getAtPresentOrganization(FieldSetEntity fse) throws BaseException { FieldSetEntity set = baseDao.getFieldSetEntity(fse.getTableName(), fse.getString(CmnConst.UUID), false); if (set == null) { return null; } String names = set.getString(CmnConst.ORGANIZATION_NAMES); JSONArray array = new JSONArray(); // 没有值 if (StringUtils.isEmpty(names)) { return array; } else { String types = set.getString(CmnConst.ORGANIZATION_TYPES); String uuids = set.getString(CmnConst.ORGANIZATION_UUIDS); String[] type = types.split(","); String[] uuid = uuids.split(","); FieldSetEntity fieldSetEntity; for (int i = 0, length = type.length; i < length; ++i) { // 员工 switch (type[i]){ case "1": fieldSetEntity = baseDao.getFieldSetEntity(CmnConst.PRODUCT_SYS_STAFFS, uuid[i], false); break; // 部门 case "2": fieldSetEntity = baseDao.getFieldSetEntity(CmnConst.PRODUCT_SYS_ORG_LEVELS, uuid[i], false); break; // 岗位 case "3": StringBuilder sql = new StringBuilder(); sql.append("SELECT b.*,a.org_level_all FROM product_sys_org_levels a LEFT JOIN ") .append("product_sys_job_posts b ON a.uuid = b.dept_uuid WHERE b.uuid = ? "); DataTableEntity data = baseDao.listTable(sql.toString(), new String[] { uuid[i] }); fieldSetEntity = data.getFieldSetEntity(0); fieldSetEntity = baseDao.listInternationDataTable(fieldSetEntity, null); break; // 个人组或单位组 default: fieldSetEntity = baseDao.getFieldSetEntity(CmnConst.PRODUCT_SYS_GROUP, uuid[i], false); } // 放入结构类型 if (fieldSetEntity != null) { fieldSetEntity.setValue("type", type[i]); // 为人员要添加对应国际化岗位 if ("1".equals(type[i])) { JSONObject object = acquireInternationalization(CmnConst.PRODUCT_SYS_JOB_POSTS, CmnConst.JOB_POST_NAME, fieldSetEntity); object.put("type", type[i]); array.add(object); } else { array.add(BaseUtil.fieldSetEntityToJson(fieldSetEntity)); } } } return array; } } /** * 获取单位 部门 岗位组下的人 员工 * * @param dataTableEntity * @return */ public HashSet analyticalFramework(DataTableEntity dataTableEntity) throws BaseException { FieldSetEntity fieldSetEntity; FieldSetEntity set; DataTableEntity data; HashSet hashSet = new HashSet<>(); if(BaseUtil.dataTableIsEmpty(dataTableEntity)){ return hashSet; } for (int i = 0, length = dataTableEntity.getRows(); i < length; i++) { fieldSetEntity = dataTableEntity.getFieldSetEntity(i); String type = fieldSetEntity.getString(CmnConst.ORGANIZATION_TYPES); String uuid = fieldSetEntity.getString(CmnConst.ORGANIZATION_UUIDS); // 员工 if ("1".equals(type)) { set = baseDao.getFieldSetEntity(CmnConst.PRODUCT_SYS_STAFFS, uuid, false); hashSet.add(set.getInteger(CmnConst.STAFF_ID)); // 部门 } else if ("2".equals(type)) { data = baseDao.listTable(CmnConst.PRODUCT_SYS_STAFFS, "dept_uuid = ?", new Object[] { uuid }); for (int j = 0, len = data.getRows(); j < len; j++) { hashSet.add(data.getFieldSetEntity(j).getInteger(CmnConst.STAFF_ID)); } // 岗位 } else if ("3".equals(type)) { data = baseDao.listTable(CmnConst.PRODUCT_SYS_STAFFS, "job_post_uuid = ?", new Object[] { uuid }); for (int j = 0, len = data.getRows(); j < len; j++) { hashSet.add(data.getFieldSetEntity(j).getInteger(CmnConst.STAFF_ID)); } // 单位组或个人组 } else { data = baseDao.listTable( "SELECT b.* FROM product_sys_group_sub a LEFT JOIN product_sys_staffs b on a.staff_id = b.staff_id WHERE group_uuid = ?", new Object[] { uuid }); for (int j = 0, len = data.getRows(); j < len; j++) { hashSet.add(data.getFieldSetEntity(j).getInteger(CmnConst.STAFF_ID)); } } } return hashSet; } /** * 封装组织架构树 * * @param data * @param org_level_code 上级code * @return * @throws BaseException */ public JSONArray OrgLevelChildrenTree(DataTableEntity data, String org_level_code) throws BaseException { JSONArray children_array = new JSONArray(); if (!StringUtils.isEmpty(org_level_code)) { for (int i = 0; i < data.getRows(); i++) { FieldSetEntity fs = data.getFieldSetEntity(i); if (org_level_code.equals(fs.getString("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.getString(CmnConst.ORG_LEVEL_NAME)); org_children.put(CmnConst.ORG_LEVEL_ALL, fs.getString(CmnConst.ORG_LEVEL_ALL)); org_children.put("children", OrgLevelChildrenTree(data, fs.getString("org_level_code"))); children_array.add(org_children); } } } return children_array; } /** * 定时执行更新组织架构 * * @throws BaseException */ public void timingOrganization() throws BaseException { DataTableEntity dt = baseDao.listTable(CmnConst.PRODUCT_SYS_ORGANIZATIONAL_STRUCTURE_STORAGE, "is_auto_update=1"); for (int i = 0; i < dt.getRows(); i++) { FieldSetEntity fs = dt.getFieldSetEntity(i); FieldSetEntity fss = new FieldSetEntity(); FieldMetaEntity f = new FieldMetaEntity(); f.setTableName(new Object[] { CmnConst.PRODUCT_SYS_ORGANIZATIONAL_STRUCTURE_STORAGE }); fss.setMeta(f); fss.setValue(CmnConst.UUID, fs.getString(CmnConst.UUID)); DataTableEntity subDt = new DataTableEntity(); String[] organization_names = fs.getString(CmnConst.ORGANIZATION_NAMES).split(","); String[] organization_types = fs.getString(CmnConst.ORGANIZATION_TYPES).split(","); String[] organization_uuids = fs.getString(CmnConst.ORGANIZATION_UUIDS).split(","); if (organization_names.length != organization_types.length || organization_types.length != organization_uuids.length) { throw new BaseException(SystemCode.SYSTEM_UPDATE_TIMINGORGANIZATION_CENTER.getText(), SystemCode.SYSTEM_UPDATE_TIMINGORGANIZATION_CENTER.getValue(), this.getClass(), "timingOrganization"); } for (int j = 0; j < organization_names.length; j++) { FieldSetEntity existencefSetEntity = null; switch (organization_types[j]){ case "1": existencefSetEntity = baseDao.getFieldSetEntity(CmnConst.PRODUCT_SYS_STAFFS, organization_uuids[j], false); break; case "2": existencefSetEntity = baseDao.getFieldSetEntity(CmnConst.PRODUCT_SYS_ORG_LEVELS, organization_uuids[j], false); break; case "3": existencefSetEntity = baseDao.getFieldSetEntity(CmnConst.PRODUCT_SYS_JOB_POSTS, organization_uuids[j], false); break; case "4": case "5": existencefSetEntity = baseDao.getFieldSetEntity(CmnConst.PRODUCT_SYS_GROUP, organization_uuids[j], false); break; default: } if (existencefSetEntity != null) { FieldSetEntity sbufss = new FieldSetEntity(); FieldMetaEntity subf = new FieldMetaEntity(); subf.setTableName(new Object[] { "organizational_sub" }); sbufss.setMeta(subf); sbufss.setValue(CmnConst.ORGANIZATION_NAMES, organization_names[j]); sbufss.setValue(CmnConst.ORGANIZATION_TYPES, organization_types[j]); sbufss.setValue(CmnConst.ORGANIZATION_UUIDS, organization_uuids[j]); subDt.addFieldSetEntity(sbufss); } } if (subDt.getRows() == 0) { baseDao.delete(CmnConst.PRODUCT_SYS_ORGANIZATIONAL_STRUCTURE_STORAGE, "uuid=?", new String[] { fs.getString(CmnConst.UUID) }); } else { fss.addSubDataTable(subDt); saveOrganization(fss); } } } /** * 修改员工岗位或部门就修改组内容 * @param department_uuid 部门uuid * @param company_uuid 岗位uuid * @param staff_uuid 员工uuid */ public void updataGruopContent(String department_uuid,String company_uuid,String staff_uuid) throws BaseException{ FieldSetEntity staff = baseDao.getFieldSetEntity(CmnConst.PRODUCT_SYS_STAFFS,staff_uuid,false); if (staff == null){ return; } String staff_id = staff.getString(CmnConst.STAFF_ID); //当前人之前岗位或部门是否在组里面 在组里 StringBuilder sql = new StringBuilder(); sql.append(" SELECT b.uuid FROM product_sys_group_select a LEFT JOIN product_sys_group_sub b on a.group_uuid = b.group_uuid ") .append(" WHERE b.staff_id = ? AND (a.organization_types = 2 or a.organization_types = 3) AND a.group_uuid not in ( ") .append(" SELECT c.group_uuid FROM product_sys_group_select c WHERE c.organization_types = 1) GROUP BY b.uuid"); DataTableEntity dataTableEntity = baseDao.listTable(sql.toString(), new Object[]{staff_id}); for (int i = 0,length = dataTableEntity.getRows(); i < length; i++) { FieldSetEntity fieldSetEntity = dataTableEntity.getFieldSetEntity(i); //删除之前的组成员 baseDao.delete(CmnConst.PRODUCT_SYS_GROUP_SUB,new String[]{fieldSetEntity.getString(CmnConst.UUID)}); } //查询当前部门或岗位是否存在组里面 DataTableEntity dataOrganization = baseDao.listTable("SELECT * FROM product_sys_group_select WHERE (organization_types = 2 AND organization_uuids = ? ) or (organization_types = 3 AND organization_uuids = ?)", new Object[]{department_uuid, company_uuid}); //当前部门或岗位在组里面 if(!BaseUtil.dataTableIsEmpty(dataOrganization)){ for (int i = 0,length = dataOrganization.getRows(); i < length; i++) { FieldSetEntity fieldOrganization = dataOrganization.getFieldSetEntity(i); //组主表uuid String group_uuid = fieldOrganization.getString(CmnConst.GROUP_UUID); //组uuid String organization_uuids = fieldOrganization.getString(CmnConst.ORGANIZATION_UUIDS); // 部门 2 if(fieldOrganization.getString(CmnConst.ORGANIZATION_TYPES).equals("2")){ addGruopContent(organization_uuids,department_uuid,group_uuid,staff_id); // 岗位 3 }else if(fieldOrganization.getString(CmnConst.ORGANIZATION_TYPES).equals("3")){ addGruopContent(organization_uuids,company_uuid,group_uuid,staff_id); } } } } /** * 组岗位部门新增员工 时时修改群主表 * @param organization_uuids 组的部门和岗位uuid * @param company_uuid 部门或岗位uuid * @param group_uuid 组uuid * @param staff_id 员工id */ public void addGruopContent(String organization_uuids,String company_uuid,String group_uuid,String staff_id) throws BaseException{ //当前人岗位跟公司组岗位相等 if(organization_uuids.equals(company_uuid)){ //查询组部门下是否有此人 DataTableEntity data = baseDao.listTable("SELECT * FROM product_sys_group_sub WHERE group_uuid = ? AND staff_id = ?",new Object[]{group_uuid,staff_id}); //为空就新增一条数据 if(BaseUtil.dataTableIsEmpty(data)){ FieldSetEntity newField = new FieldSetEntity(); newField.setTableName(CmnConst.PRODUCT_SYS_GROUP_SUB); newField.setValue(CmnConst.GROUP_UUID,group_uuid); newField.setValue(CmnConst.STAFF_ID,staff_id); newField.setValue(CmnConst.CREATED_BY,SpringMVCContextHolder.getCurrentUser().getUser_id()); newField.setValue(CmnConst.CREATED_UTC_DATETIME,new Date()); baseDao.add(newField); } } } }