package com.product.org.admin.service; import java.security.NoSuchAlgorithmException; import java.security.SecureRandom; import java.util.ArrayList; import java.util.Date; import java.util.LinkedList; import java.util.List; import java.util.Random; import com.product.admin.config.SystemCode; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import com.alibaba.druid.util.StringUtils; import com.product.org.admin.config.CmnConst; 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.permission.PermissionService; 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.org.admin.service.idel.IOperationReplaceService; import com.product.util.BaseUtil; /** * * Copyright LX-BASE * * @Title: OperationReplaceService * @Project: LX-BASE-SERVER * @Date: 2020年5月29日 上午10:59:05 * @Author: 郑盟 * @Description:业务代理设置,对应业务代理表 */ @Component public class OperationReplaceService extends AbstractBaseService implements IOperationReplaceService { @Autowired public BaseDao baseDao; @Autowired PermissionService permissionService; @Autowired public QueryFilterService queryFilterService; /** * 业务操作代替列表查询 */ public DataTableEntity listReplace(FieldSetEntity fs) throws BaseException { String dataFilter = permissionService.getDataFilter(CmnConst.ORG_LEVEL_UUID); // 郑盟 2020年12月22日 下午18:34 String queryFilter = ""; if (!BaseUtil.dataTableIsEmpty(fs.getSubDataTable("systemSeniorQueryString"))) { if (!StringUtils.isEmpty(dataFilter)) { queryFilter = " and" + queryFilterService.getQueryFilter(fs); } else { queryFilter = queryFilterService.getQueryFilter(fs); } } return baseDao.listTable(CmnConst.PRODUCT_SYS_APPLY_ON_BEHALF, dataFilter + queryFilter, new String[] {}, new String[] {}, null, fs.getInteger("pagesize"), fs.getInteger("cpage")); } /** * 业务操作代替详情查询 */ public FieldSetEntity findReplace(FieldSetEntity fs) throws BaseException { return baseDao.getFieldSetEntity(CmnConst.PRODUCT_SYS_APPLY_ON_BEHALF, fs.getString("uuid"), true); } /** * 业务操作代替新增 */ @Transactional public String addReplaceList(FieldSetEntity fs) throws BaseException { fs.setValue(CmnConst.CREATED_BY, SpringMVCContextHolder.getCurrentUser().getUser_id()); fs.setValue(CmnConst.CREATED_UTC_DATETIME, new Date()); String org_level_uuid = fs.getString(CmnConst.ORG_LEVEL_UUID); if (StringUtils.isEmpty(org_level_uuid)) { fs.setValue(CmnConst.ORG_LEVEL_UUID, SpringMVCContextHolder.getCurrentUser().getOrg_level_uuid()); } //罗鑫2021年3月1日 开始时间不能大于结束时间 String effective_utc_start_date = fs.getString("effective_utc_start_date"); if(BaseUtil.strIsNull(effective_utc_start_date)){ throw new BaseException(SystemCode.SYSTEM_VALID_TIME_WE_FAIL.getValue(), SystemCode.SYSTEM_VALID_TIME_WE_FAIL.getText(),this.getClass(),"public String addReplaceList(FieldSetEntity fs) throws BaseException"); } String effective_utc_end_date = fs.getString("effective_utc_end_date"); if(!BaseUtil.strIsNull(effective_utc_end_date)) { if (BaseUtil.temporalComparison(effective_utc_start_date, effective_utc_end_date)) { throw new BaseException(SystemCode.SYSTEM_VALID_TIME_FAIL.getValue(), SystemCode.SYSTEM_VALID_TIME_FAIL.getText(), this.getClass(), "public String addReplaceList(FieldSetEntity fs) throws BaseException"); } } // DataTableEntity subDataTable = fs.getSubDataTable(CmnConst.PRODUCT_SYS_APPLY_ON_BEHALF_DETAILS); // DataTableEntity subDataTable = fs.getSubDataTable(CmnConst.PRODUCT_SYS_APPLY_ON_BEHALF_DETAILS); // if (subDataTable != null) { // for (int i = 0; i < subDataTable.getRows(); i++) { // FieldSetEntity subfs = subDataTable.getFieldSetEntity(i); // subfs.setValue(CmnConst.ORG_LEVEL_UUID, fs.getString(CmnConst.ORG_LEVEL_UUID)); // String addReplaceSubRelation = addReplaceSubRelation(subfs, fs.getString(CmnConst.EFFECTIVE_UTC_START_DATE), // fs.getString(CmnConst.EFFECTIVE_UTC_END_DATE)); // if (addReplaceSubRelation == null) // return null; // } // } return baseDao.add(fs); } /** * 业务操作代替修改 * * @param fs * @return boolean */ @Transactional public boolean updateReplaceList(FieldSetEntity fs) throws BaseException { // DataTableEntity subDataTable = fs.getSubDataTable(CmnConst.PRODUCT_SYS_APPLY_ON_BEHALF_DETAILS); // for (int i = 0; i < subDataTable.getRows(); i++) { // FieldSetEntity subfs = subDataTable.getFieldSetEntity(i); // FieldSetEntity exampleSubFs = baseDao.getFieldSetEntity(CmnConst.PRODUCT_SYS_APPLY_ON_BEHALF_DETAILS, // subfs.getString("uuid"), false); // if (exampleSubFs == null) { // // 新增子表 // subfs.setValue(CmnConst.ORG_LEVEL_UUID, fs.getString(CmnConst.ORG_LEVEL_UUID)); // String addReplaceSubRelation = addReplaceSubRelation(subfs, fs.getString(CmnConst.EFFECTIVE_UTC_START_DATE), // fs.getString(CmnConst.EFFECTIVE_UTC_END_DATE)); // if (addReplaceSubRelation == null) // return false; // continue; // } // String type = subfs.getString("~type~"); // if (type != null && "del".equals(type)) { // // 删除子表 // boolean delReplaceSubRelation = delReplaceSubRelation(exampleSubFs); // if (!delReplaceSubRelation) // return false; // continue; // } // String role_uuid = exampleSubFs.getString(CmnConst.ROLE_UUID); // boolean forRoleUuidArr = forRoleUuidArr(role_uuid, i, fs, subfs); // // 前端发过啊里的修改后的数据替换人uuid // boolean forStaff = forStaff(exampleSubFs, subfs, role_uuid); // if (!forStaff||!forRoleUuidArr) // return false; // subfs.setValue(CmnConst.ROLE_UUID, role_uuid); // } //罗鑫2021年3月1日 开始时间不能大于结束时间 String effective_utc_start_date = fs.getString("effective_utc_start_date"); if(BaseUtil.strIsNull(effective_utc_start_date)){ throw new BaseException(SystemCode.SYSTEM_VALID_TIME_WE_FAIL.getValue(), SystemCode.SYSTEM_VALID_TIME_WE_FAIL.getText(),this.getClass(),"public boolean updateReplaceList(FieldSetEntity fs) throws BaseException"); } String effective_utc_end_date = fs.getString("effective_utc_end_date"); if(!BaseUtil.strIsNull(effective_utc_end_date)) { if (BaseUtil.temporalComparison(effective_utc_start_date, effective_utc_end_date)) { throw new BaseException(SystemCode.SYSTEM_VALID_TIME_FAIL.getValue(), SystemCode.SYSTEM_VALID_TIME_FAIL.getText(), this.getClass(), "public boolean updateReplaceList(FieldSetEntity fs) throws BaseException"); } } return updateReplaceTime(fs); } private boolean forStaff(FieldSetEntity exampleSubFs, FieldSetEntity subfs, String role_uuid) { // 修改替换人后staffs表role_uuids的处理 String replace_staff_uuid = exampleSubFs.getString(CmnConst.ON_BEHALF_STAFF_UUID); String staff_uuid_arr = subfs.getString(CmnConst.ON_BEHALF_STAFF_UUID); String[] role_uuid_arr = role_uuid.split(","); if (!replace_staff_uuid.equals(staff_uuid_arr)) { String[] split = replace_staff_uuid.split(","); for (int j = 0; j < split.length; j++) { String staff_uuid = split[j]; FieldSetEntity staffsFs = baseDao.getFieldSetEntity(CmnConst.PRODUCT_SYS_STAFFS, staff_uuid, false); String[] delArrVal = delArrVal(staffsFs.getString(CmnConst.ROLE_UUIDS).split(","), role_uuid_arr); String sqls = "UPDATE product_sys_staffs SET role_uuids = ? where uuid=?"; boolean staffUpdate = baseDao.executeUpdate(sqls, new String[] { String.join(",", delArrVal), staff_uuid }); if (!staffUpdate) return staffUpdate; } String[] split2 = staff_uuid_arr.split(","); for (int j = 0; j < split2.length; j++) { String staff_uuid = split2[j]; ArrayList list = new ArrayList<>(); FieldSetEntity staffsFs = baseDao.getFieldSetEntity(CmnConst.PRODUCT_SYS_STAFFS, staff_uuid, false); list.add(staffsFs.getString(CmnConst.ROLE_UUIDS)); list.add(role_uuid); String sqls = "UPDATE product_sys_staffs SET role_uuids = ? where uuid=?"; boolean staffUpdate = baseDao.executeUpdate(sqls, new String[] { String.join(",", list), staff_uuid }); if (!staffUpdate) return staffUpdate; } } return true; } private boolean forRoleUuidArr(String role_uuid, int i, FieldSetEntity fs, FieldSetEntity subfs) { fs.setValue(CmnConst.UPDATED_BY, SpringMVCContextHolder.getCurrentUser().getUser_id()); fs.setValue(CmnConst.UPDATED_UTC_DATETIME, new Date()); String[] role_uuid_arr = role_uuid.split(","); String sql = "UPDATE product_sys_role SET effective_start_date = ?,effective_end_date=? where uuid =?"; for (int j = 0; j < role_uuid_arr.length; j++) { boolean executeUpdate = baseDao.executeUpdate(sql, new String[] { fs.getString(CmnConst.EFFECTIVE_UTC_START_DATE), fs.getString(CmnConst.EFFECTIVE_UTC_END_DATE), role_uuid_arr[i] }); if (!executeUpdate) return executeUpdate; // 保存角色权限表表 FieldSetEntity fun_permissionFs = baseDao.getFieldSetEntityByFilter(CmnConst.PRODUCT_SYS_FUNCTION_PERMISSION, "role_uuid=?", new String[] { role_uuid_arr[i] }, false); fun_permissionFs.setValue(CmnConst.FUNCTION_UUID, subfs.getString(CmnConst.FUNCTION_UUID)); fun_permissionFs.setValue(CmnConst.BUTTON_UUID, subfs.getString(CmnConst.BUTTON_UUID)); fun_permissionFs.setValue(CmnConst.ORG_LEVEL_UUID, fs.getString(CmnConst.ORG_LEVEL_UUID)); fun_permissionFs.setValue(CmnConst.UPDATED_BY, SpringMVCContextHolder.getCurrentUser().getUser_id()); fun_permissionFs.setValue(CmnConst.UPDATED_UTC_DATETIME, new Date()); // 保存功能许可表 boolean permissionUpdate = baseDao.update(fun_permissionFs); if (!permissionUpdate) return permissionUpdate; } return true; } private boolean updateReplaceTime(FieldSetEntity fs) { String start_time = fs.getString("effective_start_time_zone"); // 郑盟 2020年12月17日 下午16:05 if (StringUtils.isEmpty(start_time)) { fs.setValue("effective_start_time_zone", null); } String end_time = fs.getString("effective_end_time_zone"); // 郑盟 2020年12月17日 下午16:05 if (StringUtils.isEmpty(end_time)) { fs.setValue("effective_end_time_zone", null); } return baseDao.update(fs); } @Transactional public boolean deleteReplaceList(FieldSetEntity fs) throws BaseException { // String uuid = fs.getString("uuid"); // FieldSetEntity fss = baseDao.getFieldSetEntity(CmnConst.PRODUCT_SYS_APPLY_ON_BEHALF, uuid, true); // DataTableEntity subDataTable = fss.getSubDataTable(CmnConst.PRODUCT_SYS_APPLY_ON_BEHALF_DETAILS); boolean delete = baseDao.delete(CmnConst.PRODUCT_SYS_APPLY_ON_BEHALF, "uuid=?", new String[] { fs.getString("uuid") }); // for (int i = 0; i < subDataTable.getRows(); i++) { // FieldSetEntity detailsFs = subDataTable.getFieldSetEntity(i); // boolean delReplaceSubRelation = delReplaceSubRelation(detailsFs); // if (!delReplaceSubRelation) // return false; // } return delete; } /** * 新增子表 * * @param detailsFs * @return * @throws BaseException */ public String addReplaceSubRelation(FieldSetEntity detailsFs, String effective_start, String effective_end) throws BaseException { String staff_uuid_arr = detailsFs.getString(CmnConst.ON_BEHALF_STAFF_UUID); if (staff_uuid_arr == null) { return null; } String[] split = staff_uuid_arr.split(","); String add = null; StringBuilder str = new StringBuilder();// 定义变长字符串 Random random = null; try { random = SecureRandom.getInstanceStrong(); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); SpringMVCContextHolder.getSystemLogger().error(e); return null; } // 随机生成源数字,并添知加到字符串 for (int j = 0; j < 8; j++) { if (random != null) { str.append(random.nextInt(10)); } } FieldSetEntity roleFs = new FieldSetEntity(); FieldMetaEntity fm = new FieldMetaEntity(); fm.setTableName(new Object[] { CmnConst.PRODUCT_SYS_ROLE }); roleFs.setMeta(fm); roleFs.setValue("role_description", "业务操作代替创建role_" + str.toString()); roleFs.setValue(CmnConst.IS_USED, "1"); roleFs.setValue(CmnConst.ORG_LEVEL_UUID, detailsFs.getString(CmnConst.ORG_LEVEL_UUID)); roleFs.setValue(CmnConst.EFFECTIVE_START_DATE, effective_start); roleFs.setValue(CmnConst.EFFECTIVE_END_DATE, effective_end); roleFs.setValue(CmnConst.CREATED_BY, SpringMVCContextHolder.getCurrentUser().getUser_id()); roleFs.setValue(CmnConst.CREATED_UTC_DATETIME, new Date()); String default_language = SpringMVCContextHolder.getCurrentUser().getDefault_language(); DataTableEntity languageDt = parseInternational("role_name", default_language, "role_" + str.toString()); roleFs.addSubDataTable(languageDt); String roleAdd = baseDao.add(roleFs); // 保存角色权限表表 FieldSetEntity fun_permissionFs = new FieldSetEntity(); fm.setTableName(new Object[] { CmnConst.PRODUCT_SYS_FUNCTION_PERMISSION }); fun_permissionFs.setMeta(fm); fun_permissionFs.setValue(CmnConst.ROLE_UUID, roleAdd); fun_permissionFs.setValue(CmnConst.FUNCTION_UUID, detailsFs.getString(CmnConst.FUNCTION_UUID)); fun_permissionFs.setValue(CmnConst.BUTTON_UUID, detailsFs.getString(CmnConst.BUTTON_UUID)); fun_permissionFs.setValue(CmnConst.ORG_LEVEL_UUID, detailsFs.getString(CmnConst.ORG_LEVEL_UUID)); fun_permissionFs.setValue(CmnConst.CREATED_BY, SpringMVCContextHolder.getCurrentUser().getUser_id()); fun_permissionFs.setValue(CmnConst.CREATED_UTC_DATETIME, new Date()); add = baseDao.add(fun_permissionFs); if (add == null) { return add; } // 保存功能许可表 for (int i = 0; i < split.length; i++) { String staff_uuid = split[i]; FieldSetEntity staffsFs = baseDao.getFieldSetEntity(CmnConst.PRODUCT_SYS_STAFFS, staff_uuid, false); if (staffsFs == null || staffsFs.getString("uuid") == null) { return null; } String role_uuids = staffsFs.getString(CmnConst.ROLE_UUIDS); ArrayList list = new ArrayList<>(); list.add(role_uuids); list.add(roleAdd); String role_uuidarr = String.join(",", list); String sql = "UPDATE product_sys_staffs SET role_uuids = ? where uuid=?"; boolean executeUpdate = baseDao.executeUpdate(sql, new String[] { role_uuidarr, staff_uuid }); // 将新增的权限放进 if (!executeUpdate) return null; } detailsFs.setValue(CmnConst.ROLE_UUID, roleAdd); return add; } /** * 删除子表以及还原其他表单数据 * * @param detailsFs * @return * @throws BaseException */ public boolean delReplaceSubRelation(FieldSetEntity detailsFs) throws BaseException { String role_uuid = detailsFs.getString(CmnConst.ROLE_UUID); String[] role_uuid_arr = role_uuid.split(","); String replace_staff_uuid_arr = detailsFs.getString(CmnConst.ON_BEHALF_STAFF_UUID); String[] split = replace_staff_uuid_arr.split(","); boolean executeUpdate = false; for (int i = 0; i < split.length; i++) { String replace_staff_uuid = split[i]; FieldSetEntity replace_staffsFs = baseDao.getFieldSetEntity(CmnConst.PRODUCT_SYS_STAFFS, replace_staff_uuid, false); if (null == replace_staffsFs) { continue; } String role_uuids = replace_staffsFs.getString(CmnConst.ROLE_UUIDS); String[] role_uuidArr = role_uuids.split(","); String[] delArrVal = delArrVal(role_uuidArr, role_uuid_arr); String join = String.join(",", delArrVal); String sql = "UPDATE product_sys_staffs SET role_uuids = ? where uuid =?"; executeUpdate = baseDao.executeUpdate(sql, new String[] { join, replace_staffsFs.getString("uuid") }); if (!executeUpdate) return executeUpdate; } boolean delPermission = baseDao.delete(CmnConst.PRODUCT_SYS_FUNCTION_PERMISSION, BaseUtil.buildQuestionMarkFilter(CmnConst.ROLE_UUID, role_uuid_arr.length, true), role_uuid_arr); if (!delPermission) return false; return baseDao.delete(CmnConst.PRODUCT_SYS_ROLE, BaseUtil.buildQuestionMarkFilter("uuid", role_uuid_arr.length, true), role_uuid_arr); } //删除数组中指定元素 public String[] delArrVal(String[] arr, String[] val) { List list = new LinkedList<>(); for (String str : arr) { // 处理第一个数组,list里面的值为1,2,3,4 if (!list.contains(str.trim())) { list.add(str.trim()); } } for (String str : val) { // 如果第二个数组存在和第一个数组相同的值,就删除 if (list.contains(str.trim())) { list.remove(str.trim()); } } String[] result = {}; // 创建空数组 return list.toArray(result); // List to Array } /** * 国际化封装 * * @param currentField 当前字段 * @param language_code 语言代码 * @param languageValue 语言内容 * @return * @throws BaseException */ public DataTableEntity parseInternational(String currentField, String language_code, String languageValue) throws BaseException { FieldSetEntity fseLanguage = new FieldSetEntity(); fseLanguage.setTableName(currentField); fseLanguage.setValue(language_code, languageValue); DataTableEntity languageDt = new DataTableEntity(); languageDt.addFieldSetEntity(fseLanguage); return languageDt; } }