xuezizhuo 1 éve
szülő
commit
f9a7114ded

+ 28 - 0
ruoyi-admin/src/main/java/com/ruoyi/web/controller/dragForm/CommonController.java

@@ -3,14 +3,21 @@ package com.ruoyi.web.controller.dragForm;
 import com.ruoyi.common.annotation.Anonymous;
 import com.ruoyi.common.core.controller.BaseController;
 import com.ruoyi.common.core.domain.AjaxResult;
+import com.ruoyi.common.core.domain.entity.SysUser;
+import com.ruoyi.common.core.domain.model.LoginUser;
 import com.ruoyi.common.core.page.TableDataInfo;
+import com.ruoyi.common.utils.SecurityUtils;
 import com.ruoyi.system.entity.CommonEntity;
 import com.ruoyi.system.entity.TableSql;
 import com.ruoyi.system.service.ICommonService;
 import com.ruoyi.system.service.ITableSqlService;
+import dm.jdbc.filter.stat.util.JSONUtils;
 import org.springframework.web.bind.annotation.*;
 
 import javax.annotation.Resource;
+import javax.servlet.http.HttpServletResponse;
+import java.util.HashMap;
+import java.util.List;
 import java.util.Map;
 
 @Anonymous
@@ -33,6 +40,21 @@ public class CommonController extends BaseController {
         return getDataTable(commonService.selectList(commonEntity.getFormMap()));
     }
 
+
+    @GetMapping("/test")
+    public List<CommonEntity> test(@RequestParam String tableName) {
+        Map<String, Object> map = new HashMap<>();
+        map.put("tableName", tableName);
+        //  df_name
+        List<CommonEntity> commonEntities = commonService.selectList(map);
+        commonEntities.forEach(item->{
+            System.out.println(item.getRetMap());
+        });
+
+
+        return commonEntities;
+    }
+
     /**
      * 批量新增
      */
@@ -64,4 +86,10 @@ public class CommonController extends BaseController {
         return getDataTable(commonService.queryTableList(commonEntity, tableSql));
     }
 
+    @GetMapping("/export")
+    public void export(HttpServletResponse response) throws Exception {
+
+
+    }
+
 }

+ 113 - 0
ruoyi-common/src/main/java/com/ruoyi/common/core/domain/entity/DataSource.java

@@ -0,0 +1,113 @@
+package com.ruoyi.common.core.domain.entity;
+
+import java.io.Serializable;
+
+
+public class DataSource implements Serializable {
+
+    private static final long serialVersionUID = 1L;
+
+    /**
+     * 字典编码
+     */
+    private Long id;
+
+    /**
+     * 数据库名
+     */
+    private String databaseName;
+
+    /**
+     * 数据库IP
+     */
+    private String databaseIp;
+
+    /**
+     * 用户名
+     */
+    private String username;
+
+    /**
+     * 密码
+     */
+    private String password;
+
+    /**
+     * 端口号
+     */
+    private Long portNumber;
+
+    /**
+     * 数据库类型
+     */
+    private String databaseType;
+
+    public Long getId() {
+        return id;
+    }
+
+    public void setId(Long id) {
+        this.id = id;
+    }
+
+    public String getDatabaseName() {
+        return databaseName;
+    }
+
+    public void setDatabaseName(String databaseName) {
+        this.databaseName = databaseName;
+    }
+
+    public String getDatabaseIp() {
+        return databaseIp;
+    }
+
+    public void setDatabaseIp(String databaseIp) {
+        this.databaseIp = databaseIp;
+    }
+
+    public String getUsername() {
+        return username;
+    }
+
+    public void setUsername(String username) {
+        this.username = username;
+    }
+
+    public String getPassword() {
+        return password;
+    }
+
+    public void setPassword(String password) {
+        this.password = password;
+    }
+
+    public Long getPortNumber() {
+        return portNumber;
+    }
+
+    public void setPortNumber(Long portNumber) {
+        this.portNumber = portNumber;
+    }
+
+    public String getDatabaseType() {
+        return databaseType;
+    }
+
+    public void setDatabaseType(String databaseType) {
+        this.databaseType = databaseType;
+    }
+
+    @Override
+    public String toString() {
+        return "DataSource{" +
+                "id=" + id +
+                ", databaseName='" + databaseName + '\'' +
+                ", databaseIp='" + databaseIp + '\'' +
+                ", username='" + username + '\'' +
+                ", password='" + password + '\'' +
+                ", portNumber=" + portNumber +
+                ", databaseType='" + databaseType + '\'' +
+                '}';
+    }
+}

