package com.product.org.admin.service; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import com.product.admin.service.OrganizationCacheService; import com.product.core.cache.DataPoolRefreshCache; import com.product.core.entity.FieldMetaEntity; import com.product.module.sys.entity.SystemUser; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import org.apache.commons.lang3.StringUtils; import com.product.admin.service.UpdateLoginUserInfoService; import com.product.core.dao.BaseDao; import com.product.core.entity.DataTableEntity; 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.org.admin.config.CmnConst; import com.product.org.admin.config.SystemCode; import com.product.org.admin.util.InternationalLanguage; import com.product.util.BaseUtil; import com.product.core.permission.PermissionService; import java.util.*; import java.util.Map.Entry; /** * Copyright © 2020 LX-BASE * * @Title: LX-BASE-SERVER * @Project: base-server-org-admin * @Date: 2020年6月10日 * @Author: Mr.Xu * @Description:岗位管理Service */ @Component public class SystemPostsService extends AbstractBaseService { @Autowired public BaseDao dao; @Autowired PermissionService permissionService; @Autowired private UpdateLoginUserInfoService updateLoginUserInfoService; @Autowired public QueryFilterService queryFilterService; @Autowired public OrganizationCacheService organizationCacheService; /** * 岗位管理列表 * * @param fse * @return * @throws BaseException */ public DataTableEntity listPosts(FieldSetEntity fse) throws BaseException { // 郑盟 2020年12月22日 下午18:34 String queryFilter = ""; if (!BaseUtil.dataTableIsEmpty(fse.getSubDataTable("systemSeniorQueryString"))) { queryFilter = " and " + queryFilterService.getQueryFilter(fse); } // 数据条件 String dataFilter; SystemUser currentUser = SpringMVCContextHolder.getCurrentUser(); if(1 == currentUser.getUserType()){ // dataFilter = CmnConst.DEPT_UUID +" = "+ "'"+currentUser.getOrg_level_uuid()+"'"; dataFilter = CmnConst.DEPT_UUID + " = 1"; }else { fse.setValue(CmnConst.ORG_LEVEL_UUID, currentUser.getOrg_level_uuid()); dataFilter = permissionService.getDataFilter(CmnConst.ORG_LEVEL_UUID) + " and " + CmnConst.DEPT_UUID + " != 1"; } // 岗位信息 DataTableEntity dtJobPostInfo = dao.listTable("product_sys_job_posts", dataFilter + queryFilter, new Object[]{}, new Object[]{}, CmnConst.SEQUENCE, fse.getInteger(CmnConst.PAGESIZE), fse.getInteger(CmnConst.CPAGE)); // 岗位信息国际化 // 遍历岗位信息 if (dtJobPostInfo != null && dtJobPostInfo.getRows() > 0) { for (int i = 0; i < dtJobPostInfo.getRows(); i++) { FieldSetEntity fseJobPostInfo = dtJobPostInfo.getFieldSetEntity(i); String job_post_uuid = fseJobPostInfo.getUUID(); // 岗位uuid String dept_uuid = fseJobPostInfo.getString("dept_uuid"); // 所属部门 // 获取岗位对应岗位等级 DataTableEntity dtPostGrade = dao.listTable("product_sys_job_posts_grades_mapping", "job_post_uuid=?", new Object[]{job_post_uuid}); StringBuilder job_post_grades = new StringBuilder(); forDtPostGrade(dtPostGrade, job_post_grades); // 装载人员数量 DataTableEntity staffsNum = dao.listTable("product_sys_staffs", "job_post_uuid=?", new Object[]{job_post_uuid}); fseJobPostInfo.setValue("staffs_number", staffsNum == null ? 0 : staffsNum.getRows()); if(1 == currentUser.getUserType()){ continue; } // 装载岗位等级 fseJobPostInfo.setValue("job_post_grades", job_post_grades); // 获取部门信息 FieldSetEntity fseDeptInfo = dao.getFieldSetEntity(CmnConst.PRODUCT_SYS_ORG_LEVELS, dept_uuid, false); if (fseDeptInfo == null) { throw new BaseException(SystemCode.SYSTEM_GET_DEPARTMENTINFO_FAIL.getValue(), SystemCode.SYSTEM_GET_DEPARTMENTINFO_FAIL.getText()); } FieldSetEntity fseDeptLanguage = dao.listInternationDataTable(fseDeptInfo, null); // 获取部门国际化全称 Map deptAllLanguage = InternationalLanguage .parseLanguage(fseDeptLanguage.getSubDataTable(CmnConst.ORG_LEVEL_ALL)); // 获取客户信息 FieldSetEntity fseClientInfo = dao.getFieldSetEntity(CmnConst.PRODUCT_SYS_CLIENTS, fseDeptInfo.getString("client_uuid"), false); if (fseClientInfo == null) { throw new BaseException(SystemCode.GET_CLINET_FIAL.getValue(), SystemCode.GET_CLINET_FIAL.getText()); } FieldSetEntity fseClientLanguage = dao.listInternationDataTable(fseClientInfo, null); // 获取部门国际化名称 Map clientNameLanguage = InternationalLanguage .parseLanguage(fseClientLanguage.getSubDataTable(CmnConst.CLIENT_NAME)); Map international = new HashMap<>(); // 创建国际化信息 DataTableEntity dtInternational = new DataTableEntity(); FieldMetaEntity fseMate = new FieldMetaEntity(); fseMate.setTableName(new Object[]{"dept_all"}); // 遍历客户名称国际化 forClientNameLanguage(clientNameLanguage, deptAllLanguage, dtInternational); international.put("dept_all", dtInternational); // 装载全路径 fseJobPostInfo.setSubData(international); } } DataTableEntity dtJobPostLanguage = dao.listInternationDataTable(dtJobPostInfo, null); dao.loadPromptData(dtJobPostLanguage); return dtJobPostLanguage; } private void forDtPostGrade(DataTableEntity dtPostGrade, StringBuilder job_post_grades) { if (dtPostGrade.getRows() > 0) { for (int j = 0; j < dtPostGrade.getRows(); j++) { FieldSetEntity fsePostGradeInfo = dtPostGrade.getFieldSetEntity(j); job_post_grades.append(fsePostGradeInfo.getUUID()); if (j != dtPostGrade.getRows() - 1) { job_post_grades.append(","); } } } } private void forClientNameLanguage(Map clientNameLanguage, Map deptAllLanguage, DataTableEntity dtInternational) { if (clientNameLanguage == null) { return; } for (Entry clientNameLanguageMap : clientNameLanguage.entrySet()) { String languageCode = clientNameLanguageMap.getKey(); String languageClientName = clientNameLanguageMap.getValue(); if (deptAllLanguage != null) { String languageDeptAll = deptAllLanguage.get(languageCode); FieldSetEntity fseLanguage = new FieldSetEntity(); fseLanguage.setTableName("dept_all"); fseLanguage.setValue(languageCode, languageClientName + ">" + languageDeptAll); dtInternational.addFieldSetEntity(fseLanguage); } else { FieldSetEntity fseLanguage = new FieldSetEntity(); fseLanguage.setTableName("dept_all"); fseLanguage.setValue(languageCode, languageClientName); dtInternational.addFieldSetEntity(fseLanguage); } } } /** * 岗位详情 * * @param fse * @return * @throws BaseException */ public FieldSetEntity findPost(FieldSetEntity fse) throws BaseException { FieldSetEntity fs = dao.getFieldSetEntity(fse.getTableName(), fse.getString(CmnConst.UUID), false); DataTableEntity dt2 = dao.listTable("product_sys_job_posts_grades_mapping", "job_post_uuid=?", new String[]{fs.getString("uuid")}); StringBuilder grade_uuids = new StringBuilder(); if (dt2.getRows() > 0) { for (int j = 0; j < dt2.getRows(); j++) { FieldSetEntity fs2 = dt2.getFieldSetEntity(j); grade_uuids.append(fs2.getString("job_grade_uuid")); if (j + 1 < dt2.getRows() && dt2.getFieldSetEntity(j + 1) != null) { grade_uuids.append(","); } } } fs.setValue("job_post_grades", grade_uuids); // xu pengcheng updateTime 2020-11-28 14:33:18 删除手动国际化代码 // 杜洪波 updatetime 2020-12-08 17:19:00 岗位详情封装使用人数 DataTableEntity postToStaff = dao.listTable(CmnConst.PRODUCT_SYS_STAFFS, "job_post_uuid=?", new Object[]{fse.getString(CmnConst.UUID)}); if (BaseUtil.dataTableIsEmpty(postToStaff)) { fs.setValue("staffs_number", 0); } else { fs.setValue("staffs_number", postToStaff.getRows()); } return dao.listInternationDataTable(fs, null); } /** * 新增岗位 * * @param fse * @return * @throws BaseException */ public String addPost(FieldSetEntity fse) throws BaseException { FieldSetEntity post = findPostByAdd(fse); if (post != null && !StringUtils.isEmpty(post.getUUID())) { throw new BaseException(SystemCode.SYSTEM_POST_EXIST_DEPT_ADD_FAIL.getValue(), SystemCode.SYSTEM_POST_EXIST_DEPT_ADD_FAIL.getText()); } fse = BaseUtil.createCreatorAndCreationTime(SpringMVCContextHolder.getCurrentUser(), fse); FieldSetEntity fs = fse; fs.remove("job_post_grades"); fs.setValue("org_level_uuid", SpringMVCContextHolder.getCurrentUser().getOrg_level_uuid()); String uuid = dao.add(fs); String job_post_grades = fse.getString("job_post_grades"); if(!BaseUtil.strIsNull(job_post_grades)){ String[] grades = job_post_grades.split(","); for (int i = 0; i < grades.length; i++) { FieldSetEntity fsg = new FieldSetEntity(); FieldMetaEntity f = new FieldMetaEntity(); f.setTableName(new Object[]{"product_sys_job_posts_grades_mapping"}); fsg.setMeta(f); fsg.setValue("job_post_uuid", uuid); fsg = BaseUtil.createCreatorAndCreationTime(SpringMVCContextHolder.getCurrentUser(), fsg); String grade_uuid = grades[i]; fsg.setValue("job_grade_uuid", grade_uuid); dao.add(fsg); } } return uuid; } /** * 查重 */ public FieldSetEntity findPostByAdd(FieldSetEntity fse) throws BaseException { fse = BaseUtil.createCreatorAndCreationTime(SpringMVCContextHolder.getCurrentUser(), fse); return dao.getFieldSetEntityByFilter( "product_sys_job_posts", "job_post_name=? and org_level_uuid=? and dept_uuid=?", new String[]{ fse.getString("job_post_name"), fse.getString("org_level_uuid"), fse.getString("dept_uuid")}, false); } /** * 修改岗位 * * @param fse * @return * @throws BaseException */ public boolean updatePost(FieldSetEntity fse) throws BaseException { FieldSetEntity fieldSetEntity = dao.getFieldSetEntity(fse.getTableName(), fse.getString(CmnConst.UUID), false); String job_post_grades = fse.getString("job_post_grades"); // 验证岗位等级是否被删除,如有被删除的验证员工是否引用了此岗位 verifPostGrades(job_post_grades, fse); // 验证是否修改所属部门 if (!fieldSetEntity.getString("dept_uuid").equals(fse.getString("dept_uuid"))) { boolean verifyPostExistStaffs = verifyPostExistStaffs(fse.getString(CmnConst.UUID)); if (verifyPostExistStaffs) { throw new BaseException(SystemCode.SYSTEM_POSTS_UPDATE_EXIST_STAFF_FAIL.getValue(), SystemCode.SYSTEM_POSTS_UPDATE_EXIST_STAFF_FAIL.getText(), this.getClass(), "updatePost"); } } fse = BaseUtil.createCreatorAndCreationTime(SpringMVCContextHolder.getCurrentUser(), fse); DataTableEntity grades_mapping = dao.listTable("product_sys_job_posts_grades_mapping", "job_post_uuid=?", new String[]{fse.getUUID()}); List list = new ArrayList<>(); if (grades_mapping.getRows() > 0) { for (int i = 0; i < grades_mapping.getRows(); i++) { FieldSetEntity fss = grades_mapping.getFieldSetEntity(i); list.add(fss.getString("job_grade_uuid")); } } addDelGrade(fse, list); fse.remove("job_post_grades"); // xupengcheng dateTime 2020年11月28日14:35:21 修改岗位更新所属岗位人员信息 updateLoginUserInfoService.updateUserInfoByPostUuid(fse.getUUID(), true); return dao.update(fse); } private void verifPostGrades(String job_post_grades, FieldSetEntity fse) { if (!StringUtils.isEmpty(job_post_grades)) { String newjob_post_grades = fse.getString("job_post_grades"); String[] job_post_grade = job_post_grades.split(","); StringBuilder uuids = new StringBuilder(); appendUuids(newjob_post_grades, job_post_grade, uuids, job_post_grades); if (uuids.length() != 0) { String uuidString = uuids.toString().substring(1); String sql = "select * from " + CmnConst.PRODUCT_SYS_STAFFS + " where job_post_grade_uuid in ('" + uuidString.replace(",", "','") + "')"; DataTableEntity listTable = dao.listTable(sql, new Object[]{}); if (listTable != null && listTable.getRows() > 0) throw new BaseException(SystemCode.SYSTEM_POSTS_DELETE_EXIST_POST_GRAGES_FAIL.getValue(), SystemCode.SYSTEM_POSTS_DELETE_EXIST_POST_GRAGES_FAIL.getText(), this.getClass(), "updatePost"); } } } private void appendUuids(String newjob_post_grades, String[] job_post_grade, StringBuilder uuids, String job_post_grades) { if (!StringUtils.isEmpty(newjob_post_grades)) { String[] newjob_post_grade = newjob_post_grades.split(","); for (int i = 0; i < job_post_grade.length; i++) { boolean is_exist = false; for (int j = 0; j < newjob_post_grade.length; j++) { if (job_post_grade[i].equals(newjob_post_grade[j])) { is_exist = true; break; } } if (!is_exist) { uuids.append(","); uuids.append(job_post_grade[i]); } } } else { // 改变 等于空 uuids.append(","); uuids.append(job_post_grades); } } private void addDelGrade(FieldSetEntity fse, List list) { // 前台等级 String[] grade_view = fse.getString("job_post_grades").split(","); // 数据库等级 List list_view = Arrays.asList(grade_view); // 新增等级 for (int i = 0; i < grade_view.length; i++) { String grade_v = grade_view[i]; if (list.contains(grade_v)) { continue; } else { FieldSetEntity fs = new FieldSetEntity(); fs.setTableName("product_sys_job_posts_grades_mapping"); fs.setValue("job_post_uuid", fse.getString("uuid")); fs.setValue("job_grade_uuid", grade_v); fs = BaseUtil.createCreatorAndCreationTime(SpringMVCContextHolder.getCurrentUser(), fs); dao.add(fs); } } // 删除等级 for (int i = 0; i < list.size(); i++) { String grade_v = list.get(i); if (list_view.contains(grade_v)) { continue; } else { FieldSetEntity fsd = dao.getFieldSetEntityByFilter("product_sys_job_posts_grades_mapping", "job_post_uuid=? and job_grade_uuid=?", new String[]{fse.getString("uuid"), grade_v}, false); dao.delete("product_sys_job_posts_grades_mapping", "uuid=?", new String[]{fsd.getString("uuid")}); } } } /** * 查询岗位是否有关联员工 * * @param post_uuid * @return * @throws BaseException */ private boolean verifyPostExistStaffs(String post_uuid) throws BaseException { DataTableEntity listTable = dao.listTable(CmnConst.PRODUCT_SYS_STAFFS, "job_post_uuid=?", new Object[]{post_uuid}); if (!BaseUtil.dataTableIsEmpty(listTable)) return true; return false; } /** * 删除岗位 * * @param fse * @return * @throws BaseException */ public boolean deletePost(FieldSetEntity fse) throws BaseException { boolean verifyPostExistStaffs = verifyPostExistStaffs(fse.getString(CmnConst.UUID)); if (verifyPostExistStaffs) { throw new BaseException(SystemCode.SYSTEM_POSTS_DELETE_EXIST_STAFF_FAIL.getValue(), SystemCode.SYSTEM_POSTS_DELETE_EXIST_STAFF_FAIL.getText(), this.getClass(), "deletePost"); } DataTableEntity dt = dao.listTable("product_sys_job_posts_grades_mapping", "job_post_uuid=?", new String[]{fse.getUUID()}); if (dt.getRows() > 0) { for (int i = 0; i < dt.getRows(); i++) { FieldSetEntity fs = dt.getFieldSetEntity(i); dao.delete("product_sys_job_posts_grades_mapping", "uuid=?", new String[]{fs.getString("uuid")}); } } return dao.delete(fse.getTableName(), new String[]{fse.getString(CmnConst.UUID)}); } // 数据迁移 public Boolean movePost() throws BaseException { DataTableEntity dt = dao.listTable("product_sys_job_posts"); if (dt.getRows() > 0) { for (int i = 0; i < dt.getRows(); i++) { FieldSetEntity fs = dt.getFieldSetEntity(i); String[] job_post_grades = fs.getString("job_post_grades").split(","); for (int j = 0; j < job_post_grades.length; j++) { String job_post_grade = job_post_grades[j]; FieldSetEntity fsd = new FieldSetEntity(); fsd.setTableName("product_sys_job_posts_grades_mapping"); fsd.setValue("job_post_uuid", fs.getString("uuid")); fsd.setValue("job_grade_uuid", job_post_grade); fsd.setValue("created_by", fs.getString("created_by")); fsd.setValue("created_utc_datetime", fs.getString("created_utc_datetime")); dao.add(fsd); } } return true; } return false; } /** * 获取通用岗位 * @param fse * @return */ public DataTableEntity getGeneralPostName(FieldSetEntity fse){ DataTableEntity dataTableEntity = dao.listTable(" SELECT * FROM product_sys_job_posts WHERE LENGTH(dept_uuid) < 16 ",new String[]{},fse.getInteger(CmnConst.PAGESIZE),fse.getInteger(CmnConst.CPAGE)); return dataTableEntity; } public Boolean addGeneralPostName(FieldSetEntity fse){ //获取org_level_uuid String orgLevelUuid = SpringMVCContextHolder.getCurrentUser().getOrg_level_uuid(); Integer userId = SpringMVCContextHolder.getCurrentUser().getUser_id(); FieldSetEntity sqlFse = dao.getFieldSetBySQL(" SELECT MAX(sequence) maxSequence FROM product_sys_job_posts ", new String[]{},false); String maxSequence = sqlFse.getString("maxSequence"); Integer indexNum = 0; if(!BaseUtil.strIsNull(maxSequence)){ indexNum = Integer.parseInt(maxSequence); } //岗位名称集 String[] job_post_name = fse.getString("job_post_names").split(","); //部门元素集 String dept = fse.getString("dept"); JSONArray deptArray = JSONArray.parseArray(dept); DataTableEntity dataTableEntity = new DataTableEntity(); FieldMetaEntity f = new FieldMetaEntity(); f.setTableName(new String[]{ "product_sys_job_posts" }); dataTableEntity.setMeta(f); for (int j = 0; j < job_post_name.length; j++) { contDept: for (int i = 0; i < deptArray.size(); i++) { JSONObject deptObject = deptArray.getJSONObject(i); JSONArray children = deptObject.getJSONArray("children"); //下面没有岗位 if(children != null && children.size() > 0){ for (int k = 0; k < children.size(); k++) { JSONObject postObject = children.getJSONObject(k); if(job_post_name[j].equals(postObject.getString("label"))){ //相同就不添加岗位 continue contDept; } } } indexNum ++; FieldSetEntity fieldSetEntity = new FieldSetEntity(); fieldSetEntity.setTableName("product_sys_job_posts"); fieldSetEntity.setValue("org_level_uuid",orgLevelUuid); fieldSetEntity.setValue("job_post_description",job_post_name[j]); fieldSetEntity.setValue("is_used",1); fieldSetEntity.setValue("sequence",indexNum); fieldSetEntity.setValue("dept_uuid",deptObject.getString("value")); fieldSetEntity.setValue("job_post_name",job_post_name[j]); fieldSetEntity.setValue("created_by" ,userId); fieldSetEntity.setValue("created_utc_datetime" , new Date()); dataTableEntity.addFieldSetEntity(fieldSetEntity); } } if(dataTableEntity.getRows() == 0){ throw new BaseException(SystemCode.SYSTEM_POST_EXIST_DEPT_ADD_FAIL.getValue(), SystemCode.SYSTEM_POST_EXIST_DEPT_ADD_FAIL.getText()); } Boolean flag = dao.add(dataTableEntity); //刷新岗位缓存 // organizationCacheService.cachePost(null); //更新缓存数据------------------------------------------- // DataPoolRefreshCache.getInstance().put("product_sys_job_posts"); return flag; } }