BaseEntityMetaObjectHandler.java 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. package com.github.jfcloud.gene.handler;
  2. import cn.hutool.core.util.StrUtil;
  3. import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
  4. import com.github.jfcloud.common.security.util.SecurityUtils;
  5. import com.github.jfcloud.gene.common.util.UserUtil;
  6. import org.apache.ibatis.reflection.MetaObject;
  7. import org.slf4j.Logger;
  8. import org.slf4j.LoggerFactory;
  9. import org.springframework.util.ClassUtils;
  10. import java.nio.charset.Charset;
  11. import java.util.Date;
  12. import java.util.HashMap;
  13. import java.util.Map;
  14. public class BaseEntityMetaObjectHandler implements MetaObjectHandler {
  15. private static final Logger log = LoggerFactory.getLogger(BaseEntityMetaObjectHandler.class);
  16. private static void fillValIfNullByName(String fieldName, Object fieldVal, MetaObject metaObject, boolean isCover) {
  17. if (fieldVal != null) {
  18. if (metaObject.hasSetter(fieldName)) {
  19. Object userSetValue = metaObject.getValue(fieldName);
  20. String setValueStr = StrUtil.str(userSetValue, Charset.defaultCharset());
  21. if (!StrUtil.isNotBlank(setValueStr) || isCover) {
  22. Class<?> getterType = metaObject.getGetterType(fieldName);
  23. if (ClassUtils.isAssignableValue(getterType, fieldVal)) {
  24. metaObject.setValue(fieldName, fieldVal);
  25. }
  26. }
  27. }
  28. }
  29. }
  30. public void insertFill(MetaObject metaObject) {
  31. log.debug("mybatis plus start insert fill ....");
  32. Map<String, Object> fieldsToFill = new HashMap<>();
  33. fieldsToFill.put("createTime", new Date());
  34. fieldsToFill.put("create_time", new Date());
  35. fieldsToFill.put("createBy", UserUtil.getUserName());
  36. fieldsToFill.put("delFlag", "0");
  37. fieldsToFill.put("deleted", "0");
  38. fieldsToFill.put("deptId", UserUtil.getDeptId());
  39. fieldsToFill.put("groupIndex", this.getGroupIndex());
  40. fieldsToFill.put("groupPi", this.getGroupPi());
  41. fieldsToFill.put("tenantId", UserUtil.getTenantId());
  42. this.fillFieldsIfExist(metaObject, fieldsToFill, false);
  43. }
  44. private void fillFieldsIfExist(MetaObject metaObject, Map<String, Object> fieldsToFill, boolean isCover) {
  45. for(Map.Entry<String, Object> entry : fieldsToFill.entrySet()) {
  46. String fieldName = entry.getKey();
  47. Object defaultValue = entry.getValue();
  48. if (metaObject.hasGetter(fieldName)) {
  49. fillValIfNullByName(fieldName, defaultValue, metaObject, isCover);
  50. }
  51. }
  52. }
  53. public void updateFill(MetaObject metaObject) {
  54. Map<String, Object> fieldsToFill = new HashMap<>();
  55. fieldsToFill.put("updateTime", new Date());
  56. fieldsToFill.put("update_time", new Date());
  57. fieldsToFill.put("updateBy", UserUtil.getUserName());
  58. this.fillFieldsIfExist(metaObject, fieldsToFill, false);
  59. }
  60. private Integer getGroupIndex() {
  61. return SecurityUtils.getGroupIndex();
  62. }
  63. private Long getGroupPi() {
  64. return SecurityUtils.getGroupPi();
  65. }
  66. }