+ 149 - 0
ruoyi-common/src/main/java/com/ruoyi/common/core/domain/entity/SysTenant.java

@@ -0,0 +1,149 @@
+package com.ruoyi.common.core.domain.entity;
+
+import com.ruoyi.common.annotation.Excel;
+import com.ruoyi.common.core.domain.BaseEntity;
+import org.apache.commons.lang3.builder.ToStringBuilder;
+import org.apache.commons.lang3.builder.ToStringStyle;
+
+import javax.validation.constraints.NotBlank;
+import javax.validation.constraints.Size;
+
+/**
+ * 部门表 sys_tenant
+ * 
+ * @author ruoyi
+ */
+public class SysTenant extends BaseEntity
+{
+    private static final long serialVersionUID = 1L;
+
+    /** 租户ID */
+    private Long tenantId;
+
+    /** 租户名称 */
+    @Excel(name = "租户名称")
+    private String tenantName;
+
+    /** 租户编号(公司统一信用代码) */
+    @Excel(name = "租户编号", readConverterExp = "公司统一信用代码")
+    private String tenantCode;
+
+    /** 负责人 */
+    @Excel(name = "负责人")
+    private String owner;
+
+    /** 联系方式 */
+    @Excel(name = "联系方式")
+    private String contactInfo;
+
+    /** 地址 */
+    @Excel(name = "地址")
+    private String address;
+
+    /** 删除标志(0代表存在 1代表删除) */
+    @Excel(name = "是否删除(0:未删除,1已删除)")
+    private String isDel;
+
+    /** 数据源id */
+    private Long datasourceId;
+
+    private DataSource dataSource;
+
+    public Long getTenantId() {
+        return tenantId;
+    }
+
+    public void setTenantId(Long tenantId) {
+        this.tenantId = tenantId;
+    }
+
+    @NotBlank(message = "租户名称不能为空")
+    @Size(min = 0, max = 100, message = "租户名称长度不能超过100个字符")
+    public String getTenantName() {
+        return tenantName;
+    }
+    public void setTenantName(String tenantName) {
+        this.tenantName = tenantName;
+    }
+
+    @NotBlank(message = "租户编号不能为空")
+    @Size(min = 18, max = 18, message = "租户编号18个字符(公司统一信用代码)")
+    public String getTenantCode() {
+        return tenantCode;
+    }
+
+    public void setTenantCode(String tenantCode) {
+        this.tenantCode = tenantCode;
+    }
+
+    public String getOwner() {
+        return owner;
+    }
+
+    public void setOwner(String owner) {
+        this.owner = owner;
+    }
+
+    public String getContactInfo() {
+        return contactInfo;
+    }
+
+    public void setContactInfo(String contactInfo) {
+        this.contactInfo = contactInfo;
+    }
+
+    public String getAddress() {
+        return address;
+    }
+
+    public void setAddress(String address) {
+        this.address = address;
+    }
+
+    public String getIsDel() {
+        return isDel;
+    }
+
+    public void setIsDel(String isDel) {
+        this.isDel = isDel;
+    }
+
+    public Long getDatasourceId() {
+        return datasourceId;
+    }
+
+    public void setDatasourceId(Long datasourceId) {
+        this.datasourceId = datasourceId;
+    }
+
+    public DataSource getDataSource() {
+        return dataSource;
+    }
+
+    public void setDataSource(DataSource dataSource) {
+        this.dataSource = dataSource;
+    }
+
+    @Override
+    public String toString() {
+        return new ToStringBuilder(this,ToStringStyle.MULTI_LINE_STYLE)
+            .append("tenantId", getTenantId())
+            .append("tenantName", getTenantName())
+            .append("tenantCode", getTenantCode())
+            .append("owner", getOwner())
+            .append("contactInfo", getContactInfo())
+            .append("address", getAddress())
+            .append("isDel", getIsDel())
+            .append("createBy", getCreateBy())
+            .append("createTime", getCreateTime())
+            .append("updateBy", getUpdateBy())
+            .append("updateTime", getUpdateTime())
+            .append("datasourceId",getTenantId())
+            .append("dataSource",getDataSource())
+            .toString();
+    }
+
+
+
+
+}

