|
|
@@ -6,6 +6,7 @@ import moment from 'moment'
|
|
|
import URLS from '../constants/url'
|
|
|
import CHART_TYPE from './chartType.json'
|
|
|
import { ArrayEquals } from '../utils/baseUtils.js'
|
|
|
+import Exportor from '../utils/exportor'
|
|
|
|
|
|
/**
|
|
|
* 获得报表中图表的真实过滤规则
|
|
|
@@ -711,29 +712,193 @@ export default {
|
|
|
yield put({ type: 'dataList/setField', name: 'loading', value: false });
|
|
|
}
|
|
|
},
|
|
|
+ // *exportToExcel(action, { select, call, put }) {
|
|
|
+ // const dashboardDesigner = yield select(state => state.present.dashboardDesigner);
|
|
|
+ // const { code, name, dataSources, filters } = dashboardDesigner;
|
|
|
+ // try {
|
|
|
+ // let body = {
|
|
|
+ // dashboardId: code,
|
|
|
+ // dashboardName: name,
|
|
|
+ // data: dataSources.map(d => {
|
|
|
+ // let { code } = d;
|
|
|
+ // return {
|
|
|
+ // dataSourceId: code,
|
|
|
+ // filter: getBodyFilters(getTrueFilters({ dataSourceCode: code }, filters))
|
|
|
+ // }
|
|
|
+ // }),
|
|
|
+ // }
|
|
|
+ // yield call(service.fetch, {
|
|
|
+ // url: URLS.DASHBOARD_EXPORT,
|
|
|
+ // requestType: 'file',
|
|
|
+ // fileName: name,
|
|
|
+ // body,
|
|
|
+ // })
|
|
|
+ // }catch(e) {
|
|
|
+ // message.error('报表导出错误: ' + e);
|
|
|
+ // }
|
|
|
+ // },
|
|
|
*exportToExcel(action, { select, call, put }) {
|
|
|
const dashboardDesigner = yield select(state => state.present.dashboardDesigner);
|
|
|
- const { code, name, dataSources, filters } = dashboardDesigner;
|
|
|
+ const { name, creatorCode, filters, items } = dashboardDesigner;
|
|
|
try {
|
|
|
- let body = {
|
|
|
- dashboardId: code,
|
|
|
- dashboardName: name,
|
|
|
- data: dataSources.map(d => {
|
|
|
- let { code } = d;
|
|
|
- return {
|
|
|
- dataSourceId: code,
|
|
|
- filter: getBodyFilters(getTrueFilters({ dataSourceCode: code }, filters))
|
|
|
+ yield put({ type: 'dashboardDesigner/setField', name: 'loading', value: true });
|
|
|
+
|
|
|
+ let sheets = [];
|
|
|
+ for(let i = 0; i < items.length; i++) {
|
|
|
+ let item = items[i];
|
|
|
+ let { chartCode, name: itemName, chartOption, viewType, chartType, content } = item;
|
|
|
+ let option = chartOption ? (chartOption.baseOption || {}) : {};
|
|
|
+ let { originConfig, xAxis, series } = option;
|
|
|
+ let { xAxis: oxAxis, yAxis: oyAxis, groupBy: ogroupBy } = (originConfig || {});
|
|
|
+ let header;
|
|
|
+ let columns = [];
|
|
|
+ let rows = [];
|
|
|
+ const TYPES = {
|
|
|
+ time: 'DateTime',
|
|
|
+ categorical: 'String',
|
|
|
+ scale: 'Number',
|
|
|
+ string: 'String'
|
|
|
+ };
|
|
|
+ if(viewType === 'chart') {
|
|
|
+ if(chartType === 'bar' || chartType === 'line') {
|
|
|
+ columns = [{
|
|
|
+ name: oxAxis.column.label,
|
|
|
+ width: 150,
|
|
|
+ type: 'String'
|
|
|
+ }, {
|
|
|
+ name: oyAxis.column.label,
|
|
|
+ width: 100,
|
|
|
+ type: 'Number'
|
|
|
+ }];
|
|
|
+ if(ogroupBy && ogroupBy.key) {
|
|
|
+ columns.push({
|
|
|
+ name: ogroupBy.label,
|
|
|
+ width: 150,
|
|
|
+ type: 'String'
|
|
|
+ });
|
|
|
+ }
|
|
|
+ xAxis[0].data.forEach((d, i) => {
|
|
|
+ if(ogroupBy && ogroupBy.key) {
|
|
|
+ series.forEach(xs => {
|
|
|
+ rows.push([d, xs.data[i], xs.name]);
|
|
|
+ })
|
|
|
+ }else {
|
|
|
+ rows.push([d, series[0].data[i]])
|
|
|
+ }
|
|
|
+ })
|
|
|
+ }else if(chartType === 'pie') {
|
|
|
+ columns = [{
|
|
|
+ name: oxAxis.column.label,
|
|
|
+ width: 150,
|
|
|
+ type: 'String'
|
|
|
+ }, {
|
|
|
+ name: oyAxis.column.label,
|
|
|
+ width: 100,
|
|
|
+ type: 'Number'
|
|
|
+ }];
|
|
|
+ rows = series[0].data.map(d => ([d.name, d.value]))
|
|
|
+ }else if(chartType === 'scatter') {
|
|
|
+ columns = [{
|
|
|
+ name: oxAxis.column.label,
|
|
|
+ width: 80,
|
|
|
+ type: 'Number'
|
|
|
+ }, {
|
|
|
+ name: oyAxis.column.label,
|
|
|
+ width: 80,
|
|
|
+ type: 'Number'
|
|
|
+ }];
|
|
|
+ if(ogroupBy && ogroupBy.key) {
|
|
|
+ columns.push({
|
|
|
+ name: ogroupBy.label,
|
|
|
+ width: 100,
|
|
|
+ type: 'String'
|
|
|
+ });
|
|
|
+ }
|
|
|
+ series.forEach(xs => {
|
|
|
+ xs.data.forEach(xd => {
|
|
|
+ if(ogroupBy && ogroupBy.key) {
|
|
|
+ rows.push([xd[0], xd[1], xs.name]);
|
|
|
+ }else {
|
|
|
+ rows.push([xd[0], xd[1]]);
|
|
|
+ }
|
|
|
+ });
|
|
|
+ })
|
|
|
+ }else if(chartType === 'dataView') {
|
|
|
+ let { viewColumns } = chartOption.originConfig || {};
|
|
|
+ columns = viewColumns.map(c => ({
|
|
|
+ name: c.label,
|
|
|
+ width: 100,
|
|
|
+ type: TYPES[c.type]
|
|
|
+ }));
|
|
|
+ const res = yield call(service.fetch, {
|
|
|
+ url: URLS.CHART_OPTION,
|
|
|
+ allow: true,
|
|
|
+ body: {
|
|
|
+ dashboardCreatorId: creatorCode,
|
|
|
+ chartId: chartCode,
|
|
|
+ filters: getBodyFilters(getTrueFilters(item, filters)),
|
|
|
+ testPage: {
|
|
|
+ pageNum: 1,
|
|
|
+ pageSize: 99999,
|
|
|
+ }
|
|
|
+ },
|
|
|
+ timeout: 30000
|
|
|
+ });
|
|
|
+ if(res.code > 0) {
|
|
|
+ res.data.valueList.list.forEach(l => {
|
|
|
+ let r = [];
|
|
|
+ viewColumns.forEach(c => {
|
|
|
+ r.push(l[c.name]);
|
|
|
+ });
|
|
|
+ rows.push(r);
|
|
|
+ })
|
|
|
+ }else {
|
|
|
+ console.error(res.msg);
|
|
|
+ }
|
|
|
+ }else if(chartType === 'aggregateTable') {
|
|
|
+ let { originConfig, data, group1Name, group2Name, group1s, group2s } = chartOption;
|
|
|
+ let { targetColumn, groupBy, statistics } = (originConfig || {});
|
|
|
+ header = `分析目标:${targetColumn.label}`
|
|
|
+ columns = groupBy.map(g => ({ name: g.label, width: 100, type: TYPES[g.type] })).concat(statistics.map(s => ({ name: s.label, width: 100, type: 'Number' })));
|
|
|
+
|
|
|
+ if(!group1Name) { // 无分组
|
|
|
+ rows = [statistics.map(s => data[s.name])];
|
|
|
+ }else {
|
|
|
+ if(!group2Name) { // 只有一个分组
|
|
|
+ rows = group1s.map((g, i) => [g].concat(statistics.map(s => data[i][s.name])));
|
|
|
+ }else { // 有两个分组
|
|
|
+ group1s.forEach((g1, i1) => {
|
|
|
+ let d = data[i1];
|
|
|
+ group2s.forEach((g2, i2) => {
|
|
|
+ let arr = statistics.map(s => d.data[i2][s.name]);
|
|
|
+ rows.push([g1, g2].concat(arr));
|
|
|
+ })
|
|
|
+ });
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|
|
|
- }),
|
|
|
+ }else if(viewType === 'richText') {
|
|
|
+ columns = [{
|
|
|
+ name: content.replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>').replace(/\"/g, '"').replace(/\'/g, '''),
|
|
|
+ width: 500,
|
|
|
+ type: 'String'
|
|
|
+ }];
|
|
|
+ rows = [];
|
|
|
+ }
|
|
|
+ sheets.push({
|
|
|
+ name: itemName || new Date().getTime(),
|
|
|
+ header,
|
|
|
+ columns,
|
|
|
+ rows
|
|
|
+ });
|
|
|
}
|
|
|
- yield call(service.fetch, {
|
|
|
- url: URLS.DASHBOARD_EXPORT,
|
|
|
- requestType: 'file',
|
|
|
- fileName: name,
|
|
|
- body,
|
|
|
- })
|
|
|
+ console.log(sheets)
|
|
|
+ let e = new Exportor({ sheets }, `${name}.xls`);
|
|
|
+ e.export();
|
|
|
}catch(e) {
|
|
|
message.error('报表导出错误: ' + e);
|
|
|
+ }finally {
|
|
|
+ yield put({ type: 'dashboardDesigner/setField', name: 'loading', value: false });
|
|
|
}
|
|
|
}
|
|
|
},
|