注册功能,图表上传压缩
This commit is contained in:
parent
72aa08a387
commit
876e98e765
@ -17,7 +17,7 @@ create table if not exists user
|
||||
deleted_flag tinyint default 0 not null comment '是否删除',
|
||||
user_account varchar(256) not null comment '账号',
|
||||
user_password varchar(512) not null comment '密码',
|
||||
username varchar(256) null comment '用户昵称',
|
||||
user_name varchar(256) null comment '用户昵称',
|
||||
user_avatar varchar(1024) null comment '用户头像',
|
||||
user_role varchar(256) default 'user' not null comment '用户角色:user/admin/ban',
|
||||
index idx_userAccount (user_account)
|
||||
@ -31,6 +31,7 @@ create table if not exists chart
|
||||
updated_time datetime null on update CURRENT_TIMESTAMP comment '更新时间',
|
||||
deleted_flag tinyint default 0 not null comment '是否删除',
|
||||
user_id bigint null comment '创建用户 id',
|
||||
chart_name varchar(128) null comment '图表名称',
|
||||
goal text null comment '分析目标',
|
||||
chart_data text null comment '图表数据',
|
||||
chart_type varchar(128) null comment '图表类型',
|
||||
|
||||
@ -2,11 +2,18 @@ package top.peng.answerbi.controller;
|
||||
|
||||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||
import com.google.gson.Gson;
|
||||
import java.io.File;
|
||||
import org.apache.commons.lang3.RandomStringUtils;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.apache.poi.util.StringUtil;
|
||||
import org.springframework.web.bind.annotation.RequestPart;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
import top.peng.answerbi.annotation.AuthCheck;
|
||||
import top.peng.answerbi.common.CommonResponse;
|
||||
import top.peng.answerbi.common.DeleteRequest;
|
||||
import top.peng.answerbi.common.ErrorCode;
|
||||
import top.peng.answerbi.common.ResultUtils;
|
||||
import top.peng.answerbi.constant.FileConstant;
|
||||
import top.peng.answerbi.constant.UserConstant;
|
||||
import top.peng.answerbi.exception.BusinessException;
|
||||
import top.peng.answerbi.exception.ThrowUtils;
|
||||
@ -14,8 +21,11 @@ import top.peng.answerbi.model.dto.chart.ChartAddRequest;
|
||||
import top.peng.answerbi.model.dto.chart.ChartEditRequest;
|
||||
import top.peng.answerbi.model.dto.chart.ChartQueryRequest;
|
||||
import top.peng.answerbi.model.dto.chart.ChartUpdateRequest;
|
||||
import top.peng.answerbi.model.dto.chart.GenChartByAiRequest;
|
||||
import top.peng.answerbi.model.dto.file.UploadFileRequest;
|
||||
import top.peng.answerbi.model.entity.Chart;
|
||||
import top.peng.answerbi.model.entity.User;
|
||||
import top.peng.answerbi.model.enums.FileUploadBizEnum;
|
||||
import top.peng.answerbi.service.ChartService;
|
||||
import top.peng.answerbi.service.UserService;
|
||||
import javax.annotation.Resource;
|
||||
@ -27,6 +37,7 @@ import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
import top.peng.answerbi.utils.ExcelUtils;
|
||||
|
||||
/**
|
||||
* 图表接口
|
||||
@ -208,4 +219,57 @@ public class ChartController {
|
||||
return ResultUtils.success(result);
|
||||
}
|
||||
|
||||
/**
|
||||
* 智能分析
|
||||
*
|
||||
* @param multipartFile
|
||||
* @param genChartByAiRequest
|
||||
* @param request
|
||||
* @return
|
||||
*/
|
||||
@PostMapping("/gen")
|
||||
public CommonResponse<String> genChartByAi(@RequestPart("file") MultipartFile multipartFile,
|
||||
GenChartByAiRequest genChartByAiRequest, HttpServletRequest request) {
|
||||
String chartName = genChartByAiRequest.getChartName();
|
||||
String goal = genChartByAiRequest.getGoal();
|
||||
String chartType = genChartByAiRequest.getChartType();
|
||||
|
||||
//校验
|
||||
//如果分析目标为空,就抛出请求参数错误异常,并给出提示
|
||||
ThrowUtils.throwIf(StringUtils.isBlank(goal),ErrorCode.PARAMS_ERROR,"分析目标为空");
|
||||
//如果名称不为空,并且名称长度大于100,就抛出异常,并给出提示
|
||||
ThrowUtils.throwIf(StringUtils.isNotBlank(chartName) && chartName.length() > 100,ErrorCode.PARAMS_ERROR,"图表名称过长");
|
||||
|
||||
//用户输入
|
||||
StringBuilder userInput = new StringBuilder();
|
||||
userInput.append("你是一个数据分析师,接下来我会给你我的分析目标和原始数据,请告诉我分析结论。").append("\n");
|
||||
userInput.append("分析目标:").append(goal).append("\n");
|
||||
|
||||
//压缩后的数据
|
||||
String result = ExcelUtils.excelToCsv(multipartFile);
|
||||
userInput.append("数据:").append(result).append("\n");
|
||||
return ResultUtils.success(userInput.toString());
|
||||
/*//读取用户上传的excel文件,进行处理
|
||||
User loginUser = userService.getLoginUser(request);
|
||||
// 文件目录:根据业务、用户来划分
|
||||
String uuid = RandomStringUtils.randomAlphanumeric(8);
|
||||
String filename = uuid + "-" + multipartFile.getOriginalFilename();
|
||||
File file = null;
|
||||
try {
|
||||
// 返回可访问地址
|
||||
return ResultUtils.success("");
|
||||
}catch (Exception e){
|
||||
//log.error("file upload error, filepath = " + filepath, e);
|
||||
throw new BusinessException(ErrorCode.SYSTEM_ERROR, "上传失败");
|
||||
}finally {
|
||||
if (file != null) {
|
||||
// 删除临时文件
|
||||
boolean delete = file.delete();
|
||||
if (!delete) {
|
||||
//log.error("file delete error, filepath = {}", filepath);
|
||||
}
|
||||
}
|
||||
}*/
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -17,6 +17,12 @@ import lombok.Data;
|
||||
*/
|
||||
@Data
|
||||
public class ChartAddRequest implements Serializable {
|
||||
|
||||
/**
|
||||
* 图表名称
|
||||
*/
|
||||
private String chartName;
|
||||
|
||||
/**
|
||||
* 分析目标
|
||||
*/
|
||||
|
||||
@ -18,6 +18,11 @@ public class ChartEditRequest implements Serializable {
|
||||
*/
|
||||
private Long id;
|
||||
|
||||
/**
|
||||
* 图表名称
|
||||
*/
|
||||
private String chartName;
|
||||
|
||||
/**
|
||||
* 分析目标
|
||||
*/
|
||||
|
||||
@ -21,6 +21,11 @@ public class ChartQueryRequest extends PageRequest implements Serializable {
|
||||
*/
|
||||
private Long id;
|
||||
|
||||
/**
|
||||
* 图表名称
|
||||
*/
|
||||
private String chartName;
|
||||
|
||||
/**
|
||||
* 分析目标
|
||||
*/
|
||||
|
||||
@ -40,6 +40,10 @@ public class ChartUpdateRequest implements Serializable {
|
||||
@TableLogic
|
||||
private Integer deletedFlag;
|
||||
|
||||
/**
|
||||
* 图表名称
|
||||
*/
|
||||
private String chartName;
|
||||
|
||||
/**
|
||||
* 分析目标
|
||||
|
||||
@ -0,0 +1,31 @@
|
||||
package top.peng.answerbi.model.dto.chart;
|
||||
|
||||
import java.io.Serializable;
|
||||
import lombok.Data;
|
||||
|
||||
/**
|
||||
* 文件上传请求
|
||||
*
|
||||
* @author yunpeng
|
||||
* @version 1.0 2023/5/16
|
||||
*/
|
||||
@Data
|
||||
public class GenChartByAiRequest implements Serializable {
|
||||
|
||||
/**
|
||||
* 图表名称
|
||||
*/
|
||||
private String chartName;
|
||||
|
||||
/**
|
||||
* 分析目标
|
||||
*/
|
||||
private String goal;
|
||||
|
||||
/**
|
||||
* 图表类型
|
||||
*/
|
||||
private String chartType;
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
}
|
||||
@ -43,6 +43,11 @@ public class Chart implements Serializable {
|
||||
*/
|
||||
private Long userId;
|
||||
|
||||
/**
|
||||
* 图表名称
|
||||
*/
|
||||
private String chartName;
|
||||
|
||||
/**
|
||||
* 分析目标
|
||||
*/
|
||||
|
||||
@ -33,6 +33,7 @@ public class ChartServiceImpl extends ServiceImpl<ChartMapper, Chart>
|
||||
return queryWrapper;
|
||||
}
|
||||
Long id = chartQueryRequest.getId();
|
||||
String chartName = chartQueryRequest.getChartName();
|
||||
String goal = chartQueryRequest.getGoal();
|
||||
String chartType = chartQueryRequest.getChartType();
|
||||
Long userId = chartQueryRequest.getUserId();
|
||||
@ -40,6 +41,7 @@ public class ChartServiceImpl extends ServiceImpl<ChartMapper, Chart>
|
||||
String sortOrder = chartQueryRequest.getSortOrder();
|
||||
|
||||
queryWrapper.eq(id != null && id > 0, "id", id);
|
||||
queryWrapper.eq(StringUtils.isNotBlank(chartName), "chart_name", goal);
|
||||
queryWrapper.eq(StringUtils.isNotBlank(goal), "goal", goal);
|
||||
queryWrapper.eq(StringUtils.isNotBlank(chartType), "chart_type", chartType);
|
||||
queryWrapper.eq(ObjectUtils.isNotEmpty(userId), "user_id", userId);
|
||||
|
||||
64
src/main/java/top/peng/answerbi/utils/ExcelUtils.java
Normal file
64
src/main/java/top/peng/answerbi/utils/ExcelUtils.java
Normal file
@ -0,0 +1,64 @@
|
||||
/*
|
||||
* @(#)ExcelUtils.java
|
||||
*
|
||||
* Copyright © 2023 YunPeng Corporation.
|
||||
*/
|
||||
package top.peng.answerbi.utils;
|
||||
|
||||
import cn.hutool.core.collection.CollUtil;
|
||||
import com.alibaba.excel.EasyExcel;
|
||||
import com.alibaba.excel.support.ExcelTypeEnum;
|
||||
import io.swagger.models.auth.In;
|
||||
import java.io.File;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.IOException;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.stream.Collectors;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.lang3.ObjectUtils;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.springframework.util.ResourceUtils;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
/**
|
||||
* ExcelUtils Excel相关工具类
|
||||
*
|
||||
* @author yunpeng
|
||||
* @version 1.0 2023/7/12
|
||||
*/
|
||||
@Slf4j
|
||||
public class ExcelUtils {
|
||||
public static String excelToCsv(MultipartFile multipartFile){
|
||||
List<Map<Integer, String>> list = null;
|
||||
try {
|
||||
list = EasyExcel.read(multipartFile.getInputStream())
|
||||
.excelType(ExcelTypeEnum.XLSX)
|
||||
.sheet()
|
||||
.headRowNumber(0)
|
||||
.doReadSync();
|
||||
} catch (IOException e) {
|
||||
log.error("表格处理错误",e);
|
||||
}
|
||||
//如果数据为空
|
||||
if (CollUtil.isEmpty(list)){
|
||||
return "";
|
||||
}
|
||||
//转换为csv
|
||||
StringBuilder sb = new StringBuilder();
|
||||
//读取表头(第一行)
|
||||
Map<Integer, String> headerMap = list.get(0);
|
||||
List<String> headerList = headerMap.values().stream().filter(ObjectUtils::isNotEmpty)
|
||||
.collect(Collectors.toList());
|
||||
sb.append(StringUtils.join(headerList,",")).append("\n");
|
||||
//读取数据
|
||||
for (int i = 1; i < list.size();i++){
|
||||
Map<Integer, String> dataMap = list.get(i);
|
||||
List<String> dataList = dataMap.values().stream().filter(ObjectUtils::isNotEmpty)
|
||||
.collect(Collectors.toList());
|
||||
sb.append(StringUtils.join(dataList,",")).append("\n");
|
||||
}
|
||||
return sb.toString();
|
||||
}
|
||||
}
|
||||
@ -2,24 +2,25 @@
|
||||
<!DOCTYPE mapper
|
||||
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
|
||||
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
|
||||
<mapper namespace="top.peng.springbootinit.mapper.ChartMapper">
|
||||
<mapper namespace="top.peng.answerbi.mapper.ChartMapper">
|
||||
|
||||
<resultMap id="BaseResultMap" type="top.peng.answerbi.model.entity.Chart">
|
||||
<id property="id" column="id" jdbcType="BIGINT"/>
|
||||
<result property="created_time" column="created_time" jdbcType="TIMESTAMP"/>
|
||||
<result property="updated_time" column="updated_time" jdbcType="TIMESTAMP"/>
|
||||
<result property="deleted_flag" column="deleted_flag" jdbcType="TINYINT"/>
|
||||
<result property="user_id" column="user_id" jdbcType="BIGINT"/>
|
||||
<result property="createdTime" column="created_time" jdbcType="TIMESTAMP"/>
|
||||
<result property="updatedTime" column="updated_time" jdbcType="TIMESTAMP"/>
|
||||
<result property="deletedFlag" column="deleted_flag" jdbcType="TINYINT"/>
|
||||
<result property="userId" column="user_id" jdbcType="BIGINT"/>
|
||||
<result property="chartName" column="chart_name" jdbcType="VARCHAR"/>
|
||||
<result property="goal" column="goal" jdbcType="VARCHAR"/>
|
||||
<result property="chart_data" column="chart_data" jdbcType="VARCHAR"/>
|
||||
<result property="chart_type" column="chart_type" jdbcType="VARCHAR"/>
|
||||
<result property="gen_chart" column="gen_chart" jdbcType="VARCHAR"/>
|
||||
<result property="gen_result" column="gen_result" jdbcType="VARCHAR"/>
|
||||
<result property="chartData" column="chart_data" jdbcType="VARCHAR"/>
|
||||
<result property="chartType" column="chart_type" jdbcType="VARCHAR"/>
|
||||
<result property="genChart" column="gen_chart" jdbcType="VARCHAR"/>
|
||||
<result property="genResult" column="gen_result" jdbcType="VARCHAR"/>
|
||||
</resultMap>
|
||||
|
||||
<sql id="Base_Column_List">
|
||||
id,created_time,updated_time,
|
||||
deleted_flag,user_id,goal,
|
||||
deleted_flag,user_id,chart_name,goal,
|
||||
chart_data,chart_type,gen_chart,
|
||||
gen_result
|
||||
</sql>
|
||||
|
||||
@ -2,7 +2,7 @@
|
||||
<!DOCTYPE mapper
|
||||
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
|
||||
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
|
||||
<mapper namespace="top.peng.springbootinit.mapper.PostFavourMapper">
|
||||
<mapper namespace="top.peng.answerbi.mapper.PostFavourMapper">
|
||||
|
||||
<resultMap id="BaseResultMap" type="top.peng.answerbi.model.entity.PostFavour">
|
||||
<id property="id" column="id" jdbcType="BIGINT"/>
|
||||
|
||||
@ -2,7 +2,7 @@
|
||||
<!DOCTYPE mapper
|
||||
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
|
||||
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
|
||||
<mapper namespace="top.peng.springbootinit.mapper.PostMapper">
|
||||
<mapper namespace="top.peng.answerbi.mapper.PostMapper">
|
||||
|
||||
<resultMap id="BaseResultMap" type="top.peng.answerbi.model.entity.Post">
|
||||
<id property="id" column="id" jdbcType="BIGINT"/>
|
||||
|
||||
@ -2,7 +2,7 @@
|
||||
<!DOCTYPE mapper
|
||||
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
|
||||
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
|
||||
<mapper namespace="top.peng.springbootinit.mapper.PostThumbMapper">
|
||||
<mapper namespace="top.peng.answerbi.mapper.PostThumbMapper">
|
||||
|
||||
<resultMap id="BaseResultMap" type="top.peng.answerbi.model.entity.PostThumb">
|
||||
<id property="id" column="id" jdbcType="BIGINT"/>
|
||||
|
||||
@ -2,18 +2,18 @@
|
||||
<!DOCTYPE mapper
|
||||
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
|
||||
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
|
||||
<mapper namespace="top.peng.springbootinit.mapper.UserMapper">
|
||||
<mapper namespace="top.peng.answerbi.mapper.UserMapper">
|
||||
|
||||
<resultMap id="BaseResultMap" type="top.peng.answerbi.model.entity.User">
|
||||
<id property="id" column="id" jdbcType="BIGINT"/>
|
||||
<result property="created_time" column="created_time" jdbcType="TIMESTAMP"/>
|
||||
<result property="updated_time" column="updated_time" jdbcType="TIMESTAMP"/>
|
||||
<result property="deleted_flag" column="deleted_flag" jdbcType="TINYINT"/>
|
||||
<result property="user_account" column="user_account" jdbcType="VARCHAR"/>
|
||||
<result property="user_password" column="user_password" jdbcType="VARCHAR"/>
|
||||
<result property="username" column="username" jdbcType="VARCHAR"/>
|
||||
<result property="user_avatar" column="user_avatar" jdbcType="VARCHAR"/>
|
||||
<result property="user_role" column="user_role" jdbcType="VARCHAR"/>
|
||||
<result property="createdTime" column="created_time" jdbcType="TIMESTAMP"/>
|
||||
<result property="updatedTime" column="updated_time" jdbcType="TIMESTAMP"/>
|
||||
<result property="deletedFlag" column="deleted_flag" jdbcType="TINYINT"/>
|
||||
<result property="userAccount" column="user_account" jdbcType="VARCHAR"/>
|
||||
<result property="userPassword" column="user_password" jdbcType="VARCHAR"/>
|
||||
<result property="userName" column="username" jdbcType="VARCHAR"/>
|
||||
<result property="userAvatar" column="user_avatar" jdbcType="VARCHAR"/>
|
||||
<result property="userRole" column="user_role" jdbcType="VARCHAR"/>
|
||||
</resultMap>
|
||||
|
||||
<sql id="Base_Column_List">
|
||||
|
||||
Loading…
Reference in New Issue
Block a user