+ 71 - 23
ruoyi-common/src/main/java/com/ruoyi/common/core/domain/entity/SysUser.java

@@ -14,7 +14,7 @@ import com.ruoyi.common.xss.Xss;
 
 /**
  * 用户对象 sys_user
- * 
+ *
  * @author ruoyi
  */
 public class SysUser extends BaseEntity
@@ -29,6 +29,9 @@ public class SysUser extends BaseEntity
     @Excel(name = "部门编号", type = Type.IMPORT)
     private Long deptId;
 
+    /** 租户ID */
+    private Long tenantId;
+
     /** 用户账号 */
     @Excel(name = "登录名称")
     private String userName;
@@ -37,6 +40,9 @@ public class SysUser extends BaseEntity
     @Excel(name = "用户名称")
     private String nickName;
 
+    /**用户类型 (00系统用户 01 租户管理员)*/
+    private String userType;
+
     /** 用户邮箱 */
     @Excel(name = "用户邮箱")
     private String email;
@@ -72,11 +78,14 @@ public class SysUser extends BaseEntity
 
     /** 部门对象 */
     @Excels({
-        @Excel(name = "部门名称", targetAttr = "deptName", type = Type.EXPORT),
-        @Excel(name = "部门负责人", targetAttr = "leader", type = Type.EXPORT)
+            @Excel(name = "部门名称", targetAttr = "deptName", type = Type.EXPORT),
+            @Excel(name = "部门负责人", targetAttr = "leader", type = Type.EXPORT)
     })
     private SysDept dept;
 
+
+    private SysTenant tenant;
+
     /** 角色对象 */
     private List<SysRole> roles;
 
@@ -89,6 +98,9 @@ public class SysUser extends BaseEntity
     /** 角色ID */
     private Long roleId;
 
+    /** 租户名称 */
+    private String tenantName;
+
     public SysUser()
     {
 
@@ -119,6 +131,10 @@ public class SysUser extends BaseEntity
         return userId != null && 1L == userId;
     }
 
+    public boolean isTenantAdmin() {
+        return "01".equals(this.userType) && this.tenantId != null;
+    }
+
     public Long getDeptId()
     {
         return deptId;
@@ -129,6 +145,13 @@ public class SysUser extends BaseEntity
         this.deptId = deptId;
     }
 
+
+    public Long getTenantId() { return tenantId; }
+
+    public void setTenantId(Long tenantId) {
+        this.tenantId = tenantId;
+    }
+
     @Xss(message = "用户昵称不能包含脚本字符")
     @Size(min = 0, max = 30, message = "用户昵称长度不能超过30个字符")
     public String getNickName()
@@ -154,6 +177,14 @@ public class SysUser extends BaseEntity
         this.userName = userName;
     }
 
+    public String getUserType() {
+        return userType;
+    }
+
+    public void setUserType(String userType) {
+        this.userType = userType;
+    }
+
     @Email(message = "邮箱格式不正确")
     @Size(min = 0, max = 50, message = "邮箱长度不能超过50个字符")
     public String getEmail()
