xujunwei 4 лет назад
Родитель
Сommit
2c1d0f0f8f

+ 22 - 0
framework-boot/src/main/java/com/mrxu/framework/boot/entity/BaseEntity.java

@@ -1,11 +1,13 @@
 package com.mrxu.framework.boot.entity;
 
 import com.baomidou.mybatisplus.annotation.IdType;
+import com.baomidou.mybatisplus.annotation.TableField;
 import com.baomidou.mybatisplus.annotation.TableId;
 import com.fasterxml.jackson.annotation.JsonFormat;
 import io.swagger.annotations.ApiModelProperty;
 import lombok.Data;
 
+import javax.validation.constraints.Size;
 import java.io.Serializable;
 import java.util.Date;
 
@@ -18,11 +20,31 @@ public class BaseEntity implements Serializable {
     @ApiModelProperty(value = "id")
     private Integer id;
 
+    @ApiModelProperty(value = "租户id")
+    @TableField(exist=false) //需要的话,子类覆盖
+    private String tenantId;
+
     @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
     @ApiModelProperty(value = "创建时间")
+    @TableField("create_time")
     private Date createTime;
 
     @ApiModelProperty(value = "创建人")
+    @TableField("create_person")
     private String createPerson;
 
+    @ApiModelProperty(value = "状态")
+    @TableField(exist=false) //需要的话,子类覆盖
+    private Integer status;
+
+    @ApiModelProperty(value = "更新时间")
+    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
+    @TableField(exist=false) //需要的话,子类覆盖
+    private Date updateTime;
+
+    @ApiModelProperty(value = "更新人")
+    @Size(max=64)
+    @TableField(exist=false) //需要的话,子类覆盖
+    private String updatePerson;
+
 }

+ 12 - 0
framework-boot/src/main/java/com/mrxu/framework/boot/entity/PageParam.java

@@ -4,9 +4,13 @@ import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
 import com.mrxu.framework.boot.MrxuConst;
 import com.mrxu.framework.common.util.MrxuAssert;
 import com.mrxu.framework.common.util.StrFunc;
+import io.swagger.annotations.ApiModelProperty;
 
 public class PageParam<T> extends Page<T> {
 
+    @ApiModelProperty(value = "租户id")
+    private String tenantId;
+
     //重写父类方法,加数据验证
     @Override
     public Page<T> setSize(long size) {
@@ -55,4 +59,12 @@ public class PageParam<T> extends Page<T> {
         }
     }
 
+    public String getTenantId() {
+        return tenantId;
+    }
+
+    public void setTenantId(String tenantId) {
+        this.tenantId = tenantId;
+    }
+
 }

+ 74 - 0
framework-boot/src/main/java/com/mrxu/framework/boot/web/BaseService.java

@@ -0,0 +1,74 @@
+package com.mrxu.framework.boot.web;
+
+import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
+import com.baomidou.mybatisplus.core.mapper.BaseMapper;
+import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
+import com.mrxu.framework.boot.entity.BaseEntity;
+
+import java.util.Date;
+import java.util.List;
+
+public class BaseService<M extends BaseMapper<T>, T extends BaseEntity> extends ServiceImpl<M,T> {
+
+    // 不用父类 saveOrUpdate 业务需要加上tenantId更新
+    public boolean saveOrUpdate(String tenantId, T bean,String username) {
+        bean.setTenantId(tenantId);
+        if(bean.getId() == null) {
+            if(username != null) {
+                bean.setCreatePerson(username);
+                bean.setCreateTime(new Date());
+            }
+            return save(bean);
+        }
+        else {
+            if(username != null) {
+                bean.setUpdatePerson(username);
+                bean.setUpdateTime(new Date());
+            }
+            QueryWrapper qw = new QueryWrapper<BaseEntity>();
+            qw.eq("id",bean.getId());
+            if(bean.getTenantId() != null) {
+                qw.eq("tenant_id",bean.getTenantId());
+            }
+            return baseMapper.update(bean,qw) > 0;
+        }
+    }
+
+    public T getById(String tenantId, Integer id) {
+        QueryWrapper<T> qw = new QueryWrapper<T>();
+        qw.eq("id",id);
+        if(tenantId != null) {
+            qw.eq("tenant_id",tenantId);
+        }
+        return baseMapper.selectOne(qw);
+    }
+
+    public T getByKey(String tenantId,String key,Object value) {
+        QueryWrapper<T> qw = new QueryWrapper<T>();
+        qw.eq(key,value);
+        if(tenantId != null) {
+            qw.eq("tenant_id",tenantId);
+        }
+        return baseMapper.selectOne(qw);
+    }
+
+    public boolean remove(String tenantId,Integer id) {
+        QueryWrapper qw = new QueryWrapper<T>();
+        qw.eq("id",id);
+        if(tenantId != null) {
+            qw.eq("tenant_id",tenantId);
+        }
+        return baseMapper.delete(qw) > 0;
+    }
+
+    public boolean removeBatch(String tenantId,List<Integer> ids) {
+        QueryWrapper qw = new QueryWrapper<BaseEntity>();
+        qw.in("id",ids);
+        if(tenantId != null) {
+            qw.eq("tenant_id",tenantId);
+        }
+        return baseMapper.delete(qw) == ids.size();
+    }
+
+
+}