审查视图

src/views/nav3/statistics.vue 7.8 KB
xudada authored
1 2 3 4 5 6 7 8 9 10
<template>
    <div>
        <el-form :inline="true" :model="formInline" class="demo-form-inline">
            <el-form-item label="">
                <div class="block">
                    <el-date-picker
                            size="medium"
                            v-model="value2"
                            type="datetimerange"
                            :picker-options="pickerOptions"
xudada authored
11
                            value-format="yyyy-MM-dd HH:mm:ss"
xudada authored
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
                            range-separator="至"
                            start-placeholder="开始日期"
                            end-placeholder="结束日期"
                            align="right">
                    </el-date-picker>
                </div>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" size="medium" @click="onSubmit">查询</el-button>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" size="medium" @click="exportToExcel">导出</el-button>
            </el-form-item>
        </el-form>
        <div style="margin-bottom: 10px"><span style="color: red">总计: {{ totalItems }} 主单: {{ emptyAwbhCount }} 分单: {{ nonEmptyAwbhCount }}</span></div>
        <div>
            <el-table
                    size="medium"
                    :data="tableData"
                    border
                    style="width: 100%">
                <el-table-column
                        prop="awba"
                        label="主单号"
                        width="180">
                </el-table-column>
                <el-table-column
                        prop="awbh"
                        label="分单号"
                        width="180">
                </el-table-column>
                <el-table-column
                        prop="billtype"
                        label="单证类型">
                </el-table-column>
                <el-table-column
                        prop="costs"
                        label="应结费用">
                </el-table-column>
                <el-table-column
                        prop="flightdate"
                        label="航班日期">
                </el-table-column>
            </el-table>
        </div>
    </div>
</template>
<script>
    import {
        selectStatistics
    } from '../../api/remote_interface/byont_import';
    import XLSX from 'xlsx';
    import { saveAs } from 'file-saver';
    import moment from 'moment';
    export default {
        data() {
            return {
                pickerOptions: {
                    shortcuts: [{
                        text: '最近一周',
                        onClick(picker) {
                            const end = new Date();
                            const start = new Date();
                            start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
                            picker.$emit('pick', [start, end]);
                        }
                    }, {
                        text: '最近一个月',
                        onClick(picker) {
                            const end = new Date();
                            const start = new Date();
                            start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
                            picker.$emit('pick', [start, end]);
                        }
                    }, {
                        text: '最近三个月',
                        onClick(picker) {
                            const end = new Date();
                            const start = new Date();
                            start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
                            picker.$emit('pick', [start, end]);
                        }
                    }]
                },
                value2: '',
                tableData: [],
                query:{
                    starttime:'',
                    endtime:''
                },
                formInline:{},
                columns: [
                    { prop: 'awba', label: '主单号' },
                    { prop: 'awbh', label: '分单号' },
                    { prop: 'billtype', label: '单证类型' },
                    { prop: 'costs', label: '应结费用' },
                    { prop: 'flightdate', label: '航班日期' }
                ]
            };
        },
        computed: {
            totalItems() {
                return this.tableData.length;
            },
            emptyAwbhCount() {
                return this.tableData.filter(item => !item.awbh).length;
            },
            nonEmptyAwbhCount() {
                return this.tableData.filter(item => item.awbh).length;
            }
        },
        methods:{
            onSubmit(){
                if(this.value2 !== null && this.value2 !== ""){
                    this.query.starttime = this.value2[0];
                    this.query.endtime = this.value2[1];
                }else{
                    return this.$message.error('请选取时间段!')
                }
                selectStatistics(this.query).then((response) => {
                    const res = response.data
                    if (res.code !== '200') {
                        return this.$message.error('获取消息收发记录,失败!')
                    }
                    // 获取列表数据
                    this.tableData = res.data;
                    this.$message.success('获取消息收发记录,成功!');
                }).catch(error => {
                    // 关闭加载
                    this.$message.error(error.toString())
                })
            },
            exportToExcel() {
                // 创建一个新的工作簿
                const wb = XLSX.utils.book_new();

                // 创建一个自定义的头部数组,按照 columns 中的顺序和 label
                const header = this.columns.map(col => col.label);

                // 将 tableData 转换成二维数组,并添加到 worksheet 中
xudada authored
152 153 154 155 156 157 158 159 160
                const data = this.tableData.map(row =>
                    this.columns.map(col => {
                        // 如果是 costs 列,则转换为数值
                        if (col.prop === 'costs') {
                            return parseFloat(row[col.prop]) || 0; // 确保无效值转换为0
                        }
                        return row[col.prop];
                    })
                );
xudada authored
161 162 163 164

                // 使用 XLSX.utils.aoa_to_sheet 创建工作表,指定头部
                const ws = XLSX.utils.aoa_to_sheet([header, ...data]);
xudada authored
165 166 167 168 169 170 171 172 173 174 175
                // 指定每一列的数据类型
                const columnTypes = this.columns.map(col => ({
                    t: col.prop === 'costs' ? 'n' : 's' // 'n' for number, 's' for string
                }));

                // 更新工作表中的单元格类型
                for (let i = 1; i <= data.length; i++) { // 从1开始,因为第0行是标题
                    const cellRef = XLSX.utils.encode_cell({ c: this.columns.findIndex(col => col.prop === 'costs'), r: i });
                    ws[cellRef].t = 'n'; // 设置单元格类型为数值
                }
xudada authored
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192
                // 添加 worksheet 到 workbook
                XLSX.utils.book_append_sheet(wb, ws, 'Sheet1');

                // 生成 Excel 文件并下载
                const wbout = XLSX.write(wb, { bookType: 'xlsx', type: 'array' });
                const blob = new Blob([wbout], { type: 'application/octet-stream' });

                // 计算明天的日期
                const tomorrow = moment().add(1, 'days').format('M月D日');

                // 使用 file-saver 保存文件,并使用明天的日期作为文件名
                saveAs(blob, `${tomorrow}航班结算统计.xlsx`);
            }
        }

    };
</script>