@@ -257,6 +288,12 @@ public class SysUser extends BaseEntity
         this.dept = dept;
     }
 
+    public SysTenant getTenant() {
+        return tenant;
+    }
+
+    public void setTenant(SysTenant tenant) { this.tenant = tenant; }
+
     public List<SysRole> getRoles()
     {
         return roles;
@@ -297,28 +334,39 @@ public class SysUser extends BaseEntity
         this.roleId = roleId;
     }
 
+    public String getTenantName() {
+        return tenantName;
+    }
+
+    public void setTenantName(String tenantName) {
+        this.tenantName = tenantName;
+    }
+
     @Override
     public String toString() {
         return new ToStringBuilder(this,ToStringStyle.MULTI_LINE_STYLE)
-            .append("userId", getUserId())
-            .append("deptId", getDeptId())
-            .append("userName", getUserName())
-            .append("nickName", getNickName())
-            .append("email", getEmail())
-            .append("phonenumber", getPhonenumber())
-            .append("sex", getSex())
-            .append("avatar", getAvatar())
-            .append("password", getPassword())
-            .append("status", getStatus())
-            .append("delFlag", getDelFlag())
-            .append("loginIp", getLoginIp())
-            .append("loginDate", getLoginDate())
-            .append("createBy", getCreateBy())
-            .append("createTime", getCreateTime())
-            .append("updateBy", getUpdateBy())
-            .append("updateTime", getUpdateTime())
-            .append("remark", getRemark())
-            .append("dept", getDept())
-            .toString();
+                .append("userId", getUserId())
+                .append("deptId", getDeptId())
+                .append("tenantId", getTenantId())
+                .append("userName", getUserName())
+                .append("nickName", getNickName())
+                .append("email", getEmail())
+                .append("phonenumber", getPhonenumber())
+                .append("sex", getSex())
+                .append("avatar", getAvatar())
+                .append("password", getPassword())
+                .append("status", getStatus())
+                .append("delFlag", getDelFlag())
+                .append("loginIp", getLoginIp())
+                .append("loginDate", getLoginDate())
+                .append("createBy", getCreateBy())
+                .append("createTime", getCreateTime())
+                .append("updateBy", getUpdateBy())
+                .append("updateTime", getUpdateTime())
+                .append("remark", getRemark())
+                .append("dept", getDept())
+                .append("tenant", getTenant())
+                .append("tenantName",getTenantName())
+                .toString();
     }
 }

+ 14 - 0
ruoyi-common/src/main/java/com/ruoyi/common/utils/SecurityUtils.java

@@ -59,6 +59,20 @@ public class SecurityUtils
         }
     }
 
+    /**
+     * 获取数据源类型
+     */
+    public static String getDatabaseType() {
+        try
+        {
+            return getLoginUser().getUser().getTenant().getDataSource().getDatabaseType();
+        }
+        catch (Exception e)
+        {
+            throw new ServiceException("获取获取数据源类型异常", HttpStatus.UNAUTHORIZED);
+        }
+    }
+
     /**
      * 获取用户
      **/

+ 13 - 0
ruoyi-system/src/main/java/com/ruoyi/system/entity/TableSql.java

@@ -35,6 +35,10 @@ public class TableSql extends BaseEntity
     @Excel(name = "sql编号,绑定tableList")
     private String sqlKey;
 
+    /** 导出的字段名以及列名 */
+    @Excel(name = "导出的字段名以及列名")
+    private String tableExportField;
+
     /** 逻辑删除 */
     private String delFlag;
 
@@ -93,6 +97,14 @@ public class TableSql extends BaseEntity
         return delFlag;
     }
 
