AOP操作日志记录
1.创建注解
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface PassportLog {String operatePage();String operateType();ClassTypEnum classType();}
2.创建切面
对于字典,可以通过注解属性去转换,枚举也可以注解属性去转换,但是需要if else判断;对于数组类型的传值,需要if else特殊处理;另外需注意反射获取不到子类的属性private
@Slf4j
@Aspect
@Component
@AllArgsConstructor
public class PassportLogAop {/*** 修改记录-字段的分割符号*/private static final String SEPARATOR = ";";@Resourceprivate PassportMapper passportMapper;@Resourceprivate PassportCardFillMapper passportCardFillMapper;@Resourceprivate PassportFamilyMembersMapper passportFamilyMembersMapper;@Resourceprivate PassportEmergencyContactMapper passportEmergencyContactMapper;@Resourceprivate PassportOperateRecordService passportOperateRecordService;@Resourceprivate SysFeign sysFeign;@Resourceprivate SystemDictService systemDictService;@Before("@annotation(passportLog)")public void before(JoinPoint point, PassportLog passportLog) {try {saveLog(point, passportLog);} catch (Exception e) {log.error(e.getMessage());}}/*** 操作日志记录** @param point* @param passportLog* @throws IntrospectionException* @throws InvocationTargetException* @throws IllegalAccessException*/public void saveLog(JoinPoint point, PassportLog passportLog) throws IntrospectionException, InvocationTargetException, IllegalAccessException {ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();if (requestAttributes == null) {return;}PassportOperateRecord record = new PassportOperateRecord();record.setDelFlag(PassportCommonConstant.NOT_DELETE);HttpServletRequest request = Objects.requireNonNull(requestAttributes).getRequest();if (ObjUtil.isNotNull(request)) {record.setOperateIp(IpUtil.getIp(request));record.setUrl(StrUtil.sub(URLUtil.getPath(request.getRequestURI()), 0, 255));record.setMethod(StrUtil.sub(point.getTarget().getClass().getName() + "." + point.getSignature().getName() + "()", 0, 255));record.setRequestMethod(request.getMethod());}record.setOperateTime(LocalDateTime.now());record.setOperatePerson(BaseUserEntity.getName());record.setOperateOffice(BaseUserEntity.getDeptName());record.setOperateRole(sysFeign.queryRoleName(new ArrayList<>(BaseUserEntity.getRoleCode())).getData());record.setOperatePage(passportLog.operatePage());record.setOperateType(passportLog.operateType());Object[] args = point.getArgs();String s = JSONUtil.toJsonStr(args[0]);ClassTypEnum classType = passportLog.classType();String prefix = "";String content = "";if (classType.equals(ClassTypEnum.PASSPORT_ACCEPT_INFO)) {//预约办理PassportAcceptInfoDTO newObj = JSONObject.parseObject(s, PassportAcceptInfoDTO.class);String requestNo = newObj.getRequestNo();record.setRequestNo(requestNo);PassportAcceptInfoDTO oldObj = new PassportAcceptInfoDTO();BeanUtil.copyProperties(passportMapper.selectById(newObj.getId()), oldObj);content = this.compare(oldObj, newObj, requestNo, prefix, content);record.setOperateContent(content);//操作类型Integer status = newObj.getStatus();if (ApplyStatusEnum.UN_ACCEPTED.getCode().equals(status)) {record.setOperateType(PassportOperateTypeConstant.QXSL);} else if (ApplyStatusEnum.PENDING_AUDITING.getCode().equals(status)) {record.setOperateType(PassportOperateTypeConstant.SLBDY);}passportOperateRecordService.save(record);} else if (classType.equals(ClassTypEnum.PASSPORT_ACCEPT_INFO_DRAFT)) {//预约办理草稿箱PassportAcceptInfoDraftDTO newObj = JSONObject.parseObject(s, PassportAcceptInfoDraftDTO.class);String requestNo = newObj.getRequestNo();record.setRequestNo(requestNo);PassportAcceptInfoDraftDTO oldObj = new PassportAcceptInfoDraftDTO();BeanUtil.copyProperties(passportMapper.selectById(newObj.getId()), oldObj);record.setOperateContent(this.compare(oldObj, newObj, requestNo, prefix, content));passportOperateRecordService.save(record);} else if (classType.equals(ClassTypEnum.TODAY_APPLY)) {//申请今日受理TodayApplyDTO newObj = JSONObject.parseObject(s, TodayApplyDTO.class);String requestNo = newObj.getRequestNo();record.setRequestNo(requestNo);record.setOperateContent("申请说明:" + newObj.getReason());passportOperateRecordService.save(record);} else if (classType.equals(ClassTypEnum.PASSPORT_SIDE_HANDLE)) {//现场办理PassportSiteHandleDTO newObj = JSONObject.parseObject(s, PassportSiteHandleDTO.class);record.setRequestNo(newObj.getRequestNo());PassportSiteHandleDTO oldObj = new PassportSiteHandleDTO();if (Objects.isNull(oldObj.getId())) {prefix = "新增:";} else {BeanUtil.copyProperties(passportMapper.selectById(newObj.getId()), oldObj);}record.setOperateContent(this.compare(oldObj, newObj, newObj.getRequestNo(), prefix, content));//操作类型Integer status = newObj.getStatus();if (ApplyStatusEnum.UN_ACCEPTED.getCode().equals(status)) {record.setOperateType(PassportOperateTypeConstant.QXSL);} else if (ApplyStatusEnum.PENDING_AUDITING.getCode().equals(status)) {record.setOperateType(PassportOperateTypeConstant.SLBDY);}passportOperateRecordService.save(record);} else if (classType.equals(ClassTypEnum.PASSPORT_SIDE_HANDLE_DRAFT)) {//现场办理草稿箱PassportSiteHandleDraftDTO newObj = JSONObject.parseObject(s, PassportSiteHandleDraftDTO.class);String requestNo = newObj.getRequestNo();record.setRequestNo(requestNo);PassportSiteHandleDraftDTO oldObj = new PassportSiteHandleDraftDTO();if (Objects.isNull(oldObj.getId())) {prefix = "新增:";} else {BeanUtil.copyProperties(passportMapper.selectById(newObj.getId()), oldObj);}content = this.compare(oldObj, newObj, requestNo, prefix, content);record.setOperateContent(content);passportOperateRecordService.save(record);} else if (classType.equals(ClassTypEnum.PASSPORT_CHECK)) {//护照情况核查、退回修改、保存、生成传真并提交审核PassportCheckDTO newObj = JSONObject.parseObject(s, PassportCheckDTO.class);record.setRequestNo(newObj.getRequestNo());PassportCheckDTO oldObj = new PassportCheckDTO();BeanUtil.copyProperties(passportMapper.selectById(newObj.getId()), oldObj);record.setOperateContent(this.compare(oldObj, newObj, newObj.getRequestNo(), prefix, content));//操作类型Integer status = newObj.getStatus();Integer problemStatus = newObj.getProblemStatus();if (ApplyStatusEnum.NOT_VERIFIED.getCode().equals(status)) {record.setOperateType(PassportOperateTypeConstant.BC);}if (PassportProblemEnum.THXG.getCode().equals(problemStatus)) {record.setOperateType(PassportOperateTypeConstant.THXG);} else if (ApplyStatusEnum.PENDING_AUDITING.getCode().equals(status)) {record.setOperateType(PassportOperateTypeConstant.SCCZBTJSH);}passportOperateRecordService.save(record);} else if (classType.equals(ClassTypEnum.PASSPORT_CHECK_BATCH)) {//批量核查List ids = JSONObject.parseObject(s, List.class);for (Object id : ids) {Passport passport = passportMapper.selectById(Long.parseLong(id.toString()));record.setRequestNo(passport.getRequestNo());record.setOperateContent("批量生成传真并提交审核");passportOperateRecordService.save(record);}} else if (classType.equals(ClassTypEnum.PASSPORT_EXAMINE_BATCH)) {//批量审核List ids = JSONObject.parseObject(s, List.class);for (Object id : ids) {Passport passport = passportMapper.selectById(Long.parseLong(id.toString()));record.setRequestNo(passport.getRequestNo());record.setOperateContent("批量审核通过");passportOperateRecordService.save(record);}} else if (classType.equals(ClassTypEnum.PASSPORT_EXAMINE)) {//审核PassportExamineDTO newObj = JSONObject.parseObject(s, PassportExamineDTO.class);record.setRequestNo(newObj.getRequestNo());PassportExamineDTO oldObj = new PassportExamineDTO();BeanUtil.copyProperties(passportMapper.selectById(newObj.getId()), oldObj);//如果老状态是复核则页面应设置为复核页面if (ApplyStatusEnum.PENDING_REVIEW.getCode().equals(oldObj.getStatus())) {record.setOperatePage(PassportOperatePageConstant.HZBL_HZFH_ZHFX);}record.setOperateContent(this.compare(oldObj, newObj, newObj.getRequestNo(), prefix, content));//操作类型Integer status = newObj.getStatus();Integer problemStatus = newObj.getProblemStatus();if (ApplyStatusEnum.PENDING_REVIEW.getCode().equals(status)) {record.setOperateType(PassportOperateTypeConstant.TJFH);} else if (ApplyStatusEnum.RETURNED_ITEMS.getCode().equals(status)) {record.setOperateType(PassportOperateTypeConstant.TJ);} else if (ApplyStatusEnum.PENDING_CERTIFICATION.getCode().equals(status)) {record.setOperateType(PassportOperateTypeConstant.TG);}if (PassportProblemEnum.THXG.getCode().equals(problemStatus)) {record.setOperateType(PassportOperateTypeConstant.THXG);} else if (PassportProblemEnum.GQ.getCode().equals(problemStatus)) {record.setOperateType(PassportOperateTypeConstant.GQ);}passportOperateRecordService.save(record);}}/*** 比较两个对象,并且返回不一致的信息** @param newObj* @param oldObj* @param requestNo* @param prefix* @return*/private String compare(Object oldObj, Object newObj, String requestNo, String prefix, String str) throws IntrospectionException, InvocationTargetException, IllegalAccessException {str += prefix;//1:获取到对象的classClass<?> oldClass = oldObj.getClass();Class<?> newClass = newObj.getClass();//2:获取到对象的属性列表Field[] oldFields = oldClass.getDeclaredFields();Field[] newFields = newClass.getDeclaredFields();for (int i = 0; i < oldFields.length; i++) {if ("serialVersionUID".equals(oldFields[i].getName())) {continue;}//将此对象的 accessible 标志设置为指示的布尔值。// 值为 true 则指示反射的对象在使用时应该取消 Java 语言访问检查。// 值为 false 则指示反射的对象应该实施 Java 语言访问检查;// 实际上setAccessible是启用和禁用访问安全检查的开关,// 并不是为true就能访问为false就不能访问 ;oldFields[i].setAccessible(true);newFields[i].setAccessible(true);//获取到字段上的注解PassportField annotation = oldFields[i].getAnnotation(PassportField.class);//当没有注解的时候跳过本次循环if (annotation == null) {continue;}//获取注解中的字段名String name = annotation.name();ListTypEnum listTypEnum = annotation.listType();SysDictTypeEnum code = annotation.code();TypeEnum typeEnum = annotation.typeEnum();PropertyDescriptor oldPd = new PropertyDescriptor(oldFields[i].getName(), oldClass);PropertyDescriptor newPd = new PropertyDescriptor(newFields[i].getName(), newClass);//调用成员方法Method oldReadMethod = oldPd.getReadMethod();Method newReadMethod = newPd.getReadMethod();//3:获取到参数数值Object oldValue = oldReadMethod.invoke(oldObj);Object newValue = newReadMethod.invoke(newObj);if (StrUtil.isBlank(name)) {//处理listif (listTypEnum.equals(ListTypEnum.CARD_FILL)) {str = this.cardFill(newValue, requestNo, str);continue;} else if (listTypEnum.equals(ListTypEnum.EMERGENCY_CONTACT)) {str = this.emergencyContact(newValue, requestNo, str);continue;} else if (listTypEnum.equals(ListTypEnum.FAMILY_MEMBERS)) {str = this.familyMember(newValue, requestNo, str);continue;}} else {//处理系统字典if (ObjUtil.isNotNull(code) && !SysDictTypeEnum.NULL.equals(code)) {if (ObjUtil.isNotNull(oldValue)) {oldValue = systemDictService.queryDictName(code, oldValue.toString());}if (ObjUtil.isNotNull(newValue) && StrUtil.isNotBlank(newValue.toString())) {newValue = systemDictService.queryDictName(code, newValue.toString());}}if (ObjUtil.isNotNull(typeEnum) && !TypeEnum.NULL.equals(typeEnum)) {if (TypeEnum.STATUS.equals(typeEnum)) {if (ObjUtil.isNotNull(oldValue) && StrUtil.isNotBlank(oldValue.toString())) {oldValue = ApplyStatusEnum.getMessage(Integer.parseInt(oldValue.toString()));}if (ObjUtil.isNotNull(newValue) && StrUtil.isNotBlank(newValue.toString())) {newValue = ApplyStatusEnum.getMessage(Integer.parseInt(newValue.toString()));}} else if (TypeEnum.SF.equals(typeEnum)) {if (ObjUtil.isNotNull(oldValue) && StrUtil.isNotBlank(oldValue.toString())) {oldValue = PassportFlagEnum.getMessage(Integer.parseInt(oldValue.toString()));}if (ObjUtil.isNotNull(newValue) && StrUtil.isNotBlank(newValue.toString())) {newValue = PassportFlagEnum.getMessage(Integer.parseInt(newValue.toString()));}} else if (TypeEnum.BIRTHPLACE.equals(typeEnum)) {if (ObjUtil.isNotNull(oldValue) && StrUtil.isNotBlank(oldValue.toString())) {oldValue = systemDictService.birthdayPlace(oldValue.toString());}if (ObjUtil.isNotNull(newValue) && StrUtil.isNotBlank(newValue.toString())) {newValue = systemDictService.birthdayPlace(newValue.toString());}} else if (TypeEnum.CARD_VALID_UNIT.equals(typeEnum)) {if (ObjUtil.isNotNull(oldValue) && StrUtil.isNotBlank(oldValue.toString())) {oldValue = CardValidUnitEnum.getMessage(oldValue.toString());}if (ObjUtil.isNotNull(newValue) && StrUtil.isNotBlank(newValue.toString())) {newValue = CardValidUnitEnum.getMessage(newValue.toString());}} else if (TypeEnum.ISSUE_PLACE.equals(typeEnum)) {if (ObjUtil.isNotNull(oldValue) && StrUtil.isNotBlank(oldValue.toString())) {oldValue = systemDictService.issuePlace(oldValue.toString());}if (ObjUtil.isNotNull(newValue) && StrUtil.isNotBlank(newValue.toString())) {newValue = systemDictService.issuePlace(newValue.toString());}} else if (TypeEnum.ISSUE_CHECK.equals(typeEnum)) {if (ObjUtil.isNotNull(oldValue) && StrUtil.isNotBlank(oldValue.toString())) {oldValue = PassportDictIssueEnum.getDiceLabel(oldValue.toString());}if (ObjUtil.isNotNull(newValue) && StrUtil.isNotBlank(newValue.toString())) {newValue = PassportDictIssueEnum.getDiceLabel(newValue.toString());}} else if (TypeEnum.EVIDENCE_WAY.equals(typeEnum)) {if (ObjUtil.isNotNull(oldValue) && StrUtil.isNotBlank(oldValue.toString())) {oldValue = PassportEvidenceWayEnum.getMessage(Integer.parseInt(oldValue.toString()));}if (ObjUtil.isNotNull(newValue) && StrUtil.isNotBlank(newValue.toString())) {newValue = PassportEvidenceWayEnum.getMessage(Integer.parseInt(newValue.toString()));}} else if (TypeEnum.DECLARATION_RELATION.equals(typeEnum)) {if (ObjUtil.isNotNull(oldValue) && StrUtil.isNotBlank(oldValue.toString())) {oldValue = PassportDictRelationEnum.getDiceLabel(oldValue.toString());}if (ObjUtil.isNotNull(newValue) && StrUtil.isNotBlank(newValue.toString())) {newValue = PassportDictRelationEnum.getDiceLabel(newValue.toString());}} else if (TypeEnum.CHECK_OFFICE.equals(typeEnum)) {if (ObjUtil.isNotNull(oldValue) && StrUtil.isNotBlank(oldValue.toString())) {String[] split = oldValue.toString().split(",");StringBuilder oldValueContent = new StringBuilder();for (String s : split) {oldValueContent.append(systemDictService.queryDictName(SysDictTypeEnum.VERIFICATION_AND_SUBMISSION_AUTHORITY, s));}oldValue = oldValueContent.toString();}if (ObjUtil.isNotNull(newValue) && StrUtil.isNotBlank(newValue.toString())) {String[] split = newValue.toString().split(",");StringBuilder newValueContent = new StringBuilder();for (String s : split) {newValueContent.append(systemDictService.queryDictName(SysDictTypeEnum.VERIFICATION_AND_SUBMISSION_AUTHORITY, s));}newValue = newValueContent.toString();}} else if (TypeEnum.EMERGENCY_RELATION.equals(typeEnum)) {if (ObjUtil.isNotNull(oldValue) && StrUtil.isNotBlank(oldValue.toString())) {oldValue = PassportDictEmergencyContactEnum.getDiceLabel(oldValue.toString());}if (ObjUtil.isNotNull(newValue) && StrUtil.isNotBlank(newValue.toString())) {newValue = PassportDictEmergencyContactEnum.getDiceLabel(newValue.toString());}} else if (TypeEnum.FAMILY_RELATION.equals(typeEnum)) {if (ObjUtil.isNotNull(oldValue) && StrUtil.isNotBlank(oldValue.toString())) {oldValue = PassportDictFamilyEnum.getDiceLabel(oldValue.toString());}if (ObjUtil.isNotNull(newValue) && StrUtil.isNotBlank(newValue.toString())) {newValue = PassportDictFamilyEnum.getDiceLabel(newValue.toString());}}}//获取差异字段str = getDifferenceFieldStr(str, name, oldValue, newValue);}}return str;}/*** 获取差异字段新旧值** @param str* @param fieldName* @param oldValue* @param newValue* @return*/private static String getDifferenceFieldStr(String str, String fieldName, Object oldValue, Object newValue) {if (null == oldValue || StringUtils.isBlank(oldValue.toString())) {oldValue = "无";}if (null == newValue || StringUtils.isBlank(newValue.toString())) {newValue = "无";}if (!oldValue.equals(newValue)) {str += fieldName + "从 " + oldValue + ",修改为 " + newValue + SEPARATOR;}return str;}/*** 处理加注** @param newValue* @param requestNo* @throws IntrospectionException* @throws InvocationTargetException* @throws IllegalAccessException*/private String cardFill(Object newValue, String requestNo, String str) throws IntrospectionException, InvocationTargetException, IllegalAccessException {List<PassportCardFillDTO> newObjects = (List<PassportCardFillDTO>) newValue;List<PassportCardFillDTO> oldObjects = passportCardFillMapper.queryCardFillByRequestNo(requestNo);if (CollectionUtils.isEmpty(newObjects)) {if (!CollectionUtils.isEmpty(oldObjects)) {for (PassportCardFillDTO oldObject : oldObjects) {str = this.compare(oldObject, new PassportCardFillDTO(), requestNo, "删除加注:", str);}}} else {if (CollectionUtils.isEmpty(oldObjects)) {for (PassportCardFillDTO newObject : newObjects) {str = this.compare(new PassportCardFillDTO(), newObject, requestNo, "新增加注:", str);}} else {for (PassportCardFillDTO newObject : newObjects) {if (Objects.isNull(newObject.getId())) {str = this.compare(new PassportCardFillDTO(), newObject, requestNo, "新增加注:", str);} else {List<PassportCardFillDTO> removeFills = new ArrayList<>();for (PassportCardFillDTO oldObject : oldObjects) {if (oldObject.getId().equals(newObject.getId())) {if (!oldObject.equals(newObject)) {str = this.compare(oldObject, newObject, requestNo, "", str);}removeFills.add(oldObject);}}oldObjects.removeAll(removeFills);for (PassportCardFillDTO oldObject : oldObjects) {str = this.compare(oldObject, new PassportCardFillDTO(), requestNo, "删除加注:", str);}}}}}return str;}/*** 处理紧急联系人** @param newValue* @param requestNo* @throws IntrospectionException* @throws InvocationTargetException* @throws IllegalAccessException*/private String emergencyContact(Object newValue, String requestNo, String str) throws IntrospectionException, InvocationTargetException, IllegalAccessException {List<PassportEmergencyContactAddDTO> newObjects = (List<PassportEmergencyContactAddDTO>) newValue;List<PassportEmergencyContactAddDTO> oldObjects = passportEmergencyContactMapper.queryEmergencyContactByRequestNo(requestNo);if (CollectionUtils.isEmpty(newObjects)) {if (CollUtil.isNotEmpty(oldObjects)) {for (PassportEmergencyContactAddDTO oldObject : oldObjects) {str = this.compare(oldObject, new PassportEmergencyContactAddDTO(), requestNo, "删除紧急联系人:", str);}}} else {if (CollUtil.isEmpty(oldObjects)) {for (PassportEmergencyContactAddDTO newObject : newObjects) {str = this.compare(new PassportEmergencyContactAddDTO(), newObject, requestNo, "新增紧急联系人:", str);}} else {for (PassportEmergencyContactAddDTO newObject : newObjects) {if (ObjUtil.isNull(newObject.getId())) {str = this.compare(new PassportEmergencyContactAddDTO(), newObject, requestNo, "新增紧急联系人:", str);} else {for (PassportEmergencyContactAddDTO oldObject : oldObjects) {if (oldObject.getId().equals(newObject.getId())) {if (!oldObject.equals(newObject)) {str = this.compare(oldObject, newObject, requestNo, "", str);}oldObjects.remove(oldObject);}}if (CollUtil.isNotEmpty(oldObjects)) {for (PassportEmergencyContactAddDTO oldObject : oldObjects) {str = this.compare(oldObject, new PassportEmergencyContactAddDTO(), requestNo, "删除紧急联系人:", str);}}}}}}return str;}/*** 处理家庭成员** @param newValue* @param requestNo* @throws IntrospectionException* @throws InvocationTargetException* @throws IllegalAccessException*/private String familyMember(Object newValue, String requestNo, String str) throws IntrospectionException, InvocationTargetException, IllegalAccessException {List<PassportFamilyMembersAddDTO> newObjects = (List<PassportFamilyMembersAddDTO>) newValue;List<PassportFamilyMembersAddDTO> oldObjects = passportFamilyMembersMapper.queryFamilyMemberByRequest(requestNo);if (CollectionUtils.isEmpty(newObjects)) {if (ObjUtil.isNotEmpty(oldObjects)) {for (PassportFamilyMembersAddDTO oldObject : oldObjects) {str = this.compare(oldObject, new PassportFamilyMembersAddDTO(), requestNo, "删除家庭成员:", str);}}} else {if (CollUtil.isEmpty(oldObjects)) {for (PassportFamilyMembersAddDTO newObject : newObjects) {str = this.compare(new PassportFamilyMembersAddDTO(), newObject, requestNo, "新增家庭成员:", str);}} else {for (PassportFamilyMembersAddDTO newObject : newObjects) {if (ObjUtil.isNull(newObject.getId())) {str = this.compare(new PassportFamilyMembersAddDTO(), newObject, requestNo, "新增家庭成员:", str);} else {for (PassportFamilyMembersAddDTO oldObject : oldObjects) {if (oldObject.getId().equals(newObject.getId())) {if (!oldObject.equals(newObject)) {str = this.compare(oldObject, newObject, requestNo, "", str);}oldObjects.remove(oldObject);}}if (CollUtil.isNotEmpty(oldObjects)) {for (PassportFamilyMembersAddDTO oldObject : oldObjects) {str = this.compare(oldObject, new PassportFamilyMembersAddDTO(), requestNo, "删除家庭成员:", str);}}}}}}return str;}}