feat(chart): 新增图表功能
This commit is contained in:
1
pom.xml
1
pom.xml
@@ -27,6 +27,7 @@
|
|||||||
<module>springboot-message</module>
|
<module>springboot-message</module>
|
||||||
<module>springboot-annotation</module>
|
<module>springboot-annotation</module>
|
||||||
<module>springboot-role</module>
|
<module>springboot-role</module>
|
||||||
|
<module>springboot-chart</module>
|
||||||
</modules>
|
</modules>
|
||||||
|
|
||||||
<properties>
|
<properties>
|
||||||
|
|||||||
38
springboot-chart/pom.xml
Normal file
38
springboot-chart/pom.xml
Normal file
@@ -0,0 +1,38 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||||
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
<parent>
|
||||||
|
<groupId>com.tiesheng.springboot-parent</groupId>
|
||||||
|
<artifactId>springboot-parent</artifactId>
|
||||||
|
<version>2.0.27</version>
|
||||||
|
</parent>
|
||||||
|
|
||||||
|
<artifactId>springboot-chart</artifactId>
|
||||||
|
|
||||||
|
<properties>
|
||||||
|
<maven.compiler.source>8</maven.compiler.source>
|
||||||
|
<maven.compiler.target>8</maven.compiler.target>
|
||||||
|
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||||
|
</properties>
|
||||||
|
|
||||||
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.projectlombok</groupId>
|
||||||
|
<artifactId>lombok</artifactId>
|
||||||
|
<version>1.18.22</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.alibaba</groupId>
|
||||||
|
<artifactId>easyexcel</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.tiesheng.springboot-parent</groupId>
|
||||||
|
<artifactId>springboot-util</artifactId>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
|
||||||
|
</project>
|
||||||
@@ -0,0 +1,105 @@
|
|||||||
|
package com.tiesheng.role.controller;
|
||||||
|
|
||||||
|
import cn.hutool.core.collection.CollUtil;
|
||||||
|
import com.alibaba.excel.EasyExcel;
|
||||||
|
import com.alibaba.excel.write.style.row.SimpleRowHeightStyleStrategy;
|
||||||
|
import com.alibaba.fastjson.JSONObject;
|
||||||
|
import com.tiesheng.annotation.operation.OperationIgnore;
|
||||||
|
import com.tiesheng.annotation.role.RoleAuthority;
|
||||||
|
import com.tiesheng.role.pojos.dto.ChartTemplateCheck;
|
||||||
|
import com.tiesheng.role.pojos.vo.TsTableVO;
|
||||||
|
import com.tiesheng.role.service.ChartService;
|
||||||
|
import com.tiesheng.role.util.AliExcelUtil;
|
||||||
|
import com.tiesheng.util.config.GlobalConfig;
|
||||||
|
import com.tiesheng.util.exception.ApiException;
|
||||||
|
import com.tiesheng.util.pojos.ApiResp;
|
||||||
|
import com.tiesheng.util.pojos.FileUploadPath;
|
||||||
|
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 javax.annotation.Resource;
|
||||||
|
import javax.validation.Valid;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
@RestController
|
||||||
|
@RequestMapping("/chart/record")
|
||||||
|
@RoleAuthority(value = "template", group = "chart")
|
||||||
|
public class ChartRecordController {
|
||||||
|
|
||||||
|
@Resource
|
||||||
|
ChartService chartService;
|
||||||
|
@Resource
|
||||||
|
GlobalConfig globalConfig;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取列表
|
||||||
|
*
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
@PostMapping("/check")
|
||||||
|
@OperationIgnore
|
||||||
|
public ApiResp<TsTableVO> check(@RequestBody @Valid ChartTemplateCheck dto) {
|
||||||
|
|
||||||
|
if (dto.getTemplate() == null) {
|
||||||
|
dto.setTemplate(chartService.getById(dto.getTemplateId()));
|
||||||
|
}
|
||||||
|
if (dto.getTemplate() == null) {
|
||||||
|
throw new ApiException("请选择或创建一个模版");
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
List<JSONObject> chartRecordVOS = chartService.getBaseMapper()
|
||||||
|
.recordCheck(dto.getTemplate().getTableName(), dto.getCountType(),
|
||||||
|
dto.getTableXField(), dto.getWhere(),
|
||||||
|
dto.getTemplate().getSField(),
|
||||||
|
dto.getTableGroupBy());
|
||||||
|
return ApiResp.respOK(TsTableVO.of(dto.getTemplate(), chartRecordVOS));
|
||||||
|
} catch (Exception e) {
|
||||||
|
throw new ApiException("表或字段配置有误,请检查");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 导出数据
|
||||||
|
*
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
@PostMapping("/export")
|
||||||
|
@OperationIgnore
|
||||||
|
public ApiResp<String> export(@RequestBody @Valid ChartTemplateCheck dto) {
|
||||||
|
TsTableVO tsTableVO = check(dto).getData();
|
||||||
|
if (tsTableVO.getDataSource().isEmpty()) {
|
||||||
|
return ApiResp.respOK("");
|
||||||
|
}
|
||||||
|
|
||||||
|
List<List<String>> headers = new ArrayList<>();
|
||||||
|
for (TsTableVO.TableColumn column : tsTableVO.getColumns()) {
|
||||||
|
headers.add(CollUtil.newArrayList(column.getTitle()));
|
||||||
|
}
|
||||||
|
|
||||||
|
List<List<String>> contents = new ArrayList<>();
|
||||||
|
for (JSONObject obj : tsTableVO.getDataSource()) {
|
||||||
|
List<String> data = new ArrayList<>();
|
||||||
|
|
||||||
|
for (TsTableVO.TableColumn column : tsTableVO.getColumns()) {
|
||||||
|
data.add(obj.getString(column.getDataIndex()));
|
||||||
|
}
|
||||||
|
contents.add(data);
|
||||||
|
}
|
||||||
|
|
||||||
|
FileUploadPath xls = FileUploadPath.random("xls");
|
||||||
|
EasyExcel.write(xls.getAbsolutePath())
|
||||||
|
.sheet("数据统计")
|
||||||
|
.registerWriteHandler(new AliExcelUtil.ChatColumnWidthStyleStrategy())
|
||||||
|
.registerWriteHandler(new SimpleRowHeightStyleStrategy((short) 24, null))
|
||||||
|
.registerWriteHandler(AliExcelUtil.getCustomStyle())
|
||||||
|
.head(headers)
|
||||||
|
.doWrite(contents);
|
||||||
|
|
||||||
|
return ApiResp.respOK(globalConfig.buildPath(xls.getHttpPath()));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,141 @@
|
|||||||
|
package com.tiesheng.role.controller;
|
||||||
|
|
||||||
|
import cn.hutool.core.bean.BeanUtil;
|
||||||
|
import cn.hutool.core.util.StrUtil;
|
||||||
|
import com.alibaba.fastjson.JSON;
|
||||||
|
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
||||||
|
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
|
||||||
|
import com.tiesheng.annotation.role.RoleAuthority;
|
||||||
|
import com.tiesheng.role.pojos.dao.CoreChartTable;
|
||||||
|
import com.tiesheng.role.pojos.dao.CoreChartTemplate;
|
||||||
|
import com.tiesheng.role.pojos.dto.ChartTableEditDTO;
|
||||||
|
import com.tiesheng.role.pojos.dto.ChartTemplateEditDTO;
|
||||||
|
import com.tiesheng.role.service.ChartService;
|
||||||
|
import com.tiesheng.util.exception.ApiException;
|
||||||
|
import com.tiesheng.util.pojos.ApiResp;
|
||||||
|
import com.tiesheng.util.pojos.IdDTO;
|
||||||
|
import com.tiesheng.util.pojos.PageDTO;
|
||||||
|
import org.springframework.web.bind.annotation.*;
|
||||||
|
|
||||||
|
import javax.annotation.Resource;
|
||||||
|
import javax.validation.Valid;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
@RestController
|
||||||
|
@RequestMapping("/chart/template")
|
||||||
|
@RoleAuthority(value = "template", group = "chart")
|
||||||
|
public class ChartTemplateController {
|
||||||
|
|
||||||
|
@Resource
|
||||||
|
ChartService chartService;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取模版列表
|
||||||
|
*
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
@GetMapping("/list")
|
||||||
|
public ApiResp<List<CoreChartTemplate>> list(PageDTO dto) {
|
||||||
|
List<CoreChartTemplate> list = chartService.list(new QueryWrapper<CoreChartTemplate>()
|
||||||
|
.eq("is_deleted", 0)
|
||||||
|
.like(StrUtil.isNotEmpty(dto.getKeyword()), "table_name", dto.getKeyword())
|
||||||
|
.orderByAsc("sort")
|
||||||
|
);
|
||||||
|
return ApiResp.respOK(list);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取模版列表
|
||||||
|
*
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
@PostMapping("/edit")
|
||||||
|
public ApiResp<String> edit(@RequestBody @Valid ChartTemplateEditDTO dto) {
|
||||||
|
dto.setSField(StrUtil.emptyToDefault(dto.getSField(), ""));
|
||||||
|
CoreChartTemplate template = BeanUtil.copyProperties(dto, CoreChartTemplate.class);
|
||||||
|
template.setExts(JSON.toJSONString(dto.getExts()));
|
||||||
|
chartService.saveOrUpdate(template);
|
||||||
|
return ApiResp.respOK("");
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取模版列表
|
||||||
|
*
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
@PostMapping("/del")
|
||||||
|
public ApiResp<String> del(@RequestBody @Valid IdDTO dto) {
|
||||||
|
chartService.update(null, new UpdateWrapper<CoreChartTemplate>()
|
||||||
|
.set("is_deleted", 1)
|
||||||
|
.eq("id", dto.getId())
|
||||||
|
.eq("is_deleted", 0)
|
||||||
|
);
|
||||||
|
return ApiResp.respOK("");
|
||||||
|
}
|
||||||
|
|
||||||
|
///////////////////////////// 表信息
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取模版列表
|
||||||
|
*
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
@GetMapping("/table/list")
|
||||||
|
public ApiResp<List<CoreChartTable>> tableList(PageDTO dto) {
|
||||||
|
List<CoreChartTable> list = chartService.getCoreChartTableMapper()
|
||||||
|
.selectList(new QueryWrapper<CoreChartTable>()
|
||||||
|
.eq("is_deleted", 0)
|
||||||
|
.like(StrUtil.isNotEmpty(dto.getKeyword()), "table_name", dto.getKeyword())
|
||||||
|
);
|
||||||
|
return ApiResp.respOK(list);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取模版列表
|
||||||
|
*
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
@PostMapping("/table/edit")
|
||||||
|
public ApiResp<String> tableEdit(@RequestBody @Valid ChartTableEditDTO dto) {
|
||||||
|
|
||||||
|
CoreChartTable exist = chartService.getCoreChartTableMapper().selectOne(
|
||||||
|
new QueryWrapper<CoreChartTable>()
|
||||||
|
.eq("table_name", dto.getTableName())
|
||||||
|
.ne(StrUtil.isNotEmpty(dto.getId()), "id", dto.getId())
|
||||||
|
.last("limit 1")
|
||||||
|
);
|
||||||
|
if (exist != null) {
|
||||||
|
throw new ApiException("不可添加重复的表名");
|
||||||
|
}
|
||||||
|
|
||||||
|
CoreChartTable template = BeanUtil.copyProperties(dto, CoreChartTable.class);
|
||||||
|
template.setIsDeleted(0);
|
||||||
|
template.setColumns(JSON.toJSONString(dto.getColumns()));
|
||||||
|
if (StrUtil.isNotEmpty(dto.getId())) {
|
||||||
|
chartService.getCoreChartTableMapper().updateById(template);
|
||||||
|
} else {
|
||||||
|
chartService.getCoreChartTableMapper().insert(template);
|
||||||
|
}
|
||||||
|
return ApiResp.respOK("");
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取模版列表
|
||||||
|
*
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
@PostMapping("/table/del")
|
||||||
|
public ApiResp<String> tableDel(@RequestBody @Valid IdDTO dto) {
|
||||||
|
chartService.getCoreChartTableMapper().update(null, new UpdateWrapper<CoreChartTable>()
|
||||||
|
.set("is_deleted", 1)
|
||||||
|
.eq("id", dto.getId())
|
||||||
|
.eq("is_deleted", 0)
|
||||||
|
);
|
||||||
|
return ApiResp.respOK("");
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
package com.tiesheng.role.mapper;
|
||||||
|
|
||||||
|
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||||
|
import com.tiesheng.role.pojos.dao.CoreChartTable;
|
||||||
|
|
||||||
|
public interface CoreChartTableMapper extends BaseMapper<CoreChartTable> {
|
||||||
|
}
|
||||||
@@ -0,0 +1,27 @@
|
|||||||
|
package com.tiesheng.role.mapper;
|
||||||
|
|
||||||
|
import com.alibaba.fastjson.JSONObject;
|
||||||
|
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||||
|
import com.tiesheng.role.pojos.dao.CoreChartTemplate;
|
||||||
|
import org.apache.ibatis.annotations.Param;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public interface CoreChartTemplateMapper extends BaseMapper<CoreChartTemplate> {
|
||||||
|
/**
|
||||||
|
* 根据字段查询数据
|
||||||
|
*
|
||||||
|
* @param table
|
||||||
|
* @param xField
|
||||||
|
* @param where
|
||||||
|
* @param sField
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
List<JSONObject> recordCheck(@Param("table") String table,
|
||||||
|
@Param("type") String type,
|
||||||
|
@Param("xField") String xField,
|
||||||
|
@Param("where") String where,
|
||||||
|
@Param("sField") String sField,
|
||||||
|
@Param("groupBy") String groupBy
|
||||||
|
);
|
||||||
|
}
|
||||||
@@ -0,0 +1,39 @@
|
|||||||
|
package com.tiesheng.role.pojos.dao;
|
||||||
|
|
||||||
|
import com.baomidou.mybatisplus.annotation.TableField;
|
||||||
|
import com.baomidou.mybatisplus.annotation.TableName;
|
||||||
|
import com.tiesheng.util.pojos.DaoBase;
|
||||||
|
import lombok.Data;
|
||||||
|
import lombok.EqualsAndHashCode;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 图表-表信息
|
||||||
|
*/
|
||||||
|
@Data
|
||||||
|
@EqualsAndHashCode(callSuper = true)
|
||||||
|
@TableName(value = "core_chart_table")
|
||||||
|
public class CoreChartTable extends DaoBase {
|
||||||
|
/**
|
||||||
|
* 表名
|
||||||
|
*/
|
||||||
|
@TableField(value = "`table_name`")
|
||||||
|
private String tableName;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 名称
|
||||||
|
*/
|
||||||
|
@TableField(value = "table_title")
|
||||||
|
private String tableTitle;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 所有字段
|
||||||
|
*/
|
||||||
|
@TableField(value = "`columns`")
|
||||||
|
private String columns;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 其他说明
|
||||||
|
*/
|
||||||
|
@TableField(value = "remark")
|
||||||
|
private String remark;
|
||||||
|
}
|
||||||
@@ -0,0 +1,81 @@
|
|||||||
|
package com.tiesheng.role.pojos.dao;
|
||||||
|
|
||||||
|
import com.baomidou.mybatisplus.annotation.TableField;
|
||||||
|
import com.baomidou.mybatisplus.annotation.TableName;
|
||||||
|
import com.tiesheng.util.pojos.DaoBase;
|
||||||
|
import lombok.Data;
|
||||||
|
import lombok.EqualsAndHashCode;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 图表-表信息
|
||||||
|
*/
|
||||||
|
@Data
|
||||||
|
@EqualsAndHashCode(callSuper = true)
|
||||||
|
@TableName(value = "core_chart_template")
|
||||||
|
public class CoreChartTemplate extends DaoBase {
|
||||||
|
/**
|
||||||
|
* 模版名称
|
||||||
|
*/
|
||||||
|
@TableField(value = "`name`")
|
||||||
|
private String name;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 表名
|
||||||
|
*/
|
||||||
|
@TableField(value = "`table_name`")
|
||||||
|
private String tableName;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 名称
|
||||||
|
*/
|
||||||
|
@TableField(value = "table_title")
|
||||||
|
private String tableTitle;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 统计方式
|
||||||
|
*/
|
||||||
|
@TableField(value = "`type`")
|
||||||
|
private String type;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* X轴字段
|
||||||
|
*/
|
||||||
|
@TableField(value = "x_field")
|
||||||
|
private String xField;
|
||||||
|
|
||||||
|
@TableField(value = "x_field_name")
|
||||||
|
private String xFieldName;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* X轴格式化,只有X轴字段为date时有效
|
||||||
|
*/
|
||||||
|
@TableField(value = "x_format")
|
||||||
|
private String xFormat;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* S轴字段
|
||||||
|
*/
|
||||||
|
@TableField(value = "s_field")
|
||||||
|
private String sField;
|
||||||
|
|
||||||
|
@TableField(value = "s_field_name")
|
||||||
|
private String sFieldName;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 附件条件
|
||||||
|
*/
|
||||||
|
@TableField(value = "exts")
|
||||||
|
private String exts;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 其他说明
|
||||||
|
*/
|
||||||
|
@TableField(value = "remark")
|
||||||
|
private String remark;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 排序
|
||||||
|
*/
|
||||||
|
@TableField(value = "sort")
|
||||||
|
private Integer sort;
|
||||||
|
}
|
||||||
@@ -0,0 +1,33 @@
|
|||||||
|
package com.tiesheng.role.pojos.dto;
|
||||||
|
|
||||||
|
import lombok.Data;
|
||||||
|
|
||||||
|
import javax.validation.constraints.NotEmpty;
|
||||||
|
import javax.validation.constraints.Size;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
@Data
|
||||||
|
public class ChartTableEditDTO {
|
||||||
|
|
||||||
|
private String id;
|
||||||
|
|
||||||
|
@NotEmpty(message = "请输入【表名】")
|
||||||
|
private String tableName;
|
||||||
|
|
||||||
|
@NotEmpty(message = "请输入【名称】")
|
||||||
|
private String tableTitle;
|
||||||
|
|
||||||
|
@Size(min = 1, message = "请配置【表字段】")
|
||||||
|
private List<TableColumn> columns;
|
||||||
|
private String remark;
|
||||||
|
|
||||||
|
|
||||||
|
@Data
|
||||||
|
public static class TableColumn {
|
||||||
|
private String key;
|
||||||
|
private String type;
|
||||||
|
private String remark;
|
||||||
|
private String enums;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,98 @@
|
|||||||
|
package com.tiesheng.role.pojos.dto;
|
||||||
|
|
||||||
|
import cn.hutool.core.util.StrUtil;
|
||||||
|
import com.alibaba.fastjson.JSON;
|
||||||
|
import com.alibaba.fastjson.annotation.JSONField;
|
||||||
|
import com.tiesheng.role.pojos.dao.CoreChartTemplate;
|
||||||
|
import lombok.Data;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Objects;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
|
@Data
|
||||||
|
public class ChartTemplateCheck {
|
||||||
|
|
||||||
|
private String templateId;
|
||||||
|
private CoreChartTemplate template;
|
||||||
|
|
||||||
|
|
||||||
|
@JSONField(serialize = false)
|
||||||
|
public String getTableXField() {
|
||||||
|
if (StrUtil.isNotEmpty(template.getXFormat())) {
|
||||||
|
return StrUtil.format("DATE_FORMAT({},'{}')", getTemplate().getXField(), getTemplate().getXFormat());
|
||||||
|
}
|
||||||
|
return getTemplate().getXField();
|
||||||
|
}
|
||||||
|
|
||||||
|
@JSONField(serialize = false)
|
||||||
|
public String getTableGroupBy() {
|
||||||
|
if (StrUtil.isNotEmpty(getTemplate().getSField())) {
|
||||||
|
return StrUtil.format("{},{}", getTableXField(), getTemplate().getSField());
|
||||||
|
}
|
||||||
|
return getTableXField();
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getCountType() {
|
||||||
|
if (Objects.equals(getTemplate().getType(), "count_1")) {
|
||||||
|
return "count(1)";
|
||||||
|
}
|
||||||
|
return "sum(" + getTemplate().getType() + ")";
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@JSONField(serialize = false)
|
||||||
|
public String getWhere() {
|
||||||
|
List<ChartTemplateEditDTO.TemplateExt> templateExts = JSON.parseArray(getTemplate().getExts(), ChartTemplateEditDTO.TemplateExt.class);
|
||||||
|
if (templateExts == null || templateExts.isEmpty()) {
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
|
||||||
|
List<String> list = new ArrayList<>();
|
||||||
|
for (ChartTemplateEditDTO.TemplateExt templateExt : templateExts) {
|
||||||
|
if (Objects.equals(templateExt.getAction(), "eq")) {
|
||||||
|
list.add(StrUtil.format("{} = '{}'", templateExt.getKey(), templateExt.getValue()));
|
||||||
|
} else if (Objects.equals(templateExt.getAction(), "ne")) {
|
||||||
|
list.add(StrUtil.format("{} != '{}'", templateExt.getKey(), templateExt.getValue()));
|
||||||
|
} else if (Objects.equals(templateExt.getAction(), "btw")) {
|
||||||
|
List<String> split = StrUtil.split(templateExt.getValue(), ",");
|
||||||
|
if (split.isEmpty()) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if (split.size() == 1) {
|
||||||
|
list.add(StrUtil.format("{} > '{}'", templateExt.getKey(), split.get(0)));
|
||||||
|
} else {
|
||||||
|
list.add(StrUtil.format("{} between '{}' and '{}'", templateExt.getKey(), split.get(0), split.get(1)));
|
||||||
|
}
|
||||||
|
} else if (Objects.equals(templateExt.getAction(), "nbtw")) {
|
||||||
|
List<String> split = StrUtil.split(templateExt.getValue(), ",");
|
||||||
|
if (split.isEmpty()) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if (split.size() == 1) {
|
||||||
|
list.add(StrUtil.format("{} < '{}'", templateExt.getKey(), split.get(0)));
|
||||||
|
} else {
|
||||||
|
list.add(StrUtil.format("{} not between '{}' and '{}'", templateExt.getKey(), split.get(0), split.get(1)));
|
||||||
|
}
|
||||||
|
} else if (Objects.equals(templateExt.getAction(), "in")) {
|
||||||
|
List<String> split = StrUtil.split(templateExt.getValue(), ",");
|
||||||
|
if (split.isEmpty()) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
list.add(StrUtil.format("{} in ({})", templateExt.getKey(), split.stream().map(it -> "'" + it + "'")
|
||||||
|
.collect(Collectors.joining(","))));
|
||||||
|
} else if (Objects.equals(templateExt.getAction(), "nin")) {
|
||||||
|
List<String> split = StrUtil.split(templateExt.getValue(), ",");
|
||||||
|
if (split.isEmpty()) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
list.add(StrUtil.format("{} not in ({})", templateExt.getKey(), split.stream().map(it -> "'" + it + "'")
|
||||||
|
.collect(Collectors.joining(","))));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return StrUtil.join(" and ", list);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,40 @@
|
|||||||
|
package com.tiesheng.role.pojos.dto;
|
||||||
|
|
||||||
|
import lombok.Data;
|
||||||
|
|
||||||
|
import javax.validation.constraints.NotEmpty;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
@Data
|
||||||
|
public class ChartTemplateEditDTO {
|
||||||
|
|
||||||
|
private String id;
|
||||||
|
|
||||||
|
@NotEmpty(message = "请输入【模版名称】")
|
||||||
|
private String name;
|
||||||
|
|
||||||
|
@NotEmpty(message = "请选择【表名】")
|
||||||
|
private String tableName;
|
||||||
|
private String tableTitle;
|
||||||
|
|
||||||
|
@NotEmpty(message = "请选择【X轴字段】")
|
||||||
|
private String xField;
|
||||||
|
private String xFieldName;
|
||||||
|
private String xFormat;
|
||||||
|
|
||||||
|
private String sField;
|
||||||
|
private String sFieldName;
|
||||||
|
private String type;
|
||||||
|
private List<TemplateExt> exts;
|
||||||
|
private String remark;
|
||||||
|
private Integer sort;
|
||||||
|
|
||||||
|
|
||||||
|
@Data
|
||||||
|
public static class TemplateExt {
|
||||||
|
private String key;
|
||||||
|
private String action;
|
||||||
|
private String value;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,66 @@
|
|||||||
|
package com.tiesheng.role.pojos.vo;
|
||||||
|
|
||||||
|
import cn.hutool.core.collection.CollUtil;
|
||||||
|
import cn.hutool.core.util.StrUtil;
|
||||||
|
import com.alibaba.fastjson.JSONObject;
|
||||||
|
import com.tiesheng.role.pojos.dao.CoreChartTemplate;
|
||||||
|
import lombok.Data;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
@Data
|
||||||
|
public class TsTableVO {
|
||||||
|
|
||||||
|
private List<TableColumn> columns;
|
||||||
|
private List<JSONObject> dataSource;
|
||||||
|
|
||||||
|
|
||||||
|
@Data
|
||||||
|
public static class TableColumn {
|
||||||
|
private String title;
|
||||||
|
private String dataIndex;
|
||||||
|
private String width;
|
||||||
|
|
||||||
|
public TableColumn(String title, String dataIndex) {
|
||||||
|
this.title = title;
|
||||||
|
this.dataIndex = dataIndex;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public TsTableVO addHeader(String title, String dataIndex) {
|
||||||
|
if (getColumns() == null) {
|
||||||
|
setColumns(CollUtil.newArrayList());
|
||||||
|
}
|
||||||
|
getColumns().add(new TableColumn(title, dataIndex));
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public TsTableVO addData(JSONObject obj) {
|
||||||
|
if (getDataSource() == null) {
|
||||||
|
setDataSource(CollUtil.newArrayList());
|
||||||
|
}
|
||||||
|
getDataSource().add(obj);
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public static TsTableVO of(CoreChartTemplate template, List<JSONObject> originData) {
|
||||||
|
|
||||||
|
TsTableVO tsTableVO = new TsTableVO();
|
||||||
|
tsTableVO.addHeader(template.getXFieldName(), "xField");
|
||||||
|
List<String> split = StrUtil.split(template.getSField(), ",");
|
||||||
|
List<String> sFieldNameList = StrUtil.split(template.getSFieldName(), ",");
|
||||||
|
if (!split.isEmpty() && split.size() == sFieldNameList.size()) {
|
||||||
|
int idx = 0;
|
||||||
|
for (String dataIndex : split) {
|
||||||
|
tsTableVO.addHeader(CollUtil.get(sFieldNameList, idx), StrUtil.toCamelCase(dataIndex));
|
||||||
|
idx++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
tsTableVO.addHeader("统计", "yField");
|
||||||
|
tsTableVO.setDataSource(originData);
|
||||||
|
return tsTableVO;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,20 @@
|
|||||||
|
package com.tiesheng.role.service;
|
||||||
|
|
||||||
|
import com.tiesheng.role.mapper.CoreChartTableMapper;
|
||||||
|
import com.tiesheng.role.mapper.CoreChartTemplateMapper;
|
||||||
|
import com.tiesheng.role.pojos.dao.CoreChartTemplate;
|
||||||
|
import com.tiesheng.util.service.TsServiceBase;
|
||||||
|
import lombok.Getter;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
import javax.annotation.Resource;
|
||||||
|
|
||||||
|
@Service
|
||||||
|
public class ChartService extends TsServiceBase<CoreChartTemplateMapper, CoreChartTemplate> {
|
||||||
|
|
||||||
|
@Getter
|
||||||
|
@Resource
|
||||||
|
CoreChartTableMapper coreChartTableMapper;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
@@ -0,0 +1,59 @@
|
|||||||
|
package com.tiesheng.role.util;
|
||||||
|
|
||||||
|
import com.alibaba.excel.metadata.Head;
|
||||||
|
import com.alibaba.excel.write.metadata.style.WriteCellStyle;
|
||||||
|
import com.alibaba.excel.write.metadata.style.WriteFont;
|
||||||
|
import com.alibaba.excel.write.style.HorizontalCellStyleStrategy;
|
||||||
|
import com.alibaba.excel.write.style.column.AbstractHeadColumnWidthStyleStrategy;
|
||||||
|
import org.apache.poi.ss.usermodel.FillPatternType;
|
||||||
|
import org.apache.poi.ss.usermodel.HorizontalAlignment;
|
||||||
|
import org.apache.poi.ss.usermodel.IndexedColors;
|
||||||
|
import org.apache.poi.ss.usermodel.VerticalAlignment;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author Administrator
|
||||||
|
*/
|
||||||
|
public class AliExcelUtil {
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 导出数据
|
||||||
|
*
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public static HorizontalCellStyleStrategy getCustomStyle() {
|
||||||
|
|
||||||
|
// 头的策略
|
||||||
|
WriteCellStyle headWriteCellStyle = new WriteCellStyle();
|
||||||
|
WriteFont headWriteFont = new WriteFont();
|
||||||
|
headWriteFont.setBold(true);
|
||||||
|
headWriteFont.setFontHeightInPoints((short) 12);
|
||||||
|
headWriteCellStyle.setWriteFont(headWriteFont);
|
||||||
|
headWriteCellStyle.setFillBackgroundColor(IndexedColors.ROYAL_BLUE.getIndex());
|
||||||
|
headWriteCellStyle.setFillPatternType(FillPatternType.SOLID_FOREGROUND);
|
||||||
|
|
||||||
|
// 内容的策略
|
||||||
|
WriteCellStyle contentWriteCellStyle = new WriteCellStyle();
|
||||||
|
WriteFont contentWriteFont = new WriteFont();
|
||||||
|
contentWriteFont.setFontHeightInPoints((short) 11);
|
||||||
|
contentWriteCellStyle.setWriteFont(contentWriteFont);
|
||||||
|
contentWriteCellStyle.setWrapped(true);
|
||||||
|
contentWriteCellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
|
||||||
|
contentWriteCellStyle.setHorizontalAlignment(HorizontalAlignment.CENTER);
|
||||||
|
|
||||||
|
return new HorizontalCellStyleStrategy(headWriteCellStyle, contentWriteCellStyle);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static class ChatColumnWidthStyleStrategy extends AbstractHeadColumnWidthStyleStrategy {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected Integer columnWidth(Head head, Integer integer) {
|
||||||
|
if (head.getColumnIndex() == 0) {
|
||||||
|
return 20;
|
||||||
|
}
|
||||||
|
return (head.getHeadNameList().get(0).length()) * 2 + 10;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,43 @@
|
|||||||
|
create table core_chart_table
|
||||||
|
(
|
||||||
|
id varchar(50) not null
|
||||||
|
primary key,
|
||||||
|
create_time datetime not null,
|
||||||
|
update_time datetime not null,
|
||||||
|
is_deleted int default 0 not null,
|
||||||
|
table_name varchar(100) not null comment '表名',
|
||||||
|
table_title varchar(500) not null comment '名称',
|
||||||
|
columns text not null comment '所有字段',
|
||||||
|
remark text null comment '其他说明'
|
||||||
|
)
|
||||||
|
ENGINE = InnoDB
|
||||||
|
CHARACTER SET = utf8mb4
|
||||||
|
COLLATE = utf8mb4_general_ci
|
||||||
|
comment '图表-表信息'
|
||||||
|
row_format = DYNAMIC;
|
||||||
|
|
||||||
|
create table core_chart_template
|
||||||
|
(
|
||||||
|
id varchar(50) not null
|
||||||
|
primary key,
|
||||||
|
create_time datetime not null,
|
||||||
|
update_time datetime not null,
|
||||||
|
is_deleted int default 0 not null,
|
||||||
|
name varchar(100) not null comment '模版名称',
|
||||||
|
table_name varchar(100) not null comment '表名',
|
||||||
|
table_title varchar(100) not null comment '名称',
|
||||||
|
type varchar(50) default 'count_1' null comment '统计方式',
|
||||||
|
x_field varchar(100) not null comment 'X轴字段',
|
||||||
|
x_field_name varchar(500) not null comment 'X轴字段',
|
||||||
|
x_format varchar(100) null comment 'X轴格式化,只有X轴字段为date时有效',
|
||||||
|
s_field varchar(100) null comment 'S轴字段',
|
||||||
|
s_field_name varchar(500) null comment 'S轴字段',
|
||||||
|
exts text null comment '附件条件',
|
||||||
|
remark text null comment '其他说明',
|
||||||
|
sort int default 0 null comment '排序'
|
||||||
|
)
|
||||||
|
ENGINE = InnoDB
|
||||||
|
CHARACTER SET = utf8mb4
|
||||||
|
COLLATE = utf8mb4_general_ci
|
||||||
|
comment '图表-表信息'
|
||||||
|
row_format = DYNAMIC;
|
||||||
@@ -0,0 +1,20 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
|
||||||
|
<mapper namespace="com.tiesheng.zaxy.alarm.mapper.CoreChartTableMapper">
|
||||||
|
<resultMap id="BaseResultMap" type="com.tiesheng.zaxy.alarm.pojos.dao.CoreChartTable">
|
||||||
|
<!--@mbg.generated-->
|
||||||
|
<!--@Table core_chart_table-->
|
||||||
|
<id column="id" jdbcType="VARCHAR" property="id" />
|
||||||
|
<result column="create_time" jdbcType="TIMESTAMP" property="createTime" />
|
||||||
|
<result column="update_time" jdbcType="TIMESTAMP" property="updateTime" />
|
||||||
|
<result column="is_deleted" jdbcType="INTEGER" property="isDeleted" />
|
||||||
|
<result column="table_name" jdbcType="VARCHAR" property="tableName" />
|
||||||
|
<result column="table_title" jdbcType="VARCHAR" property="tableTitle" />
|
||||||
|
<result column="columns" jdbcType="LONGVARCHAR" property="columns" />
|
||||||
|
<result column="remark" jdbcType="LONGVARCHAR" property="remark" />
|
||||||
|
</resultMap>
|
||||||
|
<sql id="Base_Column_List">
|
||||||
|
<!--@mbg.generated-->
|
||||||
|
id, create_time, update_time, is_deleted, `table_name`, table_title, `columns`, remark
|
||||||
|
</sql>
|
||||||
|
</mapper>
|
||||||
@@ -0,0 +1,46 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
|
||||||
|
<mapper namespace="com.tiesheng.zaxy.alarm.mapper.CoreChartTemplateMapper">
|
||||||
|
<resultMap id="BaseResultMap" type="com.tiesheng.zaxy.alarm.pojos.dao.CoreChartTemplate">
|
||||||
|
<!--@mbg.generated-->
|
||||||
|
<!--@Table core_chart_template-->
|
||||||
|
<id column="id" jdbcType="VARCHAR" property="id" />
|
||||||
|
<result column="create_time" jdbcType="TIMESTAMP" property="createTime" />
|
||||||
|
<result column="update_time" jdbcType="TIMESTAMP" property="updateTime" />
|
||||||
|
<result column="is_deleted" jdbcType="INTEGER" property="isDeleted" />
|
||||||
|
<result column="name" jdbcType="VARCHAR" property="name" />
|
||||||
|
<result column="table_name" jdbcType="VARCHAR" property="tableName" />
|
||||||
|
<result column="table_title" jdbcType="VARCHAR" property="tableTitle" />
|
||||||
|
<result column="type" jdbcType="VARCHAR" property="type" />
|
||||||
|
<result column="x_field" jdbcType="VARCHAR" property="xField" />
|
||||||
|
<result column="x_field_name" jdbcType="VARCHAR" property="xFieldName" />
|
||||||
|
<result column="x_format" jdbcType="VARCHAR" property="xFormat" />
|
||||||
|
<result column="s_field" jdbcType="VARCHAR" property="sField" />
|
||||||
|
<result column="s_field_name" jdbcType="VARCHAR" property="sFieldName" />
|
||||||
|
<result column="exts" jdbcType="LONGVARCHAR" property="exts" />
|
||||||
|
<result column="remark" jdbcType="LONGVARCHAR" property="remark" />
|
||||||
|
<result column="sort" jdbcType="INTEGER" property="sort" />
|
||||||
|
</resultMap>
|
||||||
|
<sql id="Base_Column_List">
|
||||||
|
<!--@mbg.generated-->
|
||||||
|
id, create_time, update_time, is_deleted, `name`, `table_name`, table_title, `type`,
|
||||||
|
x_field, x_field_name, x_format, s_field, s_field_name, exts, remark, sort
|
||||||
|
</sql>
|
||||||
|
|
||||||
|
<select id="recordCheck" resultType="com.alibaba.fastjson.JSONObject">
|
||||||
|
select ${xField} x_field,
|
||||||
|
<if test="sField != null and sField != ''">
|
||||||
|
${sField},
|
||||||
|
</if>
|
||||||
|
${type} y_field
|
||||||
|
from ${table}
|
||||||
|
where 1 = 1
|
||||||
|
<if test="where != null and where != ''">
|
||||||
|
and ${where}
|
||||||
|
</if>
|
||||||
|
<if test="groupBy != null and groupBy != ''">
|
||||||
|
group by ${groupBy}
|
||||||
|
</if>
|
||||||
|
order by ${groupBy} asc
|
||||||
|
</select>
|
||||||
|
</mapper>
|
||||||
Reference in New Issue
Block a user