+    public String getTableExportField() {
+        return tableExportField;
+    }
+
+    public void setTableExportField(String tableExportField) {
+        this.tableExportField = tableExportField;
+    }
+
     @Override
     public String toString() {
         return new ToStringBuilder(this,ToStringStyle.MULTI_LINE_STYLE)
@@ -106,6 +118,7 @@ public class TableSql extends BaseEntity
             .append("updateBy", getUpdateBy())
             .append("updateTime", getUpdateTime())
             .append("delFlag", getDelFlag())
+            .append("tableExportField",getTableExportField())
             .toString();
     }
 }

+ 6 - 0
ruoyi-system/src/main/java/com/ruoyi/system/service/ICommonService.java

@@ -3,6 +3,7 @@ package com.ruoyi.system.service;
 import com.ruoyi.system.entity.CommonEntity;
 import com.ruoyi.system.entity.TableSql;
 
+import javax.servlet.http.HttpServletResponse;
 import java.util.List;
 import java.util.Map;
 
@@ -34,4 +35,9 @@ public interface ICommonService {
      */
     List<CommonEntity> queryTableList(CommonEntity commonEntity, TableSql tableSql);
 
+    /**
+     * 导出列表
+     */
+    void export(HttpServletResponse response,String tableName,String sqlKey) throws Exception;
+
 }

+ 55 - 0
ruoyi-system/src/main/java/com/ruoyi/system/service/impl/CommonServiceImpl.java

@@ -2,13 +2,21 @@ package com.ruoyi.system.service.impl;
 
 import com.alibaba.fastjson2.JSON;
 import com.alibaba.fastjson2.JSONObject;
+import com.ruoyi.common.core.domain.model.LoginUser;
+import com.ruoyi.common.utils.SecurityUtils;
 import com.ruoyi.system.entity.CommonEntity;
 import com.ruoyi.system.entity.TableSql;
 import com.ruoyi.system.mapper.CommonMapper;
+import com.ruoyi.system.mapper.TableSqlMapper;
 import com.ruoyi.system.service.ICommonService;
+import org.apache.poi.xssf.usermodel.XSSFRow;
+import org.apache.poi.xssf.usermodel.XSSFSheet;
+import org.apache.poi.xssf.usermodel.XSSFWorkbook;
 import org.springframework.stereotype.Service;
 
 import javax.annotation.Resource;
+import javax.servlet.ServletOutputStream;
+import javax.servlet.http.HttpServletResponse;
 import java.util.*;
 
 import static com.ruoyi.common.utils.StringUtils.convertToCamelCase;
@@ -21,6 +29,8 @@ public class CommonServiceImpl implements ICommonService {
     @Resource
     private CommonMapper commonMapper;
 
+    @Resource
+    private TableSqlMapper tableSqlMapper;
 
     @Override
     public List<CommonEntity> selectList(Map<String, Object> map) {
@@ -92,6 +102,51 @@ public class CommonServiceImpl implements ICommonService {
         return commonMapper.queryTableList(endSQL.toString());
     }
 
+    @Override
+    public void export(HttpServletResponse response, String tableName,String sqlKey) throws Exception{
+        //查询标题信息
+        TableSql tableSql = tableSqlMapper.selectTableSqlByTSqlKey(sqlKey);
+        //查询列表信息
+        List<CommonEntity> commonEntities = commonMapper.selectList(tableName, new HashMap<>());
+
+        //获取列表标题和字段
+        Map<String,String> map = (Map)JSONObject.parseObject(tableSql.getTableExportField());
+        // 创建Excel文档
+        XSSFWorkbook workbook = new XSSFWorkbook();
+        XSSFSheet sheet = workbook.createSheet("Sheet1");
+        // 创建表头
+        XSSFRow header = sheet.createRow(0);
+        int i = 0;
+        for (Map.Entry<String,String> entry : map.entrySet()){
+            header.createCell(i).setCellValue(entry.getValue());
+            i++;
+        }
+
+//        header.createCell(0).setCellValue("姓名");
+//        header.createCell(1).setCellValue("年龄");
+//        header.createCell(2).setCellValue("性别");
+        // 填充数据
+//        List<User> users = getUserList();
+        int rowIndex = 1;
+//        for (User user : users) {
+        XSSFRow row = sheet.createRow(rowIndex++);
+        row.createCell(0).setCellValue("user.getName()");
+        row.createCell(1).setCellValue("user.getAge()");
+        row.createCell(2).setCellValue("user.getGender()");
+//        }
+
+        // 设置响应头信息
+        response.setContentType("application/vnd.ms-excel");
+        response.setHeader("Content-Disposition", "attachment; filename=users.xlsx");
+
+        // 将Excel文档写入响应流中
+        ServletOutputStream outputStream = response.getOutputStream();
+        workbook.write(outputStream);
+        outputStream.flush();
+        outputStream.close();
+
+    }
+
 
     public static String extractSubstring(String input, String identifier) {
         int startIndex = input.indexOf(identifier);

+ 2 - 2
ruoyi-system/src/main/resources/mapper/common/CommonMapper.xml

@@ -5,8 +5,8 @@
 <mapper namespace="com.ruoyi.system.mapper.CommonMapper">
 
 
-    <resultMap id="retMap" type="map" autoMapping="true">
-        <collection property="retMap" ofType="com.ruoyi.system.entity.CommonEntity" autoMapping="true"
+    <resultMap id="retMap" type="com.ruoyi.system.entity.CommonEntity" autoMapping="true">
+        <collection property="retMap" ofType="map" autoMapping="true"
                     javaType="map"></collection>
     </resultMap>
 

+ 5 - 1
ruoyi-system/src/main/resources/mapper/common/TableSqlMapper.xml

@@ -10,6 +10,7 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
         <result property="tableCondition"    column="table_condition"    />
         <result property="tableAlias"    column="table_alias"    />
         <result property="sqlKey"    column="sql_key"    />
+        <result property="tableExportField" column="table_export_field"/>
         <result property="createBy"    column="create_by"    />
         <result property="createTime"    column="create_time"    />
         <result property="updateBy"    column="update_by"    />
@@ -18,7 +19,7 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
     </resultMap>
 
     <sql id="selectTableSqlVo">
-        select t_id, table_sql, table_condition, table_alias, sql_key, create_by, create_time, update_by, update_time, del_flag from table_sql
+        select t_id, table_sql, table_condition, table_alias, sql_key, table_export_field, create_by, create_time, update_by, update_time, del_flag from table_sql
     </sql>
 
     <select id="selectTableSqlList" parameterType="com.ruoyi.system.entity.TableSql" resultMap="TableSqlResult">
@@ -51,6 +52,7 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
             <if test="tableCondition != null">table_condition,</if>
             <if test="tableAlias != null">table_alias,</if>
             <if test="sqlKey != null">sql_key,</if>
+            <if test="tableExportField != null">table_export_field,</if>
             <if test="createBy != null">create_by,</if>
             <if test="createTime != null">create_time,</if>
             <if test="updateBy != null">update_by,</if>
@@ -62,6 +64,7 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
             <if test="tableCondition != null">#{tableCondition},</if>
             <if test="tableAlias != null">#{tableAlias},</if>
             <if test="sqlKey != null">#{sqlKey},</if>
+            <if test="tableExportField != null">#{tableExportField},</if>
             <if test="createBy != null">#{createBy},</if>
             <if test="createTime != null">#{createTime},</if>
             <if test="updateBy != null">#{updateBy},</if>
@@ -77,6 +80,7 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
             <if test="tableCondition != null">table_condition = #{tableCondition},</if>
             <if test="tableAlias != null">table_alias = #{tableAlias},</if>
             <if test="sqlKey != null">sql_key = #{sqlKey},</if>
+            <if test="tableExportField != null">table_export_field = #{tableExportField},</if>
             <if test="createBy != null">create_by = #{createBy},</if>
             <if test="createTime != null">create_time = #{createTime},</if>
             <if test="updateBy != null">update_by = #{updateBy